]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiconversation.php
Removing unnecessary require_once lines (autoload!)
[quix0rs-gnu-social.git] / actions / apiconversation.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Show a stream of notices in a particular conversation
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  API
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Show a stream of notices in a particular conversation
39  *
40  * @category  API
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2011 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class ApiconversationAction extends ApiAuthAction
48 {
49     protected $conversation = null;
50     protected $notices      = null;
51
52     /**
53      * For initializing members of the class.
54      *
55      * @param array $argarray misc. arguments
56      *
57      * @return boolean true
58      */
59     function prepare($argarray)
60     {
61         parent::prepare($argarray);
62
63         $convId = $this->trimmed('id');
64
65         if (empty($convId)) {
66             // TRANS: Client exception thrown when no conversation ID is given.
67             throw new ClientException(_('No conversation ID.'));
68         }
69
70         $this->conversation = Conversation::getKV('id', $convId);
71
72         if (empty($this->conversation)) {
73             // TRANS: Client exception thrown when referring to a non-existing conversation ID (%d).
74             throw new ClientException(sprintf(_('No conversation with ID %d.'), $convId),
75                                       404);
76         }
77
78         $profile = Profile::current();
79
80         $stream = new ConversationNoticeStream($convId, $profile);
81
82         $notice = $stream->getNotices(($this->page-1) * $this->count,
83                                       $this->count,
84                                       $this->since_id,
85                                       $this->max_id);
86
87         $this->notices = $notice->fetchAll();
88
89         return true;
90     }
91
92     /**
93      * Handler method
94      *
95      * @param array $argarray is ignored since it's now passed in in prepare()
96      *
97      * @return void
98      */
99     function handle($argarray=null)
100     {
101         $sitename   = common_config('site', 'name');
102         // TRANS: Title for conversion timeline.
103         $title      = _m('TITLE', 'Conversation');
104         $id         = common_local_url('apiconversation', array('id' => $this->conversation->id, 'format' => $this->format));
105         $link       = common_local_url('conversation', array('id' => $this->conversation->id));
106
107         $self       = $id;
108
109         switch($this->format) {
110         case 'xml':
111             $this->showXmlTimeline($this->notices);
112             break;
113         case 'rss':
114             $this->showRssTimeline(
115                 $this->notices,
116                 $title,
117                 $link,
118                 null,
119                 null,
120                 null,
121                 $self
122             );
123             break;
124         case 'atom':
125
126             header('Content-Type: application/atom+xml; charset=utf-8');
127
128             $atom = new AtomNoticeFeed($this->auth_user);
129
130             $atom->setId($id);
131             $atom->setTitle($title);
132             $atom->setUpdated('now');
133
134             $atom->addLink($link);
135             $atom->setSelfLink($self);
136
137             $atom->addEntryFromNotices($this->notices);
138             $this->raw($atom->getString());
139
140             break;
141         case 'json':
142             $this->showJsonTimeline($this->notices);
143             break;
144         case 'as':
145             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
146             $doc = new ActivityStreamJSONDocument($this->auth_user);
147             $doc->setTitle($title);
148             $doc->addLink($link, 'alternate', 'text/html');
149             $doc->addItemsFromNotices($this->notices);
150             $this->raw($doc->asString());
151             break;
152         default:
153             // TRANS: Client error displayed when coming across a non-supported API method.
154             $this->clientError(_('API method not found.'), $code = 404);
155             break;
156         }
157     }
158
159     /**
160      * Return true if read only.
161      *
162      * MAY override
163      *
164      * @param array $args other arguments
165      *
166      * @return boolean is read only action?
167      */
168     function isReadOnly($args)
169     {
170         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
171             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
172             return true;
173         } else {
174             return false;
175         }
176     }
177
178     /**
179      * Return last modified, if applicable.
180      *
181      * MAY override
182      *
183      * @return string last modified http header
184      */
185     function lastModified()
186     {
187         if (!empty($this->notices) && (count($this->notices) > 0)) {
188             return strtotime($this->notices[0]->created);
189         }
190
191         return null;
192     }
193
194     /**
195      * Return etag, if applicable.
196      *
197      * MAY override
198      *
199      * @return string etag http header
200      */
201     function etag()
202     {
203         if (!empty($this->notices) && (count($this->notices) > 0)) {
204
205             $last = count($this->notices) - 1;
206
207             return '"' . implode(
208                 ':',
209                 array($this->arg('action'),
210                       common_user_cache_hash($this->auth_user),
211                       common_language(),
212                       $this->user->id,
213                       strtotime($this->notices[0]->created),
214                       strtotime($this->notices[$last]->created))
215             )
216             . '"';
217         }
218
219         return null;
220     }
221
222     /**
223      * Does this require authentication?
224      *
225      * @return boolean true if delete, else false
226      */
227     function requiresAuth()
228     {
229         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
230             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
231             return false;
232         } else {
233             return true;
234         }
235     }
236 }