]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiconversation.php
Merge remote-tracking branch 'upstream/master' into social-master
[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     protected function prepare(array $args=array())
53     {
54         parent::prepare($args);
55
56         $convId = $this->trimmed('id');
57
58         if (empty($convId)) {
59             // TRANS: Client exception thrown when no conversation ID is given.
60             throw new ClientException(_('No conversation ID.'));
61         }
62
63         $this->conversation = Conversation::getKV('id', $convId);
64
65         if (empty($this->conversation)) {
66             // TRANS: Client exception thrown when referring to a non-existing conversation ID (%d).
67             throw new ClientException(sprintf(_('No conversation with ID %d.'), $convId),
68                                       404);
69         }
70
71         $stream = new ConversationNoticeStream($convId, $this->scoped);
72
73         $notice = $stream->getNotices(($this->page-1) * $this->count,
74                                       $this->count,
75                                       $this->since_id,
76                                       $this->max_id);
77
78         $this->notices = $notice->fetchAll();
79
80         return true;
81     }
82
83     /**
84      * Handler method
85      *
86      * @param array $argarray is ignored since it's now passed in in prepare()
87      *
88      * @return void
89      */
90     function handle($argarray=null)
91     {
92         $sitename   = common_config('site', 'name');
93         // TRANS: Title for conversion timeline.
94         $title      = _m('TITLE', 'Conversation');
95         $id         = common_local_url('apiconversation', array('id' => $this->conversation->id, 'format' => $this->format));
96         $link       = common_local_url('conversation', array('id' => $this->conversation->id));
97
98         $self       = $id;
99
100         switch($this->format) {
101         case 'xml':
102             $this->showXmlTimeline($this->notices);
103             break;
104         case 'rss':
105             $this->showRssTimeline(
106                 $this->notices,
107                 $title,
108                 $link,
109                 null,
110                 null,
111                 null,
112                 $self
113             );
114             break;
115         case 'atom':
116
117             header('Content-Type: application/atom+xml; charset=utf-8');
118
119             $atom = new AtomNoticeFeed($this->auth_user);
120
121             $atom->setId($id);
122             $atom->setTitle($title);
123             $atom->setUpdated('now');
124
125             $atom->addLink($link);
126             $atom->setSelfLink($self);
127
128             $atom->addEntryFromNotices($this->notices);
129             $this->raw($atom->getString());
130
131             break;
132         case 'json':
133             $this->showJsonTimeline($this->notices);
134             break;
135         case 'as':
136             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
137             $doc = new ActivityStreamJSONDocument($this->auth_user);
138             $doc->setTitle($title);
139             $doc->addLink($link, 'alternate', 'text/html');
140             $doc->addItemsFromNotices($this->notices);
141             $this->raw($doc->asString());
142             break;
143         default:
144             // TRANS: Client error displayed when coming across a non-supported API method.
145             $this->clientError(_('API method not found.'), $code = 404);
146             break;
147         }
148     }
149
150     /**
151      * Return true if read only.
152      *
153      * MAY override
154      *
155      * @param array $args other arguments
156      *
157      * @return boolean is read only action?
158      */
159     function isReadOnly(array $args=array())
160     {
161         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
162             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
163             return true;
164         } else {
165             return false;
166         }
167     }
168
169     /**
170      * Return last modified, if applicable.
171      *
172      * MAY override
173      *
174      * @return string last modified http header
175      */
176     function lastModified()
177     {
178         if (!empty($this->notices) && (count($this->notices) > 0)) {
179             return strtotime($this->notices[0]->created);
180         }
181
182         return null;
183     }
184
185     /**
186      * Return etag, if applicable.
187      *
188      * MAY override
189      *
190      * @return string etag http header
191      */
192     function etag()
193     {
194         if (!empty($this->notices) && (count($this->notices) > 0)) {
195
196             $last = count($this->notices) - 1;
197
198             return '"' . implode(
199                 ':',
200                 array($this->arg('action'),
201                       common_user_cache_hash($this->auth_user),
202                       common_language(),
203                       $this->user->id,
204                       strtotime($this->notices[0]->created),
205                       strtotime($this->notices[$last]->created))
206             )
207             . '"';
208         }
209
210         return null;
211     }
212
213     /**
214      * Does this require authentication?
215      *
216      * @return boolean true if delete, else false
217      */
218     function requiresAuth()
219     {
220         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
221             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
222             return false;
223         } else {
224             return true;
225         }
226     }
227 }