Making nodejs Drupalchat work with Commons trusted contact 1/2

Posted by on Sep 16, 2014 in Commons, Drupal, nodeJs, PHP | No Comments

Durring this year I have worked as Lead developper on a big Drupal 7 project witch is the development of a custom social network for the French Ministery of Finance (DGFIP).

This project is based on the Drupal Commons distibution of Acquia (V 1.7) but with a lot of custom development to satisfy the particular needs of the client.

During this project I have been asked to integrate a chat in the website, and because of the huge number of people who are going to use this social network, I decided to use the nodeJs language.

Lucky for me, there is a good nodeJs module and a Drupalchat module that can work together and add a dynamic chat to Drupal. This work fine, but the client wanted some new features (as always).

They wanted a chat who works with the Drupal trusted contact, (we can say it’s like friends on facebook). Basicaly they wanted to chat only with their trusted contacts.

The Drupalchat module allow you to have a « buddy list » but it works with the relationship module. In Drupal commons, Trusted contact are not relationships but OG groups of type user.

So I had to do some cutom development.

This solution is not in production yet, and it is just a proposition, be careful with it and do not copy/past the code without understanding it.

1 – Create a new nodejs server extention.

When you want to user Drupalchat with nodejs you have to copy the nodejs extention (drupalchat_nodejs.server.extention.js) of drupalchat in the nodeJs module and tell the nodeJs server to user it in the nodeJs configuration file.

The first thing we are going to do is to create a custom module with a custom nodejs extention based on the drupalchat one. So copy the content of the file drupalchat_nodejs.server.extention.js in you own file.

First of all, you want to get the user trusted contact on the server side, luky for you, you have it in the Drupal user object, which is accessible in the nodeJs extention as authData.

In this code, we ask the nodeJs server to retrieve the user trusted contact only.

We also need to restrict the publication of the user status only to his trusted contact.

This time, if you look in the data variable (for sendName message), you can not see the user trusted contact, and that’s because, unlike the authData variable used earlier, data just contain the user uid and name.

So we need to give the right information to the nodeJs server using Drupal.

2 – Create the Drupal module

At this point, you need to create a classical .module file to alter some behaviour of the drupalchat module.

First, we are going to alter the a Drupalchat menu callback to use our own custom function.

And now we copy and past the original callback to make little changes.

We give to the nodeJs server the user trusted contact in the message, so we can use it.

Now the user chat is going to be limited only to his trusted contacts, but the work is not yet finished.

First of all, the user trusted contacts contains non-approuved trusted contacts. So if someone ask you to be your trusted contact, he is going to be able to chat with you even if you have not approuved his request.

And second of all, the user trusted contacts list is never updated. That means if you add someone as your trusted contact, you have to restart the nodeJs server or logout to be able to see this user in the chat.

But we are going to see how to change this in my next post.