Skip to content

Commit

Permalink
feature(feature/139456273/add-notifications): fix bug when accepting …
Browse files Browse the repository at this point in the history
…friends and remove current user when searching for user [139456273]
  • Loading branch information
Inumidun Amao committed Mar 22, 2017
1 parent b9863e4 commit 1b8db05
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 190 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"es6": true,
"mocha": true
},
"globals": {
"window": true,
"document": true
},
"rules": {
"one-var": 0,
"one-var-declaration-per-line": 0,
Expand Down
30 changes: 15 additions & 15 deletions app/controllers/friends.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ exports.acceptReq = (req, res) => {
});
});
} else {
const sender = req.body.sender._id;
let friendsData = { _id: req.body.user };
friendsData[sender] = req.body.sender;
const friends = new FriendsModel(friendsData);
friends.save();
deleteNotification(req.body.user, req.body.sender._id, (status) => {
if (status) {
res.json(status);
} else if (status === 404) {
res.status(200);
} else {
res.status(500);
}
});
}
const sender = req.body.sender._id;
const friendsData = { _id: req.body.user };
friendsData[sender] = req.body.sender;
const friends = new FriendsModel(friendsData);
friends.save();
deleteNotification(req.body.user, req.body.sender._id, (status) => {
if (status) {
res.json(status);
} else if (status === 404) {
res.status(200);
} else {
res.status(500);
}
});
}
});
};

Expand Down
14 changes: 13 additions & 1 deletion app/controllers/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ const mongoose = require('mongoose');

const NotificationModel = mongoose.model('Notification');

/**
* @param {Object} notificationData - contains data about sender and
* reciever of notification
* @param {Function} callback - function to call when notification
* has been saved
* @return {void} call the callback function instead
*
*/
exports.saveNotification = (notificationData, callback) => {
const newNotification = new NotificationModel({
user: notificationData.reciever,
type: 'invite',
link: `http://localhost:3000/#!/app?game=${notificationData.link}`,
link: notificationData.link,
message: `${notificationData.sender} wants you to join a game`
});

Expand Down Expand Up @@ -37,6 +45,10 @@ exports.getNotification = (req, res) => {
}
};

/**
* @param {String} user - id of user to update read column for
* @return {void}
*/
exports.updateRead = (user) => {
let error = null;
if (user) {
Expand Down
14 changes: 12 additions & 2 deletions config/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ module.exports = function (io) {
}
});

// When an invite is sent, update the notifications
socket.on('sendInvite', (params, cb) => {
Notification.saveNotification({
reciever: params.user,
sender: params.sender,
link: socket.gameID
link: params.link
}, (response) => {
if (response.status === 'success') {
cb(null, response);
Expand All @@ -97,16 +98,25 @@ module.exports = function (io) {
});
});

// Fetch notifications from databse
socket.on('loadNotification', (params, cb) => {
Notification.getNotification(params.user.id, (response) => {
cb(null, response);
});
});

// Make a global broadcast to all connected sockets
socket.on('makeBroadcast', (params) => {
socket.broadcast.emit(params.message, params.param);
const param = {};
Object.keys(params).forEach((p) => {
if (p !== 'message') {
param[p] = params[p];
}
});
socket.broadcast.emit(params.message, param);
});

// update the read column of a motification in the database
socket.on('readUpdate', (params, callback) => {
Notification.updateRead(params.user, (response) => {
if (response) {
Expand Down
23 changes: 11 additions & 12 deletions public/js/controllers/game.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
angular.module('mean.system')
.controller('GameController', ['$scope', 'game', '$timeout', '$location', 'MakeAWishFactsService',
.controller('GameController', ['socket', '$scope', 'game', '$timeout', '$location', 'MakeAWishFactsService',
'$dialog', 'playerSearch', 'invitePlayer', 'gameTour', '$window',
($scope, game, $timeout, $location, MakeAWishFactsService, $dialog, playerSearch,
(socket, $scope, game, $timeout, $location, MakeAWishFactsService, $dialog, playerSearch,
invitePlayer, gameTour) => {
$scope.hasPickedCards = false;
$scope.winningCardPicked = false;
Expand Down Expand Up @@ -164,8 +164,9 @@ angular.module('mean.system')
}
socket.emit('sendInvite', {
user: $scope.inviteeUserID,
sender: window.user.name
}, function() {
sender: window.user.name,
link: document.URL
}, () => {
invitePlayer.sendMail($scope.inviteeUserEmail, document.URL).then((data) => {
if (data === 'Accepted') {
$scope.invitedPlayers.push($scope.inviteeUserEmail);
Expand All @@ -179,18 +180,17 @@ angular.module('mean.system')
});
} else {
$('#playerAlreadyInvited').modal('show');
$scope.searchResults = [];
$scope.inviteeUserEmail = '';
$scope.inviteeUserName = '';
}
};
$scope.searchResults = [];
$scope.inviteeUserEmail = '';
$scope.inviteeUserName = '';
}
};

$scope.playerSearch = () => {
if ($scope.inviteeUserName !== '') {
playerSearch.getPlayers($scope.inviteeUserName).then((data) => {
$scope.searchUserResults = data.filter((user) => {
console.log(window.user);
if (user._id !== window.user.id ) {
if (user._id !== window.user.id) {
return user;
}
});
Expand Down Expand Up @@ -233,7 +233,6 @@ angular.module('mean.system')
};

if ($location.search().game && !(/^\d+$/).test($location.search().game)) {
console.log('joining custom game');
game.joinGame('joinGame', $location.search().game);
} else if ($location.search().custom) {
game.joinGame('joinGame', null, true);
Expand Down
Loading

0 comments on commit 1b8db05

Please sign in to comment.