Passed
Push — master ( 782554...5b604e )
by Morris
10:22
created

Notifier::getID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Comments\Notification;
26
27
use OCP\Comments\IComment;
28
use OCP\Comments\ICommentsManager;
29
use OCP\Comments\NotFoundException;
30
use OCP\Files\IRootFolder;
31
use OCP\IURLGenerator;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
use OCP\L10N\IFactory;
35
use OCP\Notification\AlreadyProcessedException;
36
use OCP\Notification\INotification;
37
use OCP\Notification\INotifier;
38
39
class Notifier implements INotifier {
40
41
	/** @var IFactory */
42
	protected $l10nFactory;
43
44
	/** @var IRootFolder  */
45
	protected $rootFolder;
46
47
	/** @var ICommentsManager  */
48
	protected $commentsManager;
49
50
	/** @var IURLGenerator */
51
	protected $url;
52
53
	/** @var IUserManager */
54
	protected $userManager;
55
56
	public function __construct(
57
		IFactory $l10nFactory,
58
		IRootFolder $rootFolder,
59
		ICommentsManager $commentsManager,
60
		IURLGenerator $url,
61
		IUserManager $userManager
62
	) {
63
		$this->l10nFactory = $l10nFactory;
64
		$this->rootFolder = $rootFolder;
65
		$this->commentsManager = $commentsManager;
66
		$this->url = $url;
67
		$this->userManager = $userManager;
68
	}
69
70
	/**
71
	 * Identifier of the notifier, only use [a-z0-9_]
72
	 *
73
	 * @return string
74
	 * @since 17.0.0
75
	 */
76
	public function getID(): string {
77
		return 'comments';
78
	}
79
80
	/**
81
	 * Human readable name describing the notifier
82
	 *
83
	 * @return string
84
	 * @since 17.0.0
85
	 */
86
	public function getName(): string {
87
		return $this->l10nFactory->get('comments')->t('Comments');
88
	}
89
90
	/**
91
	 * @param INotification $notification
92
	 * @param string $languageCode The code of the language that should be used to prepare the notification
93
	 * @return INotification
94
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
95
	 * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
96
	 * @since 9.0.0
97
	 */
98
	public function prepare(INotification $notification, string $languageCode): INotification {
99
		if($notification->getApp() !== 'comments') {
100
			throw new \InvalidArgumentException();
101
		}
102
		try {
103
			$comment = $this->commentsManager->get($notification->getObjectId());
104
		} catch(NotFoundException $e) {
105
			// needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all
106
			throw new \InvalidArgumentException('Comment not found', 0, $e);
107
		}
108
		$l = $this->l10nFactory->get('comments', $languageCode);
109
		$displayName = $comment->getActorId();
110
		$isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER;
111
		if ($comment->getActorType() === 'users') {
112
			$commenter = $this->userManager->get($comment->getActorId());
113
			if ($commenter instanceof IUser) {
114
				$displayName = $commenter->getDisplayName();
115
			}
116
		}
117
118
		switch ($notification->getSubject()) {
119
			case 'mention':
120
				$parameters = $notification->getSubjectParameters();
121
				if($parameters[0] !== 'files') {
122
					throw new \InvalidArgumentException('Unsupported comment object');
123
				}
124
				$userFolder = $this->rootFolder->getUserFolder($notification->getUser());
125
				$nodes = $userFolder->getById((int)$parameters[1]);
126
				if(empty($nodes)) {
127
					throw new AlreadyProcessedException();
128
				}
129
				$node = $nodes[0];
130
131
				$path = rtrim($node->getPath(), '/');
132
				if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) {
133
					// Remove /user/files/...
134
					$fullPath = $path;
135
					list(,,, $path) = explode('/', $fullPath, 4);
136
				}
137
				$subjectParameters = [
138
					'file' => [
139
						'type' => 'file',
140
						'id' => $comment->getObjectId(),
141
						'name' => $node->getName(),
142
						'path' => $path,
143
						'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]),
144
					],
145
				];
146
147
				if ($isDeletedActor) {
148
					$subject = $l->t('You were mentioned on “{file}”, in a comment by a user that has since been deleted');
149
				} else {
150
					$subject = $l->t('{user} mentioned you in a comment on “{file}”');
151
					$subjectParameters['user'] = [
152
						'type' => 'user',
153
						'id' => $comment->getActorId(),
154
						'name' => $displayName,
155
					];
156
				}
157
				list($message, $messageParameters) = $this->commentToRichMessage($comment);
158
				$notification->setRichSubject($subject, $subjectParameters)
159
					->setParsedSubject($this->richToParsed($subject, $subjectParameters))
160
					->setRichMessage($message, $messageParameters)
161
					->setParsedMessage($this->richToParsed($message, $messageParameters))
162
					->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')))
163
					->setLink($this->url->linkToRouteAbsolute(
164
						'comments.Notifications.view',
165
						['id' => $comment->getId()])
166
					);
167
168
				return $notification;
169
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
170
171
			default:
172
				throw new \InvalidArgumentException('Invalid subject');
173
		}
174
	}
175
176
	public function commentToRichMessage(IComment $comment): array {
177
		$message = $comment->getMessage();
178
		$messageParameters = [];
179
		$mentionTypeCount = [];
180
		$mentions = $comment->getMentions();
181
		foreach ($mentions as $mention) {
182
			if ($mention['type'] === 'user') {
183
				$user = $this->userManager->get($mention['id']);
184
				if (!$user instanceof IUser) {
185
					continue;
186
				}
187
			}
188
			if (!array_key_exists($mention['type'], $mentionTypeCount)) {
189
				$mentionTypeCount[$mention['type']] = 0;
190
			}
191
			$mentionTypeCount[$mention['type']]++;
192
			// To keep a limited character set in parameter IDs ([a-zA-Z0-9-])
193
			// the mention parameter ID does not include the mention ID (which
194
			// could contain characters like '@' for user IDs) but a one-based
195
			// index of the mentions of that type.
196
			$mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']];
197
			$message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message);
198
			try {
199
				$displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']);
200
			} catch (\OutOfBoundsException $e) {
201
				// There is no registered display name resolver for the mention
202
				// type, so the client decides what to display.
203
				$displayName = '';
204
			}
205
			$messageParameters[$mentionParameterId] = [
206
				'type' => $mention['type'],
207
				'id' => $mention['id'],
208
				'name' => $displayName
209
			];
210
		}
211
		return [$message, $messageParameters];
212
	}
213
214
	public function richToParsed(string $message, array $parameters): string {
215
		$placeholders = $replacements = [];
216
		foreach ($parameters as $placeholder => $parameter) {
217
			$placeholders[] = '{' . $placeholder . '}';
218
			if ($parameter['type'] === 'user') {
219
				$replacements[] = '@' . $parameter['name'];
220
			} else if ($parameter['type'] === 'file') {
221
				$replacements[] = $parameter['path'];
222
			} else {
223
				$replacements[] = $parameter['name'];
224
			}
225
		}
226
		return str_replace($placeholders, $replacements, $message);
227
	}
228
}
229