diff options
-rw-r--r-- | lib/AppInfo/Application.php | 3 | ||||
-rw-r--r-- | lib/Controller/RoomController.php | 3 | ||||
-rw-r--r-- | lib/Events/BeforeRoomsFetchEvent.php | 29 | ||||
-rw-r--r-- | lib/Listener/NoteToSelf.php | 98 | ||||
-rw-r--r-- | lib/Room.php | 1 | ||||
-rw-r--r-- | lib/Service/RoomService.php | 5 | ||||
-rw-r--r-- | tests/php/Service/RoomServiceTest.php | 2 |
7 files changed, 140 insertions, 1 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index ebc0e8360..efcc6f24f 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -45,6 +45,7 @@ use OCA\Talk\Dashboard\TalkWidget; use OCA\Talk\Deck\DeckPluginLoader; use OCA\Talk\Events\AttendeesAddedEvent; use OCA\Talk\Events\AttendeesRemovedEvent; +use OCA\Talk\Events\BeforeRoomsFetchEvent; use OCA\Talk\Events\BotInstallEvent; use OCA\Talk\Events\BotUninstallEvent; use OCA\Talk\Events\RoomEvent; @@ -67,6 +68,7 @@ use OCA\Talk\Listener\UserDeletedListener; use OCA\Talk\Maps\MapsPluginLoader; use OCA\Talk\Middleware\CanUseTalkMiddleware; use OCA\Talk\Middleware\InjectionMiddleware; +use OCA\Talk\NoteToSelf\Listener as NoteToSelfListener; use OCA\Talk\Notification\Listener as NotificationListener; use OCA\Talk\Notification\Notifier; use OCA\Talk\OCP\TalkBackend; @@ -127,6 +129,7 @@ class Application extends App implements IBootstrap { $context->registerEventListener(AddContentSecurityPolicyEvent::class, CSPListener::class); $context->registerEventListener(AddFeaturePolicyEvent::class, FeaturePolicyListener::class); + $context->registerEventListener(BeforeRoomsFetchEvent::class, NoteToSelfListener::class); $context->registerEventListener(BotInstallEvent::class, BotListener::class); $context->registerEventListener(BotUninstallEvent::class, BotListener::class); $context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class); diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index 9c3ddaa82..cb419043c 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -29,6 +29,7 @@ namespace OCA\Talk\Controller; use InvalidArgumentException; use OCA\Talk\Config; +use OCA\Talk\Events\BeforeRoomsFetchEvent; use OCA\Talk\Events\UserEvent; use OCA\Talk\Exceptions\ForbiddenException; use OCA\Talk\Exceptions\InvalidPasswordException; @@ -155,6 +156,8 @@ class RoomController extends AEnvironmentAwareController { $event = new UserEvent($this->userId); $this->dispatcher->dispatch(self::EVENT_BEFORE_ROOMS_GET, $event); + $event = new BeforeRoomsFetchEvent($this->userId); + $this->dispatcher->dispatchTyped($event); if ($noStatusUpdate === 0) { $isMobileApp = $this->request->isUserAgent([ diff --git a/lib/Events/BeforeRoomsFetchEvent.php b/lib/Events/BeforeRoomsFetchEvent.php new file mode 100644 index 000000000..e36be415f --- /dev/null +++ b/lib/Events/BeforeRoomsFetchEvent.php @@ -0,0 +1,29 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * 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/>. + * + */ + +namespace OCA\Talk\Events; + +class BeforeRoomsFetchEvent extends UserEvent { +} diff --git a/lib/Listener/NoteToSelf.php b/lib/Listener/NoteToSelf.php new file mode 100644 index 000000000..faedefc89 --- /dev/null +++ b/lib/Listener/NoteToSelf.php @@ -0,0 +1,98 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * 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/>. + * + */ + +namespace OCA\Talk\NoteToSelf; + +use OCA\Talk\Events\BeforeRoomsFetchEvent; +use OCA\Talk\Model\Attendee; +use OCA\Talk\Room; +use OCA\Talk\Service\AvatarService; +use OCA\Talk\Service\ParticipantService; +use OCA\Talk\Service\RoomService; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IUser; +use OCP\IUserManager; + +/** + * @template-implements IEventListener<Event> + */ +class NoteToSelf implements IEventListener { + public function __construct( + protected IConfig $config, + protected IUserManager $userManager, + protected RoomService $roomService, + protected AvatarService $avatarService, + protected ParticipantService $participantService, + protected IL10N $l, + ) { + } + + public function handle(Event $event): void { + if ($event instanceof BeforeRoomsFetchEvent) { + $this->createNoteToSelf($event); + } + } + + public function createNoteToSelf(BeforeRoomsFetchEvent $event): void { + $userId = $event->getUserId(); + $noteToSelf = $this->getNoteToSelfForUser($userId); + + if ($noteToSelf === 0) { + $currentUser = $this->userManager->get($userId); + if (!$currentUser instanceof IUser) { + return; + } + + $room = $this->roomService->createConversation( + Room::TYPE_NOTE_TO_SELF, + $this->l->t('Note to self'), + $currentUser, + 'note_to_self', + $userId + ); + $this->config->setUserValue($userId, 'spreed', 'note_to_self', (string) $room->getId()); + + $this->roomService->setDescription( + $room, + $this->l->t('A place for your private notes, thoughts and ideas'), + ); + + $this->avatarService->setAvatarFromEmoji($room, '📝', '0082c9'); + + $participant = $this->participantService->getParticipantByActor( + $room, + Attendee::ACTOR_USERS, + $userId + ); + + $this->participantService->updateFavoriteStatus($participant, true); + } + } + + public function getNoteToSelfForUser(string $userId): int { + return (int) $this->config->getUserValue($userId, 'spreed', 'note_to_self', '0'); + } +} diff --git a/lib/Room.php b/lib/Room.php index 0edc91703..f18714757 100644 --- a/lib/Room.php +++ b/lib/Room.php @@ -55,6 +55,7 @@ class Room { public const TYPE_PUBLIC = 3; public const TYPE_CHANGELOG = 4; public const TYPE_ONE_TO_ONE_FORMER = 5; + public const TYPE_NOTE_TO_SELF = 6; public const RECORDING_NONE = 0; public const RECORDING_VIDEO = 1; diff --git a/lib/Service/RoomService.php b/lib/Service/RoomService.php index f8cf00efc..c1e1ba5c9 100644 --- a/lib/Service/RoomService.php +++ b/lib/Service/RoomService.php @@ -128,6 +128,7 @@ class RoomService { Room::TYPE_GROUP, Room::TYPE_PUBLIC, Room::TYPE_CHANGELOG, + Room::TYPE_NOTE_TO_SELF, ], true)) { throw new InvalidArgumentException('type'); } @@ -411,6 +412,10 @@ class RoomService { return false; } + if ($room->getType() === Room::TYPE_NOTE_TO_SELF) { + return false; + } + if (!in_array($newType, [Room::TYPE_GROUP, Room::TYPE_PUBLIC, Room::TYPE_ONE_TO_ONE_FORMER], true)) { return false; } diff --git a/tests/php/Service/RoomServiceTest.php b/tests/php/Service/RoomServiceTest.php index 387428d90..4786d854e 100644 --- a/tests/php/Service/RoomServiceTest.php +++ b/tests/php/Service/RoomServiceTest.php @@ -222,7 +222,7 @@ class RoomServiceTest extends TestCase { [Room::TYPE_ONE_TO_ONE], [Room::TYPE_UNKNOWN], [Room::TYPE_ONE_TO_ONE_FORMER], - [6], + [7], ]; } |