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