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