123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- defined('GNUSOCIAL') || die();
- class ApiDirectMessageNewAction extends ApiAuthAction
- {
- protected $needPost = true;
- public $other = null;
- public $content = null;
-
- protected function prepare(array $args = [])
- {
- parent::prepare($args);
- if (!$this->scoped instanceof Profile) {
-
- $this->clientError(_('No such user.'), 404);
- }
- $this->content = $this->trimmed('text');
- $user_param = $this->trimmed('user');
- $user_id = $this->arg('user_id');
- $screen_name = $this->trimmed('screen_name');
- if (isset($user_param) || isset($user_id) || isset($screen_name)) {
- $this->other = $this->getTargetProfile($user_param);
- }
- return true;
- }
-
- protected function handle()
- {
- parent::handle();
- if (empty($this->content)) {
-
- $this->clientError(_('No message text!'), 406);
- } else {
- $content_shortened = $this->auth_user->shortenLinks($this->content);
- if (MessageModel::contentTooLong($content_shortened)) {
-
-
- $this->clientError(
- sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', MessageModel::maxContent()), MessageModel::maxContent()),
- 406
- );
- }
- }
- if (!$this->other instanceof Profile) {
-
- $this->clientError(_('Recipient user not found.'), 403);
- } elseif (
- $this->other->isLocal()
- && !$this->scoped->mutuallySubscribed($this->other)
- ) {
-
- $this->clientError(_('Cannot send direct messages to users who aren\'t your friend.'), 403);
- } elseif ($this->scoped->getID() === $this->other->getID()) {
-
-
- $this->clientError(_('Do not send a message to yourself; just say it to yourself quietly instead.'), 403);
- }
-
-
- if ($this->other->isLocal()) {
- $this->content = "@{$this->other->getNickname()} {$this->content}";
- } else {
- $this->content = '@' . substr($this->other->getAcctUri(), 5) . $this->content;
- }
- $message = MessageModel::saveNew(
- $this->scoped->getID(),
- $this->content,
- $this->source
- );
- Event::handle('SendDirectMessage', [$message]);
- mail_notify_message($message);
- if ($this->format == 'xml') {
- $this->showSingleXmlDirectMessage($message);
- } elseif ($this->format == 'json') {
- $this->showSingleJsondirectMessage($message);
- }
- }
- }
|