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('STATUSNET'))
36 * A class for generating JSON documents that represent an Activity Streams
40 * @author Zach Copley <zach@status.net>
41 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42 * @link http://status.net/
44 class ActivityStreamJSONDocument extends JSONActivityCollection
46 // Note: Lot of AS folks think the content type should be:
47 // 'application/stream+json; charset=utf-8', but this is more
48 // useful at the moment, because some programs actually understand
50 const CONTENT_TYPE = 'application/json; charset=utf-8';
52 /* Top level array representing the document */
53 protected $doc = array();
55 /* The current authenticated user */
57 protected $scoped = null;
59 /* Title of the document */
62 /* Links associated with this document */
65 /* Count of items in this document */
66 // XXX This is cryptically referred to in the spec: "The Stream serialization MAY contain a count property."
72 * @param User $cur the current authenticated user
75 function __construct($cur = null, $title = null, $items = null, $links = null, $url = null)
77 parent::__construct($items, $url);
79 $this->cur = $cur ?: common_current_user();
80 $this->scoped = !is_null($this->cur) ? $this->cur->getProfile() : null;
82 /* Title of the JSON document */
83 $this->title = $title;
86 $this->count = count($this->items);
89 /* Array of links associated with the document */
90 $this->links = empty($links) ? array() : $items;
92 /* URL of a document, this document? containing a list of all the items in the stream */
93 if (!empty($this->url)) {
94 $this->url = $this->url;
99 * Set the title of the document
101 * @param String $title the title
104 function setTitle($title)
106 $this->title = $title;
109 function setUrl($url)
116 * Add more than one Item to the document
118 * @param mixed $notices an array of Notice objects or handle
122 function addItemsFromNotices($notices)
124 if (is_array($notices)) {
125 foreach ($notices as $notice) {
126 $this->addItemFromNotice($notice);
129 while ($notices->fetch()) {
130 $this->addItemFromNotice($notices);
136 * Add a single Notice to the document
138 * @param Notice $notice a Notice to add
141 function addItemFromNotice($notice)
143 $act = $notice->asActivity($this->scoped);
144 $act->extra[] = $notice->noticeInfo($this->scoped);
145 array_push($this->items, $act->asArray());
150 * Add a link to the JSON document
152 * @param string $url the URL for the link
153 * @param string $rel the link relationship
155 function addLink($url = null, $rel = null, $mediaType = null)
157 $link = new ActivityStreamsLink($url, $rel, $mediaType);
158 array_push($this->links, $link->asArray());
162 * Return the entire document as a big string of JSON
164 * @return string encoded JSON output
168 $this->doc['generator'] = 'GNU social ' . GNUSOCIAL_VERSION; // extension
169 $this->doc['title'] = $this->title;
170 $this->doc['url'] = $this->url;
171 $this->doc['totalItems'] = $this->count;
172 $this->doc['items'] = $this->items;
173 $this->doc['links'] = $this->links; // extension
174 return json_encode(array_filter($this->doc)); // filter out empty elements