A one-line addition to activitystream.module, no effect if not used, would allow other modules to do quite powerful customizations of incoming streams.

The following example implementation of the proposed hook_activitystream_items_alter() is pretty crazy and may not reflect the views of its own author, but it does show the power this hook will bring.

Skip to the first comment for the actual patch, this is just a reason why!

/**
 * Implementation of hook_activitystream_items_alter().
 * 
 * Agaric added this hook to activitystream.module to make it possible to 
 * remove duplicates and modify items before they are saved to activitystream
 * nodes.
 */
function anjali_activitystream_items_alter(&$items, $user) {
  // Facebook user name, hardcoded here but could be made configurable.
  $fbname = 'Anjali';
  foreach ($items as $key => $activity) {
    // Standardize item information.  Version in title, plain body.
    if ($user->module == 'activitystream_twitter') {
      // This is from activitystream_twitter, and yes we reverse what they do.
      $userid = $user->userid;
      $items[$key]['title'] = $userid . ': ' . $activity['title'];
      $items[$key]['body'] = substr($activity['body'], strlen($userid) + 2);
      // Only look for new tweets.  Remove re-tweets from Facebook.
      $compare = substr(strip_tags($items[$key]['body']), 0, 140);
      $sql = "SELECT n.nid, n.title FROM {node} n LEFT JOIN {node_revisions} nr ON n.vid = nr.vid WHERE n.type = 'activitystream' AND nr.body LIKE '%s%%' LIMIT 1";
      if ($dup = db_fetch_object(db_query($sql, $compare))) {
        // We have a suspected duplicate that will not be saved.
        $msg = "Did not import %title because it appeared to be a duplicate of !dup";
        $vars = array(
          '%title' => $items[$key]['title'],
          '!dup' => l($dup->title, 'node/' . $dup->nid),
        );
        watchdog('anjali', $msg, $vars, WATCHDOG_NOTICE, $activity['link']);
        unset($items[$key]);
      }
      else {
        drupal_set_message('not a dupe: ' . $compare);
      }
    }
  }
}

(Should I be submitting patches for new features to the 2.x branch rather than the 1.x? This is a very simple way to extend the modules power that I hope can be committed to the upcoming release, and I'd be happy to additionally do a patch or review the "alterability" of Activitystream 6.x 2.x!)

ben, agaric

Comments

mlncn’s picture

Status: Active » Needs review

Patch attached, creates a hook_activitystream_items_alter() which does nothing in activitystream currently, but allows other contrib and custom modules to do very exciting things.

akalsey’s picture

Version: 6.x-1.0-rc2 » 6.x-2.x-dev
Status: Needs review » Needs work

All new development is going on in 2.x -- no new features will be added to 1.x.

Your issue and comment are both missing the actual patch.

I like the concept and would love to get this into Activity Stream.

mlncn’s picture

Status: Needs work » Needs review
StatusFileSize
new596 bytes

I'm blaming Drupal.org for not attaching the patch, and not telling me about the follow-up, last year. I really thought this got committed but that was #582918: Permit activitystream items to be edited- current save of activitystream nodes doesn't change the body.

So here we go, a better attempt at this patch.

mlncn’s picture

This is the updated example of making use of that hook:

/**
 * Implements hook_activitystream_activity_alter().
 */
function anjali_activitystream_activity_alter(&$activity, $user) {
  // Modifications if an activity is from the twitter activitystream_twitter.
  if ($user->module == 'activitystream_twitter') {
    // Only look for new tweets.  Remove re-tweets from Facebook.
    $compare = substr(strip_tags($activity['body']), 0, 140);
    $sql = "SELECT n.nid, n.title FROM {node} n LEFT JOIN {node_revisions} nr ON n.vid = nr.vid WHERE n.type = 'activitystream' AND nr.body LIKE '%s%%' LIMIT 1";
    // Check if duplicate and if so, blank it out.
    if ($dup = db_fetch_object(db_query($sql, $compare))) {
      $msg = "Did not import %title because it appeared to be a duplicate of !dup";
      $vars = array(
        '%title' => $items[$key]['title'],
        '!dup' => l($dup->title, 'node/' . $dup->nid),
      );
      watchdog('anjali', $msg, $vars, WATCHDOG_NOTICE, $activity['link']);
      $activity = NULL;
      return;
    }
    else {
      watchdog('anjali', 'Not dup: ' . $compare, NULL, WATCHDOG_DEBUG, $activity['link']);
    }

    // Standardize item information.  Username in title, plain body.
    // And yes, we reverse what they do.
    $userid = $user->userid;
    $activity['title'] = $userid . ': ' . $activity['title'];
    $activity['body'] = substr($activity['body'], strlen($userid) + 2);
  }
}
morbus iff’s picture

Status: Needs review » Fixed

This is available in 7.x-3.x using hook_activitystream_SERVICE_item_alter(). Example in activitystream.api.php.

Status: Fixed » Closed (fixed)
Issue tags: -DX (Developer Experience)

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