-
Notifications
You must be signed in to change notification settings - Fork 508
🏷️ /call/ reference provider #8001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nickvergessen
merged 7 commits into
master
from
feature/noid/call-url-reference-provider
Sep 29, 2022
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a72306
Add a reference provider for /call/ links
nickvergessen 088cdcf
Show chat message preview when it's being directly linked to
nickvergessen de8b51e
Invalidate cache on relevant changes
nickvergessen 440b6f2
Limit the description to open conversations and participants
nickvergessen 1add7ee
Add unit test for token and message id extraction
nickvergessen 75ef3be
Simplify getRoomIconUrl method
nickvergessen 0c7599f
Fix CS and psalm
nickvergessen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Show chat message preview when it's being directly linked to
Signed-off-by: Joas Schilling <[email protected]>
- Loading branch information
commit 088cdcf6039eb26e10809fdca816aa0623d730da
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]> | ||
| * | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
|
|
@@ -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]; | ||
| } | ||
| } | ||
nickvergessen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return [ | ||
| 'token' => $token, | ||
| 'message' => $messageId, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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); | ||
| } | ||
|
|
@@ -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), | ||
| ]); | ||
|
|
@@ -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); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.