endtwist/AjaxIM

Adding user (initiator and accepter) on the fly and updating friend list

paulm17 opened this issue · 3 comments

Scenario:

User A sends a friend request to User B.
User B then accepts the friend request, on acceptance:
User B gets User A added only if User A is online.
If User A is online then their friend list gets User B added.

You can add users via the script using:

  • //AJAXIM: Add this user to friends list
    im.addFriend(friendname, "Available", 'Friends');
  • Send notification to other friend to add this user
    im.send(friendname, "_newfriend"); //AJAXIM

In im.js around line 473: incoming: function(from, message...

add:

//Add to AJAXIM friends list
if (message == "_newfriend") {
im.addFriend(from, "Available", 'Friends');
return false;
}

But this seems hackish and is limited to the clientside.

I propose this mechanism:

  1. User A sends a friend request.
  2. User B accepts friend request.
  3. From User B: New status update is sent to the server adding User A to (User B) session.
  4. For User B: On clientside, if User A online then add to (User B) friend list.
  5. For User A: If Online, new status update is sent to User A notifying of new friend to be added (User B) and (User A) session updated.

Comments welcome.

app.js

diff --git a/app.js b/app.js
index b19c74c..d9cdf53 100755
--- a/app_old.js
+++ b/app.js
@@ -133,3 +133,23 @@ app.post('/signoff', function(req, res) {
     res.signOff();
     res.send(new packages.Success('goodbye'));
 });
+
+app.post('/friendaccept', function(req, res) {
+       var acceptee = req.body.acceptee;
+       var initiator = req.body.initiator;
+       
+    res.find(initiator, function(user) {       
+                 if(user) {                    
+                               res.message(user, new packages.Message(
+               req.session.data('username'),
+               "sysaddfriend"
+                       ));                     
+                       
+                               user.friends.push(new Array(acceptee, 'available'));                            
+            }                             
+    });
+    
+    res.find(acceptee, function(user) { 
+                       user.friends.push(new Array(initiator, 'available'));                   
+        });                            
+});

im.js

diff --git a/im.js b/var/www/html/findbodz/modules/ajaxim/js/im.js
old mode 100755
new mode 100644
index c8ec043..b466d66
--- a/im.js
+++ b/var/www/html/findbodz/modules/ajaxim/js/im.js
@@ -50,7 +50,8 @@ AjaxIM = function(options, actions) {
         listen: this.settings.pollServer + '/listen',
         send: this.settings.pollServer + '/message',
         status: this.settings.pollServer + '/status',
-            signoff: this.settings.pollServer + '/signoff'
+            signoff: this.settings.pollServer + '/signoff',
+            friendaccept: this.settings.pollServer + '/friendaccept'
     }, actions);

     // We load the theme dynamically based on the passed
@@ -412,6 +413,7 @@ $.extend(AjaxIM.prototype, {
             this.current_status = ['available', ''];
             store.set('user', message.username);
             store.set(this.username + '-status', this.current_status);
+                self.status('available', '');

             $('#imjs-friends').attr('class', 'imjs-available');
             $.each(message.friends, function() {
@@ -456,6 +458,21 @@ $.extend(AjaxIM.prototype, {
     }
     },

+    friendaccept: function(username, friend) {
+        var self = this;
+        
+        AjaxIM.post(
+            this.actions.friendaccept,
+            {acceptee: username, initiator: friend},
+            function(result) {
+                //console.log ("sent");
+            },
+            function(error) {
+               //console.log ("error");
+            }
+        );
+    },
+
     // === {{{AjaxIM.}}}**{{{incoming(from, message)}}}** ===
     //
     // Handles a new message from another user. If a chatbox for that
@@ -471,6 +488,14 @@ $.extend(AjaxIM.prototype, {
     // check if IM exists, otherwise create new window
     // TODO: If friend is not on the buddylist,
     // should add them to a temp list?
+        
+                 //Add initiator AJAXIM friends list
+                 if (message == "sysaddfriend") {
+                          im.addFriend(from, "Available", 'Friends');
+                       
+                          return false;        
+                 }           
+        

In order for this to work, you need these 2 lines when you accept the request. (in jQuery):

//AJAXIM: Send notification to other friend to add this user
im.friendaccept(username, friendname); 

//AJAXIM: Add this user to friends list     
im.addFriend(friendname, "Available", 'Friends'); 

Note:

username is the acceptee (user who accepts the friend request)
friendname is the initiator (user who initiates the friend request)

Comments welcome.

Could we possibly move this to a pull request? Just branch off your codebase for this bug (i.e., bug number and a short descriptor like "42-friend-accept") and use the Pull Request feature. It'll be easier to iterate and review the code changes moving forward.

I think its best to wait for a bit. There are 2 reasons why to wait.

  1. Integration into a notifications system, where ideally this should belong.
  2. Keeping the ajaxim codebase locally, so move the im.friendaccept/im.addFriend statements to the server, so for example with PHP you would use CURL to post the equivalent messages to NodeJS.

I'm sure we'll get to a better solution soon.

Cheers