[DirectMessage] PSR12-format

This commit is contained in:
t3nma 2020-07-28 04:46:10 +01:00 committed by Diogo Peralta Cordeiro
parent 903a9d2667
commit 58d7ab4d13
3 changed files with 144 additions and 136 deletions

View File

@ -77,8 +77,10 @@ class DirectMessagePlugin extends Plugin
); );
$m->connect( $m->connect(
'api/direct_messages/sent.:format', 'api/direct_messages/sent.:format',
['action' => 'ApiDirectMessage', [
'sent' => true], 'action' => 'ApiDirectMessage',
'sent' => true,
],
['format' => '(xml|json|rss|atom)'] ['format' => '(xml|json|rss|atom)']
); );
$m->connect( $m->connect(
@ -183,7 +185,7 @@ class DirectMessagePlugin extends Plugin
); );
$out->elementEnd('li'); $out->elementEnd('li');
} }
return true; return true;
} }
@ -216,8 +218,10 @@ class DirectMessagePlugin extends Plugin
Notice::saveActivity( Notice::saveActivity(
$act, $act,
$msg->getFrom(), $msg->getFrom(),
['source' => 'web', [
'scope' => NOTICE::MESSAGE_SCOPE] 'source' => 'web',
'scope' => NOTICE::MESSAGE_SCOPE,
]
); );
} }
} }

View File

@ -1,57 +1,52 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* StatusNet, the distributed open-source microblogging tool * Direct messaging implementation for GNU social
* *
* Show a the direct messages from or to a user * @package GNUsocial
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category API
* @package StatusNet
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Robin Millette <robin@millette.info> * @author Robin Millette <robin@millette.info>
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @copyright 2009 StatusNet, Inc. * @copyright 2009, 2020 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
if (!defined('GNUSOCIAL')) { exit(1); } defined('GNUSOCIAL') || die();
/** /**
* Show a list of direct messages from or to the authenticating user * Show a list of direct messages from or to the authenticating user
* *
* @category API * @category Plugin
* @package StatusNet * @package GNUsocial
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Robin Millette <robin@millette.info> * @author Robin Millette <robin@millette.info>
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
class ApiDirectMessageAction extends ApiAuthAction class ApiDirectMessageAction extends ApiAuthAction
{ {
var $messages = null; public $messages = null;
var $title = null; public $title = null;
var $subtitle = null; public $subtitle = null;
var $link = null; public $link = null;
var $selfuri_base = null; public $selfuri_base = null;
var $id = null; public $id = null;
/** /**
* Take arguments for running * Take arguments for running
@ -60,7 +55,7 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return boolean success flag * @return boolean success flag
*/ */
protected function prepare(array $args=array()) protected function prepare(array $args = [])
{ {
parent::prepare($args); parent::prepare($args);
@ -69,7 +64,7 @@ class ApiDirectMessageAction extends ApiAuthAction
$this->clientError(_('No such user.'), 404); $this->clientError(_('No such user.'), 404);
} }
$server = common_root_url(); $server = common_root_url();
$taguribase = TagURI::base(); $taguribase = TagURI::base();
if ($this->arg('sent')) { if ($this->arg('sent')) {
@ -121,25 +116,25 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return void * @return void
*/ */
function showMessages() public function showMessages()
{ {
switch($this->format) { switch ($this->format) {
case 'xml': case 'xml':
$this->showXmlDirectMessages(); $this->showXmlDirectMessages();
break; break;
case 'rss': case 'rss':
$this->showRssDirectMessages(); $this->showRssDirectMessages();
break; break;
case 'atom': case 'atom':
$this->showAtomDirectMessages(); $this->showAtomDirectMessages();
break; break;
case 'json': case 'json':
$this->showJsonDirectMessages(); $this->showJsonDirectMessages();
break; break;
default: default:
// TRANS: Client error displayed when coming across a non-supported API method. // TRANS: Client error displayed when coming across a non-supported API method.
$this->clientError(_('API method not found.'), $code = 404); $this->clientError(_('API method not found.'), $code = 404);
break; break;
} }
} }
@ -148,9 +143,9 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return array notices * @return array notices
*/ */
function getMessages() public function getMessages()
{ {
$message = new Message(); $message = new Message();
if ($this->arg('sent')) { if ($this->arg('sent')) {
$message->from_profile = $this->scoped->getID(); $message->from_profile = $this->scoped->getID();
@ -170,7 +165,7 @@ class ApiDirectMessageAction extends ApiAuthAction
$message->limit((($this->page - 1) * $this->count), $this->count); $message->limit((($this->page - 1) * $this->count), $this->count);
$message->find(); $message->find();
$messages = array(); $messages = [];
while ($message->fetch()) { while ($message->fetch()) {
$messages[] = clone($message); $messages[] = clone($message);
@ -186,7 +181,7 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return boolean true * @return boolean true
*/ */
function isReadOnly($args) public function isReadOnly($args)
{ {
return true; return true;
} }
@ -196,7 +191,7 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return string datestamp of the latest notice in the stream * @return string datestamp of the latest notice in the stream
*/ */
function lastModified() public function lastModified()
{ {
if (!empty($this->messages)) { if (!empty($this->messages)) {
return strtotime($this->messages[0]->created); return strtotime($this->messages[0]->created);
@ -207,7 +202,7 @@ class ApiDirectMessageAction extends ApiAuthAction
// BEGIN import from lib/apiaction.php // BEGIN import from lib/apiaction.php
function showSingleXmlDirectMessage($message) public function showSingleXmlDirectMessage($message)
{ {
$this->initDocument('xml'); $this->initDocument('xml');
$dmsg = $this->directMessageArray($message); $dmsg = $this->directMessageArray($message);
@ -215,7 +210,7 @@ class ApiDirectMessageAction extends ApiAuthAction
$this->endDocument('xml'); $this->endDocument('xml');
} }
function showSingleJsonDirectMessage($message) public function showSingleJsonDirectMessage($message)
{ {
$this->initDocument('json'); $this->initDocument('json');
$dmsg = $this->directMessageArray($message); $dmsg = $this->directMessageArray($message);
@ -223,14 +218,14 @@ class ApiDirectMessageAction extends ApiAuthAction
$this->endDocument('json'); $this->endDocument('json');
} }
function showXmlDirectMessage($dm, $namespaces=false) public function showXmlDirectMessage($dm, $namespaces = false)
{ {
$attrs = array(); $attrs = [];
if ($namespaces) { if ($namespaces) {
$attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/'; $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
} }
$this->elementStart('direct_message', $attrs); $this->elementStart('direct_message', $attrs);
foreach($dm as $element => $value) { foreach ($dm as $element => $value) {
switch ($element) { switch ($element) {
case 'sender': case 'sender':
case 'recipient': case 'recipient':
@ -247,9 +242,9 @@ class ApiDirectMessageAction extends ApiAuthAction
$this->elementEnd('direct_message'); $this->elementEnd('direct_message');
} }
function directMessageArray($message) public function directMessageArray($message)
{ {
$dmsg = array(); $dmsg = [];
$from_profile = $message->getFrom(); $from_profile = $message->getFrom();
$to_profile = $message->getTo(); $to_profile = $message->getTo();
@ -267,17 +262,23 @@ class ApiDirectMessageAction extends ApiAuthAction
return $dmsg; return $dmsg;
} }
function rssDirectMessageArray($message) public function rssDirectMessageArray($message)
{ {
$entry = array(); $entry = [];
$from = $message->getFrom(); $from = $message->getFrom();
$entry['title'] = sprintf('Message from %1$s to %2$s', $entry['title'] = sprintf(
$from->nickname, $message->getTo()->nickname); 'Message from %1$s to %2$s',
$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('showmessage', array('message' => $message->id)); $entry['link'] = common_local_url(
'showmessage',
['message' => $message->id]
);
$entry['published'] = common_date_iso8601($message->created); $entry['published'] = common_date_iso8601($message->created);
$taguribase = TagURI::base(); $taguribase = TagURI::base();
@ -312,11 +313,13 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return void * @return void
*/ */
function showXmlDirectMessages() public function showXmlDirectMessages()
{ {
$this->initDocument('xml'); $this->initDocument('xml');
$this->elementStart('direct-messages', array('type' => 'array', $this->elementStart('direct-messages', [
'xmlns:statusnet' => 'http://status.net/schema/api/1/')); 'type' => 'array',
'xmlns:statusnet' => 'http://status.net/schema/api/1/',
]);
foreach ($this->messages as $m) { foreach ($this->messages as $m) {
$dm_array = $this->directMessageArray($m); $dm_array = $this->directMessageArray($m);
@ -332,11 +335,11 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return void * @return void
*/ */
function showJsonDirectMessages() public function showJsonDirectMessages()
{ {
$this->initDocument('json'); $this->initDocument('json');
$dmsgs = array(); $dmsgs = [];
foreach ($this->messages as $m) { foreach ($this->messages as $m) {
$dm_array = $this->directMessageArray($m); $dm_array = $this->directMessageArray($m);
@ -352,7 +355,7 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return void * @return void
*/ */
function showRssDirectMessages() public function showRssDirectMessages()
{ {
$this->initDocument('rss'); $this->initDocument('rss');
@ -364,11 +367,11 @@ class ApiDirectMessageAction extends ApiAuthAction
$this->element( $this->element(
'atom:link', 'atom:link',
array( [
'type' => 'application/rss+xml', 'type' => 'application/rss+xml',
'href' => $this->selfuri_base . '.rss', 'href' => $this->selfuri_base . '.rss',
'rel' => self 'rel' => self,
), ],
null null
); );
$this->element('ttl', null, '40'); $this->element('ttl', null, '40');
@ -386,7 +389,7 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return void * @return void
*/ */
function showAtomDirectMessages() public function showAtomDirectMessages()
{ {
$this->initDocument('atom'); $this->initDocument('atom');
@ -396,16 +399,21 @@ class ApiDirectMessageAction extends ApiAuthAction
$selfuri = common_root_url() . 'api/direct_messages.atom'; $selfuri = common_root_url() . 'api/direct_messages.atom';
$this->element( $this->element(
'link', array( 'link',
'href' => $this->link, [
'rel' => 'alternate', 'href' => $this->link,
'type' => 'text/html'), 'rel' => 'alternate',
'type' => 'text/html',
],
null null
); );
$this->element( $this->element(
'link', array( 'link',
'href' => $this->selfuri_base . '.atom', 'rel' => 'self', [
'type' => 'application/atom+xml'), 'href' => $this->selfuri_base . '.atom',
'rel' => 'self',
'type' => 'application/atom+xml',
],
null null
); );
$this->element('updated', null, common_date_iso8601('now')); $this->element('updated', null, common_date_iso8601('now'));
@ -427,20 +435,20 @@ class ApiDirectMessageAction extends ApiAuthAction
* *
* @return string etag * @return string etag
*/ */
function etag() public function etag()
{ {
if (!empty($this->messages)) { if (!empty($this->messages)) {
$last = count($this->messages) - 1; $last = count($this->messages) - 1;
return '"' . implode( return '"' . implode(
':', ':',
array($this->arg('action'), [
common_user_cache_hash($this->auth_user), $this->arg('action'),
common_language(), common_user_cache_hash($this->auth_user),
strtotime($this->messages[0]->created), common_language(),
strtotime($this->messages[$last]->created) strtotime($this->messages[0]->created),
) strtotime($this->messages[$last]->created),
]
) )
. '"'; . '"';
} }

View File

@ -1,56 +1,51 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* StatusNet, the distributed open-source microblogging tool * Direct messaging implementation for GNU social
* *
* Send a direct message via the API * @package GNUsocial
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category API
* @package StatusNet
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Robin Millette <robin@millette.info> * @author Robin Millette <robin@millette.info>
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @copyright 2009 StatusNet, Inc. * @copyright 2009, 2020 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
if (!defined('GNUSOCIAL')) { exit(1); } defined('GNUSOCIAL') || die();
/** /**
* Creates a new direct message from the authenticating user to * Creates a new direct message from the authenticating user to
* the user specified by id. * the user specified by id
* *
* @category API * @category Plugin
* @package StatusNet * @package GNUsocial
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Robin Millette <robin@millette.info> * @author Robin Millette <robin@millette.info>
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
class ApiDirectMessageNewAction extends ApiAuthAction class ApiDirectMessageNewAction extends ApiAuthAction
{ {
protected $needPost = true; protected $needPost = true;
var $other = null; // Profile we're sending to public $other = null; // Profile we're sending to
var $content = null; public $content = null;
/** /**
* Take arguments for running * Take arguments for running
@ -59,7 +54,7 @@ class ApiDirectMessageNewAction extends ApiAuthAction
* *
* @return boolean success flag * @return boolean success flag
*/ */
protected function prepare(array $args=array()) protected function prepare(array $args = [])
{ {
parent::prepare($args); parent::prepare($args);
@ -102,17 +97,18 @@ class ApiDirectMessageNewAction extends ApiAuthAction
// TRANS: %d is the maximum number of characters for a message. // TRANS: %d is the maximum number of characters for a message.
$this->clientError( $this->clientError(
sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()), Message::maxContent()), sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()), Message::maxContent()),
406); 406
);
} }
} }
if (!$this->other instanceof Profile) { if (!$this->other instanceof Profile) {
// TRANS: Client error displayed if a recipient user could not be found (403). // TRANS: Client error displayed if a recipient user could not be found (403).
$this->clientError(_('Recipient user not found.'), 403); $this->clientError(_('Recipient user not found.'), 403);
} else if (!$this->scoped->mutuallySubscribed($this->other)) { } elseif (!$this->scoped->mutuallySubscribed($this->other)) {
// TRANS: Client error displayed trying to direct message another user who's not a friend (403). // TRANS: Client error displayed trying to direct message another user who's not a friend (403).
$this->clientError(_('Cannot send direct messages to users who aren\'t your friend.'), 403); $this->clientError(_('Cannot send direct messages to users who aren\'t your friend.'), 403);
} else if ($this->scoped->getID() === $this->other->getID()) { } elseif ($this->scoped->getID() === $this->other->getID()) {
// Note: sending msgs to yourself is allowed by Twitter // Note: sending msgs to yourself is allowed by Twitter