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
46 const CONTENT_TYPE = 'application/stream+json; charset=utf-8';
48 /* Top level array representing the document */
49 protected $doc = array();
51 /* The current authenticated user */
52 protected $cur = null;
57 * @param User $cur the current authenticated user
60 function __construct($cur = null)
65 /* Title of the JSON document */
66 $this->doc['title'] = null;
68 /* Array of activity items */
69 $this->doc['items'] = array();
71 /* Array of links associated with the document */
72 $this->doc['links'] = array();
77 * Set the title of the document
79 * @param String $title the title
82 function setTitle($title)
84 $this->doc['title'] = $title;
88 * Add more than one Item to the document
90 * @param mixed $notices an array of Notice objects or handle
94 function addItemsFromNotices($notices)
96 if (is_array($notices)) {
97 foreach ($notices as $notice) {
98 $this->addItemFromNotice($notice);
101 while ($notices->fetch()) {
102 $this->addItemFromNotice($notices);
108 * Add a single Notice to the document
110 * @param Notice $notice a Notice to add
113 function addItemFromNotice($notice)
115 $cur = empty($this->cur) ? common_current_user() : $this->cur;
117 $act = $notice->asActivity($cur);
118 $act->extra[] = $notice->noticeInfo($cur);
119 array_push($this->doc['items'], $act->asArray());
123 * Add a link to the JSON document
125 * @param string $url the URL for the link
126 * @param string $rel the link relationship
128 function addLink($url = null, $rel = null, $mediaType = null)
130 $link = new ActivityStreamsLink($url, $rel, $mediaType);
131 $this->doc['links'][] = $link->asArray();
135 * Return the entire document as a big string of JSON
137 * @return string encoded JSON output
141 return json_encode(array_filter($this->doc));
147 * A class for representing MediaLinks in JSON Activities
151 * @author Zach Copley <zach@status.net>
152 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
153 * @link http://status.net/
156 class ActivityStreamsMediaLink extends ActivityStreamsLink
160 function __construct(
169 parent::__construct($url, $rel, $mediaType);
170 $this->linkDict = array(
173 'duration' => $duration
181 array_filter($this->linkDict)
187 * A class for representing links in JSON Activities
191 * @author Zach Copley <zach@status.net>
192 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
193 * @link http://status.net/
196 class ActivityStreamsLink
200 function __construct($url = null, $rel = null, $mediaType = null)
202 // links MUST have a URL
204 throw new Exception('Links must have a URL.');
207 $this->linkDict = array(
209 'rel' => $rel, // extension
210 'type' => $mediaType // extension
216 return array_filter($this->linkDict);