[DirectMessage] Major plugin rework

This commit does the necessary rework to store private messages
as Notices and to support Federation. The plugin's README presents
some more detail about the changes and future work that is still
required to do.
This commit is contained in:
tenma
2019-08-19 22:51:51 +01:00
parent 0716605e94
commit db5a29fd9a
14 changed files with 952 additions and 705 deletions

View File

@@ -1,7 +1,39 @@
<?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/>.
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* GNUsocial implementation of Direct Messages
*
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
/**
* Inbox MessageListItem widget
*
* @category Plugin
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class InboxMessageListItem extends MessageListItem
{
/**
@@ -9,8 +41,8 @@ class InboxMessageListItem extends MessageListItem
*
* @return Profile The profile that matches the message
*/
function getMessageProfile()
function getMessageProfile(): ?Profile
{
return $this->message->getFrom();
return $this->message->getProfile();
}
}

View File

@@ -1,117 +1,106 @@
<?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
* GNUsocial implementation of Direct Messages
*
* Form for posting a direct message
*
* 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 Form
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @copyright 2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
if (!defined('GNUSOCIAL')) { exit(1); }
defined('GNUSOCIAL') || die();
/**
* Form for posting a direct message
*
* @category Form
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*
* @see HTMLOutputter
* @category Plugin
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class MessageForm extends Form
{
/**
* User to send a direct message to
*/
var $to = null;
protected $to = null;
protected $content = null;
/**
* Pre-filled content of the form
*/
var $content = null;
/**
* Constructor
* Constructor.
*
* @param HTMLOutputter $out output channel
* @param User $to user to send a message to
* @param string $content content to pre-fill
* @param HTMLOutputter $out output channel
* @param array|null $formOpts
*/
function __construct($out=null, $to=null, $content=null)
function __construct(HTMLOutputter $out = null, ?array $formOpts = null)
{
parent::__construct($out);
$this->to = $to;
$this->content = $content;
if (isset($formOpts['to'])) {
$this->to = $formOpts['to'];
}
$this->content = $formOpts['content'] ?? '';
}
/**
* ID of the form
* ID of the form.
*
* @return string ID of the form
*/
function id()
function id(): string
{
return 'form_notice-direct';
}
/**
* Class of the form
/**
* Class of the form.
*
* @return string class of the form
*/
function formClass()
function formClass(): string
{
return 'form_notice ajax-notice';
}
/**
* Action of the form
* Action of the form.
*
* @return string URL of the action
*/
function action()
function action(): string
{
return common_local_url('newmessage');
}
/**
* Legend of the Form
* Legend of the Form.
*
* @return void
*/
function formLegend()
{
// TRANS: Form legend for direct notice.
$this->out->element('legend', null, _('Send a direct notice'));
$this->out->element('legend', null, _m('Send a direct notice'));
}
/**
* Data elements
* Data elements.
*
* @return void
*/
@@ -119,87 +108,112 @@ class MessageForm extends Form
{
$user = common_current_user();
$mutual_users = $user->mutuallySubscribedUsers();
$recipients = [];
$default = 'default';
$mutual = array();
// TRANS: Label entry in drop-down selection box in direct-message inbox/outbox.
// TRANS: This is the default entry in the drop-down box, doubling as instructions
// TRANS: and a brake against accidental submissions with the first user in the list.
$mutual[0] = _('Select recipient:');
$subs = $user->getSubscribed();
$n_subs = 0;
while ($mutual_users->fetch()) {
if ($mutual_users->id != $user->id) {
$mutual[$mutual_users->id] = $mutual_users->nickname;
// Add local-subscriptions
while ($subs->fetch()) {
$n_subs++;
if ($subs->isLocal()) {
$value = 'profile:'.$subs->getID();
try {
$recipients[$value] = substr($subs->getAcctUri(), 5) . " [{$subs->getBestName()}]";
} catch (ProfileNoAcctUriException $e) {
$recipients[$value] = "[?@?] " . $e->profile->getBestName();
}
}
}
$mutual_users->free();
unset($mutual_users);
if (count($mutual) == 1) {
// TRANS: Entry in drop-down selection box in direct-message inbox/outbox when no one is available to message.
$mutual[0] = _('No mutual subscribers.');
if (sizeof($recipients) < $n_subs) {
// some subscriptions aren't local and therefore weren't added,
// worth checking if others want to add them
Event::handle('FillDirectMessageRecipients', [$user, &$recipients]);
}
// if we came from a profile page, then lets make the message receiver visible
if (!is_null($this->to)) {
if (isset($recipients['profile:'.$this->to->getID()])) {
$default = 'profile' . $this->to->getID();
} else {
try {
if ($this->to->isLocal()) {
$this->content = "@{$this->to->getNickname()} {$this->content}";
} else {
$this->content = substr($this->to->getAcctUri(), 5) . " {$this->content}";
}
} catch (ProfileNoAcctUriException $e) {
// well, I'm no magician
}
}
}
if ($default === 'default') {
// TRANS: Label entry in drop-down selection box in direct-message inbox/outbox.
// TRANS: This is the default entry in the drop-down box, doubling as instructions
// TRANS: and a brake against accidental submissions with the first user in the list.
$recipients[$default] = empty($recipients) ? _m('No subscriptions') : _m('Select recipient:');
}
asort($recipients);
// TRANS: Dropdown label in direct notice form.
$this->out->dropdown('to', _('To'), $mutual, null, false,
($this->to) ? $this->to->id : null);
$this->out->dropdown('to-box',
_m('To'),
$recipients,
null,
false,
$default);
$this->out->element('textarea', array('class' => 'notice_data-text',
'cols' => 35,
'rows' => 4,
'name' => 'content'),
($this->content) ? $this->content : '');
$this->out->element('textarea',
['class' => 'notice_data-text',
'cols' => 35,
'rows' => 4,
'name' => 'content'],
$this->content);
$contentLimit = Message::maxContent();
$contentLimit = MessageModel::maxContent();
if ($contentLimit > 0) {
$this->out->element('span',
array('class' => 'count'),
['class' => 'count'],
$contentLimit);
}
}
/**
* Action elements
* Action elements.
*
* @return void
*/
function formActions()
{
$this->out->element('input', array('id' => 'notice_action-submit',
'class' => 'submit',
'name' => 'message_send',
'type' => 'submit',
// TRANS: Button text for sending a direct notice.
'value' => _m('Send button for sending notice', 'Send')));
$this->out->element('input',
['id' => 'notice_action-submit',
'class' => 'submit',
'name' => 'message_send',
'type' => 'submit',
// TRANS: Button text for sending a direct notice.
'value' => _m('Send button for direct notice', 'Send')]);
}
/**
* Show the form
*
* Uses a recipe to output the form.
* Show the form.
*
* @return void
* @see Widget::show()
*/
function show()
{
$this->elementStart('div', 'input_forms');
$this->elementStart(
'div',
array(
'id' => 'input_form_direct',
'class' => 'input_form current nonav'
)
);
$this->elementStart('div',
['id' => 'input_form_direct',
'class' => 'input_form current nonav']);
parent::show();
$this->elementEnd('div');
$this->elementEnd('div');
}
}

View File

@@ -1,112 +1,102 @@
<?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
* Copyright (C) 2011, StatusNet, Inc.
* GNUsocial implementation of Direct Messages
*
* A single list item for showing in a message list
*
* PHP version 5
*
* 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 Widget
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
defined('GNUSOCIAL') || die();
/**
* A single item in a message list
*
* @category Widget
* @package StatusNet
* @category Plugin
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
abstract class MessageListItem extends Widget
{
var $message;
protected $message;
/**
* Constructor
* Constructor.
*
* @param HTMLOutputter $out Output context
* @param Message $message Message to show
* @param Notice $message Message to show
*/
function __construct($out, $message)
function __construct(HTMLOutputter $out, $message)
{
parent::__construct($out);
$this->message = $message;
}
/**
* Show the widget
* Show the widget.
*
* @return void
*/
function show()
{
$this->out->elementStart('li', array('class' => 'h-entry notice',
'id' => 'message-' . $this->message->id));
$profile = $this->getMessageProfile();
if (is_null($profile)) {
// null most probably because there are no attention profiles and
// the UI below isn't ready for that, yet.
return;
}
$this->out->elementStart('a', array('href' => $profile->profileurl,
'class' => 'p-author'));
$this->out->elementStart('li', ['class' => 'h-entry notice',
'id' => 'message-' . $this->message->getID()]);
$this->out->elementStart('a', ['href' => $profile->getUrl(),
'class' => 'p-author']);
$avatarUrl = $profile->avatarUrl(AVATAR_STREAM_SIZE);
$this->out->element('img', array('src' => $avatarUrl,
'class' => 'avatar u-photo',
'width' => AVATAR_STREAM_SIZE,
'height' => AVATAR_STREAM_SIZE,
'alt' => $profile->getBestName()));
$this->out->element('span', array('class' => 'nickname fn'), $profile->getNickname());
$this->out->element('img', ['src' => $avatarUrl,
'class' => 'avatar u-photo',
'width' => AVATAR_STREAM_SIZE,
'height' => AVATAR_STREAM_SIZE,
'alt' => $profile->getBestName()]);
$this->out->element('span', ['class' => 'nickname fn'], $profile->getNickname());
$this->out->elementEnd('a');
// FIXME: URL, image, video, audio
$this->out->elementStart('div', array('class' => 'e-content'));
$this->out->raw($this->message->rendered);
$this->out->elementStart('div', ['class' => 'e-content']);
$this->out->raw($this->message->getRendered());
$this->out->elementEnd('div');
$messageurl = common_local_url('showmessage',
array('message' => $this->message->id));
// XXX: we need to figure this out better. Is this right?
if (strcmp($this->message->uri, $messageurl) != 0 &&
preg_match('/^http/', $this->message->uri)) {
$messageurl = $this->message->uri;
}
['message' => $this->message->getID()]);
$this->out->elementStart('div', 'entry-metadata');
$this->out->elementStart('a', array('rel' => 'bookmark',
'class' => 'timestamp',
'href' => $messageurl));
$dt = common_date_iso8601($this->message->created);
$this->out->element('time', array('class' => 'dt-published',
'datetime' => common_date_iso8601($this->message->created),
// TRANS: Timestamp title (tooltip text) for NoticeListItem
'title' => common_exact_date($this->message->created)),
common_date_string($this->message->created));
$this->out->elementStart('a', ['rel' => 'bookmark',
'class' => 'timestamp',
'href' => $messageurl]);
$dt = common_date_iso8601($this->message->getCreated());
$this->out->element('time',
['class' => 'dt-published',
'datetime' => common_date_iso8601($this->message->getCreated()),
// TRANS: Timestamp title (tooltip text) for NoticeListItem
'title' => common_exact_date($this->message->getCreated())],
common_date_string($this->message->getCreated()));
$this->out->elementEnd('a');
if ($this->message->source) {
@@ -132,7 +122,7 @@ abstract class MessageListItem extends Widget
{
// A dummy array with messages. These will get extracted by xgettext and
// are used in self::showSource().
$dummy_messages = array(
$dummy_messages = [
// TRANS: A possible notice source (web interface).
_m('SOURCE','web'),
// TRANS: A possible notice source (XMPP).
@@ -142,12 +132,12 @@ abstract class MessageListItem extends Widget
// TRANS: A possible notice source (OpenMicroBlogging).
_m('SOURCE','omb'),
// TRANS: A possible notice source (Application Programming Interface).
_m('SOURCE','api'),
);
_m('SOURCE','api')
];
}
/**
* Show the source of the message
* Show the source of the message.
*
* Returns either the name (and link) of the API client that posted the notice,
* or one of other other channels.
@@ -156,7 +146,7 @@ abstract class MessageListItem extends Widget
*
* @return void
*/
function showSource($source)
function showSource(string $source)
{
$source_name = _m('SOURCE',$source);
switch ($source) {
@@ -171,8 +161,9 @@ abstract class MessageListItem extends Widget
$ns = Notice_source::getKV($source);
if ($ns) {
$this->out->elementStart('span', 'device');
$this->out->element('a', array('href' => $ns->url,
'rel' => 'external'),
$this->out->element('a',
['href' => $ns->url,
'rel' => 'external'],
$ns->name);
$this->out->elementEnd('span');
} else {
@@ -184,11 +175,11 @@ abstract class MessageListItem extends Widget
}
/**
* Return the profile to show in the message item
*
* Overridden in sub-classes to show sender, receiver, or whatever
* Return the profile to show in the message item.
*
* Overridden in sub-classes to show sender, receiver, or whatever.
*
* @return Profile profile to show avatar and name of
*/
abstract function getMessageProfile();
abstract function getMessageProfile(): ?Profile;
}

View File

@@ -0,0 +1,148 @@
<?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/>.
/**
* GNUsocial implementation of Direct Messages
*
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
/**
* Model for a direct message
*
* @category Plugin
* @package GNUsocial
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class MessageModel
{
/**
* Retrieve size-limit for messages content
*
* @return int size-limit
*/
public static function maxContent(): int
{
$desclimit = common_config('message', 'contentlimit');
// null => use global limit (distinct from 0!)
if (is_null($desclimit) || !is_int($desclimit)) {
$desclimit = common_config('site', 'textlimit');
}
return $desclimit;
}
/**
* Is message-text too long?
*
* @param string $content message-text
* @return bool true if too long, false otherwise
*/
public static function contentTooLong(string $content): bool
{
$contentlimit = self::maxContent();
return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
}
/**
* Return data object of messages received by some user.
*
* @param User $to receiver
* @param int|null $page page limiter
* @return Notice data object with stream for messages
*/
public static function inboxMessages(User $to, ?int $page = null)
{
$attention = new Attention();
$attention->selectAdd('notice_id');
$attention->whereAdd('profile_id = ' . $to->getID());
$ids = $attention->find() ? $attention->fetchAll('notice_id') : [];
$reply = new Reply();
$reply->selectAdd('notice_id');
$reply->whereAdd('profile_id = ' . $to->getID());
if ($reply->find()) {
$ids = array_unique(
array_merge($ids, $reply->fetchAll('notice_id'))
);
} else if (empty($ids)) {
return null;
}
$message = new Notice();
$message->whereAdd('scope = ' . NOTICE::MESSAGE_SCOPE);
$message->whereAddIn('id', $ids, 'int');
$message->orderBy('created DESC, id DESC');
if (!is_null($page) && $page >= 0) {
$page = ($page == 0) ? 1 : $page;
$message->limit(($page - 1) * MESSAGES_PER_PAGE,
MESSAGES_PER_PAGE + 1);
}
return $message->find() ? $message : null;
}
/**
* Return data object of messages sent by some user.
*
* @param User $from sender
* @param int|null $page page limiter
* @return Notice data object with stream for messages
*/
public static function outboxMessages(User $from, ?int $page = null)
{
$message = new Notice();
$message->profile_id = $from->getID();
$message->whereAdd('scope = ' . NOTICE::MESSAGE_SCOPE);
$message->orderBy('created DESC, id DESC');
if (!is_null($page) && $page >= 0) {
$page = ($page == 0) ? 1 : $page;
$message->limit(($page - 1) * MESSAGES_PER_PAGE,
MESSAGES_PER_PAGE + 1);
}
return $message->find() ? $message : null;
}
/**
* Save a new message.
*
* @param Profile $from sender
* @param string $content message-text
* @param string $source message's source
* @return Notice stored message
*/
public static function saveNew(Profile $from, string $content, string $source = 'web'): Notice
{
return Notice::saveNew($from->getID(),
$content,
$source,
['distribute' => false, // using events to handle remote distribution
'scope' => NOTICE::MESSAGE_SCOPE]);
}
}