This page provides a skeleton to integrate a friendlist module with Privatemsg and provide features like autocomplete for friends and blocking messages from users that aren't friends.


function mymodule_privatemsg_sql_autocomplete(&$fragments, $search, $names) {
  global $user;
  // Extend the query that searches for usernames
  
  // $fragments is explained in the api documentation in detail
  
  // The query is already set up, it's searching for usernames which start with $search and are not $names (may be empty)
  // the main table is {user} a
  
  // for example, add a join on a table where the user connections are stored
  // and specify that only users connected with the current user should be loaded
  $fragments['inner_join'] = 'INNER JOIN {my_table} m ON (m.user1 = u.uid AND m.user2 = %d)';
  $fragments['query_args'][] = $user->uid;
}

// This blocks messages between users, who are not related, it uses universal_relation_api so in theory should work with User Relationships and FriendsList modules, tested with User Relationships.

function mymodule_privatemsg_block_message($author, $recipients) {
  $blocked = array();
  foreach ($recipients as $recipient) {
	if (!module_invoke_all('socnet_is_related', $author->uid, $recipient->uid)) {
	  $blocked[] = array(
        'uid' => $recipient->uid,
        'message' => t('!name is not a friend of yours.', array('!name' => $recipient->name))
      );
    }
  }
  return $blocked;
}

Comments

MathRo’s picture

I tried to make it with D7 and it's working fine.
I just had to add "recipient" in $blocked as followed

 $blocked[] = array(
        'uid' => $recipient->uid,
        'recipient' => 'user_' . $recipient->uid,
        'message' => 'Ce referent ne vous suit pas',
     );
petermallett’s picture

The above hook implementation has a bug that took me a little while to work out. The $blocked array's first key is 'recipient', not 'uid' and the value for it is the key of the $recipients array, not the uid of the recipient. Here's the documentation for the hook: http://api.worldempire.ch/api/privatemsg/privatemsg.api.php/function/hoo...
and the version I have working:

function mymodule_privatemsg_block_message($author, $recipients, $context = array()) {
  $blocked = array();
  foreach ($recipients as $recipient_id => $recipient) {
    // Deny/block if the recipient is not the users' friend
    if (!module_invoke_all('socnet_is_related', $author->uid, $recipient->uid)) {
      $blocked[] = array(
        'recipient' => $recipient_id,
        'message' => t('!name is not a friend of yours.', array('!name' => $recipient->name))
      );
    }
  }
  return $blocked;
}

Also, an implementation of hook_privatemsg_sql_autocomplete_alter that works with Friendlist:

function mymodule_privatemsg_sql_autocomplete_alter(&$fragments, $search, $names) {
  global $user;
   $fragments['inner_join'][] = 'INNER JOIN {friendlist_statuses} m ON ((m.requester_id = u.uid AND m.requestee_id = %d) OR (m.requester_id = %d AND m.requestee_id = u.uid))';
   $fragments['query_args']['join'][] = $user->uid;
   $fragments['query_args']['join'][] = $user->uid;
   $fragments['where'][] = "m.status = 'TW_BOTH'";
}