Skip to content
Closed
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
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public function generateInvitationActivity(Room $room, array $participants): voi
return;
}

// We know the new participant is in the room,
// so skip loading them just to make sure they can read it.
// Must be overwritten later on for one-to-one chats.
$roomName = $room->getDisplayName($actorId);

foreach ($participants as $participant) {
if ($participant['actorType'] !== Attendee::ACTOR_USERS) {
// No user => no activity
Expand All @@ -244,7 +249,10 @@ public function generateInvitationActivity(Room $room, array $participants): voi
}

try {
$roomName = $room->getDisplayName($participant['actorId']);
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
// Overwrite the room name with the other participant
$roomName = $room->getDisplayName($participant['actorId']);
}
$event
->setObject('room', $room->getId(), $roomName)
->setSubject('invitation', [
Expand Down
15 changes: 0 additions & 15 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
use OCA\Talk\Deck\DeckPluginLoader;
use OCA\Talk\Events\AttendeesAddedEvent;
use OCA\Talk\Events\AttendeesRemovedEvent;
use OCA\Talk\Events\ChatEvent;
use OCA\Talk\Events\RoomEvent;
use OCA\Talk\Federation\CloudFederationProviderTalk;
use OCA\Talk\Files\Listener as FilesListener;
Expand Down Expand Up @@ -82,7 +81,6 @@
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Collaboration\Resources\IProviderManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudFederationProvider;
Expand Down Expand Up @@ -172,7 +170,6 @@ public function boot(IBootContext $context): void {
ShareListener::register($dispatcher);
StatusListener::register($dispatcher);

$this->registerRoomActivityHooks($dispatcher);
$this->registerChatHooks($dispatcher);
$context->injectFn(\Closure::fromCallable([$this, 'registerCloudFederationProviderManager']));
}
Expand Down Expand Up @@ -215,18 +212,6 @@ protected function registerNavigationLink(IServerContainer $server): void {
});
}

protected function registerRoomActivityHooks(IEventDispatcher $dispatcher): void {
$listener = function (ChatEvent $event): void {
$room = $event->getRoom();
/** @var ITimeFactory $timeFactory */
$timeFactory = $this->getContainer()->query(ITimeFactory::class);
$room->setLastActivity($timeFactory->getDateTime());
};

$dispatcher->addListener(ChatManager::EVENT_AFTER_MESSAGE_SEND, $listener);
$dispatcher->addListener(ChatManager::EVENT_AFTER_SYSTEM_MESSAGE_SEND, $listener);
}

protected function registerChatHooks(IEventDispatcher $dispatcher): void {
$listener = function (RoomEvent $event): void {
/** @var ChatManager $chatManager */
Expand Down
16 changes: 11 additions & 5 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
class ChatManager {
public const EVENT_BEFORE_SYSTEM_MESSAGE_SEND = self::class . '::preSendSystemMessage';
public const EVENT_AFTER_SYSTEM_MESSAGE_SEND = self::class . '::postSendSystemMessage';
public const EVENT_AFTER_MULTIPLE_SYSTEM_MESSAGE_SEND = self::class . '::postSendMultipleSystemMessage';
public const EVENT_BEFORE_MESSAGE_SEND = self::class . '::preSendMessage';
public const EVENT_AFTER_MESSAGE_SEND = self::class . '::postSendMessage';

Expand Down Expand Up @@ -128,7 +129,8 @@ public function addSystemMessage(
\DateTime $creationDateTime,
bool $sendNotifications,
?string $referenceId = null,
?int $parentId = null
?int $parentId = null,
bool $shouldSkipLastMessageUpdate = false
): IComment {
$comment = $this->commentsManager->create($actorType, $actorId, 'chat', (string) $chat->getId());
$comment->setMessage($message, self::MAX_CHAT_LENGTH);
Expand All @@ -152,14 +154,16 @@ public function addSystemMessage(
$comment->setVerb('system');
}

$event = new ChatEvent($chat, $comment);
$event = new ChatEvent($chat, $comment, $shouldSkipLastMessageUpdate);
$this->dispatcher->dispatch(self::EVENT_BEFORE_SYSTEM_MESSAGE_SEND, $event);
try {
$this->commentsManager->save($comment);

// Update last_message
$chat->setLastMessage($comment);
$this->unreadCountCache->clear($chat->getId() . '-');
if (!$shouldSkipLastMessageUpdate) {
// Update last_message
$chat->setLastMessage($comment);
$this->unreadCountCache->clear($chat->getId() . '-');
}

if ($sendNotifications) {
$this->notifier->notifyOtherParticipant($chat, $comment, []);
Expand Down Expand Up @@ -245,6 +249,8 @@ public function sendMessage(Room $chat, Participant $participant, string $actorT
if ($comment->getActorType() !== 'bots' || $comment->getActorId() === 'changelog') {
$chat->setLastMessage($comment);
$this->unreadCountCache->clear($chat->getId() . '-');
} else {
$chat->setLastActivity($comment->getCreationDateTime());
}

$alreadyNotifiedUsers = [];
Expand Down
42 changes: 22 additions & 20 deletions lib/Chat/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ public function notifyMentionedUsers(Room $chat, IComment $comment, array $alrea
}
$notification = $this->createNotification($chat, $comment, 'mention');
$shouldFlush = $this->notificationManager->defer();

foreach ($usersToNotify as $mentionedUser) {
if ($this->shouldMentionedUserBeNotified($mentionedUser['id'], $comment)) {
if ($this->shouldMentionedUserBeNotified($mentionedUser['id'], $comment, $chat, $mentionedUser['attendee'] ?? null)) {
$notification->setUser($mentionedUser['id']);
$this->notificationManager->notify($notification);
$alreadyNotifiedUsers[] = $mentionedUser;
Expand Down Expand Up @@ -170,18 +171,19 @@ private function addMentionAllToList(Room $chat, array $list): array {
return $usersToNotify;
}

$chatParticipants = $this->participantService->getActorsByType($chat, Attendee::ACTOR_USERS);
foreach ($chatParticipants as $participant) {
$alreadyAddedToNotify = array_filter($list, static function ($user) use ($participant): bool {
return $user['id'] === $participant->getActorId();
$attendees = $this->participantService->getActorsByType($chat, Attendee::ACTOR_USERS);
foreach ($attendees as $attendee) {
$alreadyAddedToNotify = array_filter($list, static function ($user) use ($attendee): bool {
return $user['id'] === $attendee->getActorId();
});
if (!empty($alreadyAddedToNotify)) {
continue;
}

$usersToNotify[] = [
'id' => $participant->getActorId(),
'type' => $participant->getActorType()
'id' => $attendee->getActorId(),
'type' => $attendee->getActorType(),
'attendee' => $attendee,
];
}

Expand Down Expand Up @@ -210,7 +212,7 @@ public function notifyReplyToAuthor(Room $chat, IComment $comment, IComment $rep
return [];
}

if (!$this->shouldMentionedUserBeNotified($replyTo->getActorId(), $comment)) {
if (!$this->shouldMentionedUserBeNotified($replyTo->getActorId(), $comment, $chat)) {
return [];
}

Expand Down Expand Up @@ -426,27 +428,27 @@ protected function getDefaultGroupNotification(): int {
*
* @param string $userId
* @param IComment $comment
* @param Room $room
* @param Attendee|null $attendee
* @return bool
*/
protected function shouldMentionedUserBeNotified(string $userId, IComment $comment): bool {
protected function shouldMentionedUserBeNotified(string $userId, IComment $comment, Room $room, ?Attendee $attendee = null): bool {
if ($comment->getActorType() === Attendee::ACTOR_USERS && $userId === $comment->getActorId()) {
// Do not notify the user if they mentioned themselves
return false;
}

if (!$this->userManager->userExists($userId)) {
return false;
}

try {
$room = $this->manager->getRoomById((int) $comment->getObjectId());
} catch (RoomNotFoundException $e) {
return false;
}
if (!$attendee instanceof Attendee) {
if (!$this->userManager->userExists($userId)) {
return false;
}

try {
$participant = $room->getParticipant($userId, false);
$notificationLevel = $participant->getAttendee()->getNotificationLevel();
$participant = $room->getParticipant($userId, false);
$attendee = $participant->getAttendee();
}

$notificationLevel = $attendee->getNotificationLevel();
if ($notificationLevel === Participant::NOTIFY_DEFAULT) {
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
$notificationLevel = Participant::NOTIFY_ALWAYS;
Expand Down
19 changes: 15 additions & 4 deletions lib/Chat/SystemMessage/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCA\Talk\TalkSession;
use OCA\Talk\Webinary;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\IComment;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
Expand Down Expand Up @@ -263,7 +264,15 @@ public static function register(IEventDispatcher $dispatcher): void {
|| $listener->getUserId() !== $participant['actorId']
// - has joined a listable room on their own
|| $participantType === Participant::USER) {
$listener->sendSystemMessage($room, 'user_added', ['user' => $participant['actorId']]);
$comment = $listener->sendSystemMessage(
$room,
'user_added',
['user' => $participant['actorId']],
null,
$event->shouldSkipLastMessageUpdate()
);

$event->setLastMessage($comment);
}
}
});
Expand Down Expand Up @@ -379,7 +388,7 @@ protected function attendeesRemovedEvent(AttendeesRemovedEvent $event): void {
}
}

protected function sendSystemMessage(Room $room, string $message, array $parameters = [], Participant $participant = null): void {
protected function sendSystemMessage(Room $room, string $message, array $parameters = [], Participant $participant = null, bool $shouldSkipLastMessageUpdate = false): IComment {
if ($participant instanceof Participant) {
$actorType = $participant->getAttendee()->getActorType();
$actorId = $participant->getAttendee()->getActorId();
Expand Down Expand Up @@ -408,11 +417,13 @@ protected function sendSystemMessage(Room $room, string $message, array $paramet
$referenceId = (string) $referenceId;
}

$this->chatManager->addSystemMessage(
return $this->chatManager->addSystemMessage(
$room, $actorType, $actorId,
json_encode(['message' => $message, 'parameters' => $parameters]),
$this->timeFactory->getDateTime(), $message === 'file_shared',
$referenceId
$referenceId,
null,
$shouldSkipLastMessageUpdate
);
}

Expand Down
22 changes: 21 additions & 1 deletion lib/Events/AddParticipantsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@
namespace OCA\Talk\Events;

use OCA\Talk\Room;
use OCP\Comments\IComment;

class AddParticipantsEvent extends RoomEvent {

/** @var array */
protected $participants;

/** @var bool */
protected $skipLastMessageUpdate;

/** @var IComment|null */
protected $lastMessage;

public function __construct(Room $room,
array $participants) {
array $participants,
bool $skipLastMessageUpdate = false) {
parent::__construct($room);
$this->participants = $participants;
$this->skipLastMessageUpdate = $skipLastMessageUpdate;
}

/**
Expand All @@ -43,4 +51,16 @@ public function __construct(Room $room,
public function getParticipants(): array {
return $this->participants;
}

public function shouldSkipLastMessageUpdate(): bool {
return $this->skipLastMessageUpdate;
}

public function setLastMessage(IComment $lastMessage): void {
$this->lastMessage = $lastMessage;
}

public function getLastMessage(): ?IComment {
return $this->lastMessage;
}
}
11 changes: 10 additions & 1 deletion lib/Events/ChatEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ class ChatEvent extends RoomEvent {
/** @var IComment */
protected $comment;

/** @var bool */
protected $skipLastActivityUpdate;

public function __construct(Room $room, IComment $comment) {
public function __construct(Room $room,
IComment $comment,
bool $skipLastActivityUpdate = false) {
parent::__construct($room);
$this->comment = $comment;
$this->skipLastActivityUpdate = $skipLastActivityUpdate;
}

public function getComment(): IComment {
return $this->comment;
}

public function shouldSkipLastActivityUpdate(): bool {
return $this->skipLastActivityUpdate;
}
}
18 changes: 18 additions & 0 deletions lib/Events/EndCallForEveryoneEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class EndCallForEveryoneEvent extends ModifyRoomEvent {

/** @var string[] */
protected $sessionIds;
/** @var string[] */
protected $userIds;

public function __construct(Room $room,
?Participant $actor = null) {
Expand All @@ -51,4 +53,20 @@ public function setSessionIds(array $sessionIds): void {
public function getSessionIds(): array {
return $this->sessionIds;
}

/**
* @param string[] $userIds
* @return void
*/
public function setUserIds(array $userIds): void {
$this->userIds = $userIds;
}

/**
* Only available in the after-event
* @return string[]
*/
public function getUserIds(): array {
return $this->userIds;
}
}
Loading