Completed
Pull Request — master (#44)
by Joas
02:42
created

EventHandler::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Arthur Schiwon <[email protected]>
4
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
5
 *
6
 * @author Arthur Schiwon <[email protected]>
7
 * @author Joas Schilling <[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
26
namespace OCA\AnnouncementCenter\Comments;
27
28
29
use OCP\Comments\CommentsEvent;
30
use OCP\Comments\IComment;
31
use OCP\Comments\ICommentsEventHandler;
32
use OCP\IURLGenerator;
33
use OCP\IUserManager;
34
use OCP\Notification\IManager as INotificationManager;
35
use OCP\Notification\INotification;
36
37
class EventHandler implements ICommentsEventHandler {
38
39
	/** @var IUserManager */
40
	protected $userManager;
41
	/** @var INotificationManager */
42
	protected $notificationManager;
43
	/** @var IURLGenerator */
44
	protected $urlGenerator;
45
46
	/**
47
	 * EventHandler constructor.
48
	 *
49
	 * @param IUserManager $userManager
50
	 * @param INotificationManager $notificationManager
51
	 * @param IURLGenerator $urlGenerator
52
	 */
53
	public function __construct(IUserManager $userManager, INotificationManager $notificationManager, IURLGenerator $urlGenerator) {
54
		$this->userManager = $userManager;
55
		$this->notificationManager = $notificationManager;
56
		$this->urlGenerator = $urlGenerator;
57
	}
58
59
	/**
60
	 * @param CommentsEvent $event
61
	 * @since 9.2.0
62
	 */
63
	public function handle(CommentsEvent $event) {
64
		if ($event->getComment()->getObjectType() !== 'announcement') {
65
			return;
66
		}
67
68
		$eventType = $event->getEvent();
69
		if (in_array($eventType, [
70
			CommentsEvent::EVENT_ADD,
71
			CommentsEvent::EVENT_PRE_UPDATE,
72
			CommentsEvent::EVENT_UPDATE,
73
			CommentsEvent::EVENT_DELETE,
74
		])) {
75
			$this->evaluateMentions($event);
76
		}
77
	}
78
79
	/**
80
	 * @param CommentsEvent $event
81
	 */
82
	public function evaluateMentions(CommentsEvent $event) {
83
		$comment = $event->getComment();
84
		$mentions = $this->extractMentions($comment->getMentions());
85
		if (empty($mentions)) {
86
			// no one to notify
87
			return;
88
		}
89
90
		$notification = $this->instantiateNotification($comment);
91
92
		foreach ($mentions as $user) {
93
			if( ($comment->getActorType() === 'users' && $user === $comment->getActorId())
94
				|| !$this->userManager->userExists($user)
95
			) {
96
				// do not notify unknown users or yourself
97
				continue;
98
			}
99
100
			$notification->setUser($user);
101
			if (in_array($event->getEvent(), [CommentsEvent::EVENT_DELETE, CommentsEvent::EVENT_PRE_UPDATE])) {
102
				$this->notificationManager->markProcessed($notification);
103
			} else {
104
				$this->notificationManager->notify($notification);
105
			}
106
		}
107
	}
108
109
	/**
110
	 * creates a notification instance and fills it with comment data
111
	 *
112
	 * @param IComment $comment
113
	 * @return INotification
114
	 */
115
	public function instantiateNotification(IComment $comment) {
116
		$notification = $this->notificationManager->createNotification();
117
		$notification
118
			->setApp('announcementcenter')
119
			->setObject('comment', $comment->getId())
120
			->setSubject('mention', [$comment->getObjectType(), $comment->getObjectId()])
121
			->setDateTime($comment->getCreationDateTime())
122
			->setLink($this->urlGenerator->linkToRouteAbsolute(
123
				'announcementcenter.page.followComment',
124
					['id' => $comment->getId()]
125
			));
126
127
		return $notification;
128
	}
129
130
	/**
131
	 * flattens the mention array returned from comments to a list of user ids.
132
	 *
133
	 * @param array $mentions
134
	 * @return string[] containing the mentions, e.g. ['@alice', '@bob']
135
	 */
136
	public function extractMentions($mentions) {
137
		if (empty($mentions)) {
138
			return [];
139
		}
140
		$uids = [];
141
		foreach ($mentions as $mention) {
142
			if($mention['type'] === 'user') {
143
				$uids[] = $mention['id'];
144
			}
145
		}
146
		return $uids;
147
	}
148
}
149