This is quite a simple addition, but it would make possible for third parties to integrate other modules (i.e. email/messaging/rules/heartbeat integration). This is my proposal (mentions.module, from line 134).


/**
 * Insert and Remove mentions on node/comment update/insert.
 */
function _mentions_update($type, $mentions, $mid, $auid) {
  // Build array of old mentions.
  $old_users = array();
  $result = db_query(
    "SELECT uid FROM {mentions} WHERE type = '%s' AND mid = %d", $type, $mid
  );
  while ($data = db_fetch_object($result)) {
    $old_users[] = $data->uid;
  }

  // Build array of new mentions.
  $new_users = array();
  foreach ($mentions as $mention) {
    if (!in_array($mention['user']->uid, $new_users)) {
      $new_users[] = $mention['user']->uid;
    }
  }

  // Add new user mentions.
  foreach (array_diff($new_users, $old_users) as $user) {
    db_query(
      "INSERT INTO {mentions} (type, mid, uid, auid, timestamp) VALUES ('%s', %d, %d, %d, %d)",
      $type, $mid, $user, $auid, time()
    );
	module_invoke_all('mention_added', $type, $mid, $auid, $user);
  }

  // Remove old mentions.
  foreach (array_diff($old_users, $new_users) as $user) {
    db_query(
      "DELETE FROM {mentions} WHERE type = '%s' AND mid = %d AND uid = %d", $type, $mid, $user
    );
	module_invoke_all('mention_removed', $type, $mid, $auid, $user);
  }
}

/**
 * Remove mentions on node/comment deletion.
 */
function _mentions_delete($type, $mid) {
  $result = db_query(
    "SELECT auid, uid FROM {mentions} WHERE type = '%s' AND mid = %d", $type, $mid
  );
  while ($data = db_fetch_object($result)) {
    module_invoke_all('mention_removed', $type, $mid, $data->auid, $data->uid);
  }
  
  db_query("DELETE FROM {mentions} WHERE type = '%s' AND mid = %d", $type, $mid);
}

CommentFileSizeAuthor
#2 hook-add-remove-912016-2.patch2.38 KBdsnopek

Comments

dsnopek’s picture

I second this suggestion! It could be used to implement the AppBar integration as well: #827812: Integration with Appbar

However, I think it'd be better if the hook was called only once with the full list. This could potentially allow for some optimizations on the part of the module that's hooking in.

I also worry a little about the remove hook. A node being deleted (and so all the mentions are going a way) is a little different than someone actually editing the node and removing the mention. In second case you might want to tell the user, but in the first case it probably doesn't make sense (because the node is now gone, you can't link to it).

So, for hook_mentions_removed() we should somehow distinguish between the two cases.

Given how old this issue is, I assume the maintainer isn't crazy about this idea. :-/ But if I end up using mentions, I may create a patch for it anyway.

dsnopek’s picture

Status: Active » Needs review
StatusFileSize
new2.38 KB

Attached is a patch that attempts to add these hooks.

I'm still not very happy about the hook_mentions_removed() so I'd love to hear any ideas about that! It would be much easier if there wasn't the $auid argument - then _mentions_delete() could make a single call to the hook, rather than looping. I suspect that $auid might not even be necessary on delete.

If we removed it, we could also make a seperate hook_mentions_removed_full() hook instead of the flag. That flag bothers me a little too. For modules where the full was the same as the individual, the one could call the other.

Please let me know what you think!

deciphered’s picture

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

I will be doing this in the 7.x-1.x branch, as well as Rules integration.

Once that's done, if someone wants to RTBC the D6 version/backport and the patch looks usable I'll possibly commit it, but my interest in D6 dev is very low.

dsnopek’s picture

Ah, that's great to hear that development is continuing! I look forward to seeing your Drupal 7 version. :-)

deciphered’s picture

Status: Needs review » Fixed

Hooks and Rules integration has been committed to 7.x-1.x, dev release should be out within the next 12 hours.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.