PageRenderTime 89ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/DirectMessage/actions/apidirectmessage.php

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