activityobject.php 30 KB

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