Completed
Pull Request — master (#625)
by Maxence
02:28
created

Notifier   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 0
loc 186
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 1
A getID() 0 3 1
A getName() 0 3 1
A prepare() 0 21 4
A prepareMemberNotification() 0 45 4
A prepareActions() 0 20 5
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Notification;
33
34
35
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
36
use Exception;
37
use InvalidArgumentException;
38
use OCA\Circles\AppInfo\Application;
39
use OCA\Circles\Exceptions\FederatedUserException;
40
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
41
use OCA\Circles\Exceptions\InitiatorNotFoundException;
42
use OCA\Circles\Exceptions\InvalidIdException;
43
use OCA\Circles\Exceptions\MemberNotFoundException;
44
use OCA\Circles\Exceptions\RequestBuilderException;
45
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
46
use OCA\Circles\Service\FederatedUserService;
47
use OCA\Circles\Service\MemberService;
48
use OCP\Contacts\IManager;
49
use OCP\Federation\ICloudIdManager;
50
use OCP\IL10N;
51
use OCP\IURLGenerator;
52
use OCP\L10N\IFactory;
53
use OCP\Notification\INotification;
54
use OCP\Notification\INotifier;
55
56
57
/**
58
 * Class Notifier
59
 *
60
 * @package OCA\Circles\Notification
61
 */
62
class Notifier implements INotifier {
63
64
65
	use TNC22Logger;
66
67
68
	/** @var IL10N */
69
	private $l10n;
70
71
72
	/** @var IFactory */
73
	protected $factory;
74
75
	/** @var IManager */
76
	protected $contactsManager;
77
78
	/** @var IURLGenerator */
79
	protected $urlGenerator;
80
81
	/** @var array */
82
	protected $federatedContacts;
83
84
	/** @var ICloudIdManager */
85
	protected $cloudIdManager;
86
87
	/** @var FederatedUserService */
88
	private $federatedUserService;
89
90
	/** @var MemberService */
91
	private $memberService;
92
93
94
	public function __construct(
95
		IL10N $l10n,
96
		IFactory $factory,
97
		IManager $contactsManager,
98
		IURLGenerator $urlGenerator,
99
		ICloudIdManager $cloudIdManager,
100
		MemberService $memberService,
101
		FederatedUserService $federatedUserService
102
	) {
103
		$this->l10n = $l10n;
104
		$this->factory = $factory;
105
		$this->contactsManager = $contactsManager;
106
		$this->urlGenerator = $urlGenerator;
107
		$this->cloudIdManager = $cloudIdManager;
108
109
		$this->federatedUserService = $federatedUserService;
110
		$this->memberService = $memberService;
111
112
		$this->setup('app', Application::APP_ID);
113
	}
114
115
	/**
116
	 * Identifier of the notifier, only use [a-z0-9_]
117
	 *
118
	 * @return string
119
	 * @since 17.0.0
120
	 */
121
	public function getID(): string {
122
		return Application::APP_ID;
123
	}
124
125
	/**
126
	 * Human readable name describing the notifier
127
	 *
128
	 * @return string
129
	 * @since 17.0.0
130
	 */
131
	public function getName(): string {
132
		return $this->l10n->t(Application::APP_NAME);
133
	}
134
135
	/**
136
	 * @param INotification $notification
137
	 * @param string $languageCode The code of the language that should be used to prepare the notification
138
	 *
139
	 * @return INotification
140
	 * @throws InvalidArgumentException
141
	 */
142
	public function prepare(INotification $notification, string $languageCode): INotification {
143
		if ($notification->getApp() !== Application::APP_ID) {
144
			throw new InvalidArgumentException();
145
		}
146
147
		$iconPath = $this->urlGenerator->imagePath(Application::APP_ID, 'black_circle.svg');
148
		$notification->setIcon($this->urlGenerator->getAbsoluteURL($iconPath));
149
150
		if ($notification->getObjectType() === 'member') {
151
			try {
152
				$this->prepareMemberNotification($notification);
153
			} catch (Exception $e) {
154
				// TODO: delete notification
155
			}
156
		}
157
158
		$this->prepareActions($notification);
159
160
		return $notification;
161
162
	}
163
164
165
	/**
166
	 * @param INotification $notification
167
	 *
168
	 * @throws InitiatorNotFoundException
169
	 * @throws MemberNotFoundException
170
	 * @throws RequestBuilderException
171
	 * @throws FederatedUserException
172
	 * @throws FederatedUserNotFoundException
173
	 * @throws InvalidIdException
174
	 * @throws SingleCircleNotFoundException
175
	 */
176
	private function prepareMemberNotification(INotification $notification) {
177
		$this->federatedUserService->initCurrentUser();
178
		$member = $this->memberService->getMemberById(
179
			$notification->getObjectId(),
180
			'',
181
			true
182
		);
183
184
		switch ($notification->getSubject()) {
185
			case 'memberAdd':
186
				$subject = $this->l10n->t(
187
					'You are now a member of the Circle "%2$s"',
188
					[
189
						$member->getCircle()->getDisplayName()
190
					]
191
				);
192
				break;
193
194
			case 'invitation':
195
				$subject = $this->l10n->t(
196
					'You have been invited by %1$s into the Circle "%2$s"',
197
					[
198
						$member->getInvitedBy()->getDisplayName(),
199
						$member->getCircle()->getDisplayName()
200
					]
201
				);
202
				break;
203
204
			case 'joinRequest':
205
				$subject = $this->l10n->t(
206
					'%1$s sent a request to be a member of the Circle "%2$s"',
207
					[
208
						$member->getDisplayName(),
209
						$member->getCircle()->getDisplayName()
210
					]
211
				);
212
				break;
213
214
			default:
215
				throw new InvalidArgumentException();
216
		}
217
218
		$notification->setParsedSubject($subject);
219
220
	}
221
222
223
	/**
224
	 * @param INotification $notification
225
	 */
226
	private function prepareActions(INotification $notification): void {
227
		foreach ($notification->getActions() as $action) {
228
			switch ($action->getLabel()) {
229
				case 'accept':
230
					$action->setParsedLabel($this->l10n->t('Accept'))
231
						   ->setPrimary(true);
232
					break;
233
234
				case 'refuse':
235
					$action->setParsedLabel($this->l10n->t('Refuse'));
236
					break;
237
238
				case 'leave':
239
					$action->setParsedLabel($this->l10n->t('Leave the circle'));
240
					break;
241
			}
242
243
			$notification->addParsedAction($action);
244
		}
245
	}
246
247
}
248
249