]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
Add events for representing objects as activity:object
[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 /**
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 PLACE     = 'http://activitystrea.ms/schema/1.0/place';
68     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
69     // ^^^^^^^^^^ tea!
70
71     // Atom elements we snarf
72
73     const TITLE   = 'title';
74     const SUMMARY = 'summary';
75     const ID      = 'id';
76     const SOURCE  = 'source';
77
78     const NAME  = 'name';
79     const URI   = 'uri';
80     const EMAIL = 'email';
81
82     const POSTEROUS   = 'http://posterous.com/help/rss/1.0';
83     const AUTHOR      = 'author';
84     const USERIMAGE   = 'userImage';
85     const PROFILEURL  = 'profileUrl';
86     const NICKNAME    = 'nickName';
87     const DISPLAYNAME = 'displayName';
88
89     public $element;
90     public $type;
91     public $id;
92     public $title;
93     public $summary;
94     public $content;
95     public $link;
96     public $source;
97     public $avatarLinks = array();
98     public $geopoint;
99     public $poco;
100     public $displayName;
101
102     // @todo move this stuff to it's own PHOTO activity object
103     const MEDIA_DESCRIPTION = 'description';
104
105     public $thumbnail;
106     public $largerImage;
107     public $description;
108
109     /**
110      * Constructor
111      *
112      * This probably needs to be refactored
113      * to generate a local class (ActivityPerson, ActivityFile, ...)
114      * based on the object type.
115      *
116      * @param DOMElement $element DOM thing to turn into an Activity thing
117      */
118     function __construct($element = null)
119     {
120         if (empty($element)) {
121             return;
122         }
123
124         $this->element = $element;
125
126         $this->geopoint = $this->_childContent(
127             $element,
128             ActivityContext::POINT,
129             ActivityContext::GEORSS
130         );
131
132         if ($element->tagName == 'author') {
133             $this->_fromAuthor($element);
134         } else if ($element->tagName == 'item') {
135             $this->_fromRssItem($element);
136         } else {
137             $this->_fromAtomEntry($element);
138         }
139
140         // Some per-type attributes...
141         if ($this->type == self::PERSON || $this->type == self::GROUP) {
142             $this->displayName = $this->title;
143
144             $photos = ActivityUtils::getLinks($element, 'photo');
145             if (count($photos)) {
146                 foreach ($photos as $link) {
147                     $this->avatarLinks[] = new AvatarLink($link);
148                 }
149             } else {
150                 $avatars = ActivityUtils::getLinks($element, 'avatar');
151                 foreach ($avatars as $link) {
152                     $this->avatarLinks[] = new AvatarLink($link);
153                 }
154             }
155
156             $this->poco = new PoCo($element);
157         }
158
159         if ($this->type == self::PHOTO) {
160
161             $this->thumbnail   = ActivityUtils::getLink($element, 'preview');
162             $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
163
164             $this->description = ActivityUtils::childContent(
165                 $element,
166                 ActivityObject::MEDIA_DESCRIPTION,
167                 Activity::MEDIA
168             );
169         }
170     }
171
172     private function _fromAuthor($element)
173     {
174         $this->type  = self::PERSON; // XXX: is this fair?
175         $this->title = $this->_childContent($element, self::NAME);
176
177         $this->id = $this->_childContent($element, self::URI);
178
179         if (empty($this->id)) {
180             $email = $this->_childContent($element, self::EMAIL);
181             if (!empty($email)) {
182                 // XXX: acct: ?
183                 $this->id = 'mailto:'.$email;
184             }
185         }
186     }
187
188     private function _fromAtomEntry($element)
189     {
190         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
191                                            Activity::SPEC);
192
193         if (empty($this->type)) {
194             $this->type = ActivityObject::NOTE;
195         }
196
197         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
198         $this->content = ActivityUtils::getContent($element);
199
200         // We don't like HTML in our titles, although it's technically allowed
201
202         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
203
204         $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
205
206         $this->source  = $this->_getSource($element);
207
208         $this->link = ActivityUtils::getPermalink($element);
209
210         $this->id = $this->_childContent($element, self::ID);
211
212         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
213             $this->id = $this->link;
214         }
215     }
216
217     // @todo FIXME: rationalize with Activity::_fromRssItem()
218     private function _fromRssItem($item)
219     {
220         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
221
222         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
223
224         if (!empty($contentEl)) {
225             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
226         } else {
227             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
228             if (!empty($descriptionEl)) {
229                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
230             }
231         }
232
233         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
234
235         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
236
237         if (!empty($guidEl)) {
238             $this->id = $guidEl->textContent;
239
240             if ($guidEl->hasAttribute('isPermaLink')) {
241                 // overwrites <link>
242                 $this->link = $this->id;
243             }
244         }
245     }
246
247     public static function fromRssAuthor($el)
248     {
249         $text = $el->textContent;
250
251         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
252             $email = $match[1];
253             $name = $match[2];
254         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
255             $name = $match[1];
256             $email = $match[2];
257         } else if (preg_match('/.*@.*/', $text)) {
258             $email = $text;
259             $name = null;
260         } else {
261             $name = $text;
262             $email = null;
263         }
264
265         // Not really enough info
266
267         $obj = new ActivityObject();
268
269         $obj->element = $el;
270
271         $obj->type  = ActivityObject::PERSON;
272         $obj->title = $name;
273
274         if (!empty($email)) {
275             $obj->id = 'mailto:'.$email;
276         }
277
278         return $obj;
279     }
280
281     public static function fromDcCreator($el)
282     {
283         // Not really enough info
284
285         $text = $el->textContent;
286
287         $obj = new ActivityObject();
288
289         $obj->element = $el;
290
291         $obj->title = $text;
292         $obj->type  = ActivityObject::PERSON;
293
294         return $obj;
295     }
296
297     public static function fromRssChannel($el)
298     {
299         $obj = new ActivityObject();
300
301         $obj->element = $el;
302
303         $obj->type = ActivityObject::PERSON; // @fixme guess better
304
305         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
306         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
307         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
308
309         if (empty($obj->id)) {
310             $obj->id = $obj->link;
311         }
312
313         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
314
315         if (!empty($desc)) {
316             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
317         }
318
319         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
320
321         if (!empty($imageEl)) {
322             $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
323             $al = new AvatarLink();
324             $al->url = $url;
325             $obj->avatarLinks[] = $al;
326         }
327
328         return $obj;
329     }
330
331     public static function fromPosterousAuthor($el)
332     {
333         $obj = new ActivityObject();
334
335         $obj->type = ActivityObject::PERSON; // @fixme any others...?
336
337         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
338
339         if (!empty($userImage)) {
340             $al = new AvatarLink();
341             $al->url = $userImage;
342             $obj->avatarLinks[] = $al;
343         }
344
345         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
346         $obj->id   = $obj->link;
347
348         $obj->poco = new PoCo();
349
350         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
351         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
352
353         $obj->title = $obj->poco->displayName;
354
355         return $obj;
356     }
357
358     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
359     {
360         return ActivityUtils::childContent($element, $tag, $namespace);
361     }
362
363     // Try to get a unique id for the source feed
364
365     private function _getSource($element)
366     {
367         $sourceEl = ActivityUtils::child($element, 'source');
368
369         if (empty($sourceEl)) {
370             return null;
371         } else {
372             $href = ActivityUtils::getLink($sourceEl, 'self');
373             if (!empty($href)) {
374                 return $href;
375             } else {
376                 return ActivityUtils::childContent($sourceEl, 'id');
377             }
378         }
379     }
380
381     static function fromNotice(Notice $notice)
382     {
383         $object = new ActivityObject();
384                 
385                 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
386
387                         $object->type    = ActivityObject::NOTE;
388
389                         $object->id      = $notice->uri;
390                         $object->title   = $notice->content;
391                         $object->content = $notice->rendered;
392                         $object->link    = $notice->bestUrl();
393
394                         Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
395                 }
396
397         return $object;
398     }
399
400     static function fromProfile(Profile $profile)
401     {
402         $object = new ActivityObject();
403
404                 if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
405
406                         $object->type   = ActivityObject::PERSON;
407                         $object->id     = $profile->getUri();
408                         $object->title  = $profile->getBestName();
409                         $object->link   = $profile->profileurl;
410
411                         $orig = $profile->getOriginalAvatar();
412
413                         if (!empty($orig)) {
414                                 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
415                         }
416
417                         $sizes = array(
418                                                    AVATAR_PROFILE_SIZE,
419                                                    AVATAR_STREAM_SIZE,
420                                                    AVATAR_MINI_SIZE
421                                                    );
422
423                         foreach ($sizes as $size) {
424                                 $alink  = null;
425                                 $avatar = $profile->getAvatar($size);
426
427                                 if (!empty($avatar)) {
428                                         $alink = AvatarLink::fromAvatar($avatar);
429                                 } else {
430                                         $alink = new AvatarLink();
431                                         $alink->type   = 'image/png';
432                                         $alink->height = $size;
433                                         $alink->width  = $size;
434                                         $alink->url    = Avatar::defaultImage($size);
435
436                                         if ($size == AVATAR_PROFILE_SIZE) {
437                                                 // Hack for Twitter import: we don't have a 96x96 image,
438                                                 // but we do have a 73x73 image. For now, fake it with that.
439                                                 $avatar = $profile->getAvatar(73);
440                                                 if ($avatar) {
441                                                         $alink = AvatarLink::fromAvatar($avatar);
442                                                         $alink->height= $size;
443                                                         $alink->width = $size;
444                                                 }
445                                         }
446                                 }
447
448                                 $object->avatarLinks[] = $alink;
449                         }
450
451                         if (isset($profile->lat) && isset($profile->lon)) {
452                                 $object->geopoint = (float)$profile->lat
453                                         . ' ' . (float)$profile->lon;
454                         }
455
456                         $object->poco = PoCo::fromProfile($profile);
457
458                         Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
459                 }
460
461         return $object;
462     }
463
464     static function fromGroup($group)
465     {
466         $object = new ActivityObject();
467
468                 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
469
470                         $object->type   = ActivityObject::GROUP;
471                         $object->id     = $group->getUri();
472                         $object->title  = $group->getBestName();
473                         $object->link   = $group->getUri();
474
475                         $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
476                                                                                                                           AVATAR_PROFILE_SIZE);
477
478                         $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
479                                                                                                                           AVATAR_STREAM_SIZE);
480
481                         $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
482                                                                                                                           AVATAR_MINI_SIZE);
483
484                         $object->poco = PoCo::fromGroup($group);
485
486                         Event::handle('EndActivityObjectFromGroup', array($group, &$object));
487                 }
488
489         return $object;
490     }
491
492     function asString($tag='activity:object')
493     {
494         $xs = new XMLStringer(true);
495
496         $xs->elementStart($tag);
497
498         $xs->element('activity:object-type', null, $this->type);
499
500         $xs->element(self::ID, null, $this->id);
501
502         if (!empty($this->title)) {
503             $xs->element(
504                 self::TITLE,
505                 null,
506                 common_xml_safe_str($this->title)
507             );
508         }
509
510         if (!empty($this->summary)) {
511             $xs->element(
512                 self::SUMMARY,
513                 null,
514                 common_xml_safe_str($this->summary)
515             );
516         }
517
518         if (!empty($this->content)) {
519             // XXX: assuming HTML content here
520             $xs->element(
521                 ActivityUtils::CONTENT,
522                 array('type' => 'html'),
523                 common_xml_safe_str($this->content)
524             );
525         }
526
527         if (!empty($this->link)) {
528             $xs->element(
529                 'link',
530                 array(
531                     'rel' => 'alternate',
532                     'type' => 'text/html',
533                     'href' => $this->link
534                 ),
535                 null
536             );
537         }
538
539         if ($this->type == ActivityObject::PERSON
540             || $this->type == ActivityObject::GROUP) {
541
542             foreach ($this->avatarLinks as $avatar) {
543                 $xs->element(
544                     'link', array(
545                         'rel'  => 'avatar',
546                         'type'         => $avatar->type,
547                         'media:width'  => $avatar->width,
548                         'media:height' => $avatar->height,
549                         'href' => $avatar->url
550                     ),
551                     null
552                 );
553             }
554         }
555
556         if (!empty($this->geopoint)) {
557             $xs->element(
558                 'georss:point',
559                 null,
560                 $this->geopoint
561             );
562         }
563
564         if (!empty($this->poco)) {
565             $xs->raw($this->poco->asString());
566         }
567
568         $xs->elementEnd($tag);
569
570         return $xs->getString();
571     }
572 }