]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/twitapidirect_messages.php
add inboxed and regenerate data objects
[quix0rs-gnu-social.git] / actions / twitapidirect_messages.php
index 8eee6a753ba639117d49d4e6ea8bcb12de1d2b57..ed2d5c5d1cfb3d01081cd1d664cbeb70402479c0 100644 (file)
@@ -23,23 +23,17 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
 
 class Twitapidirect_messagesAction extends TwitterapiAction {
 
-
-       function is_readonly() {
-
-               static $write_methods = array(  'direct_messages',
-                                                                               'sent');
-
-               $cmdtext = explode('.', $this->arg('method'));
-
-               if (in_array($cmdtext[0], $write_methods)) {
-                       return false;
-               }
-
-               return true;
+       function direct_messages($args, $apidata) {
+               parent::handle($args);
+               return $this->show_messages($args, $apidata, 'received');
        }
 
-       function direct_messages($args, $apidata) {
+       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 +51,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':
@@ -86,26 +90,75 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
                        common_user_error(_('API method not found!'), $code = 404);
                }
 
-               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);
-               exit();
+
+               if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+                       $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
+                       return;
+               }
+
+               $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) {
+               } else {
+                       $status = common_shorten_links($status);
+                       if (mb_strlen($status) > 140) {
+                               $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
+                                       $code = 406, $apidata['content-type']);
+                               return;
+                       }
+               }
+
+               $other = $this->get_user($this->trimmed('user'));
+
+               if (!$other) {
+                       $this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']);
+                       return;
+               } 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']);
+                       return;
+               } 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']);
+                       return;
+               }
+
+               $message = Message::saveNew($user->id, $other->id,
+                       html_entity_decode($content, ENT_NOQUOTES, 'UTF-8'), $source);
+
+               if (is_string($message)) {
+                       $this->server_error($message);
+                       return;
+               }
+
+               $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);
+               }
+
        }
 
        function destroy($args, $apidata) {
                parent::handle($args);
                common_server_error(_('API method under construction.'), $code=501);
-               exit();
        }
 
        function show_xml_dmsgs($message) {
@@ -115,18 +168,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 +191,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->show_json_objects($dmsgs);
                $this->end_document('json');
+
        }
 
        function show_rss_dmsgs($message, $title, $link, $subtitle) {
@@ -178,6 +232,7 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
 
                common_element_end('channel');
                $this->end_twitter_rss();
+
        }
 
        function show_atom_dmsgs($message, $title, $link, $subtitle) {
@@ -204,7 +259,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
+}