Completed
Push — master ( 10c099...1d7775 )
by Julius
40s queued 35s
created

NotificationHelper::sendMention()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
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 sendCardAssigned($card, $userId) {
99
		$boardId = $this->cardMapper->findBoardId($card->getId());
100
		$board = $this->getBoard($boardId);
101
102
		$notification = $this->notificationManager->createNotification();
103
		$notification
104
			->setApp('deck')
105
			->setUser((string) $userId)
106
			->setDateTime(new DateTime())
107
			->setObject('card', $card->getId())
108
				->setSubject('card-assigned', [
109
					$card->getTitle(),
110
					$board->getTitle(),
111
					$this->currentUser
112
				]);
113
		$this->notificationManager->notify($notification);
114
	}
115
116
	/**
117
	 * Send notifications that a board was shared with a user/group
118
	 *
119
	 * @param $boardId
120
	 * @param Acl $acl
121
	 * @throws \InvalidArgumentException
122
	 */
123
	public function sendBoardShared($boardId, $acl) {
124
		$board = $this->getBoard($boardId);
125
		if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
126
			$notification = $this->generateBoardShared($board, $acl->getParticipant());
127
			$this->notificationManager->notify($notification);
128
		}
129
		if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) {
130
			$group = $this->groupManager->get($acl->getParticipant());
131
			foreach ($group->getUsers() as $user) {
132
				$notification = $this->generateBoardShared($board, $user->getUID());
133
				$this->notificationManager->notify($notification);
134
			}
135
		}
136
	}
137
138
	public function sendMention(IComment $comment) {
139
		foreach ($comment->getMentions() as $mention) {
140
			$card = $this->cardMapper->find($comment->getObjectId());
141
			$boardId = $this->cardMapper->findBoardId($card->getId());
142
			$notification = $this->notificationManager->createNotification();
143
			$notification
144
				->setApp('deck')
145
				->setUser((string) $mention['id'])
146
				->setDateTime(new DateTime())
147
				->setObject('card', (string) $card->getId())
148
				->setSubject('card-comment-mentioned', [$card->getTitle(), $boardId, $this->currentUser])
149
				->setMessage($comment->getMessage());
150
			$this->notificationManager->notify($notification);
151
		}
152
	}
153
154
	/**
155
	 * @param $boardId
156
	 * @return Board
157
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
158
	 */
159
	private function getBoard($boardId) {
160
		if (!array_key_exists($boardId, $this->boards)) {
161
			$this->boards[$boardId] = $this->boardMapper->find($boardId);
162
		}
163
		return $this->boards[$boardId];
164
	}
165
166
	/**
167
	 * @param Board $board
168
	 */
169
	private function generateBoardShared($board, $userId) {
170
		$notification = $this->notificationManager->createNotification();
171
		$notification
172
			->setApp('deck')
173
			->setUser((string) $userId)
174
			->setDateTime(new DateTime())
175
			->setObject('board', $board->getId())
176
			->setSubject('board-shared', [$board->getTitle(), $this->currentUser]);
177
		return $notification;
178
	}
179
180
}
181