add some activity hooks

This commit is contained in:
Evan Prodromou 2010-08-02 17:16:04 -07:00
parent db90bcb329
commit fe2b4fdf1c

View File

@ -1184,6 +1184,9 @@ class Notice extends Memcached_DataObject
return $groups; return $groups;
} }
// This has gotten way too long. Needs to be sliced up into functional bits
// or ideally exported to a utility class.
function asAtomEntry($namespace=false, $source=false, $author=true, $cur=null) function asAtomEntry($namespace=false, $source=false, $author=true, $cur=null)
{ {
$profile = $this->getProfile(); $profile = $this->getProfile();
@ -1203,74 +1206,160 @@ class Notice extends Memcached_DataObject
$attrs = array(); $attrs = array();
} }
$xs->elementStart('entry', $attrs); if (Event::handle('StartActivityStart', array(&$this, &$xs, &$attrs))) {
$xs->elementStart('entry', $attrs);
Event::handle('EndActivityStart', array(&$this, &$xs, &$attrs));
}
if ($source) { if (Event::handle('StartActivitySource', array(&$this, &$xs))) {
$xs->elementStart('source');
$xs->element('id', null, $profile->profileurl); if ($source) {
$xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
$xs->element('link', array('href' => $profile->profileurl)); $xs->elementStart('source');
$user = User::staticGet('id', $profile->id);
if (!empty($user)) { $xs->element('id', null, $profile->profileurl);
$atom_feed = common_local_url('ApiTimelineUser', $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
array('format' => 'atom', $xs->element('link', array('href' => $profile->profileurl));
'id' => $profile->nickname));
$xs->element('link', array('rel' => 'self', $user = User::staticGet('id', $profile->id);
'type' => 'application/atom+xml',
'href' => $profile->profileurl)); if (!empty($user)) {
$xs->element('link', array('rel' => 'license', $atom_feed = common_local_url('ApiTimelineUser',
'href' => common_config('license', 'url'))); array('format' => 'atom',
'id' => $profile->nickname));
$xs->element('link', array('rel' => 'self',
'type' => 'application/atom+xml',
'href' => $profile->profileurl));
$xs->element('link', array('rel' => 'license',
'href' => common_config('license', 'url')));
}
$xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
$xs->element('updated', null, common_date_w3dtf($this->created)); // FIXME: not true!
$xs->elementEnd('source');
} }
Event::handle('EndActivitySource', array(&$this, &$xs));
$xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
$xs->element('updated', null, common_date_w3dtf($this->created));
} }
if ($source) { $title = common_xml_safe_str($this->content);
$xs->elementEnd('source');
if (Event::handle('StartActivityTitle', array(&$this, &$xs, &$title))) {
$xs->element('title', null, $title);
Event::handle('EndActivityTitle', array($this, &$xs, $title));
} }
$xs->element('title', null, common_xml_safe_str($this->content)); $atomAuthor = '';
if ($author) { if ($author) {
$xs->raw($profile->asAtomAuthor($cur)); $atomAuthor = $profile->asAtomAuthor($cur);
$xs->raw($profile->asActivityActor());
} }
$xs->element('link', array('rel' => 'alternate', if (Event::handle('StartActivityAuthor', array(&$this, &$xs, &$atomAuthor))) {
'type' => 'text/html', if (!empty($atomAuthor)) {
'href' => $this->bestUrl())); $xs->raw($atomAuthor);
Event::handle('EndActivityAuthor', array(&$this, &$xs, &$atomAuthor));
$xs->element('id', null, $this->uri);
$xs->element('published', null, common_date_w3dtf($this->created));
$xs->element('updated', null, common_date_w3dtf($this->created));
$source = null;
$ns = $this->getSource();
if ($ns) {
if (!empty($ns->name) && !empty($ns->url)) {
$source = '<a href="'
. htmlspecialchars($ns->url)
. '" rel="nofollow">'
. htmlspecialchars($ns->name)
. '</a>';
} else {
$source = $ns->code;
} }
} }
$noticeInfoAttr = array( $actor = '';
'local_id' => $this->id, // local notice ID (useful to clients for ordering)
'source' => $source, // the client name (source attribution) if ($author) {
); $actor = $profile->asActivityActor();
}
if (Event::handle('StartActivityActor', array(&$this, &$xs, &$actor))) {
if (!empty($actor)) {
$xs->raw($actor);
Event::handle('EndActivityActor', array(&$this, &$xs, &$actor));
}
}
$url = $this->bestUrl();
if (Event::handle('StartActivityLink', array(&$this, &$xs, &$url))) {
$xs->element('link', array('rel' => 'alternate',
'type' => 'text/html',
'href' => $url));
Event::handle('EndActivityLink', array(&$this, &$xs, $url));
}
$id = $this->uri;
if (Event::handle('StartActivityId', array(&$this, &$xs, &$id))) {
$xs->element('id', null, $id);
Event::handle('EndActivityId', array(&$this, &$xs, $id));
}
$published = common_date_w3dtf($this->created);
if (Event::handle('StartActivityPublished', array(&$this, &$xs, &$published))) {
$xs->element('published', null, $published);
Event::handle('EndActivityPublished', array(&$this, &$xs, $published));
}
$updated = $published; // XXX: notices are usually immutable
if (Event::handle('StartActivityUpdated', array(&$this, &$xs, &$updated))) {
$xs->element('updated', null, $updated);
Event::handle('EndActivityUpdated', array(&$this, &$xs, $updated));
}
$content = common_xml_safe_str($this->rendered);
if (Event::handle('StartActivityContent', array(&$this, &$xs, &$content))) {
$xs->element('content', array('type' => 'html'), $content);
Event::handle('EndActivityContent', array(&$this, &$xs, $content));
}
// Most of our notices represent POSTing a NOTE. This is the default verb
// for activity streams, so we normally just leave it out.
$verb = ActivityVerb::POST;
if (Event::handle('StartActivityVerb', array(&$this, &$xs, &$verb))) {
$xs->element('activity:verb', null, $verb);
Event::handle('EndActivityVerb', array(&$this, &$xs, $verb));
}
// We use the default behavior for activity streams: if there's no activity:object,
// then treat the entry itself as the object. Here, you can set the type of that object,
// which is normally a NOTE.
$type = ActivityObject::NOTE;
if (Event::handle('StartActivityDefaultObjectType', array(&$this, &$xs, &$type))) {
$xs->element('activity:object-type', null, $type);
Event::handle('EndActivityDefaultObjectType', array(&$this, &$xs, $type));
}
// Since we usually use the entry itself as an object, we don't have an explicit
// object. Some extensions may want to add them (for photo, event, music, etc.).
$objects = array();
if (Event::handle('StartActivityObjects', array(&$this, &$xs, &$objects))) {
foreach ($objects as $object) {
$xs->raw($object->asString());
}
Event::handle('EndActivityObjects', array(&$this, &$xs, $objects));
}
$noticeInfoAttr = array('local_id' => $this->id); // local notice ID (useful to clients for ordering)
$ns = $this->getSource(); $ns = $this->getSource();
if ($ns) {
if (!empty($ns)) {
$noticeInfoAttr['source'] = $ns->code;
if (!empty($ns->url)) { if (!empty($ns->url)) {
$noticeInfoAttr['source_link'] = $ns->url; $noticeInfoAttr['source_link'] = $ns->url;
if (!empty($ns->name)) {
$noticeInfoAttr['source'] = '<a href="'
. htmlspecialchars($ns->url)
. '" rel="nofollow">'
. htmlspecialchars($ns->name)
. '</a>';
}
} }
} }
@ -1284,103 +1373,139 @@ class Notice extends Memcached_DataObject
$noticeInfoAttr['repeat_of'] = $this->repeat_of; $noticeInfoAttr['repeat_of'] = $this->repeat_of;
} }
$xs->element('statusnet:notice_info', $noticeInfoAttr, null); if (Event::handle('StartActivityNoticeInfo', array(&$this, &$xs, &$noticeInfoAttr))) {
$xs->element('statusnet:notice_info', $noticeInfoAttr, null);
Event::handle('EndActivityNoticeInfo', array(&$this, &$xs, $noticeInfoAttr));
}
$replyNotice = null;
if ($this->reply_to) { if ($this->reply_to) {
$reply_notice = Notice::staticGet('id', $this->reply_to); $replyNotice = Notice::staticGet('id', $this->reply_to);
if (!empty($reply_notice)) { }
if (Event::handle('StartActivityInReplyTo', array(&$this, &$xs, &$replyNotice))) {
if (!empty($replyNotice)) {
$xs->element('link', array('rel' => 'related', $xs->element('link', array('rel' => 'related',
'href' => $reply_notice->bestUrl())); 'href' => $replyNotice->bestUrl()));
$xs->element('thr:in-reply-to', $xs->element('thr:in-reply-to',
array('ref' => $reply_notice->uri, array('ref' => $replyNotice->uri,
'href' => $reply_notice->bestUrl())); 'href' => $replyNotice->bestUrl()));
Event::handle('EndActivityInReplyTo', array(&$this, &$xs, $replyUri, $replyUrl));
} }
} }
$conv = null;
if (!empty($this->conversation)) { if (!empty($this->conversation)) {
$conv = Conversation::staticGet('id', $this->conversation); $conv = Conversation::staticGet('id', $this->conversation);
if (!empty($conv)) {
$xs->element(
'link', array(
'rel' => 'ostatus:conversation',
'href' => $conv->uri
)
);
}
} }
if (Event::handle('StartActivityConversation', array(&$this, &$xs, &$conv))) {
if (!empty($conv)) {
$xs->element('link', array('rel' => 'ostatus:conversation',
'href' => $conv->uri));
}
Event::handle('EndActivityConversation', array(&$this, &$xs, $conv));
}
$replyProfiles = array();
$reply_ids = $this->getReplies(); $reply_ids = $this->getReplies();
foreach ($reply_ids as $id) { foreach ($reply_ids as $id) {
$profile = Profile::staticGet('id', $id); $profile = Profile::staticGet('id', $id);
if (!empty($profile)) { if (!empty($profile)) {
$xs->element( $replyProfiles[] = $profile;
'link', array(
'rel' => 'ostatus:attention',
'href' => $profile->getUri()
)
);
} }
} }
if (Event::handle('StartActivityAttentionProfiles', array(&$this, &$xs, &$replyProfiles))) {
foreach ($replyProfiles as $profile) {
$xs->element('link', array('rel' => 'ostatus:attention',
'href' => $profile->getUri()));
}
Event::handle('EndActivityAttentionProfiles', array(&$this, &$xs, $replyProfiles));
}
$groups = $this->getGroups(); $groups = $this->getGroups();
foreach ($groups as $group) { if (Event::handle('StartActivityAttentionGroups', array(&$this, &$xs, &$groups))) {
$xs->element( foreach ($groups as $group) {
'link', array( $xs->element('link', array('rel' => 'ostatus:attention',
'rel' => 'ostatus:attention', 'href' => $group->permalink()));
'href' => $group->permalink() }
) Event::handle('EndActivityAttentionGroups', array(&$this, &$xs, $groups));
);
} }
$repeat = null;
if (!empty($this->repeat_of)) { if (!empty($this->repeat_of)) {
$repeat = Notice::staticGet('id', $this->repeat_of); $repeat = Notice::staticGet('id', $this->repeat_of);
}
if (Event::handle('StartActivityForward', array(&$this, &$xs, &$repeat))) {
if (!empty($repeat)) { if (!empty($repeat)) {
$xs->element( $xs->element('ostatus:forward',
'ostatus:forward', array('ref' => $repeat->uri,
array('ref' => $repeat->uri, 'href' => $repeat->bestUrl()) 'href' => $repeat->bestUrl()));
);
} }
Event::handle('EndActivityForward', array(&$this, &$xs, $repeat));
} }
$xs->element( $tags = $this->getTags();
'content',
array('type' => 'html'),
common_xml_safe_str($this->rendered)
);
$tag = new Notice_tag(); if (Event::handle('StartActivityCategories', array(&$this, &$xs, &$tags))) {
$tag->notice_id = $this->id; foreach ($tags as $tag) {
if ($tag->find()) { $xs->element('category', array('term' => $tag));
while ($tag->fetch()) {
$xs->element('category', array('term' => $tag->tag));
} }
Event::handle('EndActivityCategories', array(&$this, &$xs, $tags));
} }
$tag->free();
# Enclosures // Enclosures
$enclosures = array();
$attachments = $this->attachments(); $attachments = $this->attachments();
if($attachments){
foreach($attachments as $attachment){ foreach ($attachments as $attachment) {
$enclosure=$attachment->getEnclosure(); $enclosure = $attachment->getEnclosure();
if ($enclosure) { if ($enclosure) {
$attributes = array('rel'=>'enclosure','href'=>$enclosure->url,'type'=>$enclosure->mimetype,'length'=>$enclosure->size); $enclosures[] = $enclosure;
if($enclosure->title){
$attributes['title']=$enclosure->title;
}
$xs->element('link', $attributes, null);
}
} }
} }
if (!empty($this->lat) && !empty($this->lon)) { if (Event::handle('StartActivityEnclosures', array(&$this, &$xs, &$enclosures))) {
$xs->element('georss:point', null, $this->lat . ' ' . $this->lon); foreach ($enclosures as $enclosure) {
$attributes = array('rel' => 'enclosure',
'href' => $enclosure->url,
'type' => $enclosure->mimetype,
'length' => $enclosure->size);
if ($enclosure->title) {
$attributes['title'] = $enclosure->title;
}
$xs->element('link', $attributes, null);
}
Event::handle('EndActivityEnclosures', array(&$this, &$xs, $enclosures));
} }
$xs->elementEnd('entry'); $lat = $this->lat;
$lon = $this->lon;
if (Event::handle('StartActivityGeo', array(&$this, &$xs, &$lat, &$lon))) {
if (!empty($lat) && !empty($lon)) {
$xs->element('georss:point', null, $lat . ' ' . $lon);
}
Event::handle('EndActivityGeo', array(&$this, &$xs, $lat, $lon));
}
if (Event::handle('StartActivityEnd', array(&$this, &$xs))) {
$xs->elementEnd('entry');
Event::handle('EndActivityEnd', array(&$this, &$xs));
}
return $xs->getString(); return $xs->getString();
} }
@ -1901,4 +2026,17 @@ class Notice extends Memcached_DataObject
$this->is_local == Notice::LOCAL_NONPUBLIC); $this->is_local == Notice::LOCAL_NONPUBLIC);
} }
public function getTags()
{
$tags = array();
$tag = new Notice_tag();
$tag->notice_id = $this->id;
if ($tag->find()) {
while ($tag->fetch()) {
$tags[] = $tag->tag;
}
}
$tag->free();
return $tags;
}
} }