Passed
Pull Request — master (#813)
by Julius
05:24 queued 03:01
created

NotificationHelper::getBoard()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Notification;
25
26
use DateTime;
27
use OCA\Deck\Db\Acl;
28
use OCA\Deck\Db\Board;
29
use OCA\Deck\Db\BoardMapper;
30
use OCA\Deck\Db\CardMapper;
31
use OCA\Deck\Service\PermissionService;
32
use OCP\Comments\IComment;
33
use OCP\IGroupManager;
34
use OCP\IUser;
35
use OCP\Notification\IManager;
36
37
class NotificationHelper {
38
39
	/** @var CardMapper */
40
	protected $cardMapper;
41
	/** @var BoardMapper */
42
	protected $boardMapper;
43
	/** @var PermissionService */
44
	protected $permissionService;
45
	/** @var IManager */
46
	protected $notificationManager;
47
	/** @var IGroupManager */
48
	protected $groupManager;
49
	/** @var string */
50
	protected $currentUser;
51
	/** @var array */
52
	private $boards = [];
53
54
	public function __construct(
55
		CardMapper $cardMapper,
56
		BoardMapper $boardMapper,
57
		PermissionService $permissionService,
58
		IManager $notificationManager,
59
		IGroupManager $groupManager,
60
		$userId
61
	) {
62
		$this->cardMapper = $cardMapper;
63
		$this->boardMapper = $boardMapper;
64
		$this->permissionService = $permissionService;
65
		$this->notificationManager = $notificationManager;
66
		$this->groupManager = $groupManager;
67
		$this->currentUser = $userId;
68
	}
69
70
	public function sendCardDuedate($card) {
71
		// check if notification has already been sent
72
		// ideally notifications should not be deleted once seen by the user so we can
73
		// also deliver due date notifications for users who have been added later to a board
74
		// this should maybe be addressed in nextcloud/server
75
		if ($card->getNotified()) {
76
			return;
77
		}
78
79
		// TODO: Once assigning users is possible, those should be notified instead of all users of the board
80
		$boardId = $this->cardMapper->findBoardId($card->getId());
81
		$board = $this->getBoard($boardId);
82
		/** @var IUser $user */
83
		foreach ($this->permissionService->findUsers($boardId) as $user) {
84
			$notification = $this->notificationManager->createNotification();
85
			$notification
86
				->setApp('deck')
87
				->setUser((string) $user->getUID())
88
				->setObject('card', $card->getId())
89
				->setSubject('card-overdue', [
90
					$card->getTitle(), $board->getTitle()
91
				])
92
				->setDateTime(new DateTime($card->getDuedate()));
93
			$this->notificationManager->notify($notification);
94
		}
95
		$this->cardMapper->markNotified($card);
96
	}
97
98
	public function markDuedateAsRead($card) {
99
		$notification = $this->notificationManager->createNotification();
100
		$notification
101
			->setApp('deck')
102
			->setObject('card', $card->getId())
103
			->setSubject('card-overdue', []);
104
		$this->notificationManager->markProcessed($notification);
105
	}
106
107
	public function sendCardAssigned($card, $userId) {
108
		$boardId = $this->cardMapper->findBoardId($card->getId());
109
		$board = $this->getBoard($boardId);
110
111
		$notification = $this->notificationManager->createNotification();
112
		$notification
113
			->setApp('deck')
114
			->setUser((string) $userId)
115
			->setDateTime(new DateTime())
116
			->setObject('card', $card->getId())
117
				->setSubject('card-assigned', [
118
					$card->getTitle(),
119
					$board->getTitle(),
120
					$this->currentUser
121
				]);
122
		$this->notificationManager->notify($notification);
123
	}
124
125
	/**
126
	 * Send notifications that a board was shared with a user/group
127
	 *
128
	 * @param $boardId
129
	 * @param Acl $acl
130
	 * @throws \InvalidArgumentException
131
	 */
132
	public function sendBoardShared($boardId, $acl) {
133
		$board = $this->getBoard($boardId);
134
		if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
135
			$notification = $this->generateBoardShared($board, $acl->getParticipant());
136
			$this->notificationManager->notify($notification);
137
		}
138
		if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) {
139
			$group = $this->groupManager->get($acl->getParticipant());
140
			foreach ($group->getUsers() as $user) {
141
				$notification = $this->generateBoardShared($board, $user->getUID());
142
				$this->notificationManager->notify($notification);
143
			}
144
		}
145
	}
146
147
	public function sendMention(IComment $comment) {
148
		foreach ($comment->getMentions() as $mention) {
149
			$card = $this->cardMapper->find($comment->getObjectId());
150
			$boardId = $this->cardMapper->findBoardId($card->getId());
151
			$notification = $this->notificationManager->createNotification();
152
			$notification
153
				->setApp('deck')
154
				->setUser((string) $mention['id'])
155
				->setDateTime(new DateTime())
156
				->setObject('card', (string) $card->getId())
157
				->setSubject('card-comment-mentioned', [$card->getTitle(), $boardId, $this->currentUser])
158
				->setMessage('{message}', ['message' => $comment->getMessage()]);
159
			$this->notificationManager->notify($notification);
160
		}
161
	}
162
163
	/**
164
	 * @param $boardId
165
	 * @return Board
166
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
167
	 */
168
	private function getBoard($boardId) {
169
		if (!array_key_exists($boardId, $this->boards)) {
170
			$this->boards[$boardId] = $this->boardMapper->find($boardId);
171
		}
172
		return $this->boards[$boardId];
173
	}
174
175
	/**
176
	 * @param Board $board
177
	 */
178
	private function generateBoardShared($board, $userId) {
179
		$notification = $this->notificationManager->createNotification();
180
		$notification
181
			->setApp('deck')
182
			->setUser((string) $userId)
183
			->setDateTime(new DateTime())
184
			->setObject('board', $board->getId())
185
			->setSubject('board-shared', [$board->getTitle(), $this->currentUser]);
186
		return $notification;
187
	}
188
189
}
190