-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathReferenceProvider.php
More file actions
157 lines (134 loc) · 4.54 KB
/
Copy pathReferenceProvider.php
File metadata and controls
157 lines (134 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Provider;
use Exception;
use OCA\Polls\AppInfo\Application;
use OCA\Polls\Exceptions\ForbiddenException;
use OCA\Polls\Exceptions\NotFoundException;
use OCA\Polls\Service\PollService;
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\ISearchableReferenceProvider;
use OCP\Collaboration\Reference\Reference;
use OCP\IL10N;
use OCP\IURLGenerator;
class ReferenceProvider extends ADiscoverableReferenceProvider implements ISearchableReferenceProvider {
/** @psalm-suppress PossiblyUnusedMethod */
public function __construct(
private PollService $pollService,
private IURLGenerator $urlGenerator,
private IL10N $l10n,
private ?string $userId,
) {
}
/**
* @inheritDoc
*/
public function matchReference(string $referenceText): bool {
// validate url by checking if it contains a poll id; This is valid for internal polls
return ($this->extractPollId($referenceText) !== 0);
}
public function extractPollId(string $referenceText): int {
$matchingUrls = [
$this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID . '/vote'), // poll url base without index.php
$this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID . '/vote'), // poll url base with index.php
];
foreach ($matchingUrls as $url) {
preg_match('/^' . preg_quote($url, '/') . '?\/([0-9]+)$/', $referenceText, $matches);
if ($matches && count($matches) > 1) {
return (int)$matches[1];
}
}
return 0;
}
/**
* @inheritDoc
*/
public function resolveReference(string $referenceText): ?IReference {
if ($this->matchReference($referenceText)) {
$pollId = $this->extractPollId($referenceText);
$expired = false;
$expiry = 0;
$participated = false;
if ($pollId) {
try {
$poll = $this->pollService->get($pollId);
$title = $this->l10n->t('Poll') . ': ' . $poll->getTitle();
$description = $poll->getDescription();
$ownerId = $poll->getUser()->getId();
$ownerDisplayName = $poll->getUser()->getDisplayName();
$url = $poll->getVoteUrl();
$expired = $poll->getExpired();
$expiry = $poll->getExpire();
$participated = $poll->getCurrentUserVotes() ? true : false;
} catch (NotFoundException $e) {
$pollId = 0;
$title = $this->l10n->t('404 - Poll not found');
$description = $this->l10n->t('This poll does not exist (anymore).');
$ownerId = null;
$ownerDisplayName = $this->l10n->t('No one.');
$url = null;
} catch (ForbiddenException $e) {
$owner = $this->pollService->getPollOwnerFromDB($pollId);
$title = $this->l10n->t('Access denied');
$ownerDisplayName = $owner->getDisplayName();
$description = $this->l10n->t('You have no access to this poll. Contact %s if you think this is a mistake.', $ownerDisplayName);
$ownerId = $owner->getId();
$url = $referenceText;
} catch (Exception $e) {
// skip the reference silently
return null;
}
$reference = new Reference($referenceText);
$reference->setTitle($title);
$reference->setDescription($description ? $description : $this->l10n->t('No description available.'));
$reference->setImageUrl($this->getIconUrl());
$reference->setRichObject(Application::APP_ID . '_reference_widget', [
'id' => $pollId,
'poll' => [
'id' => $pollId,
'title' => $title,
'description' => $description ? $description : $this->l10n->t('No description available.'),
'ownerDisplayName' => $ownerDisplayName,
'ownerId' => $ownerId,
'url' => $url,
'expired' => $expired,
'expiry' => $expiry,
'participated' => $participated,
],
]);
return $reference;
}
}
return null;
}
public function getCachePrefix(string $referenceId): string {
$pollId = $this->extractPollId($referenceId);
if ($pollId !== 0) {
return (string)$pollId;
}
return $referenceId;
}
public function getCacheKey(string $referenceId): ?string {
return $this->userId ?? '';
}
public function getId(): string {
return Application::APP_ID;
}
public function getTitle(): string {
return $this->l10n->t('Poll');
}
public function getIconUrl(): string {
return $this->urlGenerator->imagePath(Application::APP_ID, 'polls.svg');
}
public function getOrder(): int {
return 51;
}
public function getSupportedSearchProviderIds(): array {
return ['search-poll'];
}
}