]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapidirect_messages.php
39780258ad179c55bb602681acb0f2f787c3ab30
[quix0rs-gnu-social.git] / actions / twitapidirect_messages.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
23
24 class Twitapidirect_messagesAction extends TwitterapiAction {
25
26
27         function is_readonly() {
28
29                 static $write_methods = array(  'direct_messages',
30                                                                                 'sent');
31
32                 $cmdtext = explode('.', $this->arg('method'));
33
34                 if (in_array($cmdtext[0], $write_methods)) {
35                         return false;
36                 }
37
38                 return true;
39         }
40
41         function direct_messages($args, $apidata) {
42                 parent::handle($args);
43
44                 $user = $apidata['user'];
45
46                 $count = $this->arg('count');
47                 $since = $this->arg('since');
48                 $since_id = $this->arg('since_id');
49                 $page = $this->arg('page');
50
51                 if (!$page) {
52                         $page = 1;
53                 }
54
55                 if (!$count) {
56                         $count = 20;
57                 }
58
59                 $message = new Message();
60                 $message->to_profile = $user->id;
61                 $message->orderBy('created DESC, id DESC');
62                 $message->limit((($page-1)*20), $count);
63
64                 $message->find();
65
66                 switch($apidata['content-type']) {
67                  case 'xml':
68                         $this->show_xml_direct_messages($message);
69                         break;
70                  case 'rss':
71                         //$this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
72                         break;
73                  case 'atom':
74                         //$this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
75                         break;
76                  case 'json':
77                         $this->show_json_direct_messages($message);
78                         break;
79                  default:
80                         common_user_error(_('API method not found!'), $code = 404);
81                 }
82
83                 exit();
84         }
85
86         function sent($args, $apidata) {
87                 parent::handle($args);
88                 common_server_error(_('API method under construction.'), $code=501);
89                 exit();
90         }
91
92         # had to change this from "new" to "create" to avoid PHP reserved word
93         function create($args, $apidata) {
94                 parent::handle($args);
95                 common_server_error(_('API method under construction.'), $code=501);
96                 exit();
97         }
98
99         function destroy($args, $apidata) {
100                 parent::handle($args);
101                 common_server_error(_('API method under construction.'), $code=501);
102                 exit();
103         }
104
105         function show_xml_direct_messages($message) {
106
107                 $this->init_document('xml');
108                 common_element_start('direct-messages', array('type' => 'array'));
109
110                 if (is_array($messages)) {
111                         foreach ($message as $m) {
112                                 $twitter_dm = $this->twitter_dm_array($m);
113                                 $this->show_twitter_xml_dm($twitter_dm);
114                         }
115                 } else {
116                         while ($message->fetch()) {
117                                 $twitter_dm = $this->twitter_dm_array($message);
118                                 $this->show_twitter_xml_dm($twitter_dm);
119                         }
120                 }
121
122                 common_element_end('direct-messages');
123                 $this->end_document('xml');
124         }
125
126         function show_json_direct_messages($message) {
127
128                 $this->init_document('json');
129
130                 $dmsgs = array();
131
132                 if (is_array($message)) {
133                         foreach ($message as $m) {
134                                 $twitter_dm = $this->twitter_dm_array($m);
135                                 array_push($dmsgs, $twitter_dm);
136                         }
137                 } else {
138                         while ($message->fetch()) {
139                                 $twitter_dm = $this->twitter_dm_array($message);
140                                 array_push($dmsgs, $twitter_dm);
141                         }
142                 }
143
144                 $this->show_twitter_json_dmsgs($dmsgs);
145
146                 $this->end_document('json');
147         }
148
149
150
151 }