Twitter-compatible API: Direct messages work (everything but destroy())
darcs-hash:20080929050826-7b5ce-201624b32e174ae85b906b92353e39aa043838fa.gz
This commit is contained in:
parent
10f4ee7abb
commit
924491f08a
|
@ -23,7 +23,6 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
|
||||||
|
|
||||||
class Twitapidirect_messagesAction extends TwitterapiAction {
|
class Twitapidirect_messagesAction extends TwitterapiAction {
|
||||||
|
|
||||||
|
|
||||||
function is_readonly() {
|
function is_readonly() {
|
||||||
|
|
||||||
static $write_methods = array( 'direct_messages',
|
static $write_methods = array( 'direct_messages',
|
||||||
|
@ -40,6 +39,15 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
|
||||||
|
|
||||||
function direct_messages($args, $apidata) {
|
function direct_messages($args, $apidata) {
|
||||||
parent::handle($args);
|
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'];
|
$user = $apidata['user'];
|
||||||
|
|
||||||
|
@ -57,18 +65,28 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = new Message();
|
$message = new Message();
|
||||||
|
|
||||||
|
$title = null;
|
||||||
|
$subtitle = null;
|
||||||
|
$link = null;
|
||||||
|
$server = common_root_url();
|
||||||
|
|
||||||
|
if ($type == 'received') {
|
||||||
$message->to_profile = $user->id;
|
$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';
|
||||||
|
}
|
||||||
|
|
||||||
$message->orderBy('created DESC, id DESC');
|
$message->orderBy('created DESC, id DESC');
|
||||||
$message->limit((($page-1)*20), $count);
|
$message->limit((($page-1)*20), $count);
|
||||||
|
|
||||||
$message->find();
|
$message->find();
|
||||||
|
|
||||||
$title = 'Direct messages to ' . $user->nickname;
|
|
||||||
$subtitle = 'All the direct messages sent to ' . $user->nickname;
|
|
||||||
|
|
||||||
$server = common_root_url();
|
|
||||||
$link = $server . $user->nickname . '/inbox';
|
|
||||||
|
|
||||||
switch($apidata['content-type']) {
|
switch($apidata['content-type']) {
|
||||||
case 'xml':
|
case 'xml':
|
||||||
$this->show_xml_dmsgs($message);
|
$this->show_xml_dmsgs($message);
|
||||||
|
@ -89,16 +107,65 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
function sent($args, $apidata) {
|
// had to change this from "new" to "create" to avoid PHP reserved word
|
||||||
|
function create($args, $apidata) {
|
||||||
parent::handle($args);
|
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();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
# had to change this from "new" to "create" to avoid PHP reserved word
|
$user = $apidata['user'];
|
||||||
function create($args, $apidata) {
|
$source = $this->trimmed('source'); // Not supported by Twitter.
|
||||||
parent::handle($args);
|
|
||||||
common_server_error(_('API method under construction.'), $code=501);
|
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();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,18 +182,19 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
|
||||||
|
|
||||||
if (is_array($messages)) {
|
if (is_array($messages)) {
|
||||||
foreach ($message as $m) {
|
foreach ($message as $m) {
|
||||||
$twitter_dm = $this->twitter_dm_array($m);
|
$twitter_dm = $this->twitter_dmsg_array($m);
|
||||||
$this->show_twitter_xml_dm($twitter_dm);
|
$this->show_twitter_xml_dmsg($twitter_dm);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while ($message->fetch()) {
|
while ($message->fetch()) {
|
||||||
$twitter_dm = $this->twitter_dm_array($message);
|
$twitter_dm = $this->twitter_dmsg_array($message);
|
||||||
$this->show_twitter_xml_dm($twitter_dm);
|
$this->show_twitter_xml_dmsg($twitter_dm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
common_element_end('direct-messages');
|
common_element_end('direct-messages');
|
||||||
$this->end_document('xml');
|
$this->end_document('xml');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function show_json_dmsgs($message) {
|
function show_json_dmsgs($message) {
|
||||||
|
@ -137,19 +205,19 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
|
||||||
|
|
||||||
if (is_array($message)) {
|
if (is_array($message)) {
|
||||||
foreach ($message as $m) {
|
foreach ($message as $m) {
|
||||||
$twitter_dm = $this->twitter_dm_array($m);
|
$twitter_dm = $this->twitter_dmsg_array($m);
|
||||||
array_push($dmsgs, $twitter_dm);
|
array_push($dmsgs, $twitter_dm);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while ($message->fetch()) {
|
while ($message->fetch()) {
|
||||||
$twitter_dm = $this->twitter_dm_array($message);
|
$twitter_dm = $this->twitter_dmsg_array($message);
|
||||||
array_push($dmsgs, $twitter_dm);
|
array_push($dmsgs, $twitter_dm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->show_twitter_json_dmsgs($dmsgs);
|
$this->show_twitter_json_dmsgs($dmsgs);
|
||||||
|
|
||||||
$this->end_document('json');
|
$this->end_document('json');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function show_rss_dmsgs($message, $title, $link, $subtitle) {
|
function show_rss_dmsgs($message, $title, $link, $subtitle) {
|
||||||
|
@ -178,6 +246,7 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
|
||||||
|
|
||||||
common_element_end('channel');
|
common_element_end('channel');
|
||||||
$this->end_twitter_rss();
|
$this->end_twitter_rss();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function show_atom_dmsgs($message, $title, $link, $subtitle) {
|
function show_atom_dmsgs($message, $title, $link, $subtitle) {
|
||||||
|
@ -204,7 +273,12 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->end_document('atom');
|
$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
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -152,7 +152,6 @@ class TwitapifriendshipsAction extends TwitterapiAction {
|
||||||
function exists($args, $apidata) {
|
function exists($args, $apidata) {
|
||||||
parent::handle($args);
|
parent::handle($args);
|
||||||
|
|
||||||
|
|
||||||
$user_a_id = $this->trimmed('user_a');
|
$user_a_id = $this->trimmed('user_a');
|
||||||
$user_b_id = $this->trimmed('user_b');
|
$user_b_id = $this->trimmed('user_b');
|
||||||
|
|
||||||
|
@ -189,24 +188,4 @@ class TwitapifriendshipsAction extends TwitterapiAction {
|
||||||
exit();
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -121,7 +121,7 @@ class TwitterapiAction extends Action {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function twitter_dm_array($message) {
|
function twitter_dmsg_array($message) {
|
||||||
|
|
||||||
$twitter_dm = array();
|
$twitter_dm = array();
|
||||||
|
|
||||||
|
@ -214,7 +214,21 @@ class TwitterapiAction extends Action {
|
||||||
exit();
|
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');
|
common_element_start('direct_message');
|
||||||
foreach($twitter_dm as $element => $value) {
|
foreach($twitter_dm as $element => $value) {
|
||||||
if ($element == 'sender' || $element == 'recipient') {
|
if ($element == 'sender' || $element == 'recipient') {
|
||||||
|
@ -405,4 +419,26 @@ class TwitterapiAction extends Action {
|
||||||
}
|
}
|
||||||
return;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user