Skip to content

Commit

Permalink
Revamp friendship. Need to find a way to make the list of online frie…
Browse files Browse the repository at this point in the history
…nd reactive.
  • Loading branch information
eprochasson committed Jul 12, 2013
1 parent ac1d4a3 commit 10bff07
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 14 deletions.
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Move images to S3 with Filepicker.
Friends management (need to take it out of the user profile).
9 changes: 9 additions & 0 deletions client/views/mailbox/conversation.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<template name="conversation">


<ul>
{{#each messages}}
{{> message}}
{{/each}}
</ul>
{{#if messagesReady}}
{{#unless allMessagesLoaded}}
<a class="read-more" href="#">Load more</a>
{{/unless}}
{{else}}
<div>{{> spinner}}</div>
{{/if}}


</template>

Expand Down
30 changes: 27 additions & 3 deletions client/views/mailbox/conversation.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
Template.conversation.helpers({
messages: function(){
//
var conversation = Conversations.findOne(Session.get('currentConversation'));
if(!conversation){
return;
return [];
}

return Messages.find(
Expand All @@ -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(){
Expand Down
8 changes: 7 additions & 1 deletion lib/collections/activities.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
// Activities between users
Activities = new Meteor.Collection('activities');
Activities = new Meteor.Collection('activities');

Activities.deny({
update: function(){return true},
remove: function(){return true},
insert: function(){return true}
});
7 changes: 7 additions & 0 deletions lib/collections/friends.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Friends = new Meteor.Collection('friends');

Friends.deny({
update: function(){return true},
remove: function(){return true},
insert: function(){return true}
});
1 change: 1 addition & 0 deletions main.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ Pictures.maxFilePerUser = -1;
Presences.checkInterval = 2000;
// How long before a user is considered out (ms).
Presences.TimeOut = 10000;

3 changes: 3 additions & 0 deletions server/activities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Activities.sendFriendRequest = function(target){

};
50 changes: 50 additions & 0 deletions server/friends.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
23 changes: 14 additions & 9 deletions server/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 10bff07

Please sign in to comment.