NotificationHelper   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 11
dl 0
loc 157
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A sendCardDuedate() 0 27 3
A markDuedateAsRead() 0 8 1
A sendCardAssigned() 0 17 1
A sendBoardShared() 0 14 4
A sendMention() 0 15 2
A getBoard() 0 6 2
A generateBoardShared() 0 10 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
	/**
71
	 * @param $card
72
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
73
	 */
74
	public function sendCardDuedate($card) {
75
		// check if notification has already been sent
76
		// ideally notifications should not be deleted once seen by the user so we can
77
		// also deliver due date notifications for users who have been added later to a board
78
		// this should maybe be addressed in nextcloud/server
79
		if ($card->getNotified()) {
80
			return;
81
		}
82
83
		// TODO: Once assigning users is possible, those should be notified instead of all users of the board
84
		$boardId = $this->cardMapper->findBoardId($card->getId());
85
		$board = $this->getBoard($boardId);
86
		/** @var IUser $user */
87
		foreach ($this->permissionService->findUsers($boardId) as $user) {
88
			$notification = $this->notificationManager->createNotification();
89
			$notification
90
				->setApp('deck')
91
				->setUser((string) $user->getUID())
92
				->setObject('card', $card->getId())
93
				->setSubject('card-overdue', [
94
					$card->getTitle(), $board->getTitle()
95
				])
96
				->setDateTime(new DateTime($card->getDuedate()));
97
			$this->notificationManager->notify($notification);
98
		}
99
		$this->cardMapper->markNotified($card);
100
	}
101
102
	public function markDuedateAsRead($card) {
103
		$notification = $this->notificationManager->createNotification();
104
		$notification
105
			->setApp('deck')
106
			->setObject('card', $card->getId())
107
			->setSubject('card-overdue', []);
108
		$this->notificationManager->markProcessed($notification);
109
	}
110
111
	public function sendCardAssigned($card, $userId) {
112
		$boardId = $this->cardMapper->findBoardId($card->getId());
113
		$board = $this->getBoard($boardId);
114
115
		$notification = $this->notificationManager->createNotification();
116
		$notification
117
			->setApp('deck')
118
			->setUser((string) $userId)
119
			->setDateTime(new DateTime())
120
			->setObject('card', $card->getId())
121
				->setSubject('card-assigned', [
122
					$card->getTitle(),
123
					$board->getTitle(),
124
					$this->currentUser
125
				]);
126
		$this->notificationManager->notify($notification);
127
	}
128
129
	/**
130
	 * Send notifications that a board was shared with a user/group
131
	 *
132
	 * @param $boardId
133
	 * @param Acl $acl
134
	 * @throws \InvalidArgumentException
135
	 */
136
	public function sendBoardShared($boardId, $acl) {
137
		$board = $this->getBoard($boardId);
138
		if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
139
			$notification = $this->generateBoardShared($board, $acl->getParticipant());
140
			$this->notificationManager->notify($notification);
141
		}
142
		if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) {
143
			$group = $this->groupManager->get($acl->getParticipant());
144
			foreach ($group->getUsers() as $user) {
145
				$notification = $this->generateBoardShared($board, $user->getUID());
146
				$this->notificationManager->notify($notification);
147
			}
148
		}
149
	}
150
151
	public function sendMention(IComment $comment) {
152
		foreach ($comment->getMentions() as $mention) {
153
			$card = $this->cardMapper->find($comment->getObjectId());
154
			$boardId = $this->cardMapper->findBoardId($card->getId());
155
			$notification = $this->notificationManager->createNotification();
156
			$notification
157
				->setApp('deck')
158
				->setUser((string) $mention['id'])
159
				->setDateTime(new DateTime())
160
				->setObject('card', (string) $card->getId())
161
				->setSubject('card-comment-mentioned', [$card->getTitle(), $boardId, $this->currentUser])
162
				->setMessage('{message}', ['message' => $comment->getMessage()]);
163
			$this->notificationManager->notify($notification);
164
		}
165
	}
166
167
	/**
168
	 * @param $boardId
169
	 * @return Board
170
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
171
	 */
172
	private function getBoard($boardId) {
173
		if (!array_key_exists($boardId, $this->boards)) {
174
			$this->boards[$boardId] = $this->boardMapper->find($boardId);
175
		}
176
		return $this->boards[$boardId];
177
	}
178
179
	/**
180
	 * @param Board $board
181
	 */
182
	private function generateBoardShared($board, $userId) {
183
		$notification = $this->notificationManager->createNotification();
184
		$notification
185
			->setApp('deck')
186
			->setUser((string) $userId)
187
			->setDateTime(new DateTime())
188
			->setObject('board', $board->getId())
189
			->setSubject('board-shared', [$board->getTitle(), $this->currentUser]);
190
		return $notification;
191
	}
192
193
}
194