Creating first activity logs

In the previous post about installing heartbeat, i mentioned decision time as a choice between rules aproach and writing custom loggings based on own events. 

In this page, i will show a couple of events where i hooked into to log activity for. The message template being used is one of your choice (here only as example).

Note that the becoming friends is  little more difficult because there can be a logging duplicate needed to make things work. In that case we alter the logging by giving a duplicate flag.

Code: 
<?php
/**
 * Implementation of hook_nodeapi().
 */

function heartbeat_example_nodeapi(&$node, $op, $arg = 0) {

  if ($op == 'insert' && $node->type == 'story') {

    // When a story is inserted, log user activity
    // Compare this approach with page. The latter works
    // with rules doing the same as this example.
    $message_id = 'heartbeat_add_node';
    $variables = array(
      '@username' => l(user_load($node->uid)->name, 'user/' . $node->uid),
      '@node_type' => 'page',
      '@node_title' => l($node->title, 'node/' . $node->nid),
    );
    heartbeat_api_log($message_id, $node->uid, 0, $node->nid, 0, $variables);
  }
}
?>
<?php
/**
 * Implementation of hook_nodeapi().
 */

function custom_nodeapi(&$node, $op, $arg = 0) {

  if ($op == 'update' && $node->type == 'profile') {
    custom_update_profile_log_activity($node);
  }
}

/**
 * Callback function to hook into the closet updating action
 */

function custom_update_profile_log_activity($node) {

  // Suppose you have variable his/her needed. This is the way
  $message_id = 'heartbeat_profile_update';
  $variables = array(
    '@user' => l(user_load($node->uid)->name, 'user/' . $node->uid),
    '@her' => $node->field_gender[0]['value'] == 2 ? t('her') : t('his'),
  );
  heartbeat_api_log($message_id, $node->uid, 0, $node->nid, 0, $variables);
}

?>
<?php
/**
 * Implementation of hook_flag(). Trigger actions if any are available.
 *   This example suggests a flag used for becoming friends
 */

function custom_flag($action, $flag, $content_id, $account) {

  if ($action == 'flag' && $flag->content_type == 'user' && $flag->name == 'friend')  {
      custom_become_friends($account, user_load($content_id));
    }
  }
}

/**
 * Callback function to hook into the become friends action
 * @param $user
 *   User that performed the action.
 * @param $account
 *   User that is being flagged.
 */

function custom_become_friends($user, $account) {

  $user = (object) $user;

  $data['uid'] = $user->uid;
  $data['uid_target'] = $account->uid;
  $data['nid'] = 0;
  $data['nid_target'] = 0;
  $data['message_id'] = 'heartbeat_flag_become_friends';
  // Relational message of heartbeat messages
  $message = heartbeat_message_load($data['message_id']);
  $data['message'] = $message->message;
  $data['message_concat'] = $message->message_concat;
  $data['variables'] = array(
    '@user1' => l($user->name, 'user/' . $user->uid),
    '@her' => $node->field_gender[0]['value'] == 2 ? t('her') : t('his'),
    '@user2' => l($account->name, 'user/' . $user->uid),
  );
  $data['access'] = HEARTBEAT_PUBLIC_TO_ALL;

  heartbeat_log($data);

  // Do the same thing but the target user as actor
  // You should not use this when using normal flags
  // they are a one way relationship (which is weird for friends)

  $reverse = $data;
  $reverse['uid'] = $account->uid;
  $reverse['uid_target'] = $user->uid;
  $reverse['variables'] = array(
    '@her' => $node->field_gender[0]['value'] == 2 ? t('her') : t('his'),
    '@user1' => l($account->name, 'user/' . $user->uid),
    '@user2' => l($user->name, 'user/' . $user->uid),
    'duplicate' => TRUE,
  );
  $reverse['extra'] = array('duplicate' => TRUE);

  heartbeat_log($reverse);
 
}
?>