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