]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
try to make replies point to the clicked-on notice
[quix0rs-gnu-social.git] / actions / twitapistatuses.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
23
24 class TwitapistatusesAction extends TwitterapiAction {
25
26         function is_readonly() {
27
28                 static $write_methods = array(  'update',
29                                                                                 'destroy');
30
31                 $cmdtext = explode('.', $this->arg('method'));
32
33                 if (in_array($cmdtext[0], $write_methods)) {
34                         return false;
35                 }
36
37                 return true;
38         }
39
40         function public_timeline($args, $apidata) {
41                 parent::handle($args);
42
43                 $sitename = common_config('site', 'name');
44                 $siteserver = common_config('site', 'server');
45                 $title = sprintf(_("%s public timeline"), $sitename);
46                 $id = "tag:$siteserver:Statuses";
47                 $link = common_root_url();
48                 $subtitle = sprintf(_("%s updates from everyone!"), $sitename);
49
50                 // Number of public statuses to return by default -- Twitter sends 20
51                 $MAX_PUBSTATUSES = 20;
52
53                 // FIXME: To really live up to the spec we need to build a list
54                 // of notices by users who have custom avatars, so fix this SQL -- Zach
55
56         $page = $this->arg('page');
57         $since_id = $this->arg('since_id');
58         $before_id = $this->arg('before_id');
59
60                 // NOTE: page, since_id, and before_id are extensions to Twitter API -- TB
61         if (!$page) {
62             $page = 1;
63         }
64         if (!$since_id) {
65             $since_id = 0;
66         }
67         if (!$before_id) {
68             $before_id = 0;
69         }
70
71                 $notice = Notice::publicStream((($page-1)*$MAX_PUBSTATUSES), $MAX_PUBSTATUSES, $since_id, $before_id);
72
73                 if ($notice) {
74
75                         switch($apidata['content-type']) {
76                                 case 'xml':
77                                         $this->show_xml_timeline($notice);
78                                         break;
79                                 case 'rss':
80                                         $this->show_rss_timeline($notice, $title, $link, $subtitle);
81                                         break;
82                                 case 'atom':
83                                         $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
84                                         break;
85                                 case 'json':
86                                         $this->show_json_timeline($notice);
87                                         break;
88                                 default:
89                                         common_user_error(_('API method not found!'), $code = 404);
90                                         break;
91                         }
92
93                 } else {
94                         common_server_error(_('Couldn\'t find any statuses.'), $code = 503);
95                 }
96
97         }
98
99         function friends_timeline($args, $apidata) {
100                 parent::handle($args);
101
102                 $since = $this->arg('since');
103                 $since_id = $this->arg('since_id');
104                 $count = $this->arg('count');
105         $page = $this->arg('page');
106         $before_id = $this->arg('before_id');
107
108         if (!$page) {
109             $page = 1;
110         }
111
112                 if (!$count) {
113                         $count = 20;
114                 }
115
116         if (!$since_id) {
117             $since_id = 0;
118         }
119
120                 // NOTE: before_id is an extension to Twitter API -- TB
121         if (!$before_id) {
122             $before_id = 0;
123         }
124
125                 $user = $this->get_user($id, $apidata);
126                 $this->auth_user = $user;
127
128                 $profile = $user->getProfile();
129
130                 $sitename = common_config('site', 'name');
131                 $siteserver = common_config('site', 'server');
132
133                 $title = sprintf(_("%s and friends"), $user->nickname);
134                 $id = "tag:$siteserver:friends:" . $user->id;
135                 $link = common_local_url('all', array('nickname' => $user->nickname));
136                 $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $user->nickname, $sitename);
137
138                 $notice = $user->noticesWithFriends(($page-1)*20, $count, $since_id, $before_id);
139
140                 switch($apidata['content-type']) {
141                  case 'xml':
142                         $this->show_xml_timeline($notice);
143                         break;
144                  case 'rss':
145                         $this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
146                         break;
147                  case 'atom':
148                         $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
149                         break;
150                  case 'json':
151                         $this->show_json_timeline($notice);
152                         break;
153                  default:
154                         common_user_error(_('API method not found!'), $code = 404);
155                 }
156
157         }
158
159         function user_timeline($args, $apidata) {
160                 parent::handle($args);
161
162                 $this->auth_user = $apidata['user'];
163                 $user = $this->get_user($apidata['api_arg'], $apidata);
164
165                 if (!$user) {
166                         $this->client_error('Not Found', 404, $apidata['content-type']);
167                         return;
168                 }
169
170                 $profile = $user->getProfile();
171
172                 if (!$profile) {
173                         common_server_error(_('User has no profile.'));
174                         return;
175                 }
176
177                 $count = $this->arg('count');
178                 $since = $this->arg('since');
179         $since_id = $this->arg('since_id');
180                 $page = $this->arg('page');
181         $before_id = $this->arg('before_id');
182
183                 if (!$page) {
184                         $page = 1;
185                 }
186
187                 if (!$count) {
188                         $count = 20;
189                 }
190
191         if (!$since_id) {
192             $since_id = 0;
193         }
194
195                 // NOTE: before_id is an extensions to Twitter API -- TB
196         if (!$before_id) {
197             $before_id = 0;
198         }
199
200                 $sitename = common_config('site', 'name');
201                 $siteserver = common_config('site', 'server');
202
203                 $title = sprintf(_("%s timeline"), $user->nickname);
204                 $id = "tag:$siteserver:user:".$user->id;
205                 $link = common_local_url('showstream', array('nickname' => $user->nickname));
206                 $subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename);
207
208                 # XXX: since
209
210                 $notice = $user->getNotices((($page-1)*20), $count, $since_id, $before_id);
211
212                 switch($apidata['content-type']) {
213                  case 'xml':
214                         $this->show_xml_timeline($notice);
215                         break;
216                  case 'rss':
217                         $this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
218                         break;
219                  case 'atom':
220                         $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
221                         break;
222                  case 'json':
223                         $this->show_json_timeline($notice);
224                         break;
225                  default:
226                         common_user_error(_('API method not found!'), $code = 404);
227                 }
228
229         }
230
231         function update($args, $apidata) {
232
233                 parent::handle($args);
234
235                 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
236                         common_user_error(_('API method not found!'), $code = 404);
237                         return;
238                 }
239
240                 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
241                         $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
242                         return;
243                 }
244
245                 $this->auth_user = $apidata['user'];
246                 $user = $this->auth_user;
247                 $status = $this->trimmed('status');
248                 $source = $this->trimmed('source');
249                 $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
250
251                 if (!$source) {
252                         $source = 'api';
253                 }
254
255                 if (!$status) {
256
257                         // XXX: Note: In this case, Twitter simply returns '200 OK'
258                         // No error is given, but the status is not posted to the
259                         // user's timeline.  Seems bad.  Shouldn't we throw an
260                         // errror? -- Zach
261                         return;
262
263                 } else if (mb_strlen($status) > 140) {
264
265                         // XXX: Twitter truncates anything over 140, flags the status
266                     // as "truncated."  Sending this error may screw up some clients
267                     // that assume Twitter will truncate for them.  Should we just
268                     // truncate too? -- Zach
269                         $this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
270                         return;
271                 }
272
273                 // Check for commands
274                 $inter = new CommandInterpreter();
275                 $cmd = $inter->handle_command($user, $status);
276
277                 if ($cmd) {
278
279                         if ($this->supported($cmd)) {
280                                 $cmd->execute(new Channel());
281                         }
282
283                         // cmd not supported?  Twitter just returns your latest status.
284                         // And, it returns your last status whether the cmd was successful
285                         // or not!
286                         $n = $user->getCurrentNotice();
287                         $apidata['api_arg'] = $n->id;
288                 } else {
289
290                         $reply_to = NULL;
291
292                         if ($in_reply_to_status_id) {
293
294                                 // check whether notice actually exists
295                                 $reply = Notice::staticGet($in_reply_to_status_id);
296
297                                 if ($reply) {
298                                         $reply_to = $in_reply_to_status_id;
299                                 } else {
300                                         $this->client_error(_('Not found'), $code = 404, $apidata['content-type']);
301                                         return;
302                                 }
303                         }
304
305                         $notice = Notice::saveNew($user->id, $status, $source, 1, $reply_to);
306
307                         if (is_string($notice)) {
308                                 $this->server_error($notice);
309                                 return;
310                         }
311
312                         common_broadcast_notice($notice);
313                         $apidata['api_arg'] = $notice->id;
314                 }
315
316                 $this->show($args, $apidata);
317         }
318
319         function replies($args, $apidata) {
320
321                 parent::handle($args);
322
323                 $since = $this->arg('since');
324                 $count = $this->arg('count');
325                 $page = $this->arg('page');
326         $since_id = $this->arg('since_id');
327         $before_id = $this->arg('before_id');
328
329                 $this->auth_user = $apidata['user'];
330                 $user = $this->auth_user;
331                 $profile = $user->getProfile();
332
333                 $sitename = common_config('site', 'name');
334                 $siteserver = common_config('site', 'server');
335
336                 $title = sprintf(_('%1$s / Updates replying to %2$s'), $sitename, $user->nickname);
337                 $id = "tag:$siteserver:replies:".$user->id;
338                 $link = common_local_url('replies', array('nickname' => $user->nickname));
339                 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'), $sitename, $user->nickname, $profile->getBestName());
340
341                 if (!$page) {
342                         $page = 1;
343                 }
344
345                 if (!$count) {
346                         $count = 20;
347                 }
348
349         if (!$since_id) {
350             $since_id = 0;
351         }
352
353                 // NOTE: before_id is an extension to Twitter API -- TB
354         if (!$before_id) {
355             $before_id = 0;
356         }
357                 $notice = $user->getReplies((($page-1)*20), $count, $since_id, $before_id);
358                 $notices = array();
359
360                 while ($notice->fetch()) {
361                         $notices[] = clone($notice);
362                 }
363
364                 switch($apidata['content-type']) {
365                  case 'xml':
366                         $this->show_xml_timeline($notices);
367                         break;
368                  case 'rss':
369                         $this->show_rss_timeline($notices, $title, $id, $link, $subtitle);
370                         break;
371                  case 'atom':
372                         $this->show_atom_timeline($notices, $title, $id, $link, $subtitle);
373                         break;
374                  case 'json':
375                         $this->show_json_timeline($notices);
376                         break;
377                  default:
378                         common_user_error(_('API method not found!'), $code = 404);
379                 }
380
381         }
382
383         function show($args, $apidata) {
384                 parent::handle($args);
385
386                 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
387                         common_user_error(_('API method not found!'), $code = 404);
388                         return;
389                 }
390
391                 $this->auth_user = $apidata['user'];
392                 $notice_id = $apidata['api_arg'];
393                 $notice = Notice::staticGet($notice_id);
394
395                 if ($notice) {
396                         if ($apidata['content-type'] == 'xml') {
397                                 $this->show_single_xml_status($notice);
398                         } elseif ($apidata['content-type'] == 'json') {
399                                 $this->show_single_json_status($notice);
400                         }
401                 } else {
402                         // XXX: Twitter just sets a 404 header and doens't bother to return an err msg
403                         $this->client_error(_('No status with that ID found.'), 404, $apidata['content-type']);
404                 }
405
406         }
407
408         function destroy($args, $apidata) {
409
410                 parent::handle($args);
411
412                 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
413                         common_user_error(_('API method not found!'), $code = 404);
414                         return;
415                 }
416
417                 // Check for RESTfulness
418                 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
419                         // XXX: Twitter just prints the err msg, no XML / JSON.
420                         $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
421                         return;
422                 }
423
424                 $this->auth_user = $apidata['user'];
425                 $user = $this->auth_user;
426                 $notice_id = $apidata['api_arg'];
427                 $notice = Notice::staticGet($notice_id);
428
429                 if (!$notice) {
430                         $this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
431                         return;
432                 }
433
434                 if ($user->id == $notice->profile_id) {
435                         $replies = new Reply;
436                         $replies->get('notice_id', $notice_id);
437                         common_dequeue_notice($notice);
438                         $replies->delete();
439                         $notice->delete();
440
441                         if ($apidata['content-type'] == 'xml') {
442                                 $this->show_single_xml_status($notice);
443                         } elseif ($apidata['content-type'] == 'json') {
444                                 $this->show_single_json_status($notice);
445                         }
446                 } else {
447                         $this->client_error(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
448                 }
449
450         }
451
452         function friends($args, $apidata) {
453                 parent::handle($args);
454                 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
455         }
456
457         function followers($args, $apidata) {
458                 parent::handle($args);
459
460                 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
461         }
462
463         function subscriptions($apidata, $other_attr, $user_attr) {
464
465                 # XXX: lite
466
467                 $this->auth_user = $apidate['user'];
468                 $user = $this->get_user($apidata['api_arg'], $apidata);
469
470                 if (!$user) {
471                         $this->client_error('Not Found', 404, $apidata['content-type']);
472                         return;
473                 }
474
475                 $page = $this->trimmed('page');
476
477                 if (!$page || !is_numeric($page)) {
478                         $page = 1;
479                 }
480
481                 $profile = $user->getProfile();
482
483                 if (!$profile) {
484                         common_server_error(_('User has no profile.'));
485                         return;
486                 }
487
488                 $sub = new Subscription();
489                 $sub->$user_attr = $profile->id;
490                 $sub->orderBy('created DESC');
491                 $sub->limit(($page-1)*100, 100);
492
493                 $others = array();
494
495                 if ($sub->find()) {
496                         while ($sub->fetch()) {
497                                 $others[] = Profile::staticGet($sub->$other_attr);
498                         }
499                 } else {
500                         // user has no followers
501                 }
502
503                 $type = $apidata['content-type'];
504
505                 $this->init_document($type);
506                 $this->show_profiles($others, $type);
507                 $this->end_document($type);
508         }
509
510         function show_profiles($profiles, $type) {
511                 switch ($type) {
512                  case 'xml':
513                         common_element_start('users', array('type' => 'array'));
514                         foreach ($profiles as $profile) {
515                                 $this->show_profile($profile);
516                         }
517                         common_element_end('users');
518                         break;
519                  case 'json':
520                         $arrays = array();
521                         foreach ($profiles as $profile) {
522                                 $arrays[] = $this->twitter_user_array($profile, true);
523                         }
524                         print json_encode($arrays);
525                         break;
526                  default:
527                         $this->client_error(_('unsupported file type'));
528                 }
529         }
530
531         function featured($args, $apidata) {
532                 parent::handle($args);
533                 common_server_error(_('API method under construction.'), $code=501);
534         }
535
536         function supported($cmd) {
537
538                 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand', 'OnCommand', 'OffCommand');
539
540                 if (in_array(get_class($cmd), $cmdlist)) {
541                         return true;
542                 }
543
544                 return false;
545         }
546
547 }