From 10bff070588bb420565343aeee8241ab222bab81 Mon Sep 17 00:00:00 2001 From: Emmanuel Prochasson Date: Fri, 12 Jul 2013 18:06:52 +0800 Subject: [PATCH] Revamp friendship. Need to find a way to make the list of online friend reactive. --- TODO | 3 +- client/views/mailbox/conversation.html | 9 +++++ client/views/mailbox/conversation.js | 30 ++++++++++++++-- lib/collections/activities.js | 8 ++++- lib/collections/friends.js | 7 ++++ main.config.js | 1 + server/activities.js | 3 ++ server/friends.js | 50 ++++++++++++++++++++++++++ server/publications.js | 23 +++++++----- 9 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 lib/collections/friends.js create mode 100644 server/activities.js create mode 100644 server/friends.js diff --git a/TODO b/TODO index f597982..d20d2da 100644 --- a/TODO +++ b/TODO @@ -4,4 +4,5 @@ Setting page Activities: what did one do, what can of activity can the others see. Check user security: do not allow user to change all their profile. myFriends and myOnlineFriends are redundant. -Move images to S3. \ No newline at end of file +Move images to S3 with Filepicker. +Friends management (need to take it out of the user profile). \ No newline at end of file diff --git a/client/views/mailbox/conversation.html b/client/views/mailbox/conversation.html index 42274bb..f17c548 100644 --- a/client/views/mailbox/conversation.html +++ b/client/views/mailbox/conversation.html @@ -1,10 +1,19 @@ diff --git a/client/views/mailbox/conversation.js b/client/views/mailbox/conversation.js index 07a803f..d47cbc4 100644 --- a/client/views/mailbox/conversation.js +++ b/client/views/mailbox/conversation.js @@ -1,9 +1,8 @@ Template.conversation.helpers({ messages: function(){ -// var conversation = Conversations.findOne(Session.get('currentConversation')); if(!conversation){ - return; + return []; } return Messages.find( @@ -15,14 +14,39 @@ Template.conversation.helpers({ }, {sort: {sent: -1}} ); + }, + messagesReady: function(){ + return !oneConversationHandle.loading(); + }, + allMessagesLoaded: function(){ + var conversation = Conversations.findOne(Session.get('currentConversation')); + if(!conversation){ + return []; + } + return !oneConversationHandle.loading() && + Messages.find({ + $or: [ + {from: Meteor.userId(), to: conversation.with}, + {to: Meteor.userId(), from: conversation.with} + ] + }).count() < oneConversationHandle.loaded(); + }, + conversation: function(){ + return Conversations.findOne(Session.get('currentConversation')); } }); + +Template.conversation.events({ + 'click .read-more': function(e){ + e.preventDefault(); + oneConversationHandle.loadNextPage(); + } +}) Template.message.helpers({ timestamp: function(){ return moment(this.sent).fromNow(); }, sender: function(){ - console.log(this); return this.from == Meteor.userId()? __('messages.you') : Meteor.users.findOne(this.from)['profile'].name; }, receiver: function(){ diff --git a/lib/collections/activities.js b/lib/collections/activities.js index b9e6f01..ec105b5 100644 --- a/lib/collections/activities.js +++ b/lib/collections/activities.js @@ -1,2 +1,8 @@ // Activities between users -Activities = new Meteor.Collection('activities'); \ No newline at end of file +Activities = new Meteor.Collection('activities'); + +Activities.deny({ + update: function(){return true}, + remove: function(){return true}, + insert: function(){return true} +}); \ No newline at end of file diff --git a/lib/collections/friends.js b/lib/collections/friends.js new file mode 100644 index 0000000..25be903 --- /dev/null +++ b/lib/collections/friends.js @@ -0,0 +1,7 @@ +Friends = new Meteor.Collection('friends'); + +Friends.deny({ + update: function(){return true}, + remove: function(){return true}, + insert: function(){return true} +}); \ No newline at end of file diff --git a/main.config.js b/main.config.js index e99ffa9..2155ca4 100644 --- a/main.config.js +++ b/main.config.js @@ -66,3 +66,4 @@ Pictures.maxFilePerUser = -1; Presences.checkInterval = 2000; // How long before a user is considered out (ms). Presences.TimeOut = 10000; + diff --git a/server/activities.js b/server/activities.js new file mode 100644 index 0000000..32b8fbc --- /dev/null +++ b/server/activities.js @@ -0,0 +1,3 @@ +Activities.sendFriendRequest = function(target){ + +}; \ No newline at end of file diff --git a/server/friends.js b/server/friends.js new file mode 100644 index 0000000..d10dc72 --- /dev/null +++ b/server/friends.js @@ -0,0 +1,50 @@ +Meteor.methods({ + addAsFriend: function(target){ + target = Meteor.users.findOne(target); + if(!target){ + throw new Meteor.Error(404, 'Person not found'); + } + if(_.contains(target.blacklist, this.userId)){ + throw new Meteor.Error(300, 'Blocked'); + } + + var id; + if(id = Friends.findOne({me: this.userId, target: target._id})){ + // Already friend. + return id; + } + + var fields = { + me: this.userId, // That's me + target: target._id, // That's the person I'm becoming friend with. + live: 1, // Link is alive. + reciprocal: 0, // Connection goes both way + timestamp: new Date().getTime() + }; + + // Answering a friend request. + if(Friends.findOne({me: target._id, target: this.userId, live: 1})){ + fields.reciprocal = 1; + Friends.update({me: target._id, target: this.userId}, {$set: {reciprocal: 1}}); + } + + id = Friends.insert(fields); + + return Activities.sendFriendRequest(this.userId, target._id) && id; + }, + removeFriend: function(target){ + // Just kill the link, keep the connection. + target = Meteor.users.findOne(target); + if(!target){ + throw new Meteor.Error(404, 'Person not found'); + } + + Friends.remove({target: target._id, me: this.userId}); + Friends.remove({me: target._id, target: this.userId}); + +// Friends.update({target: target._id, me: this.userId}, {$set: {live: 0, reciprocal: 0}}); +// Friends.update({me: target._id, target: this.userId}, {$set: {live: 0, reciprocal: 0}}); + + return true; + } +}); \ No newline at end of file diff --git a/server/publications.js b/server/publications.js index e0e2356..ce413fa 100644 --- a/server/publications.js +++ b/server/publications.js @@ -63,24 +63,29 @@ Meteor.publish("myFriendList", function(){ ); }); - // Also maintains user online/offline status Meteor.publish("myOnlineFriends", function(){ - var friends = Meteor.users.findOne(this.userId).friends || []; + + //TODO: make work. Shit is not reactive as it should be Meteor.publishWithRelations({ handle: this, - collection: Presences, - filter: {user: {$in : friends}, online: 1, invisible: false}, - options: {fields: {user: 1}, sort:{ lastseen: -1}}, - mappings: [{ // Publish people sending message as well, as they might not be in your friendlist. - key: 'user', + collection: Friends, + filter: {me: this.userId, live: 1}, + mappings: [{ + key: 'target', collection: Meteor.users, - options: {fields: Meteor.user.publicProfileInformation} + options: {fields: Meteor.user.publicProfileInformation}, + mappings: [{ + key: 'user', + reverse: true, + collection: Presences, + filter: {online: 1, invisible: false}, + options: {fields: {user: 1}, sort:{ lastseen: -1}} + }] }] }); }); - Meteor.publish("userProfile", function(userId){ // Check that this user can see each others. var source = Meteor.users.findOne(this.userId);