Skip to content
This repository was archived by the owner on Mar 26, 2022. It is now read-only.

Commit 7844d7e

Browse files
committed
Merge branch 'master' of https://github.com/CECS-478-AuRave/SecureChat into androidDeploy
2 parents 8578583 + 147bba8 commit 7844d7e

File tree

12 files changed

+74
-22
lines changed

12 files changed

+74
-22
lines changed

Assets/Logo/shush144.png

1.76 KB
Loading

Assets/Logo/shush192.png

2.58 KB
Loading

Assets/Logo/shush48.png

648 Bytes
Loading

Assets/Logo/shush512.png

11 KB
Loading

Assets/Logo/shush72.png

879 Bytes
Loading

Assets/Logo/shush96.png

1.08 KB
Loading

backend/secure-chat-express/app/api/controllers/friends.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,11 @@ module.exports.acceptFriend = function(req, res) {
164164
// Controller for deleting a friend.
165165
module.exports.deleteFriend = function(req, res) {
166166
if (req.user) {
167+
// Get the Current user.
167168
var thisUser = req.user;
169+
// get the other user's facebook from the body.
168170
var otherUserID = req.body.otherUserID;
171+
// find the friend from the current users list of friends.
169172
var otherIdIndex = thisUser.friends.indexOf(otherUserID);
170173
var thisFacebookId = thisUser.facebook.id;
171174
if (!otherUserID) {
@@ -176,24 +179,30 @@ module.exports.deleteFriend = function(req, res) {
176179
res.status(400).json({'error': 'That user is not your friend.'});
177180
return;
178181
}
182+
// Remove the friend from the current user's friend list.
179183
thisUser.friends.splice(otherIdIndex, 1);
184+
// save the current user to the database.
180185
thisUser.save(function(err, thisUser) {
181186
if (err) {
182187
res.status(500).json(err);
183188
}
184189
});
190+
// Find the other user based on the facebook id provided.
185191
User.findOne({'facebook.id':otherUserID}, function(err, otherUser) {
186192
if (!otherUser) {
187193
res.status(404).json({'error':'Other User was not found.'});
188194
} else if (err) {
189195
res.status(500).json(err);
190196
} else {
197+
// check if the other user is a friend of the current user.
191198
var thisIdIndex = otherUser.friends.indexOf(thisFacebookId);
192199
if (thisIdIndex == -1) {
193200
res.status(400).json({'error': 'That user is not your friend.'});
194201
return;
195202
}
203+
// Remove the current user from the other user's friends list.
196204
otherUser.friends.splice(thisIdIndex, 1);
205+
// Save the other user to the database.
197206
otherUser.save(function(err, otherUser) {
198207
if (err) {
199208
res.status(500).json(err);

backend/secure-chat-express/app/api/controllers/user.js

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,68 @@ module.exports.findUserById = function(req, res) {
7676

7777
module.exports.getFriend = function(req, res) {
7878
if (req.user) {
79-
if (req.user.friends.length <= 0) {
80-
res.status(404).json({'error' : 'No friends found for user'});
81-
}
82-
var friends = [];
83-
var count = 0;
84-
// for each friend for the given user add it to the friends array
85-
// if the friend was found.
86-
req.user.friends.forEach(function(friendId) {
87-
User.findOne({'facebook.id' : friendId}, function(err, friend) {
88-
if (err) {
89-
// error finding friend.
90-
res.status(500).json(err);
91-
} else if (friend) {
92-
count++; // keep track of the number of friends found.
93-
friends.push(friend);
94-
}
95-
}).then(function() {
96-
// respond with the friends array after the loop has finished running.
97-
if (count === req.user.friends.length) {
98-
res.status(200).json(friends);
99-
}
79+
// Create a new promise to find all friends.
80+
var findFriend = new Promise((resolve, reject) => {
81+
var friends = [];
82+
if (req.user.friends.length === 0) {
83+
// return empty list since no friends :(|
84+
resolve(friends);
85+
}
86+
req.user.friends.forEach((friendId) => {
87+
// Find friends and add them to the list.
88+
User.findOne({'facebook.id' : friendId})
89+
.select('name profilePhotoURL _id facebook.id')
90+
.exec(function(err, friend) {
91+
if (err) {
92+
// error finding friend.
93+
res.status(500).json(err);
94+
reject("Error");
95+
} else if (friend) {
96+
// add friend to list.
97+
friends.push(friend);
98+
if (friends.length === req.user.friends.length) {
99+
// return the list of friends.
100+
resolve(friends);
101+
}
102+
}
103+
});
104+
});
105+
});
106+
// Create a new promise to find pending friends.
107+
var findPending = new Promise((resolve, reject) => {
108+
var friends = [];
109+
if (req.user.pendingFriends.length === 0) {
110+
// return empty list since no pending friends :(|
111+
resolve(friends);
112+
}
113+
req.user.pendingFriends.forEach((friendId) => {
114+
// Find pending friends add them to the list.
115+
User.findOne({'facebook.id' : friendId})
116+
.select('name profilePhotoURL id facebook.id')
117+
.exec(function(err, friend) {
118+
if (err) {
119+
// error finding friend.
120+
res.status(500).json(err);
121+
reject("Error");
122+
} else if (friend) {
123+
// add pending friend to list.
124+
friends.push(friend);
125+
if (friends.length === req.user.pendingFriends.length) {
126+
// return the list of pending friends.
127+
resolve(friends);
128+
}
129+
}
130+
});
100131
});
101132
});
133+
// Run all promises first then respond with the data.
134+
Promise.all([findFriend, findPending]).then((data) => {
135+
res.status(200).json({
136+
'friends' : data[0],
137+
'pendingFriends' : data[1]
138+
});
139+
});
140+
102141
} else {
103142
// Respond with Unauthorized access.
104143
res.status(401).json({'error': 'Access Not Authorized.'});

backend/secure-chat-express/app/api/models/conversation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var mongoose = require('mongoose'),
33
Schema = mongoose.Schema;
44

5+
// Schema for conversation.
56
var conversationSchema = new Schema({
67
conversationID: {type: String, required: true},
78
members: [{type: Schema.ObjectId, ref: 'User'}],

backend/secure-chat-express/app/api/models/message.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var mongoose = require('mongoose'),
44
Schema = mongoose.Schema;
55

6+
// Schema for messages.
67
var messageSchema = new Schema({
78
message: {type: String, required: true},
89
from: {type: Schema.ObjectId, ref: 'User', required: true},

0 commit comments

Comments
 (0)