Skip to content

Commit

Permalink
Merge pull request #9072 from nextcloud/feature/8389/document-icon-ur…
Browse files Browse the repository at this point in the history
…l-on-call-rich-objects

Populate icon-url on call rich objects
  • Loading branch information
nickvergessen committed Apr 4, 2023
2 parents 8b927f9 + 3c00ae5 commit 0ef1c31
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 37 deletions.
5 changes: 5 additions & 0 deletions lib/Chat/Parser/UserMention.php
Expand Up @@ -29,6 +29,7 @@
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\Message;
use OCA\Talk\Room;
use OCA\Talk\Service\AvatarService;
use OCA\Talk\Service\ParticipantService;
use OCP\Comments\ICommentsManager;
use OCP\IGroup;
Expand All @@ -50,19 +51,22 @@ class UserMention {
protected IGroupManager $groupManager;
protected GuestManager $guestManager;
protected ParticipantService $participantService;
protected AvatarService $avatarService;
protected IL10N $l;

public function __construct(ICommentsManager $commentsManager,
IUserManager $userManager,
IGroupManager $groupManager,
GuestManager $guestManager,
AvatarService $avatarService,
ParticipantService $participantService,
IL10N $l) {
$this->commentsManager = $commentsManager;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->guestManager = $guestManager;
$this->participantService = $participantService;
$this->avatarService = $avatarService;
$this->l = $l;
}

Expand Down Expand Up @@ -138,6 +142,7 @@ public function parseMessage(Message $chatMessage): void {
'id' => $chatMessage->getRoom()->getToken(),
'name' => $chatMessage->getRoom()->getDisplayName($userId),
'call-type' => $this->getRoomType($chatMessage->getRoom()),
'icon-url' => $this->avatarService->getAvatarUrl($chatMessage->getRoom()),
];
} elseif ($mention['type'] === 'guest') {
try {
Expand Down
5 changes: 5 additions & 0 deletions lib/Controller/ChatController.php
Expand Up @@ -38,6 +38,7 @@
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\AttachmentService;
use OCA\Talk\Service\AvatarService;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Service\SessionService;
use OCA\Talk\Share\RoomShareProvider;
Expand Down Expand Up @@ -71,6 +72,7 @@ class ChatController extends AEnvironmentAwareController {
private ParticipantService $participantService;
private SessionService $sessionService;
protected AttachmentService $attachmentService;
protected avatarService $avatarService;
private GuestManager $guestManager;
/** @var string[] */
protected array $guestNames;
Expand All @@ -97,6 +99,7 @@ public function __construct(string $appName,
ParticipantService $participantService,
SessionService $sessionService,
AttachmentService $attachmentService,
avatarService $avatarService,
GuestManager $guestManager,
MessageParser $messageParser,
RoomShareProvider $shareProvider,
Expand All @@ -120,6 +123,7 @@ public function __construct(string $appName,
$this->participantService = $participantService;
$this->sessionService = $sessionService;
$this->attachmentService = $attachmentService;
$this->avatarService = $avatarService;
$this->guestManager = $guestManager;
$this->messageParser = $messageParser;
$this->shareProvider = $shareProvider;
Expand Down Expand Up @@ -270,6 +274,7 @@ public function shareObjectToChat(string $objectType, string $objectId, string $
}
$data['type'] = $objectType;
$data['id'] = $objectId;
$data['icon-url'] = $this->avatarService->getAvatarUrl($this->room);

if (isset($data['link']) && !$this->trustedDomainHelper->isTrustedUrl($data['link'])) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
Expand Down
11 changes: 11 additions & 0 deletions lib/Notification/Notifier.php
Expand Up @@ -36,6 +36,7 @@
use OCA\Talk\Model\Attendee;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\AvatarService;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Webinary;
use OCP\AppFramework\Utility\ITimeFactory;
Expand Down Expand Up @@ -69,6 +70,7 @@ class Notifier implements INotifier {
private IShareManager $shareManager;
protected Manager $manager;
protected ParticipantService $participantService;
protected AvatarService $avatarService;
protected INotificationManager $notificationManager;
protected ICommentsManager $commentManager;
protected MessageParser $messageParser;
Expand All @@ -92,6 +94,7 @@ public function __construct(IFactory $lFactory,
IShareManager $shareManager,
Manager $manager,
ParticipantService $participantService,
AvatarService $avatarService,
INotificationManager $notificationManager,
CommentsManager $commentManager,
MessageParser $messageParser,
Expand All @@ -109,6 +112,7 @@ public function __construct(IFactory $lFactory,
$this->shareManager = $shareManager;
$this->manager = $manager;
$this->participantService = $participantService;
$this->avatarService = $avatarService;
$this->notificationManager = $notificationManager;
$this->commentManager = $commentManager;
$this->messageParser = $messageParser;
Expand Down Expand Up @@ -322,6 +326,7 @@ protected function parseStoredRecordingFail(
'id' => $room->getId(),
'name' => $room->getDisplayName($participant->getAttendee()->getActorId()),
'call-type' => $this->getRoomType($room),
'icon-url' => $this->avatarService->getAvatarUrl($room),
],
]
);
Expand Down Expand Up @@ -385,6 +390,7 @@ protected function parseStoredRecording(
'id' => $room->getId(),
'name' => $room->getDisplayName($participant->getAttendee()->getActorId()),
'call-type' => $this->getRoomType($room),
'icon-url' => $this->avatarService->getAvatarUrl($room),
],
'file' => [
'type' => 'file',
Expand Down Expand Up @@ -481,6 +487,7 @@ protected function parseChatMessage(INotification $notification, Room $room, Par
'id' => $room->getId(),
'name' => $room->getDisplayName($notification->getUser()),
'call-type' => $this->getRoomType($room),
'icon-url' => $this->avatarService->getAvatarUrl($room),
];

$messageParameters = $notification->getMessageParameters();
Expand Down Expand Up @@ -777,6 +784,7 @@ protected function parseInvitation(INotification $notification, Room $room, IL10
'id' => $room->getId(),
'name' => $roomName,
'call-type' => $this->getRoomType($room),
'icon-url' => $this->avatarService->getAvatarUrl($room),
],
]
);
Expand All @@ -802,6 +810,7 @@ protected function parseInvitation(INotification $notification, Room $room, IL10
'id' => $room->getId(),
'name' => $roomName,
'call-type' => $this->getRoomType($room),
'icon-url' => $this->avatarService->getAvatarUrl($room),
],
]
);
Expand Down Expand Up @@ -853,6 +862,7 @@ protected function parseCall(INotification $notification, Room $room, IL10N $l):
'id' => $room->getId(),
'name' => $roomName,
'call-type' => $this->getRoomType($room),
'icon-url' => $this->avatarService->getAvatarUrl($room),
],
]
);
Expand All @@ -877,6 +887,7 @@ protected function parseCall(INotification $notification, Room $room, IL10N $l):
'id' => $room->getId(),
'name' => $roomName,
'call-type' => $this->getRoomType($room),
'icon-url' => $this->avatarService->getAvatarUrl($room),
],
]
);
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/features/bootstrap/FeatureContext.php
Expand Up @@ -2227,6 +2227,13 @@ protected function compareDataResponse(TableNode $formData = null) {
if ($result) {
$expected[$i]['messageParameters'] = str_replace($matches[0], '"' . self::$questionToPollId[$matches[1]] . '"', $expected[$i]['messageParameters']);
}
if (isset($messages[$i]['messageParameters']['object']['icon-url'])) {
$result = preg_match('/"\{VALIDATE_ICON_URL_PATTERN\}"/', $expected[$i]['messageParameters'], $matches);
if ($result) {
Assert::assertMatchesRegularExpression('/avatar(\?v=\w+)?/', $messages[$i]['messageParameters']['object']['icon-url']);
$expected[$i]['messageParameters'] = str_replace($matches[0], json_encode($messages[$i]['messageParameters']['object']['icon-url']), $expected[$i]['messageParameters']);
}
}
}

Assert::assertEquals($expected, array_map(function ($message) use ($includeParents, $includeReferenceId, $includeReactions, $includeReactionsSelf) {
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/features/chat-2/rich-object-share.feature
Expand Up @@ -10,7 +10,7 @@ Feature: chat-2/rich-object-share
When user "participant1" shares rich-object "call" "R4nd0mT0k3n" '{"name":"Another room","call-type":"group"}' to room "public room" with 201 (v1)
Then user "participant1" sees the following messages in room "public room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| public room | users | participant1 | participant1-displayname | {object} | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"},"object":{"name":"Another room","call-type":"group","type":"call","id":"R4nd0mT0k3n"}} |
| public room | users | participant1 | participant1-displayname | {object} | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"},"object":{"name":"Another room","call-type":"group","type":"call","id":"R4nd0mT0k3n","icon-url":"{VALIDATE_ICON_URL_PATTERN}"}} |

Scenario: Can not share without chat permission
Given user "participant1" creates room "public room" (v4)
Expand Down Expand Up @@ -44,7 +44,7 @@ Feature: chat-2/rich-object-share
And user "participant2" deletes message "shared::call::R4nd0mT0k3n" from room "public room" with 403
Then user "participant1" sees the following messages in room "public room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| public room | users | participant2 | participant2-displayname | {object} | {"actor":{"type":"user","id":"participant2","name":"participant2-displayname"},"object":{"name":"Another room","call-type":"group","type":"call","id":"R4nd0mT0k3n"}} |
| public room | users | participant2 | participant2-displayname | {object} | {"actor":{"type":"user","id":"participant2","name":"participant2-displayname"},"object":{"name":"Another room","call-type":"group","type":"call","id":"R4nd0mT0k3n","icon-url":"{VALIDATE_ICON_URL_PATTERN}"}} |

Scenario: Share an invalid rich object to a chat
Given user "participant1" creates room "public room" (v4)
Expand All @@ -67,7 +67,7 @@ Feature: chat-2/rich-object-share
When user "participant1" shares rich-object "call" "R4nd0mT0k3n" '{"name":"Another room","call-type":"group"}' to room "public room" with 201 (v1)
Then user "participant1" sees the following shared other in room "public room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| public room | users | participant1 | participant1-displayname | {object} | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"},"object":{"name":"Another room","call-type":"group","type":"call","id":"R4nd0mT0k3n"}} |
| public room | users | participant1 | participant1-displayname | {object} | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"},"object":{"name":"Another room","call-type":"group","type":"call","id":"R4nd0mT0k3n","icon-url":"{VALIDATE_ICON_URL_PATTERN}"}} |
When user "participant1" shares "welcome.txt" with room "public room" with OCS 100
Then user "participant1" sees the following shared file in room "public room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/features/conversation/avatar.feature
Expand Up @@ -45,3 +45,13 @@ Feature: conversation/avatar
| roomType | 1 |
| invite | participant2 |
Then user "participant1" uploads file "/img/favicon.png" as avatar of room "one2one" with 400

Scenario: User should receive the room avatar when see a rich object at media tab
Given user "participant1" creates room "public room" (v4)
| roomType | 3 |
| roomName | public room |
And user "participant1" uploads file "/img/favicon.png" as avatar of room "public room" with 200
When user "participant1" shares rich-object "call" "R4nd0mT0k3n" '{"name":"Another room","call-type":"group"}' to room "public room" with 201 (v1)
Then user "participant1" sees the following shared other in room "public room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| public room | users | participant1 | participant1-displayname | {object} | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"},"object":{"name":"Another room","call-type":"group","type":"call","id":"R4nd0mT0k3n","icon-url":"{VALIDATE_ICON_URL_PATTERN}"}} |
10 changes: 10 additions & 0 deletions tests/php/Chat/Parser/UserMentionTest.php
Expand Up @@ -31,6 +31,7 @@
use OCA\Talk\Model\Message;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\AvatarService;
use OCA\Talk\Service\ParticipantService;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
Expand All @@ -49,6 +50,8 @@ class UserMentionTest extends TestCase {
protected $groupManager;
/** @var GuestManager|MockObject */
protected $guestManager;
/** @var AvatarService|MockObject */
protected $avatarService;
/** @var ParticipantService|MockObject */
protected $participantService;
/** @var IL10N|MockObject */
Expand All @@ -63,6 +66,7 @@ public function setUp(): void {
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->guestManager = $this->createMock(GuestManager::class);
$this->avatarService = $this->createMock(AvatarService::class);
$this->participantService = $this->createMock(ParticipantService::class);
$this->l = $this->createMock(IL10N::class);

Expand All @@ -71,6 +75,7 @@ public function setUp(): void {
$this->userManager,
$this->groupManager,
$this->guestManager,
$this->avatarService,
$this->participantService,
$this->l);
}
Expand Down Expand Up @@ -431,6 +436,10 @@ public function testGetRichMessageWithAtAll(): void {
$chatMessage = new Message($room, $participant, $comment, $l);
$chatMessage->setMessage('Mention to @all', []);

$this->avatarService->method('getAvatarUrl')
->with($room)
->willReturn('getAvatarUrl');

$this->parser->parseMessage($chatMessage);

$expectedMessageParameters = [
Expand All @@ -439,6 +448,7 @@ public function testGetRichMessageWithAtAll(): void {
'id' => 'token',
'name' => 'name',
'call-type' => 'group',
'icon-url' => 'getAvatarUrl',
]
];

Expand Down
11 changes: 11 additions & 0 deletions tests/php/Controller/ChatControllerTest.php
Expand Up @@ -35,6 +35,7 @@
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\AttachmentService;
use OCA\Talk\Service\AvatarService;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Service\SessionService;
use OCA\Talk\Share\RoomShareProvider;
Expand Down Expand Up @@ -73,6 +74,8 @@ class ChatControllerTest extends TestCase {
protected $sessionService;
/** @var AttachmentService|MockObject */
protected $attachmentService;
/** @var AvatarService|MockObject */
protected $avatarService;
/** @var GuestManager|MockObject */
protected $guestManager;
/** @var MessageParser|MockObject */
Expand Down Expand Up @@ -119,6 +122,7 @@ public function setUp(): void {
$this->participantService = $this->createMock(ParticipantService::class);
$this->sessionService = $this->createMock(SessionService::class);
$this->attachmentService = $this->createMock(AttachmentService::class);
$this->avatarService = $this->createMock(AvatarService::class);
$this->guestManager = $this->createMock(GuestManager::class);
$this->messageParser = $this->createMock(MessageParser::class);
$this->roomShareProvider = $this->createMock(RoomShareProvider::class);
Expand Down Expand Up @@ -157,6 +161,7 @@ private function recreateChatController() {
$this->participantService,
$this->sessionService,
$this->attachmentService,
$this->avatarService,
$this->guestManager,
$this->messageParser,
$this->roomShareProvider,
Expand Down Expand Up @@ -633,10 +638,15 @@ public function testSendMessageByGuest() {
public function testShareObjectToChatByUser() {
$participant = $this->createMock(Participant::class);

$this->avatarService->method('getAvatarUrl')
->with($this->room)
->willReturn('getAvatarUrl');

$richData = [
'call-type' => 'one2one',
'type' => 'call',
'id' => 'R4nd0mToken',
'icon-url' => '',
];

$date = new \DateTime();
Expand All @@ -659,6 +669,7 @@ public function testShareObjectToChatByUser() {
'call-type' => 'one2one',
'type' => 'call',
'id' => 'R4nd0mToken',
'icon-url' => 'getAvatarUrl',
],
],
]),
Expand Down

0 comments on commit 0ef1c31

Please sign in to comment.