Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! Need to be combined with setLastMessage and delayed
  • Loading branch information
nickvergessen committed Feb 26, 2022
commit d32e490b2966ebd8a3310e19beeaf5516d162f33
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ protected function registerNavigationLink(IServerContainer $server): void {

protected function registerRoomActivityHooks(IEventDispatcher $dispatcher): void {
$listener = function (ChatEvent $event): void {
if ($event->shouldSkipLastActivityUpdate()) {
return;
}

$room = $event->getRoom();
/** @var ITimeFactory $timeFactory */
$timeFactory = $this->getContainer()->query(ITimeFactory::class);
Expand Down
13 changes: 8 additions & 5 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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 +153,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
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;
}
}
24 changes: 22 additions & 2 deletions lib/Service/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Talk\Service;

use Aws\IoT1ClickDevicesService\IoT1ClickDevicesServiceClient;
use OCA\Circles\CirclesManager;
use OCA\Circles\Model\Circle;
use OCA\Circles\Model\Member;
Expand Down Expand Up @@ -59,6 +60,7 @@
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroup;
Expand Down Expand Up @@ -94,6 +96,8 @@ class ParticipantService {
private $notifications;
/** @var ITimeFactory */
private $timeFactory;
/** @var ICacheFactory */
private $cacheFactory;

public function __construct(IConfig $serverConfig,
Config $talkConfig,
Expand All @@ -107,7 +111,8 @@ public function __construct(IConfig $serverConfig,
IGroupManager $groupManager,
MembershipService $membershipService,
Notifications $notifications,
ITimeFactory $timeFactory) {
ITimeFactory $timeFactory,
ICacheFactory $cacheFactory) {
$this->serverConfig = $serverConfig;
$this->talkConfig = $talkConfig;
$this->attendeeMapper = $attendeeMapper;
Expand All @@ -120,6 +125,7 @@ public function __construct(IConfig $serverConfig,
$this->groupManager = $groupManager;
$this->membershipService = $membershipService;
$this->timeFactory = $timeFactory;
$this->cacheFactory = $cacheFactory;
$this->notifications = $notifications;
}

Expand Down Expand Up @@ -367,7 +373,7 @@ public function addUsers(Room $room, array $participants, ?IUser $addedBy = null
if (empty($participants)) {
return;
}
$event = new AddParticipantsEvent($room, $participants);
$event = new AddParticipantsEvent($room, $participants, true);
$this->dispatcher->dispatch(Room::EVENT_BEFORE_USERS_ADD, $event);

$lastMessage = 0;
Expand Down Expand Up @@ -426,9 +432,23 @@ public function addUsers(Room $room, array $participants, ?IUser $addedBy = null
$this->dispatcher->dispatchTyped($attendeeEvent);

$this->dispatcher->dispatch(Room::EVENT_AFTER_USERS_ADD, $event);

$lastMessage = $event->getLastMessage();
if ($lastMessage instanceof IComment) {
$this->updateRoomLastMessage($room, $lastMessage);
}
}
}

protected function updateRoomLastMessage(Room $room, IComment $message): void {
$room->setLastMessage($message);
$room->setLastActivity($message->getCreationDateTime());
$lastMessageCache = $this->cacheFactory->createDistributed('talk/lastmsgid');
$lastMessageCache->remove($room->getToken());
$unreadCountCache = $this->cacheFactory->createDistributed('talk/unreadcount');
$unreadCountCache->clear($room->getId() . '-');
}

public function getHighestPermissionAttendee(Room $room): ?Attendee {
try {
$roomOwners = $this->attendeeMapper->getActorsByParticipantTypes($room->getId(), [Participant::OWNER]);
Expand Down