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