]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
Merge remote-tracking branch 'upstream/master' into nightly
[quix0rs-gnu-social.git] / lib / activityobject.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * An activity
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Feed
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28  * @link      http://status.net/
29  */
30
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 require_once(INSTALLDIR.'/lib/activitystreamjsondocument.php');
34
35 /**
36  * A noun-ish thing in the activity universe
37  *
38  * The activity streams spec talks about activity objects, while also having
39  * a tag activity:object, which is in fact an activity object. Aaaaaah!
40  *
41  * This is just a thing in the activity universe. Can be the subject, object,
42  * or indirect object (target!) of an activity verb. Rotten name, and I'm
43  * propagating it. *sigh*
44  *
45  * @category  OStatus
46  * @package   StatusNet
47  * @author    Evan Prodromou <evan@status.net>
48  * @copyright 2010 StatusNet, Inc.
49  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
50  * @link      http://status.net/
51  */
52 class ActivityObject
53 {
54     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
55     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
56     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
57     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
58     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
59     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
60     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
61     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
62     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
63     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
64     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
65     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
66     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
67     const _LIST     = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
68     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
69     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
70     // ^^^^^^^^^^ tea!
71     const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
72     const SERVICE   = 'http://activitystrea.ms/schema/1.0/service';
73     const IMAGE     = 'http://activitystrea.ms/schema/1.0/image';
74     const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection';
75     const APPLICATION = 'http://activitystrea.ms/schema/1.0/application';
76
77     // Atom elements we snarf
78
79     const TITLE   = 'title';
80     const SUMMARY = 'summary';
81     const ID      = 'id';
82     const SOURCE  = 'source';
83
84     const NAME  = 'name';
85     const URI   = 'uri';
86     const EMAIL = 'email';
87
88     const POSTEROUS   = 'http://posterous.com/help/rss/1.0';
89     const AUTHOR      = 'author';
90     const USERIMAGE   = 'userImage';
91     const PROFILEURL  = 'profileUrl';
92     const NICKNAME    = 'nickName';
93     const DISPLAYNAME = 'displayName';
94
95     public $element;
96     public $type;
97     public $id;
98     public $title;
99     public $summary;
100     public $content;
101     public $owner;
102     public $link;
103     public $source;
104     public $avatarLinks = array();
105     public $geopoint;
106     public $poco;
107     public $displayName;
108
109     // @todo move this stuff to it's own PHOTO activity object
110     const MEDIA_DESCRIPTION = 'description';
111
112     public $thumbnail;
113     public $largerImage;
114     public $description;
115     public $extra = array();
116
117     public $stream;
118
119     /**
120      * Constructor
121      *
122      * This probably needs to be refactored
123      * to generate a local class (ActivityPerson, ActivityFile, ...)
124      * based on the object type.
125      *
126      * @param DOMElement $element DOM thing to turn into an Activity thing
127      */
128     function __construct($element = null)
129     {
130         if (empty($element)) {
131             return;
132         }
133
134         $this->element = $element;
135
136         $this->geopoint = $this->_childContent(
137             $element,
138             ActivityContext::POINT,
139             ActivityContext::GEORSS
140         );
141
142         if ($element->tagName == 'author') {
143             $this->_fromAuthor($element);
144         } else if ($element->tagName == 'item') {
145             $this->_fromRssItem($element);
146         } else {
147             $this->_fromAtomEntry($element);
148         }
149
150         // Some per-type attributes...
151         if ($this->type == self::PERSON || $this->type == self::GROUP) {
152             $this->displayName = $this->title;
153
154             $photos = ActivityUtils::getLinks($element, 'photo');
155             if (count($photos)) {
156                 foreach ($photos as $link) {
157                     $this->avatarLinks[] = new AvatarLink($link);
158                 }
159             } else {
160                 $avatars = ActivityUtils::getLinks($element, 'avatar');
161                 foreach ($avatars as $link) {
162                     $this->avatarLinks[] = new AvatarLink($link);
163                 }
164             }
165
166             $this->poco = new PoCo($element);
167         }
168
169         if ($this->type == self::PHOTO) {
170
171             $this->thumbnail   = ActivityUtils::getLink($element, 'preview');
172             $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
173
174             $this->description = ActivityUtils::childContent(
175                 $element,
176                 ActivityObject::MEDIA_DESCRIPTION,
177                 Activity::MEDIA
178             );
179         }
180         if ($this->type == self::_LIST) {
181             $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
182             $this->owner = new ActivityObject($owner);
183         }
184     }
185
186     private function _fromAuthor($element)
187     {
188         $this->type = $this->_childContent($element,
189                                            Activity::OBJECTTYPE,
190                                            Activity::SPEC);
191
192         if (empty($this->type)) {
193             $this->type = self::PERSON; // XXX: is this fair?
194         }
195
196
197         // Start with <poco::displayName>
198
199         $this->title = ActivityUtils::childContent($element, PoCo::DISPLAYNAME, PoCo::NS);
200
201         // try falling back to <atom:title>
202
203         if (empty($this->title)) {
204             $title = ActivityUtils::childHtmlContent($element, self::TITLE);
205
206             if (!empty($title)) {
207                 $this->title = common_strip_html($title);
208             }
209         }
210
211         // fall back to <atom:name> as a last resort
212
213         if (empty($this->title)) {
214             $this->title = $this->_childContent($element, self::NAME);
215         }
216
217         // start with <atom:id>
218
219         $this->id = $this->_childContent($element, self::ID);
220
221         // fall back to <atom:uri>
222
223         if (empty($this->id)) {
224             $this->id = $this->_childContent($element, self::URI);
225         }
226
227         // fall further back to <atom:email>
228
229         if (empty($this->id)) {
230             $email = $this->_childContent($element, self::EMAIL);
231             if (!empty($email)) {
232                 // XXX: acct: ?
233                 $this->id = 'mailto:'.$email;
234             }
235         }
236
237         $this->link = ActivityUtils::getPermalink($element);
238
239         // fall finally back to <link rel=alternate>
240
241         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
242             $this->id = $this->link;
243         }
244     }
245
246     private function _fromAtomEntry($element)
247     {
248         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
249                                            Activity::SPEC);
250
251         if (empty($this->type)) {
252             $this->type = ActivityObject::NOTE;
253         }
254
255         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
256         $this->content = ActivityUtils::getContent($element);
257
258         // We don't like HTML in our titles, although it's technically allowed
259         $this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
260
261         $this->source  = $this->_getSource($element);
262
263         $this->link = ActivityUtils::getPermalink($element);
264
265         $this->id = $this->_childContent($element, self::ID);
266
267         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
268             $this->id = $this->link;
269         }
270
271         $els = $element->childNodes;
272         $out = array();
273
274         for ($i = 0; $i < $els->length; $i++) {
275             $link = $els->item($i);
276             if ($link->localName == ActivityUtils::LINK && $link->namespaceURI == ActivityUtils::ATOM) {
277                 $attrs = array();
278                 foreach ($link->attributes as $attrName=>$attrNode) {
279                     $attrs[$attrName] = $attrNode->nodeValue;
280                 }
281                 $this->extra[] = [$link->localName,
282                                     $attrs,
283                                     $link->nodeValue];
284             }
285         }
286     }
287
288     // @todo FIXME: rationalize with Activity::_fromRssItem()
289     private function _fromRssItem($item)
290     {
291         if (empty($this->type)) {
292             $this->type = ActivityObject::NOTE;
293         }
294
295         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
296
297         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
298
299         if (!empty($contentEl)) {
300             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
301         } else {
302             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
303             if (!empty($descriptionEl)) {
304                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
305             }
306         }
307
308         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
309
310         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
311
312         if (!empty($guidEl)) {
313             $this->id = $guidEl->textContent;
314
315             if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
316                 // overwrites <link>
317                 $this->link = $this->id;
318             }
319         }
320     }
321
322     public static function fromRssAuthor($el)
323     {
324         $text = $el->textContent;
325
326         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
327             $email = $match[1];
328             $name = $match[2];
329         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
330             $name = $match[1];
331             $email = $match[2];
332         } else if (preg_match('/.*@.*/', $text)) {
333             $email = $text;
334             $name = null;
335         } else {
336             $name = $text;
337             $email = null;
338         }
339
340         // Not really enough info
341
342         $obj = new ActivityObject();
343
344         $obj->element = $el;
345
346         $obj->type  = ActivityObject::PERSON;
347         $obj->title = $name;
348
349         if (!empty($email)) {
350             $obj->id = 'mailto:'.$email;
351         }
352
353         return $obj;
354     }
355
356     public static function fromDcCreator($el)
357     {
358         // Not really enough info
359
360         $text = $el->textContent;
361
362         $obj = new ActivityObject();
363
364         $obj->element = $el;
365
366         $obj->title = $text;
367         $obj->type  = ActivityObject::PERSON;
368
369         return $obj;
370     }
371
372     public static function fromRssChannel($el)
373     {
374         $obj = new ActivityObject();
375
376         $obj->element = $el;
377
378         $obj->type = ActivityObject::PERSON; // @fixme guess better
379
380         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
381         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
382         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
383
384         if (empty($obj->id)) {
385             $obj->id = $obj->link;
386         }
387
388         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
389
390         if (!empty($desc)) {
391             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
392         }
393
394         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
395
396         if (!empty($imageEl)) {
397             $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
398             $al = new AvatarLink();
399             $al->url = $url;
400             $obj->avatarLinks[] = $al;
401         }
402
403         return $obj;
404     }
405
406     public static function fromPosterousAuthor($el)
407     {
408         $obj = new ActivityObject();
409
410         $obj->type = ActivityObject::PERSON; // @fixme any others...?
411
412         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
413
414         if (!empty($userImage)) {
415             $al = new AvatarLink();
416             $al->url = $userImage;
417             $obj->avatarLinks[] = $al;
418         }
419
420         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
421         $obj->id   = $obj->link;
422
423         $obj->poco = new PoCo();
424
425         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
426         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
427
428         $obj->title = $obj->poco->displayName;
429
430         return $obj;
431     }
432
433     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
434     {
435         return ActivityUtils::childContent($element, $tag, $namespace);
436     }
437
438     // Try to get a unique id for the source feed
439
440     private function _getSource($element)
441     {
442         $sourceEl = ActivityUtils::child($element, 'source');
443
444         if (empty($sourceEl)) {
445             return null;
446         } else {
447             $href = ActivityUtils::getLink($sourceEl, 'self');
448             if (!empty($href)) {
449                 return $href;
450             } else {
451                 return ActivityUtils::childContent($sourceEl, 'id');
452             }
453         }
454     }
455
456     static function fromGroup(User_group $group)
457     {
458         $object = new ActivityObject();
459
460         if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
461
462             $object->type   = ActivityObject::GROUP;
463             $object->id     = $group->getUri();
464             $object->title  = $group->getBestName();
465             $object->link   = $group->getUri();
466
467             $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
468                                                               AVATAR_PROFILE_SIZE);
469
470             $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
471                                                               AVATAR_STREAM_SIZE);
472
473             $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
474                                                               AVATAR_MINI_SIZE);
475
476             $object->poco = PoCo::fromGroup($group);
477             Event::handle('EndActivityObjectFromGroup', array($group, &$object));
478         }
479
480         return $object;
481     }
482
483     static function fromPeopletag($ptag)
484     {
485         $object = new ActivityObject();
486         if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
487             $object->type    = ActivityObject::_LIST;
488
489             $object->id      = $ptag->getUri();
490             $object->title   = $ptag->tag;
491             $object->summary = $ptag->description;
492             $object->link    = $ptag->homeUrl();
493             $object->owner   = Profile::getKV('id', $ptag->tagger);
494             $object->poco    = PoCo::fromProfile($object->owner);
495             Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
496         }
497         return $object;
498     }
499
500     static function fromFile(File $file)
501     {
502         $object = new ActivityObject();
503
504         if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
505
506             $object->type = self::mimeTypeToObjectType($file->mimetype);
507             $object->id   = TagURI::mint(sprintf("file:%d", $file->id));
508             $object->link = $file->getAttachmentUrl();
509
510             if ($file->title) {
511                 $object->title = $file->title;
512             }
513
514             if ($file->date) {
515                 $object->date = $file->date;
516             }
517
518             try {
519                 $thumbnail = $file->getThumbnail();
520                 $object->thumbnail = $thumbnail;
521             } catch (UseFileAsThumbnailException $e) {
522                 $object->thumbnail = null;
523             } catch (UnsupportedMediaException $e) {
524                 $object->thumbnail = null;
525             }
526
527             switch (self::canonicalType($object->type)) {
528             case 'image':
529                 $object->largerImage = $file->getUrl();
530                 break;
531             case 'video':
532             case 'audio':
533                 $object->stream = $file->getUrl();
534                 break;
535             }
536
537             Event::handle('EndActivityObjectFromFile', array($file, &$object));
538         }
539
540         return $object;
541     }
542
543     static function fromNoticeSource(Notice_source $source)
544     {
545         $object = new ActivityObject();
546         $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
547                            'activity', 'feed', 'mirror', 'twitter', 'facebook');
548
549         if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
550             $object->type = ActivityObject::APPLICATION;
551
552             if (in_array($source->code, $wellKnown)) {
553                 // We use one ID for all well-known StatusNet sources
554                 $object->id = "tag:status.net,2009:notice-source:".$source->code;
555             } else if ($source->url) {
556                 // They registered with an URL
557                 $object->id = $source->url;
558             } else {
559                 // Locally-registered, no URL
560                 $object->id = TagURI::mint("notice-source:".$source->code);
561             }
562
563             if ($source->url) {
564                 $object->link = $source->url;
565             }
566
567             if ($source->name) {
568                 $object->title = $source->name;
569             } else {
570                 $object->title = $source->code;
571             }
572
573             if ($source->created) {
574                 $object->date = $source->created;
575             }
576             
577             $object->extra[] = array('status_net', array('source_code' => $source->code));
578
579             Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
580         }
581
582         return $object;
583     }
584
585     static function fromMessage(Message $message)
586     {
587         $object = new ActivityObject();
588
589         if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
590
591             $object->type    = ActivityObject::NOTE;
592             $object->id      = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
593             $object->content = $message->rendered;
594             $object->date    = $message->created;
595
596             if ($message->url) {
597                 $object->link = $message->url;
598             } else {
599                 $object->link = common_local_url('showmessage', array('message' => $message->id));
600             }
601
602             $object->extra[] = array('status_net', array('message_id' => $message->id));
603             
604             Event::handle('EndActivityObjectFromMessage', array($message, &$object));
605         }
606
607         return $object;
608     }
609
610     function outputTo($xo, $tag='activity:object')
611     {
612         if (!empty($tag)) {
613             $xo->elementStart($tag);
614         }
615
616         if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
617             $xo->element('activity:object-type', null, $this->type);
618
619             // <author> uses URI
620
621             if ($tag == 'author') {
622                 $xo->element(self::URI, null, $this->id);
623             } else {
624                 $xo->element(self::ID, null, $this->id);
625             }
626
627             if (!empty($this->title)) {
628                 $name = common_xml_safe_str($this->title);
629                 if ($tag == 'author') {
630                     // XXX: Backward compatibility hack -- atom:name should contain
631                     // full name here, instead of nickname, i.e.: $name. Change
632                     // this in the next version.
633                     $xo->element(self::NAME, null, $this->poco->preferredUsername);
634                 } else {
635                     $xo->element(self::TITLE, null, $name);
636                 }
637             }
638
639             if (!empty($this->summary)) {
640                 $xo->element(
641                     self::SUMMARY,
642                     null,
643                     common_xml_safe_str($this->summary)
644                 );
645             }
646
647             if (!empty($this->content)) {
648                 // XXX: assuming HTML content here
649                 $xo->element(
650                     ActivityUtils::CONTENT,
651                     array('type' => 'html'),
652                     common_xml_safe_str($this->content)
653                 );
654             }
655
656             if (!empty($this->link)) {
657                 $xo->element(
658                     'link',
659                     array(
660                         'rel' => 'alternate',
661                         'type' => 'text/html',
662                         'href' => $this->link
663                     ),
664                     null
665                 );
666             }
667
668             if(!empty($this->owner)) {
669                 $owner = $this->owner->asActivityNoun(self::AUTHOR);
670                 $xo->raw($owner);
671             }
672
673             if ($this->type == ActivityObject::PERSON
674                 || $this->type == ActivityObject::GROUP) {
675
676                 foreach ($this->avatarLinks as $alink) {
677                     $xo->element('link',
678                             array(
679                                 'rel'          => 'avatar',
680                                 'type'         => $alink->type,
681                                 'media:width'  => $alink->width,
682                                 'media:height' => $alink->height,
683                                 'href'         => $alink->url,
684                                 ),
685                             null);
686                 }
687             }
688
689             if (!empty($this->geopoint)) {
690                 $xo->element(
691                     'georss:point',
692                     null,
693                     $this->geopoint
694                 );
695             }
696
697             if (!empty($this->poco)) {
698                 $this->poco->outputTo($xo);
699             }
700
701             // @fixme there's no way here to make a tree; elements can only contain plaintext
702             // @fixme these may collide with JSON extensions
703             foreach ($this->extra as $el) {
704                 list($extraTag, $attrs, $content) = array_pad($el, 3, null);
705                 $xo->element($extraTag, $attrs, $content);
706             }
707
708             Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
709         }
710
711         if (!empty($tag)) {
712             $xo->elementEnd($tag);
713         }
714
715         return;
716     }
717
718     function asString($tag='activity:object')
719     {
720         $xs = new XMLStringer(true);
721
722         $this->outputTo($xs, $tag);
723
724         return $xs->getString();
725     }
726
727     /*
728      * Returns an array based on this Activity Object suitable for
729      * encoding as JSON.
730      *
731      * @return array $object the activity object array
732      */
733
734     function asArray()
735     {
736         $object = array();
737
738         if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
739             // XXX: attachments are added by Activity
740
741             // author (Add object for author? Could be useful for repeats.)
742
743             // content (Add rendered version of the notice?)
744
745             // downstreamDuplicates
746
747             // id
748
749             if ($this->id) {
750                 $object['id'] = $this->id;
751             } else if ($this->link) {
752                 $object['id'] = $this->link;
753             }
754
755             if ($this->type == ActivityObject::PERSON
756                 || $this->type == ActivityObject::GROUP) {
757
758                 // displayName
759                 $object['displayName'] = $this->title;
760
761                 // XXX: Not sure what the best avatar is to use for the
762                 // author's "image". For now, I'm using the large size.
763
764                 $imgLink          = null;
765                 $avatarMediaLinks = array();
766
767                 foreach ($this->avatarLinks as $a) {
768
769                     // Make a MediaLink for every other Avatar
770                     $avatar = new ActivityStreamsMediaLink(
771                         $a->url,
772                         $a->width,
773                         $a->height,
774                         $a->type,
775                         'avatar'
776                     );
777
778                     // Find the big avatar to use as the "image"
779                     if ($a->height == AVATAR_PROFILE_SIZE) {
780                         $imgLink = $avatar;
781                     }
782
783                     $avatarMediaLinks[] = $avatar->asArray();
784                 }
785
786                 if (!array_key_exists('status_net', $object)) {
787                     $object['status_net'] = array();
788                 }
789
790                 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
791
792                 // image
793                 if (!empty($imgLink)) {
794                     $object['image']  = $imgLink->asArray();
795                 }
796             }
797
798             // objectType
799             //
800             // We can probably use the whole schema URL here but probably the
801             // relative simple name is easier to parse
802
803             $object['objectType'] = self::canonicalType($this->type);
804
805             // summary
806             $object['summary'] = $this->summary;
807
808             // content, usually rendered HTML
809             $object['content'] = $this->content;
810
811             // published (probably don't need. Might be useful for repeats.)
812
813             // updated (probably don't need this)
814
815             // TODO: upstreamDuplicates
816
817             if ($this->link) {
818                 $object['url'] = $this->link;
819             }
820
821             /* Extensions */
822             // @fixme these may collide with XML extensions
823             // @fixme multiple tags of same name will overwrite each other
824             // @fixme text content from XML extensions will be lost
825
826             foreach ($this->extra as $e) {
827                 list($objectName, $props, $txt) = array_pad($e, 3, null);
828                 if (!empty($objectName)) {
829                     $parts = explode(":", $objectName);
830                     if (count($parts) == 2 && $parts[0] == "statusnet") {
831                         if (!array_key_exists('status_net', $object)) {
832                             $object['status_net'] = array();
833                         }
834                         $object['status_net'][$parts[1]] = $props;
835                     } else {
836                         $object[$objectName] = $props;
837                     }
838                 }
839             }
840
841             if (!empty($this->geopoint)) {
842
843                 list($lat, $lon) = explode(' ', $this->geopoint);
844
845                 if (!empty($lat) && !empty($lon)) {
846                     $object['location'] = array(
847                         'objectType' => 'place',
848                         'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
849                         'lat' => $lat,
850                         'lon' => $lon
851                     );
852
853                     $loc = Location::fromLatLon((float)$lat, (float)$lon);
854
855                     if ($loc) {
856                         $name = $loc->getName();
857
858                         if ($name) {
859                             $object['location']['displayName'] = $name;
860                         }
861                         $url = $loc->getURL();
862
863                         if ($url) {
864                             $object['location']['url'] = $url;
865                         }
866                     }
867                 }
868             }
869
870             if (!empty($this->poco)) {
871                 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
872             }
873
874             if (!empty($this->thumbnail)) {
875                 if (is_string($this->thumbnail)) {
876                     $object['image'] = array('url' => $this->thumbnail);
877                 } else {
878                     $object['image'] = array('url' => $this->thumbnail->getUrl());
879                     if ($this->thumbnail->width) {
880                         $object['image']['width'] = $this->thumbnail->width;
881                     }
882                     if ($this->thumbnail->height) {
883                         $object['image']['height'] = $this->thumbnail->height;
884                     }
885                 }
886             }
887
888             switch (self::canonicalType($this->type)) {
889             case 'image':
890                 if (!empty($this->largerImage)) {
891                     $object['fullImage'] = array('url' => $this->largerImage);
892                 }
893                 break;
894             case 'audio':
895             case 'video':
896                 if (!empty($this->stream)) {
897                     $object['stream'] = array('url' => $this->stream);
898                 }
899                 break;
900             }
901
902             Event::handle('EndActivityObjectOutputJson', array($this, &$object));
903         }
904         return array_filter($object);
905     }
906
907     public function getIdentifiers() {
908         $ids = array();
909         foreach(array('id', 'link', 'url') as $id) {
910             if (isset($this->$id)) {
911                 $ids[] = $this->$id;
912             }
913         }
914         return array_unique($ids);
915     }
916
917     static function canonicalType($type) {
918         return ActivityUtils::resolveUri($type, true);
919     }
920
921     static function mimeTypeToObjectType($mimeType) {
922         $ot = null;
923
924         // Default
925
926         if (empty($mimeType)) {
927             return self::FILE;
928         }
929
930         $parts = explode('/', $mimeType);
931
932         switch ($parts[0]) {
933         case 'image':
934             $ot = self::IMAGE;
935             break;
936         case 'audio':
937             $ot = self::AUDIO;
938             break;
939         case 'video':
940             $ot = self::VIDEO;
941             break;
942         default:
943             $ot = self::FILE;
944         }
945
946         return $ot;
947     }
948 }