]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitystreamjsondocument.php
Add avatars and notice info
[quix0rs-gnu-social.git] / lib / activitystreamjsondocument.php
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
30 if (!defined('STATUSNET'))
31 {
32     exit(1);
33 }
34
35 /**
36  * A class for generating JSON documents that represent an Activity Streams
37  *
38  * @category Feed
39  * @package  StatusNet
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/
43  */
44 class ActivityStreamJSONDocument
45 {
46
47     /* Top level array representing the document */
48     protected $doc = array();
49
50     /* The current authenticated user */
51     protected $cur = null;
52
53     /**
54      * Constructor
55      *
56      * @param User $cur the current authenticated user
57      */
58
59     function __construct($cur = null)
60     {
61
62         $this->cur = $cur;
63
64         /* Title of the JSON document */
65         $this->doc['title'] = null;
66
67         /* Array of activity items */
68         $this->doc['items'] = array();
69
70         /* Array of links associated with the document */
71         $this->doc['links'] = array();
72
73     }
74
75     /**
76      * Set the title of the document
77      *
78      * @param String $title the title
79      */
80
81     function setTitle($title)
82     {
83         $this->doc['title'] = $title;
84     }
85
86     /**
87      * Add more than one Item to the document
88      *
89      * @param mixed $notices an array of Notice objects or handle
90      *
91      */
92
93     function addItemsFromNotices($notices)
94     {
95         common_debug("addItemsFromNotices");
96         if (is_array($notices)) {
97             foreach ($notices as $notice) {
98                 $this->addItemFromNotice($notice);
99             }
100         } else {
101             while ($notices->fetch()) {
102                 $this->addItemFromNotice($notices);
103             }
104         }
105     }
106
107     /**
108      * Add a single Notice to the document
109      *
110      * @param Notice $notice a Notice to add
111      */
112
113     function addItemFromNotice($notice)
114     {
115         $cur = empty($this->cur) ? common_current_user() : $this->cur;
116
117         $act          = $notice->asActivity();
118         $act->extra[] = $notice->noticeInfo($cur);
119
120         array_push($this->doc['items'], $act->asArray());
121     }
122
123     /**
124      * Add a link to the JSON document
125      *
126      * @param string $url the URL for the link
127      * @param string $rel the link relationship
128      */
129     function addLink($url = null, $rel = null, $mediaType = null)
130     {
131         $link = new ActivityStreamsLink($url, $rel, $mediaType);
132         $this->doc['link'][] = $link->asArray();
133     }
134
135     /*
136      * Return the entire document as a big string of JSON
137      *
138      * @return string encoded JSON output
139      */
140     function asString()
141     {
142         return json_encode(array_filter($this->doc));
143     }
144
145 }
146
147 /**
148  * A class for representing MediaLinks in JSON Activities
149  *
150  * @category Feed
151  * @package  StatusNet
152  * @author   Zach Copley <zach@status.net>
153  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
154  * @link     http://status.net/
155  */
156
157 class ActivityStreamsMediaLink extends ActivityStreamsLink
158 {
159     private $linkDict;
160
161     function __construct(
162         $url       = null,
163         $width     = null,
164         $height    = null,
165         $mediaType = null,
166         $rel       = null,
167         $duration  = null
168     )
169     {
170         parent::__construct($url, $rel, $mediaType);
171         $this->linkDict = array(
172             'width'      => $width,
173             'height'     => $height,
174             'duration'   => $duration
175         );
176     }
177
178     function asArray()
179     {
180         return array_merge(
181             parent::asArray(),
182             array_filter($this->linkDict)
183         );
184     }
185 }
186
187 /**
188  * A class for representing links in JSON Activities
189  *
190  * @category Feed
191  * @package  StatusNet
192  * @author   Zach Copley <zach@status.net>
193  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
194  * @link     http://status.net/
195  */
196
197 class ActivityStreamsLink
198 {
199     private $linkDict;
200
201     function __construct($url = null, $rel = null, $mediaType = null)
202     {
203         // links MUST have a URL
204         if (empty($url)) {
205             throw new Exception('Links must have a URL.');
206         }
207
208         $this->linkDict = array(
209             'url'   => $url,
210             'rel'   => $rel,      // extension
211             'type'  => $mediaType // extension
212         );
213     }
214
215     function asArray()
216     {
217         return array_filter($this->linkDict);
218     }
219 }