]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiconversation.php
Merge branch 'master' into 1.0.x
[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
50 class ApiconversationAction extends ApiAuthAction
51 {
52         protected $conversation = null;
53         protected $notices      = null;
54         
55     /**
56      * For initializing members of the class.
57      *
58      * @param array $argarray misc. arguments
59      *
60      * @return boolean true
61      */
62
63     function prepare($argarray)
64     {
65         parent::prepare($argarray);
66         
67         $convId = $this->trimmed('id');
68         
69         if (empty($convId)) {
70                 throw new ClientException(_m('no conversation id'));
71         }
72         
73         $this->conversation = Conversation::staticGet('id', $convId);
74         
75         if (empty($this->conversation)) {
76                 throw new ClientException(sprintf(_m('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
102     function handle($argarray=null)
103     {
104         $sitename   = common_config('site', 'name');
105         // TRANS: Timeline title for user and friends. %s is a user nickname.
106         $title      = _("Conversation");
107         $id         = common_local_url('apiconversation', array('id' => $this->conversation->id, 'format' => $this->format));
108         $link       = common_local_url('conversation', array('id' => $this->conversation->id));
109
110         $self       = $id;
111         
112         switch($this->format) {
113         case 'xml':
114             $this->showXmlTimeline($this->notices);
115             break;
116         case 'rss':
117             $this->showRssTimeline(
118                 $this->notices,
119                 $title,
120                 $link,
121                 null,
122                 null,
123                 null,
124                 $self
125             );
126             break;
127         case 'atom':
128
129             header('Content-Type: application/atom+xml; charset=utf-8');
130
131             $atom = new AtomNoticeFeed($this->auth_user);
132
133             $atom->setId($id);
134             $atom->setTitle($title);
135             $atom->setUpdated('now');
136
137             $atom->addLink($link);
138             $atom->setSelfLink($self);
139
140             $atom->addEntryFromNotices($this->notices);
141             $this->raw($atom->getString());
142
143             break;
144         case 'json':
145             $this->showJsonTimeline($this->notices);
146             break;
147         case 'as':
148             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
149             $doc = new ActivityStreamJSONDocument($this->auth_user);
150             $doc->setTitle($title);
151             $doc->addLink($link, 'alternate', 'text/html');
152             $doc->addItemsFromNotices($this->notices);
153             $this->raw($doc->asString());
154             break;
155         default:
156             // TRANS: Client error displayed when coming across a non-supported API method.
157             $this->clientError(_('API method not found.'), $code = 404);
158             break;
159         }
160     }
161
162     /**
163      * Return true if read only.
164      *
165      * MAY override
166      *
167      * @param array $args other arguments
168      *
169      * @return boolean is read only action?
170      */
171
172     function isReadOnly($args)
173     {
174         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
175             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
176             return true;
177         } else {
178             return false;
179         }
180     }
181
182     /**
183      * Return last modified, if applicable.
184      *
185      * MAY override
186      *
187      * @return string last modified http header
188      */
189     function lastModified()
190     {
191         if (!empty($this->notices) && (count($this->notices) > 0)) {
192             return strtotime($this->notices[0]->created);
193         }
194
195         return null;
196     }
197
198     /**
199      * Return etag, if applicable.
200      *
201      * MAY override
202      *
203      * @return string etag http header
204      */
205
206     function etag()
207     {
208         if (!empty($this->notices) && (count($this->notices) > 0)) {
209
210             $last = count($this->notices) - 1;
211
212             return '"' . implode(
213                 ':',
214                 array($this->arg('action'),
215                       common_user_cache_hash($this->auth_user),
216                       common_language(),
217                       $this->user->id,
218                       strtotime($this->notices[0]->created),
219                       strtotime($this->notices[$last]->created))
220             )
221             . '"';
222         }
223         
224         return null;
225     }
226
227     /**
228      * Does this require authentication?
229      *
230      * @return boolean true if delete, else false
231      */
232
233     function requiresAuth()
234     {
235         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
236             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
237             return false;
238         } else {
239             return true;
240         }
241     }
242 }