Skip to content
Merged
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
Show chat message preview when it's being directly linked to
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Sep 29, 2022
commit 088cdcf6039eb26e10809fdca816aa0623d730da
156 changes: 137 additions & 19 deletions lib/Collaboration/Reference/TalkReferenceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

/**
* @copyright Copyright (c) 2022 Joas Schilling <[email protected]>
*
Expand All @@ -22,28 +23,43 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


namespace OCA\Talk\Collaboration\Reference;

use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Chat\MessageParser;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Room;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\IReferenceProvider;
use OCP\Collaboration\Reference\Reference;
use OCP\IL10N;
use OCP\IURLGenerator;

/**
* @psalm-type ReferenceMatch = array{token: string, message: ?int}
*/
class TalkReferenceProvider implements IReferenceProvider {

protected IURLGenerator $urlGenerator;
protected Manager $manager;
protected Manager $roomManager;
protected ChatManager $chatManager;
protected MessageParser $messageParser;
protected IL10N $l;
protected ?string $userId;

public function __construct(IURLGenerator $urlGenerator,
Manager $manager,
ChatManager $chatManager,
MessageParser $messageParser,
IL10N $l,
?string $userId) {
$this->urlGenerator = $urlGenerator;
$this->manager = $manager;
$this->roomManager = $manager;
$this->chatManager = $chatManager;
$this->messageParser = $messageParser;
$this->l = $l;
$this->userId = $userId;
}

Expand All @@ -52,18 +68,54 @@ public function matchReference(string $referenceText): bool {
return $this->getTalkAppLinkToken($referenceText) !== null;
}

private function getTalkAppLinkToken(string $referenceText): ?string {
/**
* @param string $referenceText
* @return array|null
* @psalm-return ReferenceMatch|null
*/
private function getTalkAppLinkToken(string $referenceText): ?array {
$indexPhpUrl = $this->urlGenerator->getAbsoluteURL('/index.php/call/');
$rewriteUrl = $this->urlGenerator->getAbsoluteURL('/call/');

if (str_starts_with($referenceText, $indexPhpUrl)) {
return substr($referenceText, strlen($indexPhpUrl)) ?: null;
$urlOfInterest = substr($referenceText, strlen($indexPhpUrl)) ?: null;
} elseif (str_starts_with($referenceText, $rewriteUrl)) {
$urlOfInterest = substr($referenceText, strlen($rewriteUrl)) ?: null;
} else {
return null;
}

$rewriteUrl = $this->urlGenerator->getAbsoluteURL('/call/');
if (str_starts_with($referenceText, $rewriteUrl)) {
return substr($referenceText, strlen($rewriteUrl)) ?: null;
$hashPosition = strpos($urlOfInterest, '#');
$queryPosition = strpos($urlOfInterest, '?');

if ($hashPosition === false && $queryPosition === false) {
return [
'token' => $urlOfInterest,
'message' => null,
];
}

return null;
if ($hashPosition !== false && $queryPosition !== false) {
$cutPosition = min($hashPosition, $queryPosition);
} elseif ($hashPosition !== false) {
$cutPosition = $hashPosition;
} else {
$cutPosition = $queryPosition;
}

$token = substr($urlOfInterest, 0, $cutPosition);
$messageId = null;
if ($hashPosition !== false) {
$afterHash = substr($urlOfInterest, $hashPosition + 1);
if (preg_match('/^message_(\d+)$/', $afterHash, $matches)) {
$messageId = $matches[1];
}
}

return [
'token' => $token,
'message' => $messageId,
];
}

/**
Expand All @@ -74,7 +126,7 @@ public function resolveReference(string $referenceText): ?IReference {
$reference = new Reference($referenceText);
try {
$this->fetchReference($reference);
} catch (RoomNotFoundException $e) {
} catch (RoomNotFoundException|ParticipantNotFoundException $e) {
$reference->setRichObject('call', null);
$reference->setAccessible(false);
}
Expand All @@ -92,21 +144,82 @@ private function fetchReference(Reference $reference): void {
throw new RoomNotFoundException();
}

$token = $this->getTalkAppLinkToken($reference->getId());
if ($token === null) {
$referenceMatch = $this->getTalkAppLinkToken($reference->getId());
if ($referenceMatch === null) {
throw new RoomNotFoundException();
}

$room = $this->manager->getRoomByToken($token, $this->userId);
$room = $this->roomManager->getRoomForUserByToken($referenceMatch['token'], $this->userId);

/**
* Default handling:
* Title is the conversation name
* Description the conversation description
*/
$roomName = $room->getDisplayName($this->userId);
$title = $roomName;
$description = $room->getDescription();

$reference->setTitle($room->getDisplayName($this->userId));
$reference->setDescription($room->getDescription());
$participant = null;
if (!empty($referenceMatch['message'])) {
try {
$participant = $room->getParticipant($this->userId);
} catch (ParticipantNotFoundException $e) {
}
}

/**
* If linking to a comment and the user is already a participant
* Title is "Message of {user} in {conversation}"
* Description is the plain text chat message
*/
if ($participant && !empty($referenceMatch['message'])) {
$comment = $this->chatManager->getComment($room, $referenceMatch['message']);
$message = $this->messageParser->createMessage($room, $participant, $comment, $this->l);
$this->messageParser->parseMessage($message);

$placeholders = $replacements = [];
foreach ($message->getMessageParameters() as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';
if ($parameter['type'] === 'user' || $parameter['type'] === 'guest') {
$replacements[] = '@' . $parameter['name'];
} else {
$replacements[] = $parameter['name'];
}
}
$description = str_replace($placeholders, $replacements, $message->getMessage());

$titleLine = $this->l->t('Message of {user} in {conversation}');
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
$titleLine = $this->l->t('Message of {user}');
}

$displayName = $message->getActorDisplayName();
if ($message->getActorType() === Attendee::ACTOR_GUESTS) {
if ($displayName === '') {
$displayName = $this->l->t('Guest');
} else {
$displayName = $this->l->t('%s (guest)', $displayName);
}
} elseif ($displayName === '') {
$titleLine = $this->l->t('Message of a deleted user in {conversation}');
}

$title = str_replace(
['{user}', '{conversation}'],
[$displayName, $title],
$titleLine
);
}

$reference->setTitle($title);
$reference->setDescription($description);
$reference->setUrl($this->urlGenerator->linkToRouteAbsolute('spreed.Page.showCall', ['token' => $room->getToken()]));
$reference->setImageUrl($this->getRoomIconUrl($room, $this->userId));

$reference->setRichObject('call', [
'id' => $room->getToken(),
'name' => $reference->getTitle(),
'name' => $roomName,
'link' => $reference->getUrl(),
'call-type' => $this->getRoomType($room),
]);
Expand All @@ -116,7 +229,12 @@ private function fetchReference(Reference $reference): void {
* @inheritDoc
*/
public function getCachePrefix(string $referenceId): string {
return $this->getTalkAppLinkToken($referenceId) ?? '';
$referenceMatch = $this->getTalkAppLinkToken($referenceId);
if ($referenceMatch === null) {
return '';
}

return $referenceMatch['token'] . '#' . ($referenceMatch['message'] ?? 0);
}

/**
Expand Down