apidirectmessage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show a the direct messages from or to a user
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category API
  23. * @package StatusNet
  24. * @author Adrian Lang <mail@adrianlang.de>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Robin Millette <robin@millette.info>
  27. * @author Zach Copley <zach@status.net>
  28. * @copyright 2009 StatusNet, Inc.
  29. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  30. * @link http://status.net/
  31. */
  32. if (!defined('STATUSNET')) {
  33. exit(1);
  34. }
  35. /**
  36. * Show a list of direct messages from or to the authenticating user
  37. *
  38. * @category API
  39. * @package StatusNet
  40. * @author Adrian Lang <mail@adrianlang.de>
  41. * @author Evan Prodromou <evan@status.net>
  42. * @author Robin Millette <robin@millette.info>
  43. * @author Zach Copley <zach@status.net>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://status.net/
  46. */
  47. class ApiDirectMessageAction extends ApiAuthAction
  48. {
  49. var $messages = null;
  50. var $title = null;
  51. var $subtitle = null;
  52. var $link = null;
  53. var $selfuri_base = null;
  54. var $id = null;
  55. /**
  56. * Take arguments for running
  57. *
  58. * @param array $args $_REQUEST args
  59. *
  60. * @return boolean success flag
  61. */
  62. function prepare($args)
  63. {
  64. parent::prepare($args);
  65. $this->user = $this->auth_user;
  66. if (empty($this->user)) {
  67. // TRANS: Client error given when a user was not found (404).
  68. $this->clientError(_('No such user.'), 404);
  69. }
  70. $server = common_root_url();
  71. $taguribase = TagURI::base();
  72. if ($this->arg('sent')) {
  73. // Action was called by /api/direct_messages/sent.format
  74. $this->title = sprintf(
  75. // TRANS: Title. %s is a user nickname.
  76. _("Direct messages from %s"),
  77. $this->user->nickname
  78. );
  79. $this->subtitle = sprintf(
  80. // TRANS: Subtitle. %s is a user nickname.
  81. _("All the direct messages sent from %s"),
  82. $this->user->nickname
  83. );
  84. $this->link = $server . $this->user->nickname . '/outbox';
  85. $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
  86. $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
  87. } else {
  88. $this->title = sprintf(
  89. // TRANS: Title. %s is a user nickname.
  90. _("Direct messages to %s"),
  91. $this->user->nickname
  92. );
  93. $this->subtitle = sprintf(
  94. // TRANS: Subtitle. %s is a user nickname.
  95. _("All the direct messages sent to %s"),
  96. $this->user->nickname
  97. );
  98. $this->link = $server . $this->user->nickname . '/inbox';
  99. $this->selfuri_base = common_root_url() . 'api/direct_messages';
  100. $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
  101. }
  102. $this->messages = $this->getMessages();
  103. return true;
  104. }
  105. /**
  106. * Handle the request
  107. *
  108. * Show the messages
  109. *
  110. * @param array $args $_REQUEST data (unused)
  111. *
  112. * @return void
  113. */
  114. function handle($args)
  115. {
  116. parent::handle($args);
  117. $this->showMessages();
  118. }
  119. /**
  120. * Show the messages
  121. *
  122. * @return void
  123. */
  124. function showMessages()
  125. {
  126. switch($this->format) {
  127. case 'xml':
  128. $this->showXmlDirectMessages();
  129. break;
  130. case 'rss':
  131. $this->showRssDirectMessages();
  132. break;
  133. case 'atom':
  134. $this->showAtomDirectMessages();
  135. break;
  136. case 'json':
  137. $this->showJsonDirectMessages();
  138. break;
  139. default:
  140. // TRANS: Client error displayed when coming across a non-supported API method.
  141. $this->clientError(_('API method not found.'), $code = 404);
  142. break;
  143. }
  144. }
  145. /**
  146. * Get notices
  147. *
  148. * @return array notices
  149. */
  150. function getMessages()
  151. {
  152. $message = new Message();
  153. if ($this->arg('sent')) {
  154. $message->from_profile = $this->user->id;
  155. } else {
  156. $message->to_profile = $this->user->id;
  157. }
  158. if (!empty($this->max_id)) {
  159. $message->whereAdd('id <= ' . $this->max_id);
  160. }
  161. if (!empty($this->since_id)) {
  162. $message->whereAdd('id > ' . $this->since_id);
  163. }
  164. $message->orderBy('created DESC, id DESC');
  165. $message->limit((($this->page - 1) * $this->count), $this->count);
  166. $message->find();
  167. $messages = array();
  168. while ($message->fetch()) {
  169. $messages[] = clone($message);
  170. }
  171. return $messages;
  172. }
  173. /**
  174. * Is this action read only?
  175. *
  176. * @param array $args other arguments
  177. *
  178. * @return boolean true
  179. */
  180. function isReadOnly($args)
  181. {
  182. return true;
  183. }
  184. /**
  185. * When was this notice last modified?
  186. *
  187. * @return string datestamp of the latest notice in the stream
  188. */
  189. function lastModified()
  190. {
  191. if (!empty($this->messages)) {
  192. return strtotime($this->messages[0]->created);
  193. }
  194. return null;
  195. }
  196. // BEGIN import from lib/apiaction.php
  197. function showSingleXmlDirectMessage($message)
  198. {
  199. $this->initDocument('xml');
  200. $dmsg = $this->directMessageArray($message);
  201. $this->showXmlDirectMessage($dmsg, true);
  202. $this->endDocument('xml');
  203. }
  204. function showSingleJsonDirectMessage($message)
  205. {
  206. $this->initDocument('json');
  207. $dmsg = $this->directMessageArray($message);
  208. $this->showJsonObjects($dmsg);
  209. $this->endDocument('json');
  210. }
  211. function showXmlDirectMessage($dm, $namespaces=false)
  212. {
  213. $attrs = array();
  214. if ($namespaces) {
  215. $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
  216. }
  217. $this->elementStart('direct_message', $attrs);
  218. foreach($dm as $element => $value) {
  219. switch ($element) {
  220. case 'sender':
  221. case 'recipient':
  222. $this->showTwitterXmlUser($value, $element);
  223. break;
  224. case 'text':
  225. $this->element($element, null, common_xml_safe_str($value));
  226. break;
  227. default:
  228. $this->element($element, null, $value);
  229. break;
  230. }
  231. }
  232. $this->elementEnd('direct_message');
  233. }
  234. function directMessageArray($message)
  235. {
  236. $dmsg = array();
  237. $from_profile = $message->getFrom();
  238. $to_profile = $message->getTo();
  239. $dmsg['id'] = intval($message->id);
  240. $dmsg['sender_id'] = intval($from_profile->id);
  241. $dmsg['text'] = trim($message->content);
  242. $dmsg['recipient_id'] = intval($to_profile->id);
  243. $dmsg['created_at'] = $this->dateTwitter($message->created);
  244. $dmsg['sender_screen_name'] = $from_profile->nickname;
  245. $dmsg['recipient_screen_name'] = $to_profile->nickname;
  246. $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
  247. $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
  248. return $dmsg;
  249. }
  250. function rssDirectMessageArray($message)
  251. {
  252. $entry = array();
  253. $from = $message->getFrom();
  254. $entry['title'] = sprintf('Message from %1$s to %2$s',
  255. $from->nickname, $message->getTo()->nickname);
  256. $entry['content'] = common_xml_safe_str($message->rendered);
  257. $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
  258. $entry['published'] = common_date_iso8601($message->created);
  259. $taguribase = TagURI::base();
  260. $entry['id'] = "tag:$taguribase:$entry[link]";
  261. $entry['updated'] = $entry['published'];
  262. $entry['author-name'] = $from->getBestName();
  263. $entry['author-uri'] = $from->homepage;
  264. $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE);
  265. try {
  266. $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
  267. $entry['avatar-type'] = $avatar->mediatype;
  268. } catch (Exception $e) {
  269. $entry['avatar-type'] = 'image/png';
  270. }
  271. // RSS item specific
  272. $entry['description'] = $entry['content'];
  273. $entry['pubDate'] = common_date_rfc2822($message->created);
  274. $entry['guid'] = $entry['link'];
  275. return $entry;
  276. }
  277. // END import from lib/apiaction.php
  278. /**
  279. * Shows a list of direct messages as Twitter-style XML array
  280. *
  281. * @return void
  282. */
  283. function showXmlDirectMessages()
  284. {
  285. $this->initDocument('xml');
  286. $this->elementStart('direct-messages', array('type' => 'array',
  287. 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
  288. foreach ($this->messages as $m) {
  289. $dm_array = $this->directMessageArray($m);
  290. $this->showXmlDirectMessage($dm_array);
  291. }
  292. $this->elementEnd('direct-messages');
  293. $this->endDocument('xml');
  294. }
  295. /**
  296. * Shows a list of direct messages as a JSON encoded array
  297. *
  298. * @return void
  299. */
  300. function showJsonDirectMessages()
  301. {
  302. $this->initDocument('json');
  303. $dmsgs = array();
  304. foreach ($this->messages as $m) {
  305. $dm_array = $this->directMessageArray($m);
  306. array_push($dmsgs, $dm_array);
  307. }
  308. $this->showJsonObjects($dmsgs);
  309. $this->endDocument('json');
  310. }
  311. /**
  312. * Shows a list of direct messages as RSS items
  313. *
  314. * @return void
  315. */
  316. function showRssDirectMessages()
  317. {
  318. $this->initDocument('rss');
  319. $this->element('title', null, $this->title);
  320. $this->element('link', null, $this->link);
  321. $this->element('description', null, $this->subtitle);
  322. $this->element('language', null, 'en-us');
  323. $this->element(
  324. 'atom:link',
  325. array(
  326. 'type' => 'application/rss+xml',
  327. 'href' => $this->selfuri_base . '.rss',
  328. 'rel' => self
  329. ),
  330. null
  331. );
  332. $this->element('ttl', null, '40');
  333. foreach ($this->messages as $m) {
  334. $entry = $this->rssDirectMessageArray($m);
  335. $this->showTwitterRssItem($entry);
  336. }
  337. $this->endTwitterRss();
  338. }
  339. /**
  340. * Shows a list of direct messages as Atom entries
  341. *
  342. * @return void
  343. */
  344. function showAtomDirectMessages()
  345. {
  346. $this->initDocument('atom');
  347. $this->element('title', null, $this->title);
  348. $this->element('id', null, $this->id);
  349. $selfuri = common_root_url() . 'api/direct_messages.atom';
  350. $this->element(
  351. 'link', array(
  352. 'href' => $this->link,
  353. 'rel' => 'alternate',
  354. 'type' => 'text/html'),
  355. null
  356. );
  357. $this->element(
  358. 'link', array(
  359. 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
  360. 'type' => 'application/atom+xml'),
  361. null
  362. );
  363. $this->element('updated', null, common_date_iso8601('now'));
  364. $this->element('subtitle', null, $this->subtitle);
  365. foreach ($this->messages as $m) {
  366. $entry = $this->rssDirectMessageArray($m);
  367. $this->showTwitterAtomEntry($entry);
  368. }
  369. $this->endDocument('atom');
  370. }
  371. /**
  372. * An entity tag for this notice
  373. *
  374. * Returns an Etag based on the action name, language, and
  375. * timestamps of the notice
  376. *
  377. * @return string etag
  378. */
  379. function etag()
  380. {
  381. if (!empty($this->messages)) {
  382. $last = count($this->messages) - 1;
  383. return '"' . implode(
  384. ':',
  385. array($this->arg('action'),
  386. common_user_cache_hash($this->auth_user),
  387. common_language(),
  388. strtotime($this->messages[0]->created),
  389. strtotime($this->messages[$last]->created)
  390. )
  391. )
  392. . '"';
  393. }
  394. return null;
  395. }
  396. }