Merge branch 'activityhooks' into 0.9.x

Conflicts:
	classes/Notice.php
This commit is contained in:
Evan Prodromou 2010-08-03 16:01:18 -07:00
commit 2ba36fc242
5 changed files with 1096 additions and 131 deletions

View File

@ -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 <event>
- &$notice: notice being output
- &$xs: XMLStringer for output
- &$attrs: <entry> attributes (mostly namespace declarations, if any)
EndActivityStart: end the opening tag for an activity <event>
- &$notice: notice being output
- &$xs: XMLStringer for output
- $attrs: <entry> attributes (mostly namespace declarations, if any)
StartActivitySource: before outputting the <source> element for a notice activity
- &$notice: notice being output
- &$xs: XMLStringer for output
EndActivitySource: after outputting the <source> 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 </entry> in a notice activity entry (last chance for data!)
- &$notice: notice being output
- &$xs: XMLStringer for output
EndActivityEnd: after the closing </entry> in a notice activity entry
- &$notice: notice being output
- &$xs: XMLStringer for output

View File

@ -1198,6 +1198,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();
@ -1217,74 +1220,176 @@ 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) {
$atom_feed = $profile->getAtomFeed();
if (!empty($atom_feed)) {
$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));
$xs->element('link', array('rel' => 'self',
'type' => 'application/atom+xml',
'href' => $atom_feed));
$xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
$notice = $profile->getCurrentNotice();
if (!empty($notice)) {
$xs->element('updated', null, self::utcDate($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));
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 = '<a href="'
. htmlspecialchars($ns->url)
. '" rel="nofollow">'
. htmlspecialchars($ns->name)
. '</a>';
} 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 = self::utcDate($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'] = '<a href="'
. htmlspecialchars($ns->url)
. '" rel="nofollow">'
. htmlspecialchars($ns->name)
. '</a>';
}
}
}
@ -1298,117 +1403,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, $replyNotice));
}
}
$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)) {
// XXX: Deprecate this for 'mentioned'
$xs->element(
'link', array(
'rel' => 'ostatus:attention',
'href' => $profile->getUri()
)
);
$xs->element(
'link', array(
'rel' => 'mentioned',
'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) {
// XXX: Deprecate this for 'mentioned'
$xs->element(
'link', array(
'rel' => 'ostatus:attention',
'href' => $group->permalink()
)
);
$xs->element(
'link', array(
'rel' => 'mentioned',
'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();
}
@ -1929,4 +2056,24 @@ 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;
}
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);
}
}

View File

@ -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)
@ -947,4 +946,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;
}
}

View File

@ -956,4 +956,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;
}
}

View File

@ -0,0 +1,564 @@
<?php
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
print "This script must be run from the command line\n";
exit();
}
// XXX: we should probably have some common source for this stuff
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('STATUSNET', true);
require_once INSTALLDIR . '/lib/common.php';
class ActivityGenerationTests extends PHPUnit_Framework_TestCase
{
var $author1 = null;
var $author2 = null;
var $targetUser1 = null;
var $targetUser2 = null;
var $targetGroup1 = null;
var $targetGroup2 = null;
function __construct()
{
parent::__construct();
$authorNick1 = 'activitygenerationtestsuser' . common_good_rand(4);
$authorNick2 = 'activitygenerationtestsuser' . common_good_rand(4);
$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',
'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));
$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()
{
$notice = $this->_fakeNotice();
$entry = $notice->asAtomEntry(true);
$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 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(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'));
}
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 testAuthorContent()
{
$notice = $this->_fakeNotice();
// Test with author
$entry = $notice->asAtomEntry(false, false, true);
$element = $this->_entryToElement($entry, true);
$author = ActivityUtils::child($element, 'author');
$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);
$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 testReplyAttention()
{
$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 testMultipleReplyAttention()
{
$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 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()
{
$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 testTag()
{
$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()
{
$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()
{
$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()
{
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)
{
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 = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
$xml .= '<feed';
if ($namespace) {
$xml .= ' xmlns="http://www.w3.org/2005/Atom"';
$xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
$xml .= ' xmlns:georss="http://www.georss.org/georss"';
$xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
$xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
$xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
$xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
$xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
}
$xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
$doc = DOMDocument::loadXML($xml);
$feed = $doc->documentElement;
$entries = $feed->getElementsByTagName('entry');
return $entries->item(0);
}
}