3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
24 class Twitapidirect_messagesAction extends TwitterapiAction {
26 function direct_messages($args, $apidata) {
27 parent::handle($args);
28 return $this->show_messages($args, $apidata, 'received');
31 function sent($args, $apidata) {
32 parent::handle($args);
33 return $this->show_messages($args, $apidata, 'sent');
36 function show_messages($args, $apidata, $type) {
38 $user = $apidata['user'];
40 $count = $this->arg('count');
41 $since = $this->arg('since');
42 $since_id = $this->arg('since_id');
43 $page = $this->arg('page');
53 $message = new Message();
58 $server = common_root_url();
60 if ($type == 'received') {
61 $message->to_profile = $user->id;
62 $title = sprintf(_("Direct messages to %s"), $user->nickname);
63 $subtitle = sprintf(_("All the direct messages sent to %s"), $user->nickname);
64 $link = $server . $user->nickname . '/inbox';
66 $message->from_profile = $user->id;
67 $title = _('Direct Messages You\'ve Sent');
68 $subtitle = sprintf(_("All the direct messages sent from %s"), $user->nickname);
69 $link = $server . $user->nickname . '/outbox';
72 $message->orderBy('created DESC, id DESC');
73 $message->limit((($page-1)*20), $count);
76 switch($apidata['content-type']) {
78 $this->show_xml_dmsgs($message);
81 $this->show_rss_dmsgs($message, $title, $link, $subtitle);
84 $this->show_atom_dmsgs($message, $title, $link, $subtitle);
87 $this->show_json_dmsgs($message);
90 common_user_error(_('API method not found!'), $code = 404);
95 // had to change this from "new" to "create" to avoid PHP reserved word
96 function create($args, $apidata) {
97 parent::handle($args);
99 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
100 $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
104 $user = $apidata['user'];
105 $source = $this->trimmed('source'); // Not supported by Twitter.
111 $content = $this->trimmed('text');
114 $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']);
115 } else if (mb_strlen($status) > 140) {
116 $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
117 $code = 406, $apidata['content-type']);
121 $other = $this->get_user($this->trimmed('user'));
124 $this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']);
126 } else if (!$user->mutuallySubscribed($other)) {
127 $this->client_error(_('Can\'t send direct messages to users who aren\'t your friend.'),
128 $code = 403, $apidata['content-type']);
130 } else if ($user->id == $other->id) {
131 // Sending msgs to yourself is allowed by Twitter
132 $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'),
133 $code = 403, $apidata['content-type']);
137 $message = Message::saveNew($user->id, $other->id,
138 html_entity_decode($content, ENT_NOQUOTES, 'UTF-8'), $source);
140 if (is_string($message)) {
141 $this->server_error($message);
145 $this->notify($user, $other, $message);
147 if ($apidata['content-type'] == 'xml') {
148 $this->show_single_xml_dmsg($message);
149 } elseif ($apidata['content-type'] == 'json') {
150 $this->show_single_json_dmsg($message);
155 function destroy($args, $apidata) {
156 parent::handle($args);
157 common_server_error(_('API method under construction.'), $code=501);
160 function show_xml_dmsgs($message) {
162 $this->init_document('xml');
163 common_element_start('direct-messages', array('type' => 'array'));
165 if (is_array($messages)) {
166 foreach ($message as $m) {
167 $twitter_dm = $this->twitter_dmsg_array($m);
168 $this->show_twitter_xml_dmsg($twitter_dm);
171 while ($message->fetch()) {
172 $twitter_dm = $this->twitter_dmsg_array($message);
173 $this->show_twitter_xml_dmsg($twitter_dm);
177 common_element_end('direct-messages');
178 $this->end_document('xml');
182 function show_json_dmsgs($message) {
184 $this->init_document('json');
188 if (is_array($message)) {
189 foreach ($message as $m) {
190 $twitter_dm = $this->twitter_dmsg_array($m);
191 array_push($dmsgs, $twitter_dm);
194 while ($message->fetch()) {
195 $twitter_dm = $this->twitter_dmsg_array($message);
196 array_push($dmsgs, $twitter_dm);
200 $this->show_json_objects($dmsgs);
201 $this->end_document('json');
205 function show_rss_dmsgs($message, $title, $link, $subtitle) {
207 $this->init_document('rss');
209 common_element_start('channel');
210 common_element('title', NULL, $title);
212 common_element('link', NULL, $link);
213 common_element('description', NULL, $subtitle);
214 common_element('language', NULL, 'en-us');
215 common_element('ttl', NULL, '40');
217 if (is_array($message)) {
218 foreach ($message as $m) {
219 $entry = $this->twitter_rss_dmsg_array($m);
220 $this->show_twitter_rss_item($entry);
223 while ($message->fetch()) {
224 $entry = $this->twitter_rss_dmsg_array($message);
225 $this->show_twitter_rss_item($entry);
229 common_element_end('channel');
230 $this->end_twitter_rss();
234 function show_atom_dmsgs($message, $title, $link, $subtitle) {
236 $this->init_document('atom');
238 common_element('title', NULL, $title);
239 $siteserver = common_config('site', 'server');
240 common_element('id', NULL, "tag:$siteserver,2008:DirectMessage");
241 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), NULL);
242 common_element('updated', NULL, common_date_iso8601(strftime('%c')));
243 common_element('subtitle', NULL, $subtitle);
245 if (is_array($message)) {
246 foreach ($message as $m) {
247 $entry = $this->twitter_rss_dmsg_array($m);
248 $this->show_twitter_atom_entry($entry);
251 while ($message->fetch()) {
252 $entry = $this->twitter_rss_dmsg_array($message);
253 $this->show_twitter_atom_entry($entry);
257 $this->end_document('atom');
260 // swiped from MessageAction. Should it be place in util.php?
261 function notify($from, $to, $message) {
262 mail_notify_message($message, $from, $to);
263 # XXX: Jabber, SMS notifications... probably queued