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