[DirectMessage] Update ApiDirectMessage action

- getMessages() is now fetching from the Notice table as supposed
- every show{format}* method is properly updated to use Notice objects
- json and xml responses retrieve multi-recipients without compromising
  backwards compatibility
This commit is contained in:
t3nma 2020-07-28 05:49:37 +01:00 committed by Diogo Peralta Cordeiro
parent 8b0abc0382
commit 8ca412bd1d

View File

@ -139,19 +139,71 @@ class ApiDirectMessageAction extends ApiAuthAction
} }
/** /**
* Get notices * Get messages
* *
* @return array notices * @return array
*/ */
public function getMessages() public function getMessages(): array
{ {
$message = new Message(); $message = $this->arg('sent')
? $this->getOutboxMessages()
: $this->getInboxMessages();
if ($this->arg('sent')) { $ret = [];
$message->from_profile = $this->scoped->getID();
} else { if (!is_null($message)) {
$message->to_profile = $this->scoped->getID(); while ($message->fetch()) {
$ret[] = clone $message;
} }
}
return $ret;
}
/**
* Return data object of the messages received by some user.
*
* @return Notice data object
*/
private function getInboxMessages()
{
// fetch all notice IDs related to the user
$attention = new Attention();
$attention->selectAdd('notice_id');
$attention->whereAdd('profile_id = ' . $this->scoped->getID());
$ids = $attention->find() ? $attention->fetchAll('notice_id') : [];
// get the messages
$message = new Notice();
$message->whereAdd('scope = ' . NOTICE::MESSAGE_SCOPE);
if (!empty($this->max_id)) {
$message->whereAdd('id <= ' . $this->max_id);
}
if (!empty($this->since_id)) {
$message->whereAdd('id > ' . $this->since_id);
}
$message->whereAddIn('id', $ids, 'int');
$message->orderBy('created DESC, id DESC');
$message->limit((($this->page - 1) * $this->count), $this->count);
return $message->find() ? $message : null;
}
/**
* Return data object of the messages sent by some user.
*
* @return Notice data object
*/
private function getOutboxMessages()
{
$message = new Notice();
$message->profile_id = $this->scoped->getID();
$message->whereAdd('scope = ' . NOTICE::MESSAGE_SCOPE);
if (!empty($this->max_id)) { if (!empty($this->max_id)) {
$message->whereAdd('id <= ' . $this->max_id); $message->whereAdd('id <= ' . $this->max_id);
@ -163,15 +215,8 @@ class ApiDirectMessageAction extends ApiAuthAction
$message->orderBy('created DESC, id DESC'); $message->orderBy('created DESC, id DESC');
$message->limit((($this->page - 1) * $this->count), $this->count); $message->limit((($this->page - 1) * $this->count), $this->count);
$message->find();
$messages = []; return $message->find() ? $message : null;
while ($message->fetch()) {
$messages[] = clone($message);
}
return $messages;
} }
/** /**
@ -226,17 +271,15 @@ class ApiDirectMessageAction extends ApiAuthAction
} }
$this->elementStart('direct_message', $attrs); $this->elementStart('direct_message', $attrs);
foreach ($dm as $element => $value) { foreach ($dm as $element => $value) {
switch ($element) { if ($element === 'text') {
case 'sender':
case 'recipient':
$this->showTwitterXmlUser($value, $element);
break;
case 'text':
$this->element($element, null, common_xml_safe_str($value)); $this->element($element, null, common_xml_safe_str($value));
break; } elseif (
default: $element === 'sender'
|| preg_match('/recipient$|recipient_[0-9]+/', $element) == 1
) {
$this->showTwitterXmlUser($value, $element);
} else {
$this->element($element, null, $value); $this->element($element, null, $value);
break;
} }
} }
$this->elementEnd('direct_message'); $this->elementEnd('direct_message');
@ -246,18 +289,34 @@ class ApiDirectMessageAction extends ApiAuthAction
{ {
$dmsg = []; $dmsg = [];
$from_profile = $message->getFrom(); $from = $message->getProfile();
$to_profile = $message->getTo(); $to = $message->getAttentionProfiles();
$dmsg['id'] = intval($message->id); $dmsg['id'] = intval($message->id);
$dmsg['sender_id'] = intval($from_profile->id); $dmsg['sender_id'] = (int) $from->id;
$dmsg['text'] = trim($message->content); $dmsg['text'] = trim($message->content);
$dmsg['recipient_id'] = intval($to_profile->id);
$dmsg['total_recipients'] = (int) count($to);
$dmsg['recipient_id'] = (int) $to[0]->id;
for ($i = 1; $i < count($to); ++$i) {
$dmsg['recipient_id_' . $i] = (int) $to[$i]->id;
}
$dmsg['created_at'] = $this->dateTwitter($message->created); $dmsg['created_at'] = $this->dateTwitter($message->created);
$dmsg['sender_screen_name'] = $from_profile->nickname; $dmsg['sender_screen_name'] = $from->nickname;
$dmsg['recipient_screen_name'] = $to_profile->nickname; $dmsg['recipient_screen_name'] = $to[0]->nickname;
$dmsg['sender'] = $this->twitterUserArray($from_profile, false);
$dmsg['recipient'] = $this->twitterUserArray($to_profile, false); for ($i = 1; $i < count($to); ++$i) {
$dmsg['recipient_screen_name_' . $i] = $to[$i]->nickname;
}
$dmsg['sender'] = $this->twitterUserArray($from);
$dmsg['recipient'] = $this->twitterUserArray($to[0]);
for ($i = 1; $i < count($to); ++$i) {
$dmsg['recipient_' . $i] = $this->twitterUserArray($to[$i]);
}
return $dmsg; return $dmsg;
} }
@ -266,13 +325,11 @@ class ApiDirectMessageAction extends ApiAuthAction
{ {
$entry = []; $entry = [];
$from = $message->getFrom(); $from = $message->getProfile();
$to = $message->getAttentionProfiles();
$entry['title'] = sprintf( $entry['title'] = 'Message from ' . $from->nickname . ' to ';
'Message from %1$s to %2$s', $entry['title'] .= (count($to) == 1) ? $to[0]->nickname : 'many';
$from->nickname,
$message->getTo()->nickname
);
$entry['content'] = common_xml_safe_str($message->rendered); $entry['content'] = common_xml_safe_str($message->rendered);
$entry['link'] = common_local_url( $entry['link'] = common_local_url(