Skip to content

Commit

Permalink
fix(chat): Better handling of captioned messages in federated convers…
Browse files Browse the repository at this point in the history
…ations

Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed May 3, 2024
1 parent 27329f6 commit f35c638
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
15 changes: 12 additions & 3 deletions lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ protected function parseMessage(Message $chatMessage): void {
}
} elseif ($message === 'file_shared') {
try {
$parsedParameters['file'] = $this->getFileFromShare($participant, $parameters['share']);
$parsedParameters['file'] = $this->getFileFromShare($room, $participant, $parameters['share']);
$parsedMessage = '{file}';
$metaData = $parameters['metaData'] ?? [];
if (isset($metaData['messageType'])) {
Expand All @@ -509,6 +509,13 @@ protected function parseMessage(Message $chatMessage): void {
$parsedMessage = $this->l->t('{actor} shared a file which is no longer available');
if ($currentUserIsActor) {
$parsedMessage = $this->l->t('You shared a file which is no longer available');
} elseif ($currentActorType === Attendee::ACTOR_FEDERATED_USERS) {
$parsedMessage = $this->l->t('File shares are currently not supported in federated conversations');
$metaData = $parameters['metaData'] ?? [];
if (isset($metaData['caption']) && $metaData['caption'] !== '') {
$parsedMessage = '*' . $parsedMessage . "*\n\n" . $metaData['caption'];
$chatMessage->setMessageType(ChatManager::VERB_MESSAGE);
}
}
}
} elseif ($message === 'object_shared') {
Expand Down Expand Up @@ -724,10 +731,10 @@ protected function parseDeletedMessage(Message $chatMessage): void {
* @throws NotFoundException
* @throws ShareNotFound
*/
protected function getFileFromShare(?Participant $participant, string $shareId): array {
protected function getFileFromShare(Room $room, ?Participant $participant, string $shareId): array {
$share = $this->shareProvider->getShareById((int) $shareId);

if ($participant && !$participant->isGuest()) {
if ($participant && $participant->getAttendee()->getActorType() === Attendee::ACTOR_USERS) {
if ($share->getShareOwner() !== $participant->getAttendee()->getActorId()) {
$userFolder = $this->rootFolder->getUserFolder($participant->getAttendee()->getActorId());
if ($userFolder instanceof Node) {
Expand Down Expand Up @@ -767,6 +774,8 @@ protected function getFileFromShare(?Participant $participant, string $shareId):
$url = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', [
'fileid' => $node->getId(),
]);
} elseif ($participant && $room->getType() !== Room::TYPE_PUBLIC && $participant->getAttendee()->getActorType() === Attendee::ACTOR_FEDERATED_USERS) {
throw new ShareNotFound();
} else {
$node = $share->getNode();
$name = $node->getName();
Expand Down
38 changes: 19 additions & 19 deletions tests/php/Chat/Parser/SystemMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,12 @@ public function testParseMessage(string $message, array $parameters, ?string $re
if (is_subclass_of($parameters['share'], \Exception::class)) {
$parser->expects($this->once())
->method('getFileFromShare')
->with($participant, $parameters['share'])
->with($room, $participant, $parameters['share'])
->willThrowException(new $parameters['share']());
} else {
$parser->expects($this->once())
->method('getFileFromShare')
->with($participant, $parameters['share'])
->with($room, $participant, $parameters['share'])
->willReturn(['id' => 'file-from-share']);
}
} else {
Expand Down Expand Up @@ -588,6 +588,7 @@ public function testParseMessageThrows(?string $return): void {
}

public function testGetFileFromShareForGuest(): void {
$room = $this->createMock(Room::class);
$node = $this->createMock(Node::class);
$node->expects($this->once())
->method('getId')
Expand Down Expand Up @@ -639,9 +640,6 @@ public function testGetFileFromShareForGuest(): void {
->willReturn(['width' => 1234, 'height' => 4567]);

$participant = $this->createMock(Participant::class);
$participant->expects($this->once())
->method('isGuest')
->willReturn(true);

$parser = $this->getParser();
$this->assertSame([
Expand All @@ -657,10 +655,11 @@ public function testGetFileFromShareForGuest(): void {
'preview-available' => 'yes',
'width' => '1234',
'height' => '4567',
], self::invokePrivate($parser, 'getFileFromShare', [$participant, '23']));
], self::invokePrivate($parser, 'getFileFromShare', [$room, $participant, '23']));
}

public function testGetFileFromShareForOwner(): void {
$room = $this->createMock(Room::class);
$node = $this->createMock(Node::class);
$node->expects($this->exactly(2))
->method('getId')
Expand Down Expand Up @@ -713,9 +712,6 @@ public function testGetFileFromShareForOwner(): void {
->method('getMetadataPhotosSizeForFileId');

$participant = $this->createMock(Participant::class);
$participant->expects($this->once())
->method('isGuest')
->willReturn(false);
$attendee = Attendee::fromRow([
'actor_type' => 'users',
'actor_id' => 'owner',
Expand All @@ -736,19 +732,17 @@ public function testGetFileFromShareForOwner(): void {
'permissions' => '27',
'mimetype' => 'httpd/unix-directory',
'preview-available' => 'no',
], self::invokePrivate($parser, 'getFileFromShare', [$participant, '23']));
], self::invokePrivate($parser, 'getFileFromShare', [$room, $participant, '23']));
}

public function testGetFileFromShareForRecipient(): void {
$room = $this->createMock(Room::class);
$share = $this->createMock(IShare::class);
$share->expects($this->any())
->method('getNodeId')
->willReturn(54);

$participant = $this->createMock(Participant::class);
$participant->expects($this->once())
->method('isGuest')
->willReturn(false);
$attendee = Attendee::fromRow([
'actor_type' => 'users',
'actor_id' => 'user',
Expand Down Expand Up @@ -823,19 +817,17 @@ public function testGetFileFromShareForRecipient(): void {
'permissions' => '27',
'mimetype' => 'application/octet-stream',
'preview-available' => 'no',
], self::invokePrivate($parser, 'getFileFromShare', [$participant, '23']));
], self::invokePrivate($parser, 'getFileFromShare', [$room, $participant, '23']));
}

public function testGetFileFromShareForRecipientThrows(): void {
$room = $this->createMock(Room::class);
$share = $this->createMock(IShare::class);
$share->expects($this->any())
->method('getNodeId')
->willReturn(54);

$participant = $this->createMock(Participant::class);
$participant->expects($this->once())
->method('isGuest')
->willReturn(false);
$attendee = Attendee::fromRow([
'actor_type' => 'users',
'actor_id' => 'user',
Expand Down Expand Up @@ -865,19 +857,27 @@ public function testGetFileFromShareForRecipientThrows(): void {

$parser = $this->getParser();
$this->expectException(NotFoundException::class);
self::invokePrivate($parser, 'getFileFromShare', [$participant, '23']);
self::invokePrivate($parser, 'getFileFromShare', [$room, $participant, '23']);
}

public function testGetFileFromShareThrows(): void {
$room = $this->createMock(Room::class);
$this->shareProvider->expects($this->once())
->method('getShareById')
->with('23')
->willThrowException(new ShareNotFound());

$participant = $this->createMock(Participant::class);
$attendee = Attendee::fromRow([
'actor_type' => 'users',
'actor_id' => 'user',
]);
$participant->expects($this->any())
->method('getAttendee')
->willReturn($attendee);
$parser = $this->getParser();
$this->expectException(ShareNotFound::class);
self::invokePrivate($parser, 'getFileFromShare', [$participant, '23']);
self::invokePrivate($parser, 'getFileFromShare', [$room, $participant, '23']);
}

public static function dataGetActor(): array {
Expand Down

0 comments on commit f35c638

Please sign in to comment.