-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathMentionController.php
More file actions
75 lines (66 loc) · 2.38 KB
/
MentionController.php
File metadata and controls
75 lines (66 loc) · 2.38 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
<?php
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Richdocuments\Controller;
use OCA\Richdocuments\AppInfo\Application;
use OCA\Richdocuments\Notification\Notifier;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\UserRateLimit;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IRootFolder;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\Notification\IManager;
class MentionController extends Controller {
public function __construct(
$appName,
IRequest $request,
private IRootFolder $rootFolder,
private IManager $manager,
private ITimeFactory $timeFactory,
private IUserManager $userManager,
private ?string $userId,
) {
parent::__construct($appName, $request);
}
#[NoAdminRequired]
#[UserRateLimit(limit: 5, period: 120)]
public function mention(int $fileId, string $mention): DataResponse {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$file = $userFolder->getFirstNodeById($fileId);
if ($file === null) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
// Reverse the array of users to pop off the first user later
$userResults = array_reverse($this->userManager->searchDisplayName($mention, 1));
if (count($userResults) < 1) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
// Get the first user returned in the array
$user = array_pop($userResults);
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$file = $userFolder->getFirstNodeById($fileId);
if ($file === null) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
$notification = $this->manager->createNotification();
$notification->setUser($user->getUID())
->setApp(Application::APPNAME)
->setSubject(Notifier::TYPE_MENTIONED, [
Notifier::SUBJECT_MENTIONED_SOURCE_USER => $this->userId,
Notifier::SUBJECT_MENTIONED_TARGET_USER => $mention,
])
->setObject('file', (string)$fileId);
if ($this->manager->getCount($notification) === 0) {
$notification->setDateTime(\DateTime::createFromImmutable($this->timeFactory->now()));
$this->manager->notify($notification);
return new DataResponse([], Http::STATUS_OK);
}
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
}