activitystreamjsondocument.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Class for serializing Activity Streams in JSON
  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 Zach Copley <zach@status.net>
  25. * @copyright 2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) {
  30. exit(1);
  31. }
  32. /**
  33. * A class for generating JSON documents that represent an Activity Streams
  34. *
  35. * @category Feed
  36. * @package StatusNet
  37. * @author Zach Copley <zach@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://status.net/
  40. */
  41. class ActivityStreamJSONDocument extends JSONActivityCollection
  42. {
  43. // Note: Lot of AS folks think the content type should be:
  44. // 'application/stream+json; charset=utf-8', but this is more
  45. // useful at the moment, because some programs actually understand
  46. // it.
  47. const CONTENT_TYPE = 'application/json; charset=utf-8';
  48. /* Top level array representing the document */
  49. protected $doc = [];
  50. /* The current authenticated user */
  51. protected $cur;
  52. protected $scoped = null;
  53. /* Title of the document */
  54. protected $title;
  55. /* Links associated with this document */
  56. protected $links;
  57. /* Count of items in this document */
  58. // XXX This is cryptically referred to in the spec: "The Stream serialization MAY contain a count property."
  59. protected $count;
  60. /**
  61. * Constructor
  62. *
  63. * @param User $cur the current authenticated user
  64. * @throws UserNoProfileException
  65. */
  66. public function __construct($cur = null, $title = null, array $items = [], $links = null, $url = null)
  67. {
  68. parent::__construct($items, $url);
  69. $this->cur = $cur ?: common_current_user();
  70. $this->scoped = !is_null($this->cur) ? $this->cur->getProfile() : null;
  71. /* Title of the JSON document */
  72. $this->title = $title;
  73. if (!empty($items)) {
  74. $this->count = count($this->items);
  75. }
  76. /* Array of links associated with the document */
  77. $this->links = empty($links) ? [] : $items;
  78. /* URL of a document, this document? containing a list of all the items in the stream */
  79. if (!empty($url)) {
  80. $this->url = $url;
  81. }
  82. }
  83. /**
  84. * Set the title of the document
  85. *
  86. * @param string $title the title
  87. */
  88. public function setTitle($title)
  89. {
  90. $this->title = $title;
  91. }
  92. public function setUrl($url)
  93. {
  94. $this->url = $url;
  95. }
  96. /**
  97. * Add more than one Item to the document
  98. *
  99. * @param mixed $notices an array of Notice objects or handle
  100. * @throws ClientException
  101. * @throws ServerException
  102. */
  103. public function addItemsFromNotices($notices)
  104. {
  105. if (is_array($notices)) {
  106. foreach ($notices as $notice) {
  107. $this->addItemFromNotice($notice);
  108. }
  109. } else {
  110. while ($notices->fetch()) {
  111. $this->addItemFromNotice($notices);
  112. }
  113. }
  114. }
  115. /**
  116. * Add a single Notice to the document
  117. *
  118. * @param Notice $notice a Notice to add
  119. */
  120. public function addItemFromNotice($notice)
  121. {
  122. try {
  123. $act = $notice->asActivity($this->scoped);
  124. } catch (Exception $e) {
  125. // We know exceptions like
  126. // "No result found on Fave lookup."
  127. // may happen because of deleted notices etc.
  128. // These are irrelevant for the feed purposes.
  129. return;
  130. }
  131. $act->extra[] = $notice->noticeInfo($this->scoped);
  132. array_push($this->items, $act->asArray());
  133. $this->count++;
  134. }
  135. /**
  136. * Add a link to the JSON document
  137. *
  138. * @param string $url the URL for the link
  139. * @param string $rel the link relationship
  140. * @throws Exception
  141. */
  142. public function addLink($url = null, $rel = null, $mediaType = null)
  143. {
  144. $link = new ActivityStreamsLink($url, $rel, $mediaType);
  145. array_push($this->links, $link->asArray());
  146. }
  147. /*
  148. * Return the entire document as a big string of JSON
  149. *
  150. * @return string encoded JSON output
  151. */
  152. public function asString()
  153. {
  154. $this->doc['generator'] = 'GNU social ' . GNUSOCIAL_VERSION; // extension
  155. $this->doc['title'] = $this->title;
  156. $this->doc['url'] = $this->url;
  157. $this->doc['totalItems'] = $this->count;
  158. $this->doc['items'] = $this->items;
  159. $this->doc['links'] = $this->links; // extension
  160. return json_encode(array_filter($this->doc)); // filter out empty elements
  161. }
  162. }