]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
notice_inbox.id -> notice_inbox.notice_id
[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, $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                 # FriendFeed's SUP protocol
195                 # Also added RSS and Atom feeds
196
197                 $suplink = common_local_url('sup', NULL, $user->id);
198                 header('X-SUP-ID: '.$suplink);
199
200                 # XXX: since
201
202                 $notice = $user->getNotices((($page-1)*20), $count, $since_id, $before_id);
203
204                 switch($apidata['content-type']) {
205                  case 'xml':
206                         $this->show_xml_timeline($notice);
207                         break;
208                  case 'rss':
209                         $this->show_rss_timeline($notice, $title, $link, $subtitle, $suplink);
210                         break;
211                  case 'atom':
212                         $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, $suplink);
213                         break;
214                  case 'json':
215                         $this->show_json_timeline($notice);
216                         break;
217                  default:
218                         common_user_error(_('API method not found!'), $code = 404);
219                 }
220
221         }
222
223         function update($args, $apidata) {
224
225                 parent::handle($args);
226
227                 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
228                         common_user_error(_('API method not found!'), $code = 404);
229                         return;
230                 }
231
232                 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
233                         $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
234                         return;
235                 }
236
237                 $this->auth_user = $apidata['user'];
238                 $user = $this->auth_user;
239                 $status = $this->trimmed('status');
240                 $source = $this->trimmed('source');
241                 $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
242
243                 if (!$source) {
244                         $source = 'api';
245                 }
246
247                 if (!$status) {
248
249                         // XXX: Note: In this case, Twitter simply returns '200 OK'
250                         // No error is given, but the status is not posted to the
251                         // user's timeline.  Seems bad.  Shouldn't we throw an
252                         // errror? -- Zach
253                         return;
254
255                 } else if (mb_strlen($status) > 140) {
256
257                         // XXX: Twitter truncates anything over 140, flags the status
258                     // as "truncated."  Sending this error may screw up some clients
259                     // that assume Twitter will truncate for them.  Should we just
260                     // truncate too? -- Zach
261                         $this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
262                         return;
263                 }
264
265                 // Check for commands
266                 $inter = new CommandInterpreter();
267                 $cmd = $inter->handle_command($user, $status);
268
269                 if ($cmd) {
270
271                         if ($this->supported($cmd)) {
272                                 $cmd->execute(new Channel());
273                         }
274
275                         // cmd not supported?  Twitter just returns your latest status.
276                         // And, it returns your last status whether the cmd was successful
277                         // or not!
278                         $n = $user->getCurrentNotice();
279                         $apidata['api_arg'] = $n->id;
280                 } else {
281
282                         $reply_to = NULL;
283
284                         if ($in_reply_to_status_id) {
285
286                                 // check whether notice actually exists
287                                 $reply = Notice::staticGet($in_reply_to_status_id);
288
289                                 if ($reply) {
290                                         $reply_to = $in_reply_to_status_id;
291                                 } else {
292                                         $this->client_error(_('Not found'), $code = 404, $apidata['content-type']);
293                                         return;
294                                 }
295                         }
296
297                         $notice = Notice::saveNew($user->id, html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
298                                 $source, 1, $reply_to);
299
300                         if (is_string($notice)) {
301                                 $this->server_error($notice);
302                                 return;
303                         }
304
305                         common_broadcast_notice($notice);
306                         $apidata['api_arg'] = $notice->id;
307                 }
308
309                 $this->show($args, $apidata);
310         }
311
312         function replies($args, $apidata) {
313
314                 parent::handle($args);
315
316                 $since = $this->arg('since');
317                 $count = $this->arg('count');
318                 $page = $this->arg('page');
319         $since_id = $this->arg('since_id');
320         $before_id = $this->arg('before_id');
321
322                 $this->auth_user = $apidata['user'];
323                 $user = $this->auth_user;
324                 $profile = $user->getProfile();
325
326                 $sitename = common_config('site', 'name');
327                 $siteserver = common_config('site', 'server');
328
329                 $title = sprintf(_('%1$s / Updates replying to %2$s'), $sitename, $user->nickname);
330                 $id = "tag:$siteserver:replies:".$user->id;
331                 $link = common_local_url('replies', array('nickname' => $user->nickname));
332                 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'), $sitename, $user->nickname, $profile->getBestName());
333
334                 if (!$page) {
335                         $page = 1;
336                 }
337
338                 if (!$count) {
339                         $count = 20;
340                 }
341
342         if (!$since_id) {
343             $since_id = 0;
344         }
345
346                 // NOTE: before_id is an extension to Twitter API -- TB
347         if (!$before_id) {
348             $before_id = 0;
349         }
350                 $notice = $user->getReplies((($page-1)*20), $count, $since_id, $before_id);
351                 $notices = array();
352
353                 while ($notice->fetch()) {
354                         $notices[] = clone($notice);
355                 }
356
357                 switch($apidata['content-type']) {
358                  case 'xml':
359                         $this->show_xml_timeline($notices);
360                         break;
361                  case 'rss':
362                         $this->show_rss_timeline($notices, $title, $link, $subtitle);
363                         break;
364                  case 'atom':
365                         $this->show_atom_timeline($notices, $title, $id, $link, $subtitle);
366                         break;
367                  case 'json':
368                         $this->show_json_timeline($notices);
369                         break;
370                  default:
371                         common_user_error(_('API method not found!'), $code = 404);
372                 }
373
374         }
375
376         function show($args, $apidata) {
377                 parent::handle($args);
378
379                 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
380                         common_user_error(_('API method not found!'), $code = 404);
381                         return;
382                 }
383
384                 $this->auth_user = $apidata['user'];
385                 $notice_id = $apidata['api_arg'];
386                 $notice = Notice::staticGet($notice_id);
387
388                 if ($notice) {
389                         if ($apidata['content-type'] == 'xml') {
390                                 $this->show_single_xml_status($notice);
391                         } elseif ($apidata['content-type'] == 'json') {
392                                 $this->show_single_json_status($notice);
393                         }
394                 } else {
395                         // XXX: Twitter just sets a 404 header and doens't bother to return an err msg
396                         $this->client_error(_('No status with that ID found.'), 404, $apidata['content-type']);
397                 }
398
399         }
400
401         function destroy($args, $apidata) {
402
403                 parent::handle($args);
404
405                 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
406                         common_user_error(_('API method not found!'), $code = 404);
407                         return;
408                 }
409
410                 // Check for RESTfulness
411                 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
412                         // XXX: Twitter just prints the err msg, no XML / JSON.
413                         $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
414                         return;
415                 }
416
417                 $this->auth_user = $apidata['user'];
418                 $user = $this->auth_user;
419                 $notice_id = $apidata['api_arg'];
420                 $notice = Notice::staticGet($notice_id);
421
422                 if (!$notice) {
423                         $this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
424                         return;
425                 }
426
427                 if ($user->id == $notice->profile_id) {
428                         $replies = new Reply;
429                         $replies->get('notice_id', $notice_id);
430                         common_dequeue_notice($notice);
431                         $replies->delete();
432                         $notice->delete();
433
434                         if ($apidata['content-type'] == 'xml') {
435                                 $this->show_single_xml_status($notice);
436                         } elseif ($apidata['content-type'] == 'json') {
437                                 $this->show_single_json_status($notice);
438                         }
439                 } else {
440                         $this->client_error(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
441                 }
442
443         }
444
445         function friends($args, $apidata) {
446                 parent::handle($args);
447                 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
448         }
449
450         function followers($args, $apidata) {
451                 parent::handle($args);
452
453                 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
454         }
455
456         function subscriptions($apidata, $other_attr, $user_attr) {
457
458                 # XXX: lite
459
460                 $this->auth_user = $apidate['user'];
461                 $user = $this->get_user($apidata['api_arg'], $apidata);
462
463                 if (!$user) {
464                         $this->client_error('Not Found', 404, $apidata['content-type']);
465                         return;
466                 }
467
468                 $page = $this->trimmed('page');
469
470                 if (!$page || !is_numeric($page)) {
471                         $page = 1;
472                 }
473
474                 $profile = $user->getProfile();
475
476                 if (!$profile) {
477                         common_server_error(_('User has no profile.'));
478                         return;
479                 }
480
481                 $sub = new Subscription();
482                 $sub->$user_attr = $profile->id;
483                 $sub->orderBy('created DESC');
484                 $sub->limit(($page-1)*100, 100);
485
486                 $others = array();
487
488                 if ($sub->find()) {
489                         while ($sub->fetch()) {
490                                 $others[] = Profile::staticGet($sub->$other_attr);
491                         }
492                 } else {
493                         // user has no followers
494                 }
495
496                 $type = $apidata['content-type'];
497
498                 $this->init_document($type);
499                 $this->show_profiles($others, $type);
500                 $this->end_document($type);
501         }
502
503         function show_profiles($profiles, $type) {
504                 switch ($type) {
505                  case 'xml':
506                         common_element_start('users', array('type' => 'array'));
507                         foreach ($profiles as $profile) {
508                                 $this->show_profile($profile);
509                         }
510                         common_element_end('users');
511                         break;
512                  case 'json':
513                         $arrays = array();
514                         foreach ($profiles as $profile) {
515                                 $arrays[] = $this->twitter_user_array($profile, true);
516                         }
517                         print json_encode($arrays);
518                         break;
519                  default:
520                         $this->client_error(_('unsupported file type'));
521                 }
522         }
523
524         function featured($args, $apidata) {
525                 parent::handle($args);
526                 common_server_error(_('API method under construction.'), $code=501);
527         }
528
529         function supported($cmd) {
530
531                 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand', 'OnCommand', 'OffCommand');
532
533                 if (in_array(get_class($cmd), $cmdlist)) {
534                         return true;
535                 }
536
537                 return false;
538         }
539
540 }