Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2022-09-23 14:45:53 +0300
committerbackportbot-nextcloud[bot] <backportbot-nextcloud[bot]@users.noreply.github.com>2022-09-29 19:27:59 +0300
commitb5acdd2a4b82814ce27961ed4bb0d16deb9ec7b7 (patch)
tree986c9b67a22fc6785e8ba49a485b7420cc03e5ad /lib
parent76266285de3dc7884500125ce565cc495c556b2a (diff)
Add a reference provider for /call/ links
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/AppInfo/Application.php3
-rw-r--r--lib/Collaboration/Reference/TalkReferenceProvider.php161
2 files changed, 164 insertions, 0 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php
index 3e44c14c7..965abbf85 100644
--- a/lib/AppInfo/Application.php
+++ b/lib/AppInfo/Application.php
@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace OCA\Talk\AppInfo;
+use OCA\Talk\Collaboration\Reference\TalkReferenceProvider;
use OCP\Util;
use OCA\Circles\Events\AddingCircleMemberEvent;
use OCA\Circles\Events\CircleDestroyedEvent;
@@ -145,6 +146,8 @@ class Application extends App implements IBootstrap {
$context->registerProfileLinkAction(TalkAction::class);
+ $context->registerReferenceProvider(TalkReferenceProvider::class);
+
$context->registerTalkBackend(TalkBackend::class);
}
diff --git a/lib/Collaboration/Reference/TalkReferenceProvider.php b/lib/Collaboration/Reference/TalkReferenceProvider.php
new file mode 100644
index 000000000..acfa43e74
--- /dev/null
+++ b/lib/Collaboration/Reference/TalkReferenceProvider.php
@@ -0,0 +1,161 @@
+<?php
+
+declare(strict_types = 1);
+/**
+ * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+namespace OCA\Talk\Collaboration\Reference;
+
+use OCA\Talk\Exceptions\RoomNotFoundException;
+use OCA\Talk\Manager;
+use OCA\Talk\Room;
+use OCP\Collaboration\Reference\IReference;
+use OCP\Collaboration\Reference\IReferenceProvider;
+use OCP\Collaboration\Reference\Reference;
+use OCP\IURLGenerator;
+
+class TalkReferenceProvider implements IReferenceProvider {
+
+ protected IURLGenerator $urlGenerator;
+ protected Manager $manager;
+ protected ?string $userId;
+
+ public function __construct(IURLGenerator $urlGenerator,
+ Manager $manager,
+ ?string $userId) {
+ $this->urlGenerator = $urlGenerator;
+ $this->manager = $manager;
+ $this->userId = $userId;
+ }
+
+
+ public function matchReference(string $referenceText): bool {
+ return $this->getTalkAppLinkToken($referenceText) !== null;
+ }
+
+ private function getTalkAppLinkToken(string $referenceText): ?string {
+ $indexPhpUrl = $this->urlGenerator->getAbsoluteURL('/index.php/call/');
+ if (str_starts_with($referenceText, $indexPhpUrl)) {
+ return substr($referenceText, strlen($indexPhpUrl)) ?: null;
+ }
+
+ $rewriteUrl = $this->urlGenerator->getAbsoluteURL('/call/');
+ if (str_starts_with($referenceText, $rewriteUrl)) {
+ return substr($referenceText, strlen($rewriteUrl)) ?: null;
+ }
+
+ return null;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function resolveReference(string $referenceText): ?IReference {
+ if ($this->matchReference($referenceText)) {
+ $reference = new Reference($referenceText);
+ try {
+ $this->fetchReference($reference);
+ } catch (RoomNotFoundException $e) {
+ $reference->setRichObject('call', null);
+ $reference->setAccessible(false);
+ }
+ return $reference;
+ }
+
+ return null;
+ }
+
+ /**
+ * @throws RoomNotFoundException
+ */
+ private function fetchReference(Reference $reference): void {
+ if ($this->userId === null) {
+ throw new RoomNotFoundException();
+ }
+
+ $token = $this->getTalkAppLinkToken($reference->getId());
+ if ($token === null) {
+ throw new RoomNotFoundException();
+ }
+
+ $room = $this->manager->getRoomByToken($token, $this->userId);
+
+ $reference->setTitle($room->getDisplayName($this->userId));
+ $reference->setDescription($room->getDescription());
+ $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(),
+ 'link' => $reference->getUrl(),
+ 'call-type' => $this->getRoomType($room),
+ ]);
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getCachePrefix(string $referenceId): string {
+ return $this->getTalkAppLinkToken($referenceId) ?? '';
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getCacheKey(string $referenceId): ?string {
+ return $this->userId ?? '';
+ }
+
+ protected function getRoomIconUrl(Room $room, string $userId): string {
+ if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
+ $participants = json_decode($room->getName(), true);
+
+ foreach ($participants as $p) {
+ if ($p !== $userId) {
+ return $this->urlGenerator->linkToRouteAbsolute(
+ 'core.avatar.getAvatar',
+ [
+ 'userId' => $p,
+ 'size' => 64,
+ ]
+ );
+ }
+ }
+ }
+
+ return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('spreed', 'changelog.svg'));
+ }
+
+ protected function getRoomType(Room $room): string {
+ switch ($room->getType()) {
+ case Room::TYPE_ONE_TO_ONE:
+ return 'one2one';
+ case Room::TYPE_GROUP:
+ return 'group';
+ case Room::TYPE_PUBLIC:
+ return 'public';
+ default:
+ return 'unknown';
+ }
+ }
+}