From fe2b4fdf1c16eb2ecbbe1cd5e7f0fc1f477799ad Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 2 Aug 2010 17:16:04 -0700 Subject: [PATCH 1/9] add some activity hooks --- classes/Notice.php | 358 +++++++++++++++++++++++++++++++-------------- 1 file changed, 248 insertions(+), 110 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index 8552248bad..592e2f3842 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1184,6 +1184,9 @@ class Notice extends Memcached_DataObject 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) { $profile = $this->getProfile(); @@ -1203,74 +1206,160 @@ class Notice extends Memcached_DataObject $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) { - $xs->elementStart('source'); - $xs->element('id', null, $profile->profileurl); - $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name')); - $xs->element('link', array('href' => $profile->profileurl)); - $user = User::staticGet('id', $profile->id); - if (!empty($user)) { - $atom_feed = common_local_url('ApiTimelineUser', - 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'))); + if (Event::handle('StartActivitySource', array(&$this, &$xs))) { + + if ($source) { + + $xs->elementStart('source'); + + $xs->element('id', null, $profile->profileurl); + $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name')); + $xs->element('link', array('href' => $profile->profileurl)); + + $user = User::staticGet('id', $profile->id); + + if (!empty($user)) { + $atom_feed = common_local_url('ApiTimelineUser', + 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'); } - - $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE)); - $xs->element('updated', null, common_date_w3dtf($this->created)); + Event::handle('EndActivitySource', array(&$this, &$xs)); } - if ($source) { - $xs->elementEnd('source'); + $title = common_xml_safe_str($this->content); + + 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) { - $xs->raw($profile->asAtomAuthor($cur)); - $xs->raw($profile->asActivityActor()); + $atomAuthor = $profile->asAtomAuthor($cur); } - $xs->element('link', array('rel' => 'alternate', - 'type' => 'text/html', - 'href' => $this->bestUrl())); - - $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 = '' - . htmlspecialchars($ns->name) - . ''; - } else { - $source = $ns->code; + if (Event::handle('StartActivityAuthor', array(&$this, &$xs, &$atomAuthor))) { + if (!empty($atomAuthor)) { + $xs->raw($atomAuthor); + Event::handle('EndActivityAuthor', array(&$this, &$xs, &$atomAuthor)); } } - $noticeInfoAttr = array( - 'local_id' => $this->id, // local notice ID (useful to clients for ordering) - 'source' => $source, // the client name (source attribution) - ); + $actor = ''; + + 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(); - if ($ns) { + + if (!empty($ns)) { + $noticeInfoAttr['source'] = $ns->code; if (!empty($ns->url)) { $noticeInfoAttr['source_link'] = $ns->url; + if (!empty($ns->name)) { + $noticeInfoAttr['source'] = '' + . htmlspecialchars($ns->name) + . ''; + } } } @@ -1284,103 +1373,139 @@ class Notice extends Memcached_DataObject $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) { - $reply_notice = Notice::staticGet('id', $this->reply_to); - if (!empty($reply_notice)) { + $replyNotice = Notice::staticGet('id', $this->reply_to); + } + + if (Event::handle('StartActivityInReplyTo', array(&$this, &$xs, &$replyNotice))) { + if (!empty($replyNotice)) { $xs->element('link', array('rel' => 'related', - 'href' => $reply_notice->bestUrl())); + 'href' => $replyNotice->bestUrl())); $xs->element('thr:in-reply-to', - array('ref' => $reply_notice->uri, - 'href' => $reply_notice->bestUrl())); + array('ref' => $replyNotice->uri, + 'href' => $replyNotice->bestUrl())); + Event::handle('EndActivityInReplyTo', array(&$this, &$xs, $replyUri, $replyUrl)); } } + $conv = null; + if (!empty($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(); foreach ($reply_ids as $id) { $profile = Profile::staticGet('id', $id); - if (!empty($profile)) { - $xs->element( - 'link', array( - 'rel' => 'ostatus:attention', - 'href' => $profile->getUri() - ) - ); + if (!empty($profile)) { + $replyProfiles[] = $profile; } } + 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(); - foreach ($groups as $group) { - $xs->element( - 'link', array( - 'rel' => 'ostatus:attention', - 'href' => $group->permalink() - ) - ); + if (Event::handle('StartActivityAttentionGroups', array(&$this, &$xs, &$groups))) { + foreach ($groups as $group) { + $xs->element('link', array('rel' => 'ostatus:attention', + 'href' => $group->permalink())); + } + Event::handle('EndActivityAttentionGroups', array(&$this, &$xs, $groups)); } + $repeat = null; + if (!empty($this->repeat_of)) { $repeat = Notice::staticGet('id', $this->repeat_of); + } + + if (Event::handle('StartActivityForward', array(&$this, &$xs, &$repeat))) { if (!empty($repeat)) { - $xs->element( - 'ostatus:forward', - array('ref' => $repeat->uri, 'href' => $repeat->bestUrl()) - ); + $xs->element('ostatus:forward', + array('ref' => $repeat->uri, + 'href' => $repeat->bestUrl())); } + + Event::handle('EndActivityForward', array(&$this, &$xs, $repeat)); } - $xs->element( - 'content', - array('type' => 'html'), - common_xml_safe_str($this->rendered) - ); + $tags = $this->getTags(); - $tag = new Notice_tag(); - $tag->notice_id = $this->id; - if ($tag->find()) { - while ($tag->fetch()) { - $xs->element('category', array('term' => $tag->tag)); + if (Event::handle('StartActivityCategories', array(&$this, &$xs, &$tags))) { + foreach ($tags as $tag) { + $xs->element('category', array('term' => $tag)); } + Event::handle('EndActivityCategories', array(&$this, &$xs, $tags)); } - $tag->free(); - # Enclosures + // Enclosures + + $enclosures = array(); + $attachments = $this->attachments(); - if($attachments){ - foreach($attachments as $attachment){ - $enclosure=$attachment->getEnclosure(); - if ($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); - } + + foreach ($attachments as $attachment) { + $enclosure = $attachment->getEnclosure(); + if ($enclosure) { + $enclosures[] = $enclosure; } } - if (!empty($this->lat) && !empty($this->lon)) { - $xs->element('georss:point', null, $this->lat . ' ' . $this->lon); + if (Event::handle('StartActivityEnclosures', array(&$this, &$xs, &$enclosures))) { + 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(); } @@ -1901,4 +2026,17 @@ class Notice extends Memcached_DataObject $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; + } } From 936f97b91479ece8ddc942bdb87c117f5d200a81 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 2 Aug 2010 17:56:23 -0700 Subject: [PATCH 2/9] document activity entry hooks --- EVENTS.txt | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) diff --git a/EVENTS.txt b/EVENTS.txt index cf9c6123f3..7784e7d42e 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -818,3 +818,230 @@ EndDeleteUser: handling the post for deleting a user - $action: action being shown - $user: user being deleted +StartActivityStart: starting the output for a notice activity +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$attrs: attributes (mostly namespace declarations, if any) + +EndActivityStart: end the opening tag for an activity +- &$notice: notice being output +- &$xs: XMLStringer for output +- $attrs: attributes (mostly namespace declarations, if any) + +StartActivitySource: before outputting the element for a notice activity +- &$notice: notice being output +- &$xs: XMLStringer for output + +EndActivitySource: after outputting the element for a notice activity +- &$notice: notice being output +- &$xs: XMLStringer for output + +StartActivityTitle: before outputting notice activity title +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$title: title of the notice, mutable + +EndActivityTitle: after outputting notice activity title +- $notice: notice being output +- &$xs: XMLStringer for output +- $title: title of the notice + +StartActivityAuthor: before outputting atom author +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$atomAuthor: string for XML representing atom author + +EndActivityAuthor: after outputting atom author +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$atomAuthor: string for XML representing atom author + +StartActivityActor: before outputting activity actor element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$actor: string for XML representing activity actor + +EndActivityActor: after outputting activity actor element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$actor: string for XML representing activity actor + +StartActivityLink: before outputting activity HTML link element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$url: URL for activity HTML link element for a notice activity entry + +EndActivityLink: before outputting activity HTML link element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $url: URL for activity HTML link element for a notice activity entry + +StartActivityId: before outputting atom:id element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$id: atom:id (notice URI by default) + +EndActivityId: after outputting atom:id element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $id: atom:id (notice URI by default) + +StartActivityPublished: before outputting atom:published element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$published: atom:published value (notice created by default) + +EndActivityPublished: before outputting atom:published element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $published: atom:published value (notice created by default) + +StartActivityUpdated: before outputting atom:updated element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$updated: atom:updated value (same as atom:published by default) + +EndActivityUpdated: after outputting atom:updated element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $updated: atom:updated value (same as atom:published by default) + +StartActivityContent: before outputting atom:content element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$content: atom:content value (notice rendered HTML by default) + +EndActivityContent: after outputting atom:content element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $content: atom:content value (notice rendered HTML by default) + +StartActivityVerb: before outputting activity:verb element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$verb: activity:verb URI ('http://activitystrea.ms/schema/1.0/post' by default) + +EndActivityVerb: after outputting activity:verb element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $verb: activity:verb URI ('http://activitystrea.ms/schema/1.0/post' by default) + +StartActivityDefaultObjectType: before outputting activity:object-type element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$type: activity:object-type URI for default object ('http://activitystrea.ms/schema/1.0/note' by default) + +EndActivityDefaultObjectType: after outputting activity:verb element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $type: activity:object-type URI for default object ('http://activitystrea.ms/schema/1.0/note' by default) + +StartActivityObjects: before outputting activity:object elements for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$objects: array of ActivityObject objects to output (empty by default) + +EndActivityObjects: after outputting activity:object elements for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $objects: array of ActivityObject objects to output (empty by default) + +StartActivityNoticeInfo: before outputting statusnet:notice-info element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$noticeInfoAttr: array of attributes for notice info element + +EndActivityNoticeInfo: after outputting statusnet:notice-info element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $noticeInfoAttr: array of attributes for notice info element + +StartActivityInReplyTo: before outputting thr:in-reply-to element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$replyNotice: Notice object the main notice is in-reply-to + +EndActivityInReplyTo: after outputting thr:in-reply-to element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $replyNotice: Notice object the main notice is in-reply-to + +StartActivityConversation: before outputting ostatus:conversation link element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$conv: Conversation object + +EndActivityConversation: after outputting ostatus:conversation link element for a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $conv: Conversation object + +StartActivityAttentionProfiles: before outputting ostatus:attention link element for people in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$replyProfiles: array of profiles of people being replied to + +EndActivityAttentionProfiles: after outputting ostatus:attention link element for people in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $replyProfiles: array of Profile object of people being replied to + +StartActivityAttentionGroups: before outputting ostatus:attention link element for groups in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$groups: array of Group objects of groups being addressed + +EndActivityAttentionGroups: after outputting ostatus:attention link element for groups in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $groups: array of Group objects of groups being addressed + +StartActivityForward: before outputting ostatus:forward link element in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$repeat: Notice that was repeated + +EndActivityForward: after outputting ostatus:forward link element in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $repeat: Notice that was repeated + +StartActivityCategories: before outputting atom:category elements in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$tags: array of strings for tags on the notice (used for categories) + +EndActivityCategories: after outputting atom:category elements in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $tags: array of strings for tags on the notice (used for categories) + +StartActivityEnclosures: before outputting enclosure link elements in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$enclosures: array of enclosure objects (see File::getEnclosure() for details) + +EndActivityEnclosures: after outputting enclosure link elements in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $enclosures: array of enclosure objects (see File::getEnclosure() for details) + +StartActivityGeo: before outputting geo:rss element in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- &$lat: latitude +- &$lon: longitude + +EndActivityGeo: after outputting geo:rss element in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output +- $lat: latitude +- $lon: longitude + +StartActivityEnd: before the closing in a notice activity entry (last chance for data!) +- &$notice: notice being output +- &$xs: XMLStringer for output + +EndActivityEnd: after the closing in a notice activity entry +- &$notice: notice being output +- &$xs: XMLStringer for output From f12cafb275ea16903d5d5edeb3b9e46b9f7b2667 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 2 Aug 2010 17:56:44 -0700 Subject: [PATCH 3/9] correct output for EndActivityInReplyTo event --- classes/Notice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Notice.php b/classes/Notice.php index 592e2f3842..f6e9eb585d 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1391,7 +1391,7 @@ class Notice extends Memcached_DataObject $xs->element('thr:in-reply-to', array('ref' => $replyNotice->uri, 'href' => $replyNotice->bestUrl())); - Event::handle('EndActivityInReplyTo', array(&$this, &$xs, $replyUri, $replyUrl)); + Event::handle('EndActivityInReplyTo', array(&$this, &$xs, $replyNotice)); } } From 1a6148f0e41708c32fd410cc7530d9fb1efebcae Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 13:41:44 -0700 Subject: [PATCH 4/9] initial unit tests for activity generation --- tests/ActivityGenerationTests.php | 182 ++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 tests/ActivityGenerationTests.php diff --git a/tests/ActivityGenerationTests.php b/tests/ActivityGenerationTests.php new file mode 100644 index 0000000000..a2e3c2480a --- /dev/null +++ b/tests/ActivityGenerationTests.php @@ -0,0 +1,182 @@ +author1 = User::register(array('nickname' => $authorNick1, + 'email' => $authorNick1 . '@example.net', + 'email_confirmed' => true)); + + $this->author2 = User::register(array('nickname' => $authorNick2, + 'email' => $authorNick2 . '@example.net', + 'email_confirmed' => true)); + + $this->targetUser1 = User::register(array('nickname' => $targetNick1, + 'email' => $targetNick1 . '@example.net', + 'email_confirmed' => true)); + + $this->targetUser2 = User::register(array('nickname' => $targetNick2, + 'email' => $targetNick2 . '@example.net', + 'email_confirmed' => true)); + + } + + public function testBasicNoticeActivity() + { + $notice = $this->_fakeNotice(); + + $entry = $notice->asAtomEntry(true); + + echo $entry; + + $element = $this->_entryToElement($entry, false); + + $this->assertEquals($notice->uri, ActivityUtils::childContent($element, 'id')); + $this->assertEquals($notice->content, ActivityUtils::childContent($element, 'title')); + $this->assertEquals($notice->rendered, ActivityUtils::childContent($element, 'content')); + $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'published'))); + $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'updated'))); + $this->assertEquals(ActivityVerb::POST, ActivityUtils::childContent($element, 'verb', Activity::SPEC)); + $this->assertEquals(ActivityObject::NOTE, ActivityUtils::childContent($element, 'object-type', Activity::SPEC)); + } + + public function testNamespaceFlag() + { + $notice = $this->_fakeNotice(); + + $entry = $notice->asAtomEntry(true); + + $element = $this->_entryToElement($entry, false); + + $this->assertTrue($element->hasAttribute('xmlns')); + $this->assertTrue($element->hasAttribute('xmlns:thr')); + $this->assertTrue($element->hasAttribute('xmlns:georss')); + $this->assertTrue($element->hasAttribute('xmlns:activity')); + $this->assertTrue($element->hasAttribute('xmlns:media')); + $this->assertTrue($element->hasAttribute('xmlns:poco')); + $this->assertTrue($element->hasAttribute('xmlns:ostatus')); + $this->assertTrue($element->hasAttribute('xmlns:statusnet')); + + $entry = $notice->asAtomEntry(false); + + $element = $this->_entryToElement($entry, true); + + $this->assertFalse($element->hasAttribute('xmlns')); + $this->assertFalse($element->hasAttribute('xmlns:thr')); + $this->assertFalse($element->hasAttribute('xmlns:georss')); + $this->assertFalse($element->hasAttribute('xmlns:activity')); + $this->assertFalse($element->hasAttribute('xmlns:media')); + $this->assertFalse($element->hasAttribute('xmlns:poco')); + $this->assertFalse($element->hasAttribute('xmlns:ostatus')); + $this->assertFalse($element->hasAttribute('xmlns:statusnet')); + } + + public function testReplyActivity() + { + $this->assertTrue(FALSE); + } + + public function testMultipleReplyActivity() + { + $this->assertTrue(FALSE); + } + + public function testGroupPostActivity() + { + $this->assertTrue(FALSE); + } + + public function testMultipleGroupPostActivity() + { + $this->assertTrue(FALSE); + } + + public function testRepeatActivity() + { + $this->assertTrue(FALSE); + } + + public function testTaggedActivity() + { + $this->assertTrue(FALSE); + } + + public function testGeotaggedActivity() + { + $this->assertTrue(FALSE); + } + + public function tearDown() + { + $this->author1->delete(); + $this->author2->delete(); + $this->targetUser1->delete(); + $this->targetUser2->delete(); + } + + private function _fakeNotice($user = null, $text = null) + { + if (empty($user)) { + $user = $this->author1; + } + + if (empty($text)) { + $text = "fake-o text-o " . common_good_rand(32); + } + + return Notice::saveNew($user->id, $text, 'test', array('uri' => null)); + } + + private function _entryToElement($entry, $namespace = false) + { + $xml = ''."\n\n"; + $xml .= '' . "\n"; + $doc = DOMDocument::loadXML($xml); + $feed = $doc->documentElement; + $entries = $feed->getElementsByTagName('entry'); + + return $entries->item(0); + } +} From 6756a752c4835f4fd7ab46266f008b2d0a594fb1 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 14:17:36 -0700 Subject: [PATCH 5/9] add some more tests for replies and group posts --- tests/ActivityGenerationTests.php | 249 +++++++++++++++++++++++++++--- 1 file changed, 227 insertions(+), 22 deletions(-) diff --git a/tests/ActivityGenerationTests.php b/tests/ActivityGenerationTests.php index a2e3c2480a..9c7f13208f 100644 --- a/tests/ActivityGenerationTests.php +++ b/tests/ActivityGenerationTests.php @@ -23,16 +23,18 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase var $targetGroup1 = null; var $targetGroup2 = null; - public function setUp() + function __construct() { - $authorNick1 = 'activitygenerationtestsuser' . common_good_rand(16); - $authorNick2 = 'activitygenerationtestsuser' . common_good_rand(16); + parent::__construct(); - $targetNick1 = 'activitygenerationteststarget' . common_good_rand(16); - $targetNick2 = 'activitygenerationteststarget' . common_good_rand(16); + $authorNick1 = 'activitygenerationtestsuser' . common_good_rand(4); + $authorNick2 = 'activitygenerationtestsuser' . common_good_rand(4); - $groupNick1 = 'activitygenerationtestsgroup' . common_good_rand(16); - $groupNick2 = 'activitygenerationtestsgroup' . common_good_rand(16); + $targetNick1 = 'activitygenerationteststarget' . common_good_rand(4); + $targetNick2 = 'activitygenerationteststarget' . common_good_rand(4); + + $groupNick1 = 'activitygenerationtestsgroup' . common_good_rand(4); + $groupNick2 = 'activitygenerationtestsgroup' . common_good_rand(4); $this->author1 = User::register(array('nickname' => $authorNick1, 'email' => $authorNick1 . '@example.net', @@ -50,6 +52,24 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase 'email' => $targetNick2 . '@example.net', 'email_confirmed' => true)); + $this->targetGroup1 = User_group::register(array('nickname' => $groupNick1, + 'userid' => $this->author1->id, + 'aliases' => array(), + 'local' => true, + 'location' => null, + 'description' => null, + 'fullname' => null, + 'homepage' => null, + 'mainpage' => null)); + $this->targetGroup2 = User_group::register(array('nickname' => $groupNick2, + 'userid' => $this->author1->id, + 'aliases' => array(), + 'local' => true, + 'location' => null, + 'description' => null, + 'fullname' => null, + 'homepage' => null, + 'mainpage' => null)); } public function testBasicNoticeActivity() @@ -58,8 +78,6 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $entry = $notice->asAtomEntry(true); - echo $entry; - $element = $this->_entryToElement($entry, false); $this->assertEquals($notice->uri, ActivityUtils::childContent($element, 'id')); @@ -102,27 +120,190 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertFalse($element->hasAttribute('xmlns:statusnet')); } - public function testReplyActivity() + public function testSourceFlag() + { + $notice = $this->_fakeNotice(); + + // Test with no source + + $entry = $notice->asAtomEntry(false, false); + + $element = $this->_entryToElement($entry, true); + + $source = ActivityUtils::child($element, 'source'); + + $this->assertNull($source); + + // Test with source + + $entry = $notice->asAtomEntry(false, true); + + $element = $this->_entryToElement($entry, true); + + $source = ActivityUtils::child($element, 'source'); + + $this->assertNotNull($source); + } + + public function testSourceContent() + { + $notice = $this->_fakeNotice(); + // make a time difference! + sleep(2); + $notice2 = $this->_fakeNotice(); + + $entry = $notice->asAtomEntry(false, true); + + $element = $this->_entryToElement($entry, true); + + $source = ActivityUtils::child($element, 'source'); + + $atomUrl = common_local_url('ApiTimelineUser', array('id' => $this->author1->id, 'format' => 'atom')); + + $profile = $this->author1->getProfile(); + + $this->assertEquals($atomUrl, ActivityUtils::childContent($source, 'id')); + $this->assertEquals($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml')); + $this->assertEquals($profile->profileurl, ActivityUtils::getPermalink($source)); + $this->assertEquals($notice2->created, strtotime(ActivityUtils::childContent($source, 'updated'))); + // XXX: do we care here? + $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title'))); + $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license')); + } + + public function testAuthorFlag() + { + $notice = $this->_fakeNotice(); + + // Test with no author + + $entry = $notice->asAtomEntry(false, false, false); + + $element = $this->_entryToElement($entry, true); + + $this->assertNull(ActivityUtils::child($element, 'author')); + $this->assertNull(ActivityUtils::child($element, 'actor', Activity::SPEC)); + + // Test with source + + $entry = $notice->asAtomEntry(false, false, true); + + $element = $this->_entryToElement($entry, true); + + $author = ActivityUtils::child($element, 'author'); + $actor = ActivityUtils::child($element, 'actor', Activity::SPEC); + + $this->assertFalse(is_null($author)); + $this->assertFalse(is_null($actor)); + } + + public function testCurArgument() { $this->assertTrue(FALSE); } - public function testMultipleReplyActivity() + public function testReplyLink() { - $this->assertTrue(FALSE); + $orig = $this->_fakeNotice($this->targetUser1); + + $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4); + + $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id)); + + $entry = $reply->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $irt = ActivityUtils::child($element, 'in-reply-to', 'http://purl.org/syndication/thread/1.0'); + + $this->assertNotNull($irt); + $this->assertEquals($orig->uri, $irt->getAttribute('ref')); + $this->assertEquals($orig->bestUrl(), $irt->getAttribute('href')); } - public function testGroupPostActivity() + public function testReplyAttention() { - $this->assertTrue(FALSE); + $orig = $this->_fakeNotice($this->targetUser1); + + $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4); + + $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id)); + + $entry = $reply->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'ostatus:attention')); } - public function testMultipleGroupPostActivity() + public function testMultipleReplyAttention() { - $this->assertTrue(FALSE); + $orig = $this->_fakeNotice($this->targetUser1); + + $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4); + + $reply = Notice::saveNew($this->targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id)); + + $text = "@" . $this->targetUser1->nickname . " @" . $this->targetUser2->nickname . " reply text " . common_good_rand(4); + + $reply2 = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id)); + + $entry = $reply2->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $links = ActivityUtils::getLinks($element, 'ostatus:attention'); + + $this->assertEquals(2, count($links)); + + $hrefs = array(); + + foreach ($links as $link) { + $hrefs[] = $link->getAttribute('href'); + } + + $this->assertTrue(in_array($this->targetUser1->uri, $hrefs)); + $this->assertTrue(in_array($this->targetUser2->uri, $hrefs)); } - public function testRepeatActivity() + public function testGroupPostAttention() + { + $text = "!" . $this->targetGroup1->nickname . " reply text " . common_good_rand(4); + + $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null)); + + $entry = $notice->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'ostatus:attention')); + } + + public function testMultipleGroupPostAttention() + { + $text = "!" . $this->targetGroup1->nickname . " !" . $this->targetGroup2->nickname . " reply text " . common_good_rand(4); + + $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null)); + + $entry = $notice->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $links = ActivityUtils::getLinks($element, 'ostatus:attention'); + + $this->assertEquals(2, count($links)); + + $hrefs = array(); + + foreach ($links as $link) { + $hrefs[] = $link->getAttribute('href'); + } + + $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs)); + $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs)); + } + + public function testRepeatLink() { $this->assertTrue(FALSE); } @@ -137,12 +318,36 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertTrue(FALSE); } - public function tearDown() + public function testNoticeInfo() { - $this->author1->delete(); - $this->author2->delete(); - $this->targetUser1->delete(); - $this->targetUser2->delete(); + $this->assertTrue(FALSE); + } + + function __destruct() + { + if (!is_null($this->author1)) { + $this->author1->delete(); + } + + if (!is_null($this->author2)) { + $this->author2->delete(); + } + + if (!is_null($this->targetUser1)) { + $this->targetUser1->delete(); + } + + if (!is_null($this->targetUser2)) { + $this->targetUser2->delete(); + } + + if (!is_null($this->targetGroup1)) { + $this->targetGroup1->delete(); + } + + if (!is_null($this->targetGroup2)) { + $this->targetGroup2->delete(); + } } private function _fakeNotice($user = null, $text = null) From 8d19162122bef8c0661473fc444cceeab7b5ac5a Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 15:26:19 -0700 Subject: [PATCH 6/9] more tests for activity generation --- tests/ActivityGenerationTests.php | 176 ++++++++++++++++++++++++++++-- 1 file changed, 169 insertions(+), 7 deletions(-) diff --git a/tests/ActivityGenerationTests.php b/tests/ActivityGenerationTests.php index 9c7f13208f..cc82151b99 100644 --- a/tests/ActivityGenerationTests.php +++ b/tests/ActivityGenerationTests.php @@ -197,9 +197,21 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertFalse(is_null($actor)); } - public function testCurArgument() + public function testAuthorContent() { - $this->assertTrue(FALSE); + $notice = $this->_fakeNotice(); + + // Test with author + + $entry = $notice->asAtomEntry(false, false, true); + + $element = $this->_entryToElement($entry, true); + + $author = ActivityUtils::child($element, 'author'); + $actor = ActivityUtils::child($element, 'actor', Activity::SPEC); + + $this->assertEquals($this->author1->nickname, ActivityUtils::childContent($author, 'name')); + $this->assertEquals($this->author1->uri, ActivityUtils::childContent($author, 'uri')); } public function testReplyLink() @@ -305,22 +317,172 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase public function testRepeatLink() { - $this->assertTrue(FALSE); + $notice = $this->_fakeNotice($this->author1); + $repeat = $notice->repeat($this->author2->id, 'test'); + + $entry = $repeat->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $forward = ActivityUtils::child($element, 'forward', "http://ostatus.org/schema/1.0"); + + $this->assertNotNull($forward); + $this->assertEquals($notice->uri, $forward->getAttribute('ref')); + $this->assertEquals($notice->bestUrl(), $forward->getAttribute('href')); } - public function testTaggedActivity() + public function testTag() { - $this->assertTrue(FALSE); + $tag1 = common_good_rand(4); + + $notice = $this->_fakeNotice($this->author1, '#' . $tag1); + + $entry = $notice->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $category = ActivityUtils::child($element, 'category'); + + $this->assertNotNull($category); + $this->assertEquals($tag1, $category->getAttribute('term')); + } + + public function testMultiTag() + { + $tag1 = common_good_rand(4); + $tag2 = common_good_rand(4); + + $notice = $this->_fakeNotice($this->author1, '#' . $tag1 . ' #' . $tag2); + + $entry = $notice->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $categories = $element->getElementsByTagName('category'); + + $this->assertNotNull($categories); + $this->assertEquals(2, $categories->length); + + $terms = array(); + + for ($i = 0; $i < $categories->length; $i++) { + $cat = $categories->item($i); + $terms[] = $cat->getAttribute('term'); + } + + $this->assertTrue(in_array($tag1, $terms)); + $this->assertTrue(in_array($tag2, $terms)); } public function testGeotaggedActivity() { - $this->assertTrue(FALSE); + $notice = Notice::saveNew($this->author1->id, common_good_rand(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6)); + + $entry = $notice->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $this->assertEquals('45.5 -73.6', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss")); } public function testNoticeInfo() { - $this->assertTrue(FALSE); + $notice = $this->_fakeNotice(); + + $entry = $notice->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/"); + + $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id')); + $this->assertEquals($notice->source, $noticeInfo->getAttribute('source')); + $this->assertEquals('', $noticeInfo->getAttribute('repeat_of')); + $this->assertEquals('', $noticeInfo->getAttribute('repeated')); + $this->assertEquals('', $noticeInfo->getAttribute('favorite')); + $this->assertEquals('', $noticeInfo->getAttribute('source_link')); + } + + public function testNoticeInfoRepeatOf() + { + $notice = $this->_fakeNotice(); + + $repeat = $notice->repeat($this->author2->id, 'test'); + + $entry = $repeat->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/"); + + $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of')); + } + + public function testNoticeInfoRepeated() + { + $notice = $this->_fakeNotice(); + + $repeat = $notice->repeat($this->author2->id, 'test'); + + $entry = $notice->asAtomEntry(false, false, false, $this->author2); + + $element = $this->_entryToElement($entry, true); + + $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/"); + + $this->assertEquals('true', $noticeInfo->getAttribute('repeated')); + + $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1); + + $element = $this->_entryToElement($entry, true); + + $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/"); + + $this->assertEquals('false', $noticeInfo->getAttribute('repeated')); + } + + public function testNoticeInfoFave() + { + $notice = $this->_fakeNotice(); + + $fave = Fave::addNew($this->author2->getProfile(), $notice); + + // Should be set if user has faved + + $entry = $notice->asAtomEntry(false, false, false, $this->author2); + + $element = $this->_entryToElement($entry, true); + + $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/"); + + $this->assertEquals('true', $noticeInfo->getAttribute('favorite')); + + // Shouldn't be set if user has not faved + + $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1); + + $element = $this->_entryToElement($entry, true); + + $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/"); + + $this->assertEquals('false', $noticeInfo->getAttribute('favorite')); + } + + public function testConversationLink() + { + $orig = $this->_fakeNotice($this->targetUser1); + + $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4); + + $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id)); + + $conv = Conversation::staticGet('id', $reply->conversation); + + $entry = $reply->asAtomEntry(); + + $element = $this->_entryToElement($entry, true); + + $this->assertEquals($conv->uri, ActivityUtils::getLink($element, 'ostatus:conversation')); } function __destruct() From 744233c6dc385a5870652ab70e7141e75aaff783 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 15:49:49 -0700 Subject: [PATCH 7/9] add actor info to tests --- tests/ActivityGenerationTests.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/ActivityGenerationTests.php b/tests/ActivityGenerationTests.php index cc82151b99..52077ee570 100644 --- a/tests/ActivityGenerationTests.php +++ b/tests/ActivityGenerationTests.php @@ -165,7 +165,7 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $this->assertEquals($atomUrl, ActivityUtils::childContent($source, 'id')); $this->assertEquals($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml')); $this->assertEquals($profile->profileurl, ActivityUtils::getPermalink($source)); - $this->assertEquals($notice2->created, strtotime(ActivityUtils::childContent($source, 'updated'))); + $this->assertEquals(strtotime($notice2->created), strtotime(ActivityUtils::childContent($source, 'updated'))); // XXX: do we care here? $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title'))); $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license')); @@ -208,12 +208,27 @@ class ActivityGenerationTests extends PHPUnit_Framework_TestCase $element = $this->_entryToElement($entry, true); $author = ActivityUtils::child($element, 'author'); - $actor = ActivityUtils::child($element, 'actor', Activity::SPEC); $this->assertEquals($this->author1->nickname, ActivityUtils::childContent($author, 'name')); $this->assertEquals($this->author1->uri, ActivityUtils::childContent($author, 'uri')); } + public function testActorContent() + { + $notice = $this->_fakeNotice(); + + // Test with author + + $entry = $notice->asAtomEntry(false, false, true); + + $element = $this->_entryToElement($entry, true); + + $actor = ActivityUtils::child($element, 'actor', Activity::SPEC); + + $this->assertEquals($this->author1->uri, ActivityUtils::childContent($actor, 'id')); + $this->assertEquals($this->author1->nickname, ActivityUtils::childContent($actor, 'title')); + } + public function testReplyLink() { $orig = $this->_fakeNotice($this->targetUser1); From f83171824f835ff9cd24bf0aea26f13c62b806cf Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 15:50:21 -0700 Subject: [PATCH 8/9] correctly show for atom feeds --- classes/Notice.php | 50 ++++++++++++++++++++----------- classes/Profile.php | 29 +++++++++++++----- plugins/OStatus/OStatusPlugin.php | 12 ++++++++ 3 files changed, 67 insertions(+), 24 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index f6e9eb585d..61844d487e 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1215,29 +1215,45 @@ class Notice extends Memcached_DataObject if ($source) { - $xs->elementStart('source'); + $atom_feed = $profile->getAtomFeed(); - $xs->element('id', null, $profile->profileurl); - $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name')); - $xs->element('link', array('href' => $profile->profileurl)); + if (!empty($atom_feed)) { - $user = User::staticGet('id', $profile->id); + $xs->elementStart('source'); + + // XXX: we should store the actual feed ID + + $xs->element('id', null, $atom_feed); + + // XXX: we should store the actual feed title + + $xs->element('title', null, $profile->getBestName()); + + $xs->element('link', array('rel' => 'alternate', + 'type' => 'text/html', + 'href' => $profile->profileurl)); - if (!empty($user)) { - $atom_feed = common_local_url('ApiTimelineUser', - 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'))); + 'href' => $atom_feed)); + + $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE)); + + $notice = $profile->getCurrentNotice(); + + if (!empty($notice)) { + $xs->element('updated', null, common_date_w3dtf($notice->created)); + } + + $user = User::staticGet('id', $profile->id); + + if (!empty($user)) { + $xs->element('link', array('rel' => 'license', + 'href' => common_config('license', 'url'))); + } + + $xs->elementEnd('source'); } - - $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)); } diff --git a/classes/Profile.php b/classes/Profile.php index a303469e96..abd6eb0315 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -152,17 +152,16 @@ class Profile extends Memcached_DataObject * * @return mixed Notice or null */ + function getCurrentNotice() { - $notice = new Notice(); - $notice->profile_id = $this->id; - // @fixme change this to sort on notice.id only when indexes are updated - $notice->orderBy('created DESC, notice.id DESC'); - $notice->limit(1); - if ($notice->find(true)) { + $notice = $this->getNotices(0, 1); + + if ($notice->fetch()) { return $notice; + } else { + return null; } - return null; } function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0) @@ -943,4 +942,20 @@ class Profile extends Memcached_DataObject return $result; } + + function getAtomFeed() + { + $feed = null; + + if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) { + $user = User::staticGet('id', $this->id); + if (!empty($user)) { + $feed = common_local_url('ApiTimelineUser', array('id' => $user->id, + 'format' => 'atom')); + } + Event::handle('EndProfileGetAtomFeed', array($this, $feed)); + } + + return $feed; + } } diff --git a/plugins/OStatus/OStatusPlugin.php b/plugins/OStatus/OStatusPlugin.php index c61e2cc5f3..4fc9d41088 100644 --- a/plugins/OStatus/OStatusPlugin.php +++ b/plugins/OStatus/OStatusPlugin.php @@ -953,4 +953,16 @@ class OStatusPlugin extends Plugin } return false; } + + public function onStartProfileGetAtomFeed($profile, &$feed) + { + $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id); + + if (empty($oprofile)) { + return true; + } + + $feed = $oprofile->feeduri; + return false; + } } From cc71f1ae826cc7c186f38ac902d963dc8f65caeb Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 3 Aug 2010 15:55:40 -0700 Subject: [PATCH 9/9] output Atom dates in UTC --- classes/Notice.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index 61844d487e..b849225fdb 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1242,7 +1242,7 @@ class Notice extends Memcached_DataObject $notice = $profile->getCurrentNotice(); if (!empty($notice)) { - $xs->element('updated', null, common_date_w3dtf($notice->created)); + $xs->element('updated', null, self::utcDate($notice->created)); } $user = User::staticGet('id', $profile->id); @@ -1307,7 +1307,7 @@ class Notice extends Memcached_DataObject Event::handle('EndActivityId', array(&$this, &$xs, $id)); } - $published = common_date_w3dtf($this->created); + $published = self::utcDate($this->created); if (Event::handle('StartActivityPublished', array(&$this, &$xs, &$published))) { $xs->element('published', null, $published); @@ -2055,4 +2055,11 @@ class Notice extends Memcached_DataObject $tag->free(); return $tags; } + + static private function utcDate($dt) + { + $dateStr = date('d F Y H:i:s', strtotime($dt)); + $d = new DateTime($dateStr, new DateTimeZone('UTC')); + return $d->format(DATE_W3C); + } }