Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
use OCA\Spreed\Signaling\Listener as SignalingListener;
use OCA\Spreed\TalkSession;
use OCP\AppFramework\App;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Collaboration\Resources\IManager as IResourceManager;
Expand Down Expand Up @@ -129,16 +128,7 @@ public function register(): void {

protected function registerNotifier(IServerContainer $server): void {
$manager = $server->getNotificationManager();
$manager->registerNotifier(function() use ($server) {
return $server->query(Notifier::class);
}, function() use ($server) {
$l = $server->getL10N('spreed');

return [
'id' => 'spreed',
'name' => $l->t('Talk'),
];
});
$manager->registerNotifierService(Notifier::class);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Register with the class name, instead of a closure

}

protected function registerCollaborationResourceProvider(IServerContainer $server): void {
Expand Down
149 changes: 111 additions & 38 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\IAction;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
Expand Down Expand Up @@ -90,23 +92,42 @@ public function __construct(IFactory $lFactory,
$this->definitions = $definitions;
}

/**
* Identifier of the notifier, only use [a-z0-9_]
*
* @return string
* @since 17.0.0
*/
public function getID(): string {
return 'talk';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New method in 17, replacing the id return of the info closure from the old registration way

}

/**
* Human readable name describing the notifier
*
* @return string
* @since 17.0.0
*/
public function getName(): string {
return $this->lFactory->get('spreed')->t('Talk');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New method in 17, replacing the name return of the info closure from the old registration way

}

/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
* @since 9.0.0
*/
public function prepare(INotification $notification, $languageCode): INotification {
public function prepare(INotification $notification, string $languageCode): INotification {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type hint for $languageCode is new, and the : INotification return type is enforced now too.

if ($notification->getApp() !== 'spreed') {
throw new \InvalidArgumentException('Incorrect app');
}

$userId = $notification->getUser();
$user = $this->userManager->get($userId);
if (!$user instanceof IUser || $this->config->isDisabledForUser($user)) {
$this->notificationManager->markProcessed($notification);
throw new \InvalidArgumentException('User can not use Talk');
throw new AlreadyProcessedException();
}

$l = $this->lFactory->get('spreed', $languageCode);
Expand All @@ -119,17 +140,15 @@ public function prepare(INotification $notification, $languageCode): INotificati
$room = $this->manager->getRoomById((int) $notification->getObjectId());
} catch (RoomNotFoundException $e) {
// Room does not exist
$this->notificationManager->markProcessed($notification);
throw new \InvalidArgumentException('Invalid room');
throw new AlreadyProcessedException();
}
}

try {
$participant = $room->getParticipant($userId);
} catch (ParticipantNotFoundException $e) {
// Room does not exist
$this->notificationManager->markProcessed($notification);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of marking as processed in the prepare method, throw a AlreadyProcessedException which will correctly delete the notification on all devices.

throw new \InvalidArgumentException('User is not part of the room anymore');
throw new AlreadyProcessedException();
}

$notification
Expand Down Expand Up @@ -195,20 +214,20 @@ protected function parseChatMessage(INotification $notification, Room $room, Par

$messageParameters = $notification->getMessageParameters();
if (!isset($messageParameters['commentId'])) {
throw new \InvalidArgumentException('Unknown comment');
throw new AlreadyProcessedException();
}

try {
$comment = $this->commentManager->get($messageParameters['commentId']);
} catch (NotFoundException $e) {
throw new \InvalidArgumentException('Unknown comment');
throw new AlreadyProcessedException();
}

$message = $this->messageParser->createMessage($room, $participant, $comment, $l);
$this->messageParser->parseMessage($message);

if (!$message->getVisibility()) {
throw new \InvalidArgumentException('Invisible comment');
throw new AlreadyProcessedException();
}

$placeholders = $replacements = [];
Expand Down Expand Up @@ -252,6 +271,7 @@ protected function parseChatMessage(INotification $notification, Room $room, Par
$subject = $l->t('A guest mentioned you in conversation {call}');
}
}
$notification = $this->addActionButton($notification, $l->t('View chat'), false);

if ($richSubjectParameters['user'] === null) {
unset($richSubjectParameters['user']);
Expand Down Expand Up @@ -293,6 +313,7 @@ protected function getRoomType(Room $room): string {
* @param IL10N $l
* @return INotification
* @throws \InvalidArgumentException
* @throws AlreadyProcessedException
*/
protected function parseInvitation(INotification $notification, Room $room, IL10N $l): INotification {
if ($notification->getObjectType() !== 'room') {
Expand All @@ -304,17 +325,22 @@ protected function parseInvitation(INotification $notification, Room $room, IL10

$user = $this->userManager->get($uid);
if (!$user instanceof IUser) {
throw new \InvalidArgumentException('Calling user does not exist anymore');
throw new AlreadyProcessedException();
}

$roomName = $room->getDisplayName($notification->getUser());
if ($room->getType() === Room::ONE_TO_ONE_CALL) {
$subject = $l->t('{user} invited you to a private conversation');
if ($room->hasSessionsInCall()) {
$notification = $this->addActionButton($notification, $l->t('Join call'));
} else {
$notification = $this->addActionButton($notification, $l->t('View chat'), false);
}

$notification
->setParsedSubject(
$l->t('%s invited you to a private conversation', [$user->getDisplayName()])
)
->setParsedSubject(str_replace('{user}', $user->getDisplayName(), $subject))
->setRichSubject(
$l->t('{user} invited you to a private conversation'), [
$subject, [
'user' => [
'type' => 'user',
'id' => $uid,
Expand All @@ -330,12 +356,17 @@ protected function parseInvitation(INotification $notification, Room $room, IL10
);

} else if (\in_array($room->getType(), [Room::GROUP_CALL, Room::PUBLIC_CALL], true)) {
$subject = $l->t('{user} invited you to a group conversation: {call}');
if ($room->hasSessionsInCall()) {
$notification = $this->addActionButton($notification, $l->t('Join call'));
} else {
$notification = $this->addActionButton($notification, $l->t('View chat'), false);
}

$notification
->setParsedSubject(
$l->t('%s invited you to a group conversation: %s', [$user->getDisplayName(), $roomName])
)
->setParsedSubject(str_replace(['{user}', '{call}'], [$user->getDisplayName(), $roomName], $subject))
->setRichSubject(
$l->t('{user} invited you to a group conversation: {call}'), [
$subject, [
'user' => [
'type' => 'user',
'id' => $uid,
Expand All @@ -350,7 +381,7 @@ protected function parseInvitation(INotification $notification, Room $room, IL10
]
);
} else {
throw new \InvalidArgumentException('Unknown room type');
throw new AlreadyProcessedException();
}

return $notification;
Expand All @@ -362,6 +393,7 @@ protected function parseInvitation(INotification $notification, Room $room, IL10
* @param IL10N $l
* @return INotification
* @throws \InvalidArgumentException
* @throws AlreadyProcessedException
*/
protected function parseCall(INotification $notification, Room $room, IL10N $l): INotification {
if ($notification->getObjectType() !== 'call') {
Expand All @@ -374,12 +406,18 @@ protected function parseCall(INotification $notification, Room $room, IL10N $l):
$calleeId = $parameters['callee'];
$user = $this->userManager->get($calleeId);
if ($user instanceof IUser) {
if ($room->hasSessionsInCall()) {
$notification = $this->addActionButton($notification, $l->t('Answer call'));
$subject = $l->t('{user} wants to talk with you');
} else {
$notification = $this->addActionButton($notification, $l->t('Call back'));
$subject = $l->t('You missed a call from {user}');
}

$notification
->setParsedSubject(
str_replace('{user}', $user->getDisplayName(), $l->t('{user} wants to talk with you'))
)
->setParsedSubject(str_replace('{user}', $user->getDisplayName(), $subject))
->setRichSubject(
$l->t('{user} wants to talk with you'), [
$subject, [
'user' => [
'type' => 'user',
'id' => $calleeId,
Expand All @@ -394,16 +432,22 @@ protected function parseCall(INotification $notification, Room $room, IL10N $l):
]
);
} else {
throw new \InvalidArgumentException('Calling user does not exist anymore');
throw new AlreadyProcessedException();
}

} else if (\in_array($room->getType(), [Room::GROUP_CALL, Room::PUBLIC_CALL], true)) {
if ($room->hasSessionsInCall()) {
$notification = $this->addActionButton($notification, $l->t('Join call'));
$subject = $l->t('A group call has started in {call}');
} else {
$notification = $this->addActionButton($notification, $l->t('View chat'), false);
$subject = $l->t('You missed a group call in {call}');
}

$notification
->setParsedSubject(
str_replace('{call}', $roomName, $l->t('A group call has started in {call}'))
)
->setParsedSubject(str_replace('{call}', $roomName, $subject))
->setRichSubject(
$l->t('A group call has started in {call}'), [
$subject, [
'call' => [
'type' => 'call',
'id' => $room->getId(),
Expand All @@ -414,7 +458,7 @@ protected function parseCall(INotification $notification, Room $room, IL10N $l):
);

} else {
throw new \InvalidArgumentException('Unknown room type');
throw new AlreadyProcessedException();
}

return $notification;
Expand All @@ -426,6 +470,7 @@ protected function parseCall(INotification $notification, Room $room, IL10N $l):
* @param IL10N $l
* @return INotification
* @throws \InvalidArgumentException
* @throws AlreadyProcessedException
*/
protected function parsePasswordRequest(INotification $notification, Room $room, IL10N $l): INotification {
if ($notification->getObjectType() !== 'call') {
Expand All @@ -435,7 +480,7 @@ protected function parsePasswordRequest(INotification $notification, Room $room,
try {
$share = $this->shareManager->getShareByToken($room->getObjectId());
} catch (ShareNotFound $e) {
throw new \InvalidArgumentException('Unknown share');
throw new AlreadyProcessedException();
}

try {
Expand All @@ -445,17 +490,27 @@ protected function parsePasswordRequest(INotification $notification, Room $room,
'name' => $share->getNode()->getName(),
];
} catch (\OCP\Files\NotFoundException $e) {
throw new \InvalidArgumentException('Unknown file');
throw new AlreadyProcessedException();
}

$callIsActive = $room->hasSessionsInCall();
if ($callIsActive) {
$notification = $this->addActionButton($notification, $l->t('Answer call'));
} else {
$notification = $this->addActionButton($notification, $l->t('Call back'));
}

if ($share->getShareType() === Share::SHARE_TYPE_EMAIL) {
$sharedWith = $share->getSharedWith();
if ($callIsActive) {
$subject = $l->t('{email} is requesting the password to access {file}');
} else {
$subject = $l->t('{email} tried to requested the password to access {file}');
}

$notification
->setParsedSubject(str_replace(['{email}', '{file}'], [$sharedWith, $file['name']], $l->t('{email} requested the password to access {file}')))
->setRichSubject(
$l->t('{email} requested the password to access {file}'),
[
->setParsedSubject(str_replace(['{email}', '{file}'], [$sharedWith, $file['name']], $subject))
->setRichSubject($subject, [
'email' => [
'type' => 'email',
'id' => $sharedWith,
Expand All @@ -465,11 +520,29 @@ protected function parsePasswordRequest(INotification $notification, Room $room,
]
);
} else {
if ($callIsActive) {
$subject = $l->t('Someone is requesting the password to access {file}');
} else {
$subject = $l->t('Someone tried to requested the password to access {file}');
}

$notification
->setParsedSubject(str_replace('{file}', $file['name'], $l->t('Someone requested the password to access {file}')))
->setRichSubject($l->t('Someone requested the password to access {file}'), ['file' => $file]);
->setParsedSubject(str_replace('{file}', $file['name'], $subject))
->setRichSubject($subject, ['file' => $file]);
}

return $notification;
}

protected function addActionButton(INotification $notification, string $label, bool $primary = true): INotification {
$action = $notification->createAction();
$action->setLabel($label)
->setParsedLabel($label)
->setLink($notification->getLink(), IAction::TYPE_WEB)
->setPrimary($primary);

$notification->addParsedAction($action);

return $notification;
}
}
15 changes: 10 additions & 5 deletions tests/php/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use OCP\RichObjectStrings\Definitions;
Expand Down Expand Up @@ -707,11 +708,11 @@ public function testPrepareChatMessage(bool $isMention, int $roomType, array $su
public function dataPrepareThrows() {
return [
['Incorrect app', 'invalid-app', null, null, null, null, null],
['User can not use Talk', 'spreed', true, null, null, null, null],
['Invalid room', 'spreed', false, false, null, null, null],
'User can not use Talk' => [AlreadyProcessedException::class, 'spreed', true, null, null, null, null],
'Invalid room' => [AlreadyProcessedException::class, 'spreed', false, false, null, null, null],
['Unknown subject', 'spreed', false, true, 'invalid-subject', null, null],
['Unknown object type', 'spreed', false, true, 'invitation', null, 'invalid-object-type'],
['Calling user does not exist anymore', 'spreed', false, true, 'invitation', ['admin'], 'room'],
'Calling user does not exist anymore' => [AlreadyProcessedException::class, 'spreed', false, true, 'invitation', ['admin'], 'room'],
['Unknown object type', 'spreed', false, true, 'mention', null, 'invalid-object-type'],
];
}
Expand Down Expand Up @@ -806,8 +807,12 @@ public function testPrepareThrows($message, $app, $isDisabledForUser, $validRoom
->method('getObjectType')
->willReturn($objectType);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($message);
if ($message === AlreadyProcessedException::class) {
$this->expectException(AlreadyProcessedException::class);
} else {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($message);
}
$this->notifier->prepare($n, 'de');
}
}