123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class ApiDirectMessageAction extends ApiAuthAction
- {
- var $messages = null;
- var $title = null;
- var $subtitle = null;
- var $link = null;
- var $selfuri_base = null;
- var $id = null;
-
- function prepare($args)
- {
- parent::prepare($args);
- $this->user = $this->auth_user;
- if (empty($this->user)) {
-
- $this->clientError(_('No such user.'), 404);
- }
- $server = common_root_url();
- $taguribase = TagURI::base();
- if ($this->arg('sent')) {
-
- $this->title = sprintf(
-
- _("Direct messages from %s"),
- $this->user->nickname
- );
- $this->subtitle = sprintf(
-
- _("All the direct messages sent from %s"),
- $this->user->nickname
- );
- $this->link = $server . $this->user->nickname . '/outbox';
- $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
- $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
- } else {
- $this->title = sprintf(
-
- _("Direct messages to %s"),
- $this->user->nickname
- );
- $this->subtitle = sprintf(
-
- _("All the direct messages sent to %s"),
- $this->user->nickname
- );
- $this->link = $server . $this->user->nickname . '/inbox';
- $this->selfuri_base = common_root_url() . 'api/direct_messages';
- $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
- }
- $this->messages = $this->getMessages();
- return true;
- }
-
- function handle($args)
- {
- parent::handle($args);
- $this->showMessages();
- }
-
- function showMessages()
- {
- switch($this->format) {
- case 'xml':
- $this->showXmlDirectMessages();
- break;
- case 'rss':
- $this->showRssDirectMessages();
- break;
- case 'atom':
- $this->showAtomDirectMessages();
- break;
- case 'json':
- $this->showJsonDirectMessages();
- break;
- default:
-
- $this->clientError(_('API method not found.'), $code = 404);
- break;
- }
- }
-
- function getMessages()
- {
- $message = new Message();
- if ($this->arg('sent')) {
- $message->from_profile = $this->user->id;
- } else {
- $message->to_profile = $this->user->id;
- }
- if (!empty($this->max_id)) {
- $message->whereAdd('id <= ' . $this->max_id);
- }
- if (!empty($this->since_id)) {
- $message->whereAdd('id > ' . $this->since_id);
- }
- $message->orderBy('created DESC, id DESC');
- $message->limit((($this->page - 1) * $this->count), $this->count);
- $message->find();
- $messages = array();
- while ($message->fetch()) {
- $messages[] = clone($message);
- }
- return $messages;
- }
-
- function isReadOnly($args)
- {
- return true;
- }
-
- function lastModified()
- {
- if (!empty($this->messages)) {
- return strtotime($this->messages[0]->created);
- }
- return null;
- }
-
- function showSingleXmlDirectMessage($message)
- {
- $this->initDocument('xml');
- $dmsg = $this->directMessageArray($message);
- $this->showXmlDirectMessage($dmsg, true);
- $this->endDocument('xml');
- }
- function showSingleJsonDirectMessage($message)
- {
- $this->initDocument('json');
- $dmsg = $this->directMessageArray($message);
- $this->showJsonObjects($dmsg);
- $this->endDocument('json');
- }
- function showXmlDirectMessage($dm, $namespaces=false)
- {
- $attrs = array();
- if ($namespaces) {
- $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
- }
- $this->elementStart('direct_message', $attrs);
- foreach($dm as $element => $value) {
- switch ($element) {
- case 'sender':
- case 'recipient':
- $this->showTwitterXmlUser($value, $element);
- break;
- case 'text':
- $this->element($element, null, common_xml_safe_str($value));
- break;
- default:
- $this->element($element, null, $value);
- break;
- }
- }
- $this->elementEnd('direct_message');
- }
- function directMessageArray($message)
- {
- $dmsg = array();
- $from_profile = $message->getFrom();
- $to_profile = $message->getTo();
- $dmsg['id'] = intval($message->id);
- $dmsg['sender_id'] = intval($from_profile->id);
- $dmsg['text'] = trim($message->content);
- $dmsg['recipient_id'] = intval($to_profile->id);
- $dmsg['created_at'] = $this->dateTwitter($message->created);
- $dmsg['sender_screen_name'] = $from_profile->nickname;
- $dmsg['recipient_screen_name'] = $to_profile->nickname;
- $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
- $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
- return $dmsg;
- }
- function rssDirectMessageArray($message)
- {
- $entry = array();
- $from = $message->getFrom();
- $entry['title'] = sprintf('Message from %1$s to %2$s',
- $from->nickname, $message->getTo()->nickname);
- $entry['content'] = common_xml_safe_str($message->rendered);
- $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
- $entry['published'] = common_date_iso8601($message->created);
- $taguribase = TagURI::base();
- $entry['id'] = "tag:$taguribase:$entry[link]";
- $entry['updated'] = $entry['published'];
- $entry['author-name'] = $from->getBestName();
- $entry['author-uri'] = $from->homepage;
- $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE);
- try {
- $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
- $entry['avatar-type'] = $avatar->mediatype;
- } catch (Exception $e) {
- $entry['avatar-type'] = 'image/png';
- }
-
- $entry['description'] = $entry['content'];
- $entry['pubDate'] = common_date_rfc2822($message->created);
- $entry['guid'] = $entry['link'];
- return $entry;
- }
-
-
- function showXmlDirectMessages()
- {
- $this->initDocument('xml');
- $this->elementStart('direct-messages', array('type' => 'array',
- 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
- foreach ($this->messages as $m) {
- $dm_array = $this->directMessageArray($m);
- $this->showXmlDirectMessage($dm_array);
- }
- $this->elementEnd('direct-messages');
- $this->endDocument('xml');
- }
-
- function showJsonDirectMessages()
- {
- $this->initDocument('json');
- $dmsgs = array();
- foreach ($this->messages as $m) {
- $dm_array = $this->directMessageArray($m);
- array_push($dmsgs, $dm_array);
- }
- $this->showJsonObjects($dmsgs);
- $this->endDocument('json');
- }
-
- function showRssDirectMessages()
- {
- $this->initDocument('rss');
- $this->element('title', null, $this->title);
- $this->element('link', null, $this->link);
- $this->element('description', null, $this->subtitle);
- $this->element('language', null, 'en-us');
- $this->element(
- 'atom:link',
- array(
- 'type' => 'application/rss+xml',
- 'href' => $this->selfuri_base . '.rss',
- 'rel' => self
- ),
- null
- );
- $this->element('ttl', null, '40');
- foreach ($this->messages as $m) {
- $entry = $this->rssDirectMessageArray($m);
- $this->showTwitterRssItem($entry);
- }
- $this->endTwitterRss();
- }
-
- function showAtomDirectMessages()
- {
- $this->initDocument('atom');
- $this->element('title', null, $this->title);
- $this->element('id', null, $this->id);
- $selfuri = common_root_url() . 'api/direct_messages.atom';
- $this->element(
- 'link', array(
- 'href' => $this->link,
- 'rel' => 'alternate',
- 'type' => 'text/html'),
- null
- );
- $this->element(
- 'link', array(
- 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
- 'type' => 'application/atom+xml'),
- null
- );
- $this->element('updated', null, common_date_iso8601('now'));
- $this->element('subtitle', null, $this->subtitle);
- foreach ($this->messages as $m) {
- $entry = $this->rssDirectMessageArray($m);
- $this->showTwitterAtomEntry($entry);
- }
- $this->endDocument('atom');
- }
-
- function etag()
- {
- if (!empty($this->messages)) {
- $last = count($this->messages) - 1;
- return '"' . implode(
- ':',
- array($this->arg('action'),
- common_user_cache_hash($this->auth_user),
- common_language(),
- strtotime($this->messages[0]->created),
- strtotime($this->messages[$last]->created)
- )
- )
- . '"';
- }
- return null;
- }
- }
|