|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* @copyright Copyright (c) 2022 Julius Härtl <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @author Julius Härtl <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @license GNU AGPL version 3 or any later version |
|
10
|
|
|
* |
|
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
14
|
|
|
* License, or (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU Affero General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OC\Core\Controller; |
|
26
|
|
|
|
|
27
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
28
|
|
|
use OCP\Collaboration\Reference\IDiscoverableReferenceProvider; |
|
29
|
|
|
use OCP\Collaboration\Reference\IReferenceManager; |
|
30
|
|
|
use OCP\IRequest; |
|
31
|
|
|
|
|
32
|
|
|
class ReferenceApiController extends \OCP\AppFramework\OCSController { |
|
33
|
|
|
private IReferenceManager $referenceManager; |
|
34
|
|
|
private ?string $userId; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(string $appName, |
|
37
|
|
|
IRequest $request, |
|
38
|
|
|
IReferenceManager $referenceManager, |
|
39
|
|
|
?string $userId) { |
|
40
|
|
|
parent::__construct($appName, $request); |
|
41
|
|
|
$this->referenceManager = $referenceManager; |
|
42
|
|
|
$this->userId = $userId; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @NoAdminRequired |
|
47
|
|
|
*/ |
|
48
|
|
|
public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse { |
|
49
|
|
|
$references = $this->referenceManager->extractReferences($text); |
|
50
|
|
|
|
|
51
|
|
|
$result = []; |
|
52
|
|
|
$index = 0; |
|
53
|
|
|
foreach ($references as $reference) { |
|
54
|
|
|
if ($index++ >= $limit) { |
|
55
|
|
|
break; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference) : null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return new DataResponse([ |
|
62
|
|
|
'references' => $result |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @NoAdminRequired |
|
68
|
|
|
*/ |
|
69
|
|
|
public function resolveOne(string $reference): DataResponse { |
|
70
|
|
|
$resolvedReference = $this->referenceManager->resolveReference(trim($reference)); |
|
71
|
|
|
|
|
72
|
|
|
$response = new DataResponse(['references' => [ $reference => $resolvedReference ]]); |
|
73
|
|
|
$response->cacheFor(3600, false, true); |
|
74
|
|
|
return $response; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @NoAdminRequired |
|
79
|
|
|
* |
|
80
|
|
|
* @param string[] $references |
|
81
|
|
|
*/ |
|
82
|
|
|
public function resolve(array $references, int $limit = 1): DataResponse { |
|
83
|
|
|
$result = []; |
|
84
|
|
|
$index = 0; |
|
85
|
|
|
foreach ($references as $reference) { |
|
86
|
|
|
if ($index++ >= $limit) { |
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$result[$reference] = $this->referenceManager->resolveReference($reference); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return new DataResponse([ |
|
94
|
|
|
'references' => array_filter($result) |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @NoAdminRequired |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getProvidersInfo(): DataResponse { |
|
102
|
|
|
$providers = $this->referenceManager->getDiscoverableProviders(); |
|
103
|
|
|
$jsonProviders = array_map(static function (IDiscoverableReferenceProvider $provider) { |
|
104
|
|
|
return $provider->jsonSerialize(); |
|
105
|
|
|
}, $providers); |
|
106
|
|
|
return new DataResponse($jsonProviders); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @NoAdminRequired |
|
111
|
|
|
*/ |
|
112
|
|
|
public function touchProvider(string $providerId, ?int $timestamp = null): DataResponse { |
|
113
|
|
|
if ($this->userId !== null) { |
|
114
|
|
|
$success = $this->referenceManager->touchProvider($this->userId, $providerId, $timestamp); |
|
115
|
|
|
return new DataResponse(['success' => $success]); |
|
116
|
|
|
} |
|
117
|
|
|
return new DataResponse(['success' => false]); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|