From 34b570352f9a21ddbc7a0d0925828f29a38d8d04 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Tue, 29 Apr 2014 20:37:58 +0200 Subject: [PATCH] Fix Direct Message functionality. --- actions/apidirectmessagenew.php | 6 +++--- actions/newmessage.php | 20 +++++++++++--------- classes/Profile.php | 20 ++++++++++++++++++-- classes/User.php | 4 ++-- lib/command.php | 4 ++-- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/actions/apidirectmessagenew.php b/actions/apidirectmessagenew.php index 9624e51695..653fa3a9ed 100644 --- a/actions/apidirectmessagenew.php +++ b/actions/apidirectmessagenew.php @@ -51,7 +51,7 @@ class ApiDirectMessageNewAction extends ApiAuthAction { protected $needPost = true; - var $other = null; + var $other = null; // Profile we're sending to var $content = null; /** @@ -77,7 +77,7 @@ class ApiDirectMessageNewAction extends ApiAuthAction $screen_name = $this->trimmed('screen_name'); if (isset($user_param) || isset($user_id) || isset($screen_name)) { - $this->other = $this->getTargetUser($user_param); + $this->other = $this->getTargetProfile($user_param); } return true; @@ -108,7 +108,7 @@ class ApiDirectMessageNewAction extends ApiAuthAction } } - if (empty($this->other)) { + if (!$this->other instanceof Profile) { // TRANS: Client error displayed if a recipient user could not be found (403). $this->clientError(_('Recipient user not found.'), 403); } else if (!$this->user->mutuallySubscribed($this->other)) { diff --git a/actions/newmessage.php b/actions/newmessage.php index 96ada1cee8..428a55762c 100644 --- a/actions/newmessage.php +++ b/actions/newmessage.php @@ -78,21 +78,24 @@ class NewmessageAction extends FormAction { parent::prepare($args); - $user = $this->scoped->getUser(); - $this->content = $this->trimmed('content'); $this->to = $this->trimmed('to'); if ($this->to) { - $this->other = User::getKV('id', $this->to); + $this->other = Profile::getKV('id', $this->to); - if (!$this->other) { + if (!$this->other instanceof Profile) { // TRANS: Client error displayed trying to send a direct message to a non-existing user. $this->clientError(_('No such user.'), 404); } - if (!$user->mutuallySubscribed($this->other)) { + if (!$this->other->isLocal()) { + // TRANS: Explains that current federation does not support direct, private messages yet. + $this->clientError(_('You cannot send direct messages to federated users yet.')); + } + + if (!$this->scoped->mutuallySubscribed($this->other)) { // TRANS: Client error displayed trying to send a direct message to a user while sender and // TRANS: receiver are not subscribed to each other. $this->clientError(_('You cannot send a message to this user.'), 404); @@ -106,14 +109,13 @@ class NewmessageAction extends FormAction { parent::handlePost(); - assert($this->scoped); // XXX: maybe an error instead... - $user = $this->scoped->getUser(); + assert($this->scoped instanceof Profile); // XXX: maybe an error instead... if (!$this->content) { // TRANS: Form validator error displayed trying to send a direct message without content. $this->clientError(_('No content!')); } else { - $content_shortened = $user->shortenLinks($this->content); + $content_shortened = $this->scoped->shortenLinks($this->content); if (Message::contentTooLong($content_shortened)) { // TRANS: Form validation error displayed when message content is too long. @@ -128,7 +130,7 @@ class NewmessageAction extends FormAction if (!$this->other) { // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient. $this->clientError(_('No recipient specified.')); - } else if (!$user->mutuallySubscribed($this->other)) { + } else if (!$this->scoped->mutuallySubscribed($this->other)) { // TRANS: Client error displayed trying to send a direct message to a user while sender and // TRANS: receiver are not subscribed to each other. $this->clientError(_('You cannot send a message to this user.'), 404); diff --git a/classes/Profile.php b/classes/Profile.php index c40e0e0b17..3f0b87e79f 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -756,7 +756,7 @@ class Profile extends Managed_DataObject * @param Profile $other * @return boolean */ - function hasPendingSubscription($other) + function hasPendingSubscription(Profile $other) { return Subscription_queue::exists($this, $other); } @@ -767,7 +767,7 @@ class Profile extends Managed_DataObject * @param Profile $other * @return boolean */ - function mutuallySubscribed($other) + function mutuallySubscribed(Profile $other) { return $this->isSubscribed($other) && $other->isSubscribed($this); @@ -1548,4 +1548,20 @@ class Profile extends Managed_DataObject { return $this; } + + /** + * This will perform shortenLinks with the connected User object. + * + * Won't work on remote profiles or groups, so expect a + * NoSuchUserException if you don't know it's a local User. + * + * @param string $text String to shorten + * @param boolean $always Disrespect minimum length etc. + * + * @return string link-shortened $text + */ + public function shortenLinks($text, $always=false) + { + return $this->getUser()->shortenLinks($text, $always); + } } diff --git a/classes/User.php b/classes/User.php index 55e5277245..7a4564c80d 100644 --- a/classes/User.php +++ b/classes/User.php @@ -142,7 +142,7 @@ class User extends Managed_DataObject return $this->getProfile()->isSubscribed($other); } - function hasPendingSubscription($other) + function hasPendingSubscription(Profile $other) { return $this->getProfile()->hasPendingSubscription($other); } @@ -440,7 +440,7 @@ class User extends Managed_DataObject return $this->getProfile()->hasFave($notice); } - function mutuallySubscribed($other) + function mutuallySubscribed(Profile $other) { return $this->getProfile()->mutuallySubscribed($other); } diff --git a/lib/command.php b/lib/command.php index d15fe43ce1..bc0dd894de 100644 --- a/lib/command.php +++ b/lib/command.php @@ -586,7 +586,7 @@ class MessageCommand extends Command function handle($channel) { try { - $other = $this->getUser($this->other); + $other = $this->getUser($this->other)->getProfile(); } catch (CommandException $e) { try { $profile = $this->getProfile($this->other); @@ -619,7 +619,7 @@ class MessageCommand extends Command return; } - if (!$other) { + if (!$other instanceof Profile) { // TRANS: Error text shown when trying to send a direct message to a user that does not exist. $channel->error($this->user, _('No such user.')); return; -- 2.39.2