Allow modules to alter the items gathered by activitystream submodules: hook_activitystream_items_alter()
| Project: | Activity Stream |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | agaric |
| Status: | needs work |
| Issue tags: | DX (Developer Experience) |
Jump to:
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!
<?php
/**
* 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

#1
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.
#2
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.