Hi Everyone. Here is a small module that adds a simple ajax layer to private message and alerts you of new messages using the jquery growl plugin (http://plugins.jquery.com/project/Growl). The plugin is released under GPL, so it's included in the tar ball.

Simply turning the module on will 'cause the site to start pinging the server every 5 seconds to check for new messages. There currently are no settings, though there are a few things which should probably be made configurable such as interval time and what exactly is displayed in the message. It provides a link to open the full message and reply.

The module itself basically just adds the jquery and provides the menu callback, which returns json and the display is handled in the pmgrowl.js file.

I'm sure there are some imperfections, but would love to get some feedback on it and hopefully someone will find this useful.

Comments

naheemsays’s picture

As a n00b to "Growl", how would you test this? Is just activating the module enough or does there need to be something installed/configured on the site/PC too?

Dave.Ingram’s picture

Simply enabling the module should take care of it. Growl is a jQuery plugin and it is included in the module and automatically added to the page. jQuery is also loaded onto the site automatically, so basically you should just automatically see a little popup in the top right when you receive a new private message.

Hope this helps!

naheemsays’s picture

Version: » 6.x-1.x-dev

hm.. not seeing anything - no dialogue boxes, updates etc and I did clear my cache.

I am using privatemsg 1.x-dev.

Dave.Ingram’s picture

Version: 6.x-1.x-dev »

I've not tested this on 1.x-dev, only on 2.x-dev, so it may well be that there were some api changes that affect this. You could try with the 2.x version.

naheemsays’s picture

ok. Just to make you aware, the 2.x-dev branch name is misleading - its the same as 1.0-rc2 and way older than the 1.x-dev branch which was had significant api changes (and had many new features added and bugs fixed).

Saying that, looking at the come, you do not seem to use any api functions, so I have no idea why it is broken.

litwol’s picture

Version: » 6.x-1.x-dev
naheemsays’s picture

Just a note - I am looking into why it does not work, and its not due to api changes. the problem is the various places in pmgrowl.js where the url is given as /messages/% (not the trail at the start) and I am testing this on a localhost where the site url is like http://localhost/testsite/ but the above code will go to localhost only.

The drupal answer is to use Drupal.settings.basePath to get the correct basepath.

naheemsays’s picture

StatusFileSize
new34.75 KB

updated pmgrowl attached that should work for all sites.

The body however should be clipped - if its a long message - it can extend to go off the page.

If this module was improved further - things I would like to see:

1. default checking time to be longer. maybe 30 seconds and potentially configurable. Any idea where to set this?
2. Message body clipping as said.
3. An option to instead of showing all the messages, to show just a "you have new messages" notification instead of trying to turn it into instant messaging.
4. A setting to allow each user to turn this off on an individual basis.
5. Not sure this is possible, but there may be tricks - some sort of persistence. Closing a message should not mean that it pops up again after you visit a new page. That is ok for one or two notifications, but if there are like 10 new messages... annoying would be the least of it.

naheemsays’s picture

StatusFileSize
new38.51 KB

Another slight modification - moved the is_new check into the query - I am sure this is much lighter than doing a check for every row in a loop.

As to number 5 is there a simple way to write to/read from a cookie? I know that there is a jquery plugin for that, but this is supposed to be something quick and simple...

A theme problem too: "View all" looks like view al to me. No idea if the last l is cut off somehow or hidden due to theming...

naheemsays’s picture

StatusFileSize
new7 KB

and this one truncates the body to 300 characters.

A theme problem too: "View all" looks like view al to me. No idea if the last l is cut off somehow or hidden due to theming...

Its actually not just that - the last letter on every line is slightly clipped.

uprojects’s picture

Small module but make the difference !

Dave.Ingram’s picture

Hi nbz,

Thanks for all your work on this. Later today I'll be updating again with my latest revision. It includes a settings page for setting # of seconds between message checks, and just working out the last parts of making the message stay closed between requests.

On the messages being cut off, I'm not experiencing that myself. What browser are you using?

Thanks,

Dave

naheemsays’s picture

Firefox 3.5b4 - so the bug could be in there (I also tried using cookies in the javascript code [for a different featureset - just an announcement that the user has new messages, and to only show that announcement if the value of new messages is different in the cookie than what the value from the database] and that works well on other tested browsers, not 3.5beta - where instead of overwriting an existing one, it adds a new cookie.)

naheemsays’s picture

@dave ingram - in your version, can you also make sure you use the change of menu location? just having messages/json does not seem a good idea. in the version I uploaded, I changed it to pmgrowl_json. (and also the change to "read privatemsg" instead of "access content" as the access check)

I am using the plugin for a different purpose - to notify of new messages only and I ahve modified the code to be as follows:

pmgrowl.module, I modified the pmgrowl_json function to be:

function pmgrowl_json() {
  $data = privatemsg_unread_count();
  drupal_json($data);
}

and then the pmgrowl.js is

$(document).ready(function() {

  // Function borrowed from http://www.quirksmode.org/js/cookies.html
  function readCookie() {
    var nameEQ = "privatemsg_unread_messages=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
  }

  var pmGrowlCheckNew = function() {
    $.getJSON(Drupal.settings.basePath + 'messages/pmgrowl_json', function(data) {
      var saved = readCookie();
      if ((data != saved) && (data > 0) && (navigator.userAgent.indexOf('Firefox/3.5b4') ==-1)) {
        $.jGrowl('You have <a href="' + Drupal.settings.basePath + 'messages">' + data + ' unread message(s)</a>.', { sticky: true, header: 'Unread Messages' });
        document.cookie =  'privatemsg_unread_messages=' + data + '; path=/;';
      }
    });
  }

  pmGrowlCheckNew();
  var messageTimer = setInterval(pmGrowlCheckNew, 60000);
});

Note that the bit (navigator.userAgent.indexOf('Firefox/3.5b4') ==-1) means that the notifications will not work on firefox 3.5beta4 - the cookie handling there is broken and new cookies do not seem to overwrite old ones

(I assume the 60000 number makes the script check every 60 seconds?).

Dave.Ingram’s picture

StatusFileSize
new6.68 KB

OK. Latest version sets is_new to false when you close an alert, so it doesn't pop up on subsequent page loads. It adds a settings form to set the number of seconds between page loads, which defaults now to 30 seconds.

I also did some code cleanup and added quite a bit more commenting to the JS file and slightly more validation for submissions.

naheemsays’s picture

Since the body can be trimmed for large messages, its probably not a good idea to mark as read when closing a notification - instead maybe a separate mark as read button?

Dave.Ingram’s picture

Status: Needs review » Closed (fixed)

This module now has an official project page. Let's move further discussion to the issue queue at http://www.drupal.org/project/pmgrowl. I can now accept proper patches for areas which still need improvement. Thanks!

litwol’s picture

Project: Privatemsg » Private Message Growl
Version: 6.x-1.x-dev »
Status: Closed (fixed) » Active

Very good job Dave. i think it was the right more.

naheemsays’s picture

Status: Active » Closed (fixed)