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