3 * StatusNet, the distributed open-source microblogging tool
5 * Class for serializing Activity Streams in JSON
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.
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.
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/>.
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/
30 if (!defined('GNUSOCIAL')) { exit(1); }
33 * A class for generating JSON documents that represent an Activity Streams
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/
41 class ActivityStreamJSONDocument extends JSONActivityCollection
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
47 const CONTENT_TYPE = 'application/json; charset=utf-8';
49 /* Top level array representing the document */
50 protected $doc = array();
52 /* The current authenticated user */
54 protected $scoped = null;
56 /* Title of the document */
59 /* Links associated with this document */
62 /* Count of items in this document */
63 // XXX This is cryptically referred to in the spec: "The Stream serialization MAY contain a count property."
69 * @param User $cur the current authenticated user
72 function __construct($cur = null, $title = null, array $items=[], $links = null, $url = null)
74 parent::__construct($items, $url);
76 $this->cur = $cur ?: common_current_user();
77 $this->scoped = !is_null($this->cur) ? $this->cur->getProfile() : null;
79 /* Title of the JSON document */
80 $this->title = $title;
83 $this->count = count($this->items);
86 /* Array of links associated with the document */
87 $this->links = empty($links) ? array() : $items;
89 /* URL of a document, this document? containing a list of all the items in the stream */
90 if (!empty($this->url)) {
91 $this->url = $this->url;
96 * Set the title of the document
98 * @param String $title the title
101 function setTitle($title)
103 $this->title = $title;
106 function setUrl($url)
113 * Add more than one Item to the document
115 * @param mixed $notices an array of Notice objects or handle
119 function addItemsFromNotices($notices)
121 if (is_array($notices)) {
122 foreach ($notices as $notice) {
123 $this->addItemFromNotice($notice);
126 while ($notices->fetch()) {
127 $this->addItemFromNotice($notices);
133 * Add a single Notice to the document
135 * @param Notice $notice a Notice to add
138 function addItemFromNotice($notice)
140 $act = $notice->asActivity($this->scoped);
141 $act->extra[] = $notice->noticeInfo($this->scoped);
142 array_push($this->items, $act->asArray());
147 * Add a link to the JSON document
149 * @param string $url the URL for the link
150 * @param string $rel the link relationship
152 function addLink($url = null, $rel = null, $mediaType = null)
154 $link = new ActivityStreamsLink($url, $rel, $mediaType);
155 array_push($this->links, $link->asArray());
159 * Return the entire document as a big string of JSON
161 * @return string encoded JSON output
165 $this->doc['generator'] = 'GNU social ' . GNUSOCIAL_VERSION; // extension
166 $this->doc['title'] = $this->title;
167 $this->doc['url'] = $this->url;
168 $this->doc['totalItems'] = $this->count;
169 $this->doc['items'] = $this->items;
170 $this->doc['links'] = $this->links; // extension
171 return json_encode(array_filter($this->doc)); // filter out empty elements