You are here

private function heartbeatParser::remove_message_duplicates in Heartbeat 7

Same name and namespace in other branches
  1. 6.4 includes/heartbeatparser.inc \heartbeatParser::remove_message_duplicates()
  2. 6.3 includes/heartbeatparser.inc \heartbeatParser::remove_message_duplicates()

Function to remove duplicate messages Duplicate messages whish are exactly the same as already build messages, delete them and increment the count of the message instance Duplicate reversive messages that occur at the same server request time are deleted as well

1 call to heartbeatParser::remove_message_duplicates()
heartbeatParser::prepare_candidates in includes/heartbeatparser.inc
Prepare message candidates This is an important method that handles several things

File

includes/heartbeatparser.inc, line 30
HeartbeatParser object Parses database messages into a well formed stream of activity messages.

Class

heartbeatParser
Class heartbeatParser

Code

private function remove_message_duplicates($message_set) {
  global $user;

  // hash holding all unique values
  $duplicates = array();
  $users_relations = array();
  foreach ($message_set as $key => $message) {

    // Remove duplicates that were created by duplicate user logging
    // This happens when two users were active for the same event
    if (isset($message->variables['duplicate']) && $message->variables['duplicate']) {
      $users_relations[$message->message_id][$message->uid . '-' . $message->uid_target] = array(
        'duplicate' => isset($users_relations[$message->message_id][$message->uid_target . '-' . $message->uid]) ? TRUE : FALSE,
        'uid' => $message->uid,
        'relation' => $message->uid_target,
        'message_id' => $message->message_id,
        'key' => $key,
      );
    }
  }
  if (!empty($users_relations)) {
    foreach ($users_relations as $type => $relations) {

      // Check which of the duplicates should be second
      foreach ($relations as $uid => $relationship) {
        $uid = $relationship['uid'];
        $rel_uid = $relationship['relation'];
        if ($user->uid == $uid && isset($relations[$rel_uid . '-' . $uid])) {
          $duplicates[] = $relations[$rel_uid . '-' . $uid]['key'];
        }
        elseif ($user->uid == $rel_uid) {
          $duplicates[] = $relationship['key'];
        }
        elseif (isset($relations[$rel_uid . '-' . $uid]) && $relationship['duplicate']) {
          $duplicates[] = $relations[$rel_uid . '-' . $uid]['key'];
        }
      }
    }
  }
  $duplicates = array_unique($duplicates);
  foreach ($duplicates as $duplicate_key => $duplicate) {
    unset($message_set[$duplicate]);
  }
  return $message_set;
}