From 924491f08a24201c7bda45b4d4be16ccebba97c0 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Mon, 29 Sep 2008 01:08:26 -0400 Subject: [PATCH] Twitter-compatible API: Direct messages work (everything but destroy()) darcs-hash:20080929050826-7b5ce-201624b32e174ae85b906b92353e39aa043838fa.gz --- actions/twitapidirect_messages.php | 122 +++++++++++++++++++++++------ actions/twitapifriendships.php | 95 +++++++++------------- lib/twitterapi.php | 40 +++++++++- 3 files changed, 173 insertions(+), 84 deletions(-) diff --git a/actions/twitapidirect_messages.php b/actions/twitapidirect_messages.php index 8eee6a753b..8f0ecb4498 100644 --- a/actions/twitapidirect_messages.php +++ b/actions/twitapidirect_messages.php @@ -23,7 +23,6 @@ require_once(INSTALLDIR.'/lib/twitterapi.php'); class Twitapidirect_messagesAction extends TwitterapiAction { - function is_readonly() { static $write_methods = array( 'direct_messages', @@ -40,6 +39,15 @@ class Twitapidirect_messagesAction extends TwitterapiAction { function direct_messages($args, $apidata) { parent::handle($args); + return $this->show_messages($args, $apidata, 'received'); + } + + function sent($args, $apidata) { + parent::handle($args); + return $this->show_messages($args, $apidata, 'sent'); + } + + function show_messages($args, $apidata, $type) { $user = $apidata['user']; @@ -57,17 +65,27 @@ class Twitapidirect_messagesAction extends TwitterapiAction { } $message = new Message(); - $message->to_profile = $user->id; - $message->orderBy('created DESC, id DESC'); - $message->limit((($page-1)*20), $count); - $message->find(); + $title = null; + $subtitle = null; + $link = null; + $server = common_root_url(); - $title = 'Direct messages to ' . $user->nickname; - $subtitle = 'All the direct messages sent to ' . $user->nickname; + if ($type == 'received') { + $message->to_profile = $user->id; + $title = sprintf(_("Direct messages to %s"), $user->nickname); + $subtitle = sprintf(_("All the direct messages sent to %s"), $user->nickname); + $link = $server . $user->nickname . '/inbox'; + } else { + $message->from_profile = $user->id; + $title = _('Direct Messages You\'ve Sent'); + $subtitle = sprintf(_("All the direct messages sent from %s"), $user->nickname); + $link = $server . $user->nickname . '/outbox'; + } - $server = common_root_url(); - $link = $server . $user->nickname . '/inbox'; + $message->orderBy('created DESC, id DESC'); + $message->limit((($page-1)*20), $count); + $message->find(); switch($apidata['content-type']) { case 'xml': @@ -89,16 +107,65 @@ class Twitapidirect_messagesAction extends TwitterapiAction { exit(); } - function sent($args, $apidata) { - parent::handle($args); - common_server_error(_('API method under construction.'), $code=501); - exit(); - } - - # had to change this from "new" to "create" to avoid PHP reserved word + // had to change this from "new" to "create" to avoid PHP reserved word function create($args, $apidata) { parent::handle($args); - common_server_error(_('API method under construction.'), $code=501); + + if ($_SERVER['REQUEST_METHOD'] != 'POST') { + $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']); + exit(); + } + + $user = $apidata['user']; + $source = $this->trimmed('source'); // Not supported by Twitter. + + if (!$source) { + $source = 'api'; + } + + $content = $this->trimmed('text'); + + if (!$content) { + $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']); + } else if (mb_strlen($status) > 140) { + $this->client_error(_('That\'s too long. Max message size is 140 chars.'), + $code = 406, $apidata['content-type']); + exit(); + } + + common_debug($this->trimmed('user')); + + $other = $this->get_user($this->trimmed('user')); + + if (!$other) { + $this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']); + exit(); + } else if (!$user->mutuallySubscribed($other)) { + $this->client_error(_('Can\'t send direct messages to users who aren\'t your friend.'), + $code = 403, $apidata['content-type']); + exit(); + } else if ($user->id == $other->id) { + // Sending msgs to yourself is allowed by Twitter + $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'), + $code = 403, $apidata['content-type']); + exit(); + } + + $message = Message::saveNew($user->id, $other->id, $content, $source); + + if (is_string($message)) { + $this->server_error($message); + exit(); + } + + $this->notify($user, $other, $message); + + if ($apidata['content-type'] == 'xml') { + $this->show_single_xml_dmsg($message); + } elseif ($apidata['content-type'] == 'json') { + $this->show_single_json_dmsg($message); + } + exit(); } @@ -115,18 +182,19 @@ class Twitapidirect_messagesAction extends TwitterapiAction { if (is_array($messages)) { foreach ($message as $m) { - $twitter_dm = $this->twitter_dm_array($m); - $this->show_twitter_xml_dm($twitter_dm); + $twitter_dm = $this->twitter_dmsg_array($m); + $this->show_twitter_xml_dmsg($twitter_dm); } } else { while ($message->fetch()) { - $twitter_dm = $this->twitter_dm_array($message); - $this->show_twitter_xml_dm($twitter_dm); + $twitter_dm = $this->twitter_dmsg_array($message); + $this->show_twitter_xml_dmsg($twitter_dm); } } common_element_end('direct-messages'); $this->end_document('xml'); + } function show_json_dmsgs($message) { @@ -137,19 +205,19 @@ class Twitapidirect_messagesAction extends TwitterapiAction { if (is_array($message)) { foreach ($message as $m) { - $twitter_dm = $this->twitter_dm_array($m); + $twitter_dm = $this->twitter_dmsg_array($m); array_push($dmsgs, $twitter_dm); } } else { while ($message->fetch()) { - $twitter_dm = $this->twitter_dm_array($message); + $twitter_dm = $this->twitter_dmsg_array($message); array_push($dmsgs, $twitter_dm); } } $this->show_twitter_json_dmsgs($dmsgs); - $this->end_document('json'); + } function show_rss_dmsgs($message, $title, $link, $subtitle) { @@ -178,6 +246,7 @@ class Twitapidirect_messagesAction extends TwitterapiAction { common_element_end('channel'); $this->end_twitter_rss(); + } function show_atom_dmsgs($message, $title, $link, $subtitle) { @@ -204,7 +273,12 @@ class Twitapidirect_messagesAction extends TwitterapiAction { } $this->end_document('atom'); + } + // swiped from MessageAction. Should it be place in util.php? + function notify($from, $to, $message) { + mail_notify_message($message, $from, $to); + # XXX: Jabber, SMS notifications... probably queued } } \ No newline at end of file diff --git a/actions/twitapifriendships.php b/actions/twitapifriendships.php index 3cc925c369..d97b7c08ea 100644 --- a/actions/twitapifriendships.php +++ b/actions/twitapifriendships.php @@ -24,16 +24,16 @@ require_once(INSTALLDIR.'/lib/twitterapi.php'); class TwitapifriendshipsAction extends TwitterapiAction { function is_readonly() { - + static $write_methods = array( 'create', 'destroy'); - - $cmdtext = explode('.', $this->arg('method')); - - if (in_array($cmdtext[0], $write_methods)) { + + $cmdtext = explode('.', $this->arg('method')); + + if (in_array($cmdtext[0], $write_methods)) { return false; } - + return true; } @@ -53,33 +53,33 @@ class TwitapifriendshipsAction extends TwitterapiAction { $this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']); exit(); } - + $user = $apidata['user']; - + if ($user->isSubscribed($other)) { $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname); $this->client_error($errmsg, 403, $apidata['content-type']); exit(); } - + $sub = new Subscription(); - + $sub->query('BEGIN'); - + $sub->subscriber = $user->id; $sub->subscribed = $other->id; $sub->created = DB_DataObject_Cast::dateTime(); # current time - + $result = $sub->insert(); if (!$result) { $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname); - $this->client_error($errmsg, 400, $apidata['content-type']); + $this->client_error($errmsg, 400, $apidata['content-type']); exit(); } - + $sub->query('COMMIT'); - + mail_subscribe_notify($other, $user); $type = $apidata['content-type']; @@ -88,10 +88,10 @@ class TwitapifriendshipsAction extends TwitterapiAction { $this->end_document($type); exit(); } - + //destroy // - //Discontinues friendship with the user specified in the ID parameter as the authenticating user. Returns the un-friended user in the requested format when successful. Returns a string describing the failure condition when unsuccessful. + //Discontinues friendship with the user specified in the ID parameter as the authenticating user. Returns the un-friended user in the requested format when successful. Returns a string describing the failure condition when unsuccessful. // //URL: http://twitter.com/friendships/destroy/id.format // @@ -100,76 +100,75 @@ class TwitapifriendshipsAction extends TwitterapiAction { //Parameters: // //* id. Required. The ID or screen name of the user with whom to discontinue friendship. Ex: http://twitter.com/friendships/destroy/12345.json or http://twitter.com/friendships/destroy/bob.xml - + function destroy($args, $apidata) { parent::handle($args); - + if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) { $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']); exit(); } - + $id = $apidata['api_arg']; # We can't subscribe to a remote person, but we can unsub - + $other = $this->get_profile($id); $user = $apidata['user']; - + $sub = new Subscription(); $sub->subscriber = $user->id; $sub->subscribed = $other->id; - + if ($sub->find(TRUE)) { $sub->query('BEGIN'); $sub->delete(); $sub->query('COMMIT'); } else { - $this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']); + $this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']); exit(); } $type = $apidata['content-type']; - $this->init_document($type); + $this->init_document($type); $this->show_profile($other, $type); $this->end_document($type); exit(); } // Tests if a friendship exists between two users. - // - // + // + // // URL: http://twitter.com/friendships/exists.format - // + // // Formats: xml, json, none - // + // // Parameters: - // + // // * user_a. Required. The ID or screen_name of the first user to test friendship for. // * user_b. Required. The ID or screen_name of the second user to test friendship for. // * Ex: http://twitter.com/friendships/exists.xml?user_a=alice&user_b=bob - + function exists($args, $apidata) { parent::handle($args); - - + $user_a_id = $this->trimmed('user_a'); $user_b_id = $this->trimmed('user_b'); - + $user_a = $this->get_user($user_a_id); $user_b = $this->get_user($user_b_id); - + if (!$user_a || !$user_b) { $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']); exit(); } - + if ($user_a->isSubscribed($user_b)) { $result = 'true'; } else { $result = 'false'; } - + switch ($apidata['content-type']) { case 'xml': $this->init_document('xml'); @@ -185,28 +184,8 @@ class TwitapifriendshipsAction extends TwitterapiAction { print $result; // Really? --Zach break; } - + exit(); } - function get_profile($id) { - if (is_numeric($id)) { - return Profile::staticGet($id); - } else { - $user = User::staticGet('nickname', $id); - if ($user) { - return $user->getProfile(); - } else { - return NULL; - } - } - } - - function get_user($id) { - if (is_numeric($id)) { - return User::staticGet($id); - } else { - return User::staticGet('nickname', $id); - } - } } \ No newline at end of file diff --git a/lib/twitterapi.php b/lib/twitterapi.php index d427a7b4e8..5bcb538f9f 100644 --- a/lib/twitterapi.php +++ b/lib/twitterapi.php @@ -121,7 +121,7 @@ class TwitterapiAction extends Action { } - function twitter_dm_array($message) { + function twitter_dmsg_array($message) { $twitter_dm = array(); @@ -214,7 +214,21 @@ class TwitterapiAction extends Action { exit(); } - function show_twitter_xml_dm($twitter_dm) { + function show_single_xml_dmsg($message) { + $this->init_document('xml'); + $dmsg = $this->twitter_dmsg_array($message); + $this->show_twitter_xml_dmsg($dmsg); + $this->end_document('xml'); + } + + function show_single_json_dmsg($message) { + $this->init_document('json'); + $dmsg = $this->twitter_dmsg_array($message); + $this->show_twitter_json_dm($dmsg); + $this->end_document('json'); + } + + function show_twitter_xml_dmsg($twitter_dm) { common_element_start('direct_message'); foreach($twitter_dm as $element => $value) { if ($element == 'sender' || $element == 'recipient') { @@ -405,4 +419,26 @@ class TwitterapiAction extends Action { } return; } + + function get_user($id) { + if (is_numeric($id)) { + return User::staticGet($id); + } else { + return User::staticGet('nickname', $id); + } + } + + function get_profile($id) { + if (is_numeric($id)) { + return Profile::staticGet($id); + } else { + $user = User::staticGet('nickname', $id); + if ($user) { + return $user->getProfile(); + } else { + return NULL; + } + } + } + } \ No newline at end of file -- 2.39.5