From 2796ac52280ba30d89b50cc5907cc1209856c8b1 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 23 Jul 2020 13:52:41 +0000 Subject: [PATCH] [NOTIFICATION][DATABASE] Update user notification prefs table, implementation of Notification and define a base class for notification transport --- src/Entity/UserNotificationPrefs.php | 16 ++-- src/Util/Notification/AbstractTransport.php | 99 +++++++++++++++++++++ src/Util/Notification/Notification.php | 60 +++++++++++++ 3 files changed, 167 insertions(+), 8 deletions(-) create mode 100644 src/Util/Notification/AbstractTransport.php create mode 100644 src/Util/Notification/Notification.php diff --git a/src/Entity/UserNotificationPrefs.php b/src/Entity/UserNotificationPrefs.php index 60fe95af7f..84ef5b0f37 100644 --- a/src/Entity/UserNotificationPrefs.php +++ b/src/Entity/UserNotificationPrefs.php @@ -200,15 +200,15 @@ class UserNotificationPrefs 'name' => 'user_notification_prefs', 'fields' => [ 'user_id' => ['type' => 'int', 'not null' => true], - 'service_name' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'name on this service'], - 'transport' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'transport (ex xmpp, aim)'], - 'profile_id' => ['type' => 'int', 'default' => null, 'description' => 'If not null, settings are specific only to a given profiles'], - 'posts_by_followed' => ['type' => 'bool', 'not null' => true, 'default' => false, 'description' => 'Notify when a new notice by someone we follow is made'], - 'mention' => ['type' => 'bool', 'not null' => true, 'default' => false, 'description' => 'Notify when mentioned by someone we do not follow'], - 'follow' => ['type' => 'bool', 'not null' => true, 'default' => false, 'description' => 'Notify someone follows us'], - 'favorite' => ['type' => 'bool', 'not null' => true, 'default' => false, 'description' => 'Notify someone favorites a notice by us'], + 'transport' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'transport (ex email. xmpp, aim)'], + 'target_profile_id' => ['type' => 'int', 'default' => null, 'description' => 'If not null, settings are specific only to a given profiles'], + 'notice_by_followed' => ['type' => 'bool', 'not null' => true, 'default' => true, 'description' => 'Notify when a new notice by someone we follow is made'], + 'mention' => ['type' => 'bool', 'not null' => true, 'default' => true, 'description' => 'Notify when mentioned by someone we do not follow'], + 'reply' => ['type' => 'bool', 'not null' => true, 'default' => true, 'description' => 'Notify when someone replies to a notice made by us'], + 'follow' => ['type' => 'bool', 'not null' => true, 'default' => true, 'description' => 'Notify someone follows us'], + 'favorite' => ['type' => 'bool', 'not null' => true, 'default' => true, 'description' => 'Notify someone favorites a notice by us'], 'nudge' => ['type' => 'bool', 'not null' => true, 'default' => false, 'description' => 'Notify someone nudges us'], - 'dm' => ['type' => 'bool', 'not null' => true, 'default' => false, 'description' => 'Notify someone sends us a direct message'], + 'dm' => ['type' => 'bool', 'not null' => true, 'default' => true, 'description' => 'Notify someone sends us a direct message'], 'post_on_status_change' => ['type' => 'bool', 'not null' => true, 'default' => false, 'description' => 'Post a notice when our status in service changes'], 'enable_posting' => ['type' => 'bool', 'default' => true, 'description' => 'Enable posting from this service'], 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'], diff --git a/src/Util/Notification/AbstractTransport.php b/src/Util/Notification/AbstractTransport.php new file mode 100644 index 0000000000..1629852c0e --- /dev/null +++ b/src/Util/Notification/AbstractTransport.php @@ -0,0 +1,99 @@ +. +// }}} + +/** + * Base class for Transports + * + * @package GNUsocial + * @category Util + * + * @author Hugo Sales + * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ + +namespace App\Util\Notification; + +abstract class AbstractTransport +{ + /** + * Get the display name of this transport + */ + abstract public function getName(): string; + + /** + * Get the identifier used in code for this transport + */ + abstract public function getIdentifier(): string; + + /** + * Send a given Notification through this transport + */ + abstract public function send(Notification $n): bool; + + /** + * Get the display help message for one of the Notification-constants type + */ + public function getHelpMessage(int $t): string + { + switch ($t) { + case Notification::NOTICE_BY_FOLLOWED: + return _m('Send me alerts of mentions by those I follow through {name}', ['{name}' => $this->getName()]); + case Notification::MENTION: + return _m('Send me alerts of mentions through {name}', ['{name}' => $this->getName()]); + case Notification::REPLY: + return _m('Send me alerts of replies to my notice through {name}', ['{name}' => $this->getName()]); + case Notification::FOLLOW: + return _m('Send me alerts of new follows through {name}', ['{name}' => $this->getName()]); + case Notification::FAVORITE: + return _m('Send me alerts of new favorites on my notices through {name}', ['{name}' => $this->getName()]); + case Notification::NUDGE: + return _m('Send me alerts when someone calls for my attention through {name}', ['{name}' => $this->getName()]); + case Notification::DM: + return _m('Send me alerts of new direct messages through {name}', ['{name}' => $this->getName()]); + default: + throw new \InvalidArgumentException('Given an invalid Notification constant value'); + } + } + + /** + * Get the display label message for one of the Notification-constants type + */ + public function getLabelMessage(int $t): string + { + switch ($t) { + case Notification::NOTICE_BY_FOLLOWED: + return _m('Notify me of new notices'); + case Notification::MENTION: + return _m('Notify me of mentions'); + case Notification::REPLY: + return _m('Notify me of replies'); + case Notification::FOLLOW: + return _m('Notify me of new follows'); + case Notification::FAVORITE: + return _m('Notify me of new favorites'); + case Notification::NUDGE: + return _m('Notify me when nudged'); + case Notification::DM: + return _m('Notify of new DMs'); + default: + throw new \InvalidArgumentException('Given an invalid Notification constant value'); + } + } +} diff --git a/src/Util/Notification/Notification.php b/src/Util/Notification/Notification.php new file mode 100644 index 0000000000..7b18d83bd1 --- /dev/null +++ b/src/Util/Notification/Notification.php @@ -0,0 +1,60 @@ +. +// }}} + +/** + * Common utility functions + * + * @package GNUsocial + * @category Util + * + * @author Hugo Sales + * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ + +namespace App\Util\Notification; + +use App\Entity\Profile; + +class Notification +{ + public const NOTICE_BY_FOLLOWED = 1; + public const MENTION = 2; + public const REPLY = 3; + public const FOLLOW = 4; + public const FAVORITE = 5; + public const NUDGE = 6; + public const DM = 7; + + /** + * One of the above constants + */ + private int $type; + + /** + * Who caused this notification + */ + private Profile $profile; + + public function __construct(int $type, Profile $profile) + { + $this->tyoe = $type; + $this->profile = $profile; + } +}