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 is_readonly() {
28 static $write_methods = array( 'direct_messages',
31 $cmdtext = explode('.', $this->arg('method'));
33 if (in_array($cmdtext[0], $write_methods)) {
40 function direct_messages($args, $apidata) {
41 parent::handle($args);
42 return $this->show_messages($args, $apidata, 'received');
45 function sent($args, $apidata) {
46 parent::handle($args);
47 return $this->show_messages($args, $apidata, 'sent');
50 function show_messages($args, $apidata, $type) {
52 $user = $apidata['user'];
54 $count = $this->arg('count');
55 $since = $this->arg('since');
56 $since_id = $this->arg('since_id');
57 $page = $this->arg('page');
67 $message = new Message();
72 $server = common_root_url();
74 if ($type == 'received') {
75 $message->to_profile = $user->id;
76 $title = sprintf(_("Direct messages to %s"), $user->nickname);
77 $subtitle = sprintf(_("All the direct messages sent to %s"), $user->nickname);
78 $link = $server . $user->nickname . '/inbox';
80 $message->from_profile = $user->id;
81 $title = _('Direct Messages You\'ve Sent');
82 $subtitle = sprintf(_("All the direct messages sent from %s"), $user->nickname);
83 $link = $server . $user->nickname . '/outbox';
86 $message->orderBy('created DESC, id DESC');
87 $message->limit((($page-1)*20), $count);
90 switch($apidata['content-type']) {
92 $this->show_xml_dmsgs($message);
95 $this->show_rss_dmsgs($message, $title, $link, $subtitle);
98 $this->show_atom_dmsgs($message, $title, $link, $subtitle);
101 $this->show_json_dmsgs($message);
104 common_user_error(_('API method not found!'), $code = 404);
110 // had to change this from "new" to "create" to avoid PHP reserved word
111 function create($args, $apidata) {
112 parent::handle($args);
114 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
115 $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
119 $user = $apidata['user'];
120 $source = $this->trimmed('source'); // Not supported by Twitter.
126 $content = $this->trimmed('text');
129 $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']);
130 } else if (mb_strlen($status) > 140) {
131 $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
132 $code = 406, $apidata['content-type']);
136 $other = $this->get_user($this->trimmed('user'));
139 $this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']);
141 } else if (!$user->mutuallySubscribed($other)) {
142 $this->client_error(_('Can\'t send direct messages to users who aren\'t your friend.'),
143 $code = 403, $apidata['content-type']);
145 } else if ($user->id == $other->id) {
146 // Sending msgs to yourself is allowed by Twitter
147 $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'),
148 $code = 403, $apidata['content-type']);
152 $message = Message::saveNew($user->id, $other->id, $content, $source);
154 if (is_string($message)) {
155 $this->server_error($message);
159 $this->notify($user, $other, $message);
161 if ($apidata['content-type'] == 'xml') {
162 $this->show_single_xml_dmsg($message);
163 } elseif ($apidata['content-type'] == 'json') {
164 $this->show_single_json_dmsg($message);
170 function destroy($args, $apidata) {
171 parent::handle($args);
172 common_server_error(_('API method under construction.'), $code=501);
176 function show_xml_dmsgs($message) {
178 $this->init_document('xml');
179 common_element_start('direct-messages', array('type' => 'array'));
181 if (is_array($messages)) {
182 foreach ($message as $m) {
183 $twitter_dm = $this->twitter_dmsg_array($m);
184 $this->show_twitter_xml_dmsg($twitter_dm);
187 while ($message->fetch()) {
188 $twitter_dm = $this->twitter_dmsg_array($message);
189 $this->show_twitter_xml_dmsg($twitter_dm);
193 common_element_end('direct-messages');
194 $this->end_document('xml');
198 function show_json_dmsgs($message) {
200 $this->init_document('json');
204 if (is_array($message)) {
205 foreach ($message as $m) {
206 $twitter_dm = $this->twitter_dmsg_array($m);
207 array_push($dmsgs, $twitter_dm);
210 while ($message->fetch()) {
211 $twitter_dm = $this->twitter_dmsg_array($message);
212 array_push($dmsgs, $twitter_dm);
216 $this->show_json_objects($dmsgs);
217 $this->end_document('json');
221 function show_rss_dmsgs($message, $title, $link, $subtitle) {
223 $this->init_document('rss');
225 common_element_start('channel');
226 common_element('title', NULL, $title);
228 common_element('link', NULL, $link);
229 common_element('description', NULL, $subtitle);
230 common_element('language', NULL, 'en-us');
231 common_element('ttl', NULL, '40');
233 if (is_array($message)) {
234 foreach ($message as $m) {
235 $entry = $this->twitter_rss_dmsg_array($m);
236 $this->show_twitter_rss_item($entry);
239 while ($message->fetch()) {
240 $entry = $this->twitter_rss_dmsg_array($message);
241 $this->show_twitter_rss_item($entry);
245 common_element_end('channel');
246 $this->end_twitter_rss();
250 function show_atom_dmsgs($message, $title, $link, $subtitle) {
252 $this->init_document('atom');
254 common_element('title', NULL, $title);
255 $siteserver = common_config('site', 'server');
256 common_element('id', NULL, "tag:$siteserver,2008:DirectMessage");
257 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), NULL);
258 common_element('updated', NULL, common_date_iso8601(strftime('%c')));
259 common_element('subtitle', NULL, $subtitle);
261 if (is_array($message)) {
262 foreach ($message as $m) {
263 $entry = $this->twitter_rss_dmsg_array($m);
264 $this->show_twitter_atom_entry($entry);
267 while ($message->fetch()) {
268 $entry = $this->twitter_rss_dmsg_array($message);
269 $this->show_twitter_atom_entry($entry);
273 $this->end_document('atom');
276 // swiped from MessageAction. Should it be place in util.php?
277 function notify($from, $to, $message) {
278 mail_notify_message($message, $from, $to);
279 # XXX: Jabber, SMS notifications... probably queued