Passed
Push — master ( fce6df...8e01ff )
by Georg
14:04 queued 11s
created
apps/user_status/lib/Service/JSDataService.php 1 patch
Indentation   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -30,55 +30,55 @@
 block discarded – undo
30 30
 
31 31
 class JSDataService implements \JsonSerializable {
32 32
 
33
-	/** @var IUserSession */
34
-	private $userSession;
33
+    /** @var IUserSession */
34
+    private $userSession;
35 35
 
36
-	/** @var StatusService */
37
-	private $statusService;
36
+    /** @var StatusService */
37
+    private $statusService;
38 38
 
39
-	/**
40
-	 * JSDataService constructor.
41
-	 *
42
-	 * @param IUserSession $userSession
43
-	 * @param StatusService $statusService
44
-	 */
45
-	public function __construct(IUserSession $userSession,
46
-								StatusService $statusService) {
47
-		$this->userSession = $userSession;
48
-		$this->statusService = $statusService;
49
-	}
39
+    /**
40
+     * JSDataService constructor.
41
+     *
42
+     * @param IUserSession $userSession
43
+     * @param StatusService $statusService
44
+     */
45
+    public function __construct(IUserSession $userSession,
46
+                                StatusService $statusService) {
47
+        $this->userSession = $userSession;
48
+        $this->statusService = $statusService;
49
+    }
50 50
 
51
-	public function jsonSerialize() {
52
-		$user = $this->userSession->getUser();
51
+    public function jsonSerialize() {
52
+        $user = $this->userSession->getUser();
53 53
 
54
-		if ($user === null) {
55
-			return [];
56
-		}
54
+        if ($user === null) {
55
+            return [];
56
+        }
57 57
 
58
-		try {
59
-			$status = $this->statusService->findByUserId($user->getUID());
60
-		} catch (DoesNotExistException $ex) {
61
-			return [
62
-				'userId' => $user->getUID(),
63
-				'message' => null,
64
-				'messageId' => null,
65
-				'messageIsPredefined' => false,
66
-				'icon' => null,
67
-				'clearAt' => null,
68
-				'status' => 'offline',
69
-				'statusIsUserDefined' => false,
70
-			];
71
-		}
58
+        try {
59
+            $status = $this->statusService->findByUserId($user->getUID());
60
+        } catch (DoesNotExistException $ex) {
61
+            return [
62
+                'userId' => $user->getUID(),
63
+                'message' => null,
64
+                'messageId' => null,
65
+                'messageIsPredefined' => false,
66
+                'icon' => null,
67
+                'clearAt' => null,
68
+                'status' => 'offline',
69
+                'statusIsUserDefined' => false,
70
+            ];
71
+        }
72 72
 
73
-		return [
74
-			'userId' => $status->getUserId(),
75
-			'message' => $status->getCustomMessage(),
76
-			'messageId' => $status->getMessageId(),
77
-			'messageIsPredefined' => $status->getMessageId() !== null,
78
-			'icon' => $status->getCustomIcon(),
79
-			'clearAt' => $status->getClearAt(),
80
-			'status' => $status->getStatus(),
81
-			'statusIsUserDefined' => $status->getIsUserDefined(),
82
-		];
83
-	}
73
+        return [
74
+            'userId' => $status->getUserId(),
75
+            'message' => $status->getCustomMessage(),
76
+            'messageId' => $status->getMessageId(),
77
+            'messageIsPredefined' => $status->getMessageId() !== null,
78
+            'icon' => $status->getCustomIcon(),
79
+            'clearAt' => $status->getClearAt(),
80
+            'status' => $status->getStatus(),
81
+            'statusIsUserDefined' => $status->getIsUserDefined(),
82
+        ];
83
+    }
84 84
 }
Please login to merge, or discard this patch.
apps/user_status/lib/Service/EmojiService.php 1 patch
Indentation   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -34,67 +34,67 @@
 block discarded – undo
34 34
  */
35 35
 class EmojiService {
36 36
 
37
-	/** @var IDBConnection */
38
-	private $db;
39
-
40
-	/**
41
-	 * EmojiService constructor.
42
-	 *
43
-	 * @param IDBConnection $db
44
-	 */
45
-	public function __construct(IDBConnection $db) {
46
-		$this->db = $db;
47
-	}
48
-
49
-	/**
50
-	 * @return bool
51
-	 */
52
-	public function doesPlatformSupportEmoji(): bool {
53
-		return $this->db->supports4ByteText() &&
54
-			\class_exists(\IntlBreakIterator::class);
55
-	}
56
-
57
-	/**
58
-	 * @param string $emoji
59
-	 * @return bool
60
-	 */
61
-	public function isValidEmoji(string $emoji): bool {
62
-		$intlBreakIterator = \IntlBreakIterator::createCharacterInstance();
63
-		$intlBreakIterator->setText($emoji);
64
-
65
-		$characterCount = 0;
66
-		while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) {
67
-			$characterCount++;
68
-		}
69
-
70
-		if ($characterCount !== 1) {
71
-			return false;
72
-		}
73
-
74
-		$codePointIterator = \IntlBreakIterator::createCodePointInstance();
75
-		$codePointIterator->setText($emoji);
76
-
77
-		foreach ($codePointIterator->getPartsIterator() as $codePoint) {
78
-			$codePointType = \IntlChar::charType($codePoint);
79
-
80
-			// If the current code-point is an emoji or a modifier (like a skin-tone)
81
-			// just continue and check the next character
82
-			if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL ||
83
-				$codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER ||
84
-				$codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL) {
85
-				continue;
86
-			}
87
-
88
-			// If it's neither a modifier nor an emoji, we only allow
89
-			// a zero-width-joiner or a variation selector 16
90
-			$codePointValue = \IntlChar::ord($codePoint);
91
-			if ($codePointValue === 8205 || $codePointValue === 65039) {
92
-				continue;
93
-			}
94
-
95
-			return false;
96
-		}
97
-
98
-		return true;
99
-	}
37
+    /** @var IDBConnection */
38
+    private $db;
39
+
40
+    /**
41
+     * EmojiService constructor.
42
+     *
43
+     * @param IDBConnection $db
44
+     */
45
+    public function __construct(IDBConnection $db) {
46
+        $this->db = $db;
47
+    }
48
+
49
+    /**
50
+     * @return bool
51
+     */
52
+    public function doesPlatformSupportEmoji(): bool {
53
+        return $this->db->supports4ByteText() &&
54
+            \class_exists(\IntlBreakIterator::class);
55
+    }
56
+
57
+    /**
58
+     * @param string $emoji
59
+     * @return bool
60
+     */
61
+    public function isValidEmoji(string $emoji): bool {
62
+        $intlBreakIterator = \IntlBreakIterator::createCharacterInstance();
63
+        $intlBreakIterator->setText($emoji);
64
+
65
+        $characterCount = 0;
66
+        while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) {
67
+            $characterCount++;
68
+        }
69
+
70
+        if ($characterCount !== 1) {
71
+            return false;
72
+        }
73
+
74
+        $codePointIterator = \IntlBreakIterator::createCodePointInstance();
75
+        $codePointIterator->setText($emoji);
76
+
77
+        foreach ($codePointIterator->getPartsIterator() as $codePoint) {
78
+            $codePointType = \IntlChar::charType($codePoint);
79
+
80
+            // If the current code-point is an emoji or a modifier (like a skin-tone)
81
+            // just continue and check the next character
82
+            if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL ||
83
+                $codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER ||
84
+                $codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL) {
85
+                continue;
86
+            }
87
+
88
+            // If it's neither a modifier nor an emoji, we only allow
89
+            // a zero-width-joiner or a variation selector 16
90
+            $codePointValue = \IntlChar::ord($codePoint);
91
+            if ($codePointValue === 8205 || $codePointValue === 65039) {
92
+                continue;
93
+            }
94
+
95
+            return false;
96
+        }
97
+
98
+        return true;
99
+    }
100 100
 }
Please login to merge, or discard this patch.
apps/user_status/lib/Service/PredefinedStatusService.php 1 patch
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -36,152 +36,152 @@
 block discarded – undo
36 36
  * @package OCA\UserStatus\Service
37 37
  */
38 38
 class PredefinedStatusService {
39
-	private const MEETING = 'meeting';
40
-	private const COMMUTING = 'commuting';
41
-	private const SICK_LEAVE = 'sick-leave';
42
-	private const VACATIONING = 'vacationing';
43
-	private const REMOTE_WORK = 'remote-work';
44
-
45
-	/** @var IL10N */
46
-	private $l10n;
47
-
48
-	/**
49
-	 * DefaultStatusService constructor.
50
-	 *
51
-	 * @param IL10N $l10n
52
-	 */
53
-	public function __construct(IL10N $l10n) {
54
-		$this->l10n = $l10n;
55
-	}
56
-
57
-	/**
58
-	 * @return array
59
-	 */
60
-	public function getDefaultStatuses(): array {
61
-		return [
62
-			[
63
-				'id' => self::MEETING,
64
-				'icon' => '
Please login to merge, or discard this patch.
apps/user_status/lib/Service/StatusService.php 2 patches
Indentation   +290 added lines, -290 removed lines patch added patch discarded remove patch
@@ -42,294 +42,294 @@
 block discarded – undo
42 42
  */
43 43
 class StatusService {
44 44
 
45
-	/** @var UserStatusMapper */
46
-	private $mapper;
47
-
48
-	/** @var ITimeFactory */
49
-	private $timeFactory;
50
-
51
-	/** @var PredefinedStatusService */
52
-	private $predefinedStatusService;
53
-
54
-	/** @var EmojiService */
55
-	private $emojiService;
56
-
57
-	/** @var string[] */
58
-	private $allowedStatusTypes = [
59
-		'online',
60
-		'away',
61
-		'dnd',
62
-		'invisible',
63
-		'offline'
64
-	];
65
-
66
-	/** @var int */
67
-	private $maximumMessageLength = 80;
68
-
69
-	/**
70
-	 * StatusService constructor.
71
-	 *
72
-	 * @param UserStatusMapper $mapper
73
-	 * @param ITimeFactory $timeFactory
74
-	 * @param PredefinedStatusService $defaultStatusService,
75
-	 * @param EmojiService $emojiService
76
-	 */
77
-	public function __construct(UserStatusMapper $mapper,
78
-								ITimeFactory $timeFactory,
79
-								PredefinedStatusService $defaultStatusService,
80
-								EmojiService $emojiService) {
81
-		$this->mapper = $mapper;
82
-		$this->timeFactory = $timeFactory;
83
-		$this->predefinedStatusService = $defaultStatusService;
84
-		$this->emojiService = $emojiService;
85
-	}
86
-
87
-	/**
88
-	 * @param int|null $limit
89
-	 * @param int|null $offset
90
-	 * @return UserStatus[]
91
-	 */
92
-	public function findAll(?int $limit = null, ?int $offset = null): array {
93
-		return array_map(function ($status) {
94
-			return $this->processStatus($status);
95
-		}, $this->mapper->findAll($limit, $offset));
96
-	}
97
-
98
-	/**
99
-	 * @param string $userId
100
-	 * @return UserStatus
101
-	 * @throws DoesNotExistException
102
-	 */
103
-	public function findByUserId(string $userId):UserStatus {
104
-		return $this->processStatus($this->mapper->findByUserId($userId));
105
-	}
106
-
107
-	/**
108
-	 * @param string $userId
109
-	 * @param string $status
110
-	 * @param int|null $statusTimestamp
111
-	 * @param bool $isUserDefined
112
-	 * @return UserStatus
113
-	 * @throws InvalidStatusTypeException
114
-	 */
115
-	public function setStatus(string $userId,
116
-							  string $status,
117
-							  ?int $statusTimestamp,
118
-							  bool $isUserDefined): UserStatus {
119
-		try {
120
-			$userStatus = $this->mapper->findByUserId($userId);
121
-		} catch (DoesNotExistException $ex) {
122
-			$userStatus = new UserStatus();
123
-			$userStatus->setUserId($userId);
124
-		}
125
-
126
-		// Check if status-type is valid
127
-		if (!\in_array($status, $this->allowedStatusTypes, true)) {
128
-			throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported');
129
-		}
130
-		if ($statusTimestamp === null) {
131
-			$statusTimestamp = $this->timeFactory->getTime();
132
-		}
133
-
134
-		$userStatus->setStatus($status);
135
-		$userStatus->setStatusTimestamp($statusTimestamp);
136
-		$userStatus->setIsUserDefined($isUserDefined);
137
-
138
-		if ($userStatus->getId() === null) {
139
-			return $this->mapper->insert($userStatus);
140
-		}
141
-
142
-		return $this->mapper->update($userStatus);
143
-	}
144
-
145
-	/**
146
-	 * @param string $userId
147
-	 * @param string $messageId
148
-	 * @param int|null $clearAt
149
-	 * @return UserStatus
150
-	 * @throws InvalidMessageIdException
151
-	 * @throws InvalidClearAtException
152
-	 */
153
-	public function setPredefinedMessage(string $userId,
154
-										 string $messageId,
155
-										 ?int $clearAt): UserStatus {
156
-		try {
157
-			$userStatus = $this->mapper->findByUserId($userId);
158
-		} catch (DoesNotExistException $ex) {
159
-			$userStatus = new UserStatus();
160
-			$userStatus->setUserId($userId);
161
-			$userStatus->setStatus('offline');
162
-			$userStatus->setStatusTimestamp(0);
163
-			$userStatus->setIsUserDefined(false);
164
-		}
165
-
166
-		if (!$this->predefinedStatusService->isValidId($messageId)) {
167
-			throw new InvalidMessageIdException('Message-Id "' . $messageId . '" is not supported');
168
-		}
169
-
170
-		// Check that clearAt is in the future
171
-		if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
172
-			throw new InvalidClearAtException('ClearAt is in the past');
173
-		}
174
-
175
-		$userStatus->setMessageId($messageId);
176
-		$userStatus->setCustomIcon(null);
177
-		$userStatus->setCustomMessage(null);
178
-		$userStatus->setClearAt($clearAt);
179
-
180
-		if ($userStatus->getId() === null) {
181
-			return $this->mapper->insert($userStatus);
182
-		}
183
-
184
-		return $this->mapper->update($userStatus);
185
-	}
186
-
187
-	/**
188
-	 * @param string $userId
189
-	 * @param string|null $statusIcon
190
-	 * @param string|null $message
191
-	 * @param int|null $clearAt
192
-	 * @return UserStatus
193
-	 * @throws InvalidClearAtException
194
-	 * @throws InvalidStatusIconException
195
-	 * @throws StatusMessageTooLongException
196
-	 */
197
-	public function setCustomMessage(string $userId,
198
-									 ?string $statusIcon,
199
-									 string $message,
200
-									 ?int $clearAt): UserStatus {
201
-		try {
202
-			$userStatus = $this->mapper->findByUserId($userId);
203
-		} catch (DoesNotExistException $ex) {
204
-			$userStatus = new UserStatus();
205
-			$userStatus->setUserId($userId);
206
-			$userStatus->setStatus('offline');
207
-			$userStatus->setStatusTimestamp(0);
208
-			$userStatus->setIsUserDefined(false);
209
-		}
210
-
211
-		// Check if statusIcon contains only one character
212
-		if ($statusIcon !== null && !$this->emojiService->isValidEmoji($statusIcon)) {
213
-			throw new InvalidStatusIconException('Status-Icon is longer than one character');
214
-		}
215
-		// Check for maximum length of custom message
216
-		if (\mb_strlen($message) > $this->maximumMessageLength) {
217
-			throw new StatusMessageTooLongException('Message is longer than supported length of ' . $this->maximumMessageLength . ' characters');
218
-		}
219
-		// Check that clearAt is in the future
220
-		if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
221
-			throw new InvalidClearAtException('ClearAt is in the past');
222
-		}
223
-
224
-		$userStatus->setMessageId(null);
225
-		$userStatus->setCustomIcon($statusIcon);
226
-		$userStatus->setCustomMessage($message);
227
-		$userStatus->setClearAt($clearAt);
228
-
229
-		if ($userStatus->getId() === null) {
230
-			return $this->mapper->insert($userStatus);
231
-		}
232
-
233
-		return $this->mapper->update($userStatus);
234
-	}
235
-
236
-	/**
237
-	 * @param string $userId
238
-	 * @return bool
239
-	 */
240
-	public function clearStatus(string $userId): bool {
241
-		try {
242
-			$userStatus = $this->mapper->findByUserId($userId);
243
-		} catch (DoesNotExistException $ex) {
244
-			// if there is no status to remove, just return
245
-			return false;
246
-		}
247
-
248
-		$userStatus->setStatus('offline');
249
-		$userStatus->setStatusTimestamp(0);
250
-		$userStatus->setIsUserDefined(false);
251
-
252
-		$this->mapper->update($userStatus);
253
-		return true;
254
-	}
255
-
256
-	/**
257
-	 * @param string $userId
258
-	 * @return bool
259
-	 */
260
-	public function clearMessage(string $userId): bool {
261
-		try {
262
-			$userStatus = $this->mapper->findByUserId($userId);
263
-		} catch (DoesNotExistException $ex) {
264
-			// if there is no status to remove, just return
265
-			return false;
266
-		}
267
-
268
-		$userStatus->setMessageId(null);
269
-		$userStatus->setCustomMessage(null);
270
-		$userStatus->setCustomIcon(null);
271
-		$userStatus->setClearAt(null);
272
-
273
-		$this->mapper->update($userStatus);
274
-		return true;
275
-	}
276
-
277
-	/**
278
-	 * @param string $userId
279
-	 * @return bool
280
-	 */
281
-	public function removeUserStatus(string $userId): bool {
282
-		try {
283
-			$userStatus = $this->mapper->findByUserId($userId);
284
-		} catch (DoesNotExistException $ex) {
285
-			// if there is no status to remove, just return
286
-			return false;
287
-		}
288
-
289
-		$this->mapper->delete($userStatus);
290
-		return true;
291
-	}
292
-
293
-	/**
294
-	 * Processes a status to check if custom message is still
295
-	 * up to date and provides translated default status if needed
296
-	 *
297
-	 * @param UserStatus $status
298
-	 * @returns UserStatus
299
-	 */
300
-	private function processStatus(UserStatus $status): UserStatus {
301
-		$clearAt = $status->getClearAt();
302
-		if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
303
-			$this->cleanStatus($status);
304
-		}
305
-		if ($status->getMessageId() !== null) {
306
-			$this->addDefaultMessage($status);
307
-		}
308
-
309
-		return $status;
310
-	}
311
-
312
-	/**
313
-	 * @param UserStatus $status
314
-	 */
315
-	private function cleanStatus(UserStatus $status): void {
316
-		$status->setMessageId(null);
317
-		$status->setCustomIcon(null);
318
-		$status->setCustomMessage(null);
319
-		$status->setClearAt(null);
320
-
321
-		$this->mapper->update($status);
322
-	}
323
-
324
-	/**
325
-	 * @param UserStatus $status
326
-	 */
327
-	private function addDefaultMessage(UserStatus $status): void {
328
-		// If the message is predefined, insert the translated message and icon
329
-		$predefinedMessage = $this->predefinedStatusService->getDefaultStatusById($status->getMessageId());
330
-		if ($predefinedMessage !== null) {
331
-			$status->setCustomMessage($predefinedMessage['message']);
332
-			$status->setCustomIcon($predefinedMessage['icon']);
333
-		}
334
-	}
45
+    /** @var UserStatusMapper */
46
+    private $mapper;
47
+
48
+    /** @var ITimeFactory */
49
+    private $timeFactory;
50
+
51
+    /** @var PredefinedStatusService */
52
+    private $predefinedStatusService;
53
+
54
+    /** @var EmojiService */
55
+    private $emojiService;
56
+
57
+    /** @var string[] */
58
+    private $allowedStatusTypes = [
59
+        'online',
60
+        'away',
61
+        'dnd',
62
+        'invisible',
63
+        'offline'
64
+    ];
65
+
66
+    /** @var int */
67
+    private $maximumMessageLength = 80;
68
+
69
+    /**
70
+     * StatusService constructor.
71
+     *
72
+     * @param UserStatusMapper $mapper
73
+     * @param ITimeFactory $timeFactory
74
+     * @param PredefinedStatusService $defaultStatusService,
75
+     * @param EmojiService $emojiService
76
+     */
77
+    public function __construct(UserStatusMapper $mapper,
78
+                                ITimeFactory $timeFactory,
79
+                                PredefinedStatusService $defaultStatusService,
80
+                                EmojiService $emojiService) {
81
+        $this->mapper = $mapper;
82
+        $this->timeFactory = $timeFactory;
83
+        $this->predefinedStatusService = $defaultStatusService;
84
+        $this->emojiService = $emojiService;
85
+    }
86
+
87
+    /**
88
+     * @param int|null $limit
89
+     * @param int|null $offset
90
+     * @return UserStatus[]
91
+     */
92
+    public function findAll(?int $limit = null, ?int $offset = null): array {
93
+        return array_map(function ($status) {
94
+            return $this->processStatus($status);
95
+        }, $this->mapper->findAll($limit, $offset));
96
+    }
97
+
98
+    /**
99
+     * @param string $userId
100
+     * @return UserStatus
101
+     * @throws DoesNotExistException
102
+     */
103
+    public function findByUserId(string $userId):UserStatus {
104
+        return $this->processStatus($this->mapper->findByUserId($userId));
105
+    }
106
+
107
+    /**
108
+     * @param string $userId
109
+     * @param string $status
110
+     * @param int|null $statusTimestamp
111
+     * @param bool $isUserDefined
112
+     * @return UserStatus
113
+     * @throws InvalidStatusTypeException
114
+     */
115
+    public function setStatus(string $userId,
116
+                                string $status,
117
+                              ?int $statusTimestamp,
118
+                                bool $isUserDefined): UserStatus {
119
+        try {
120
+            $userStatus = $this->mapper->findByUserId($userId);
121
+        } catch (DoesNotExistException $ex) {
122
+            $userStatus = new UserStatus();
123
+            $userStatus->setUserId($userId);
124
+        }
125
+
126
+        // Check if status-type is valid
127
+        if (!\in_array($status, $this->allowedStatusTypes, true)) {
128
+            throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported');
129
+        }
130
+        if ($statusTimestamp === null) {
131
+            $statusTimestamp = $this->timeFactory->getTime();
132
+        }
133
+
134
+        $userStatus->setStatus($status);
135
+        $userStatus->setStatusTimestamp($statusTimestamp);
136
+        $userStatus->setIsUserDefined($isUserDefined);
137
+
138
+        if ($userStatus->getId() === null) {
139
+            return $this->mapper->insert($userStatus);
140
+        }
141
+
142
+        return $this->mapper->update($userStatus);
143
+    }
144
+
145
+    /**
146
+     * @param string $userId
147
+     * @param string $messageId
148
+     * @param int|null $clearAt
149
+     * @return UserStatus
150
+     * @throws InvalidMessageIdException
151
+     * @throws InvalidClearAtException
152
+     */
153
+    public function setPredefinedMessage(string $userId,
154
+                                            string $messageId,
155
+                                         ?int $clearAt): UserStatus {
156
+        try {
157
+            $userStatus = $this->mapper->findByUserId($userId);
158
+        } catch (DoesNotExistException $ex) {
159
+            $userStatus = new UserStatus();
160
+            $userStatus->setUserId($userId);
161
+            $userStatus->setStatus('offline');
162
+            $userStatus->setStatusTimestamp(0);
163
+            $userStatus->setIsUserDefined(false);
164
+        }
165
+
166
+        if (!$this->predefinedStatusService->isValidId($messageId)) {
167
+            throw new InvalidMessageIdException('Message-Id "' . $messageId . '" is not supported');
168
+        }
169
+
170
+        // Check that clearAt is in the future
171
+        if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
172
+            throw new InvalidClearAtException('ClearAt is in the past');
173
+        }
174
+
175
+        $userStatus->setMessageId($messageId);
176
+        $userStatus->setCustomIcon(null);
177
+        $userStatus->setCustomMessage(null);
178
+        $userStatus->setClearAt($clearAt);
179
+
180
+        if ($userStatus->getId() === null) {
181
+            return $this->mapper->insert($userStatus);
182
+        }
183
+
184
+        return $this->mapper->update($userStatus);
185
+    }
186
+
187
+    /**
188
+     * @param string $userId
189
+     * @param string|null $statusIcon
190
+     * @param string|null $message
191
+     * @param int|null $clearAt
192
+     * @return UserStatus
193
+     * @throws InvalidClearAtException
194
+     * @throws InvalidStatusIconException
195
+     * @throws StatusMessageTooLongException
196
+     */
197
+    public function setCustomMessage(string $userId,
198
+                                     ?string $statusIcon,
199
+                                        string $message,
200
+                                     ?int $clearAt): UserStatus {
201
+        try {
202
+            $userStatus = $this->mapper->findByUserId($userId);
203
+        } catch (DoesNotExistException $ex) {
204
+            $userStatus = new UserStatus();
205
+            $userStatus->setUserId($userId);
206
+            $userStatus->setStatus('offline');
207
+            $userStatus->setStatusTimestamp(0);
208
+            $userStatus->setIsUserDefined(false);
209
+        }
210
+
211
+        // Check if statusIcon contains only one character
212
+        if ($statusIcon !== null && !$this->emojiService->isValidEmoji($statusIcon)) {
213
+            throw new InvalidStatusIconException('Status-Icon is longer than one character');
214
+        }
215
+        // Check for maximum length of custom message
216
+        if (\mb_strlen($message) > $this->maximumMessageLength) {
217
+            throw new StatusMessageTooLongException('Message is longer than supported length of ' . $this->maximumMessageLength . ' characters');
218
+        }
219
+        // Check that clearAt is in the future
220
+        if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
221
+            throw new InvalidClearAtException('ClearAt is in the past');
222
+        }
223
+
224
+        $userStatus->setMessageId(null);
225
+        $userStatus->setCustomIcon($statusIcon);
226
+        $userStatus->setCustomMessage($message);
227
+        $userStatus->setClearAt($clearAt);
228
+
229
+        if ($userStatus->getId() === null) {
230
+            return $this->mapper->insert($userStatus);
231
+        }
232
+
233
+        return $this->mapper->update($userStatus);
234
+    }
235
+
236
+    /**
237
+     * @param string $userId
238
+     * @return bool
239
+     */
240
+    public function clearStatus(string $userId): bool {
241
+        try {
242
+            $userStatus = $this->mapper->findByUserId($userId);
243
+        } catch (DoesNotExistException $ex) {
244
+            // if there is no status to remove, just return
245
+            return false;
246
+        }
247
+
248
+        $userStatus->setStatus('offline');
249
+        $userStatus->setStatusTimestamp(0);
250
+        $userStatus->setIsUserDefined(false);
251
+
252
+        $this->mapper->update($userStatus);
253
+        return true;
254
+    }
255
+
256
+    /**
257
+     * @param string $userId
258
+     * @return bool
259
+     */
260
+    public function clearMessage(string $userId): bool {
261
+        try {
262
+            $userStatus = $this->mapper->findByUserId($userId);
263
+        } catch (DoesNotExistException $ex) {
264
+            // if there is no status to remove, just return
265
+            return false;
266
+        }
267
+
268
+        $userStatus->setMessageId(null);
269
+        $userStatus->setCustomMessage(null);
270
+        $userStatus->setCustomIcon(null);
271
+        $userStatus->setClearAt(null);
272
+
273
+        $this->mapper->update($userStatus);
274
+        return true;
275
+    }
276
+
277
+    /**
278
+     * @param string $userId
279
+     * @return bool
280
+     */
281
+    public function removeUserStatus(string $userId): bool {
282
+        try {
283
+            $userStatus = $this->mapper->findByUserId($userId);
284
+        } catch (DoesNotExistException $ex) {
285
+            // if there is no status to remove, just return
286
+            return false;
287
+        }
288
+
289
+        $this->mapper->delete($userStatus);
290
+        return true;
291
+    }
292
+
293
+    /**
294
+     * Processes a status to check if custom message is still
295
+     * up to date and provides translated default status if needed
296
+     *
297
+     * @param UserStatus $status
298
+     * @returns UserStatus
299
+     */
300
+    private function processStatus(UserStatus $status): UserStatus {
301
+        $clearAt = $status->getClearAt();
302
+        if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
303
+            $this->cleanStatus($status);
304
+        }
305
+        if ($status->getMessageId() !== null) {
306
+            $this->addDefaultMessage($status);
307
+        }
308
+
309
+        return $status;
310
+    }
311
+
312
+    /**
313
+     * @param UserStatus $status
314
+     */
315
+    private function cleanStatus(UserStatus $status): void {
316
+        $status->setMessageId(null);
317
+        $status->setCustomIcon(null);
318
+        $status->setCustomMessage(null);
319
+        $status->setClearAt(null);
320
+
321
+        $this->mapper->update($status);
322
+    }
323
+
324
+    /**
325
+     * @param UserStatus $status
326
+     */
327
+    private function addDefaultMessage(UserStatus $status): void {
328
+        // If the message is predefined, insert the translated message and icon
329
+        $predefinedMessage = $this->predefinedStatusService->getDefaultStatusById($status->getMessageId());
330
+        if ($predefinedMessage !== null) {
331
+            $status->setCustomMessage($predefinedMessage['message']);
332
+            $status->setCustomIcon($predefinedMessage['icon']);
333
+        }
334
+    }
335 335
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
 	 * @return UserStatus[]
91 91
 	 */
92 92
 	public function findAll(?int $limit = null, ?int $offset = null): array {
93
-		return array_map(function ($status) {
93
+		return array_map(function($status) {
94 94
 			return $this->processStatus($status);
95 95
 		}, $this->mapper->findAll($limit, $offset));
96 96
 	}
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
 
126 126
 		// Check if status-type is valid
127 127
 		if (!\in_array($status, $this->allowedStatusTypes, true)) {
128
-			throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported');
128
+			throw new InvalidStatusTypeException('Status-type "'.$status.'" is not supported');
129 129
 		}
130 130
 		if ($statusTimestamp === null) {
131 131
 			$statusTimestamp = $this->timeFactory->getTime();
@@ -164,7 +164,7 @@  discard block
 block discarded – undo
164 164
 		}
165 165
 
166 166
 		if (!$this->predefinedStatusService->isValidId($messageId)) {
167
-			throw new InvalidMessageIdException('Message-Id "' . $messageId . '" is not supported');
167
+			throw new InvalidMessageIdException('Message-Id "'.$messageId.'" is not supported');
168 168
 		}
169 169
 
170 170
 		// Check that clearAt is in the future
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
 		}
215 215
 		// Check for maximum length of custom message
216 216
 		if (\mb_strlen($message) > $this->maximumMessageLength) {
217
-			throw new StatusMessageTooLongException('Message is longer than supported length of ' . $this->maximumMessageLength . ' characters');
217
+			throw new StatusMessageTooLongException('Message is longer than supported length of '.$this->maximumMessageLength.' characters');
218 218
 		}
219 219
 		// Check that clearAt is in the future
220 220
 		if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
Please login to merge, or discard this patch.
apps/user_status/lib/BackgroundJob/ClearOldStatusesBackgroundJob.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -36,28 +36,28 @@
 block discarded – undo
36 36
  */
37 37
 class ClearOldStatusesBackgroundJob extends TimedJob {
38 38
 
39
-	/** @var UserStatusMapper */
40
-	private $mapper;
41
-
42
-	/**
43
-	 * ClearOldStatusesBackgroundJob constructor.
44
-	 *
45
-	 * @param ITimeFactory $time
46
-	 * @param UserStatusMapper $mapper
47
-	 */
48
-	public function __construct(ITimeFactory $time,
49
-								UserStatusMapper $mapper) {
50
-		parent::__construct($time);
51
-		$this->mapper = $mapper;
52
-
53
-		// Run every time the cron is run
54
-		$this->setInterval(60);
55
-	}
56
-
57
-	/**
58
-	 * @inheritDoc
59
-	 */
60
-	protected function run($argument) {
61
-		$this->mapper->clearOlderThan($this->time->getTime());
62
-	}
39
+    /** @var UserStatusMapper */
40
+    private $mapper;
41
+
42
+    /**
43
+     * ClearOldStatusesBackgroundJob constructor.
44
+     *
45
+     * @param ITimeFactory $time
46
+     * @param UserStatusMapper $mapper
47
+     */
48
+    public function __construct(ITimeFactory $time,
49
+                                UserStatusMapper $mapper) {
50
+        parent::__construct($time);
51
+        $this->mapper = $mapper;
52
+
53
+        // Run every time the cron is run
54
+        $this->setInterval(60);
55
+    }
56
+
57
+    /**
58
+     * @inheritDoc
59
+     */
60
+    protected function run($argument) {
61
+        $this->mapper->clearOlderThan($this->time->getTime());
62
+    }
63 63
 }
Please login to merge, or discard this patch.
apps/user_status/lib/AppInfo/Application.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -44,31 +44,31 @@
 block discarded – undo
44 44
  */
45 45
 class Application extends App implements IBootstrap {
46 46
 
47
-	/** @var string */
48
-	public const APP_ID = 'user_status';
47
+    /** @var string */
48
+    public const APP_ID = 'user_status';
49 49
 
50
-	/**
51
-	 * Application constructor.
52
-	 *
53
-	 * @param array $urlParams
54
-	 */
55
-	public function __construct(array $urlParams = []) {
56
-		parent::__construct(self::APP_ID, $urlParams);
57
-	}
50
+    /**
51
+     * Application constructor.
52
+     *
53
+     * @param array $urlParams
54
+     */
55
+    public function __construct(array $urlParams = []) {
56
+        parent::__construct(self::APP_ID, $urlParams);
57
+    }
58 58
 
59
-	/**
60
-	 * @inheritDoc
61
-	 */
62
-	public function register(IRegistrationContext $context): void {
63
-		// Register OCS Capabilities
64
-		$context->registerCapability(Capabilities::class);
59
+    /**
60
+     * @inheritDoc
61
+     */
62
+    public function register(IRegistrationContext $context): void {
63
+        // Register OCS Capabilities
64
+        $context->registerCapability(Capabilities::class);
65 65
 
66
-		// Register Event Listeners
67
-		$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
68
-		$context->registerEventListener(UserLiveStatusEvent::class, UserLiveStatusListener::class);
69
-		$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
70
-	}
66
+        // Register Event Listeners
67
+        $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
68
+        $context->registerEventListener(UserLiveStatusEvent::class, UserLiveStatusListener::class);
69
+        $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
70
+    }
71 71
 
72
-	public function boot(IBootContext $context): void {
73
-	}
72
+    public function boot(IBootContext $context): void {
73
+    }
74 74
 }
Please login to merge, or discard this patch.
apps/user_status/lib/Migration/Version0001Date20200602134824.php 1 patch
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -37,61 +37,61 @@
 block discarded – undo
37 37
  */
38 38
 class Version0001Date20200602134824 extends SimpleMigrationStep {
39 39
 
40
-	/**
41
-	 * @param IOutput $output
42
-	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
43
-	 * @param array $options
44
-	 * @return null|ISchemaWrapper
45
-	 * @since 20.0.0
46
-	 */
47
-	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
48
-		/** @var ISchemaWrapper $schema */
49
-		$schema = $schemaClosure();
40
+    /**
41
+     * @param IOutput $output
42
+     * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
43
+     * @param array $options
44
+     * @return null|ISchemaWrapper
45
+     * @since 20.0.0
46
+     */
47
+    public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
48
+        /** @var ISchemaWrapper $schema */
49
+        $schema = $schemaClosure();
50 50
 
51
-		$statusTable = $schema->createTable('user_status');
52
-		$statusTable->addColumn('id', Types::BIGINT, [
53
-			'autoincrement' => true,
54
-			'notnull' => true,
55
-			'length' => 20,
56
-			'unsigned' => true,
57
-		]);
58
-		$statusTable->addColumn('user_id', Types::STRING, [
59
-			'notnull' => true,
60
-			'length' => 255,
61
-		]);
62
-		$statusTable->addColumn('status', Types::STRING, [
63
-			'notnull' => true,
64
-			'length' => 255,
65
-		]);
66
-		$statusTable->addColumn('status_timestamp', Types::INTEGER, [
67
-			'notnull' => true,
68
-			'length' => 11,
69
-			'unsigned' => true,
70
-		]);
71
-		$statusTable->addColumn('is_user_defined', Types::BOOLEAN, [
72
-			'notnull' => true,
73
-		]);
74
-		$statusTable->addColumn('message_id', Types::STRING, [
75
-			'notnull' => false,
76
-			'length' => 255,
77
-		]);
78
-		$statusTable->addColumn('custom_icon', Types::STRING, [
79
-			'notnull' => false,
80
-			'length' => 255,
81
-		]);
82
-		$statusTable->addColumn('custom_message', Types::TEXT, [
83
-			'notnull' => false,
84
-		]);
85
-		$statusTable->addColumn('clear_at', Types::INTEGER, [
86
-			'notnull' => false,
87
-			'length' => 11,
88
-			'unsigned' => true,
89
-		]);
51
+        $statusTable = $schema->createTable('user_status');
52
+        $statusTable->addColumn('id', Types::BIGINT, [
53
+            'autoincrement' => true,
54
+            'notnull' => true,
55
+            'length' => 20,
56
+            'unsigned' => true,
57
+        ]);
58
+        $statusTable->addColumn('user_id', Types::STRING, [
59
+            'notnull' => true,
60
+            'length' => 255,
61
+        ]);
62
+        $statusTable->addColumn('status', Types::STRING, [
63
+            'notnull' => true,
64
+            'length' => 255,
65
+        ]);
66
+        $statusTable->addColumn('status_timestamp', Types::INTEGER, [
67
+            'notnull' => true,
68
+            'length' => 11,
69
+            'unsigned' => true,
70
+        ]);
71
+        $statusTable->addColumn('is_user_defined', Types::BOOLEAN, [
72
+            'notnull' => true,
73
+        ]);
74
+        $statusTable->addColumn('message_id', Types::STRING, [
75
+            'notnull' => false,
76
+            'length' => 255,
77
+        ]);
78
+        $statusTable->addColumn('custom_icon', Types::STRING, [
79
+            'notnull' => false,
80
+            'length' => 255,
81
+        ]);
82
+        $statusTable->addColumn('custom_message', Types::TEXT, [
83
+            'notnull' => false,
84
+        ]);
85
+        $statusTable->addColumn('clear_at', Types::INTEGER, [
86
+            'notnull' => false,
87
+            'length' => 11,
88
+            'unsigned' => true,
89
+        ]);
90 90
 
91
-		$statusTable->setPrimaryKey(['id']);
92
-		$statusTable->addUniqueIndex(['user_id'], 'user_status_uid_ix');
93
-		$statusTable->addIndex(['clear_at'], 'user_status_clr_ix');
91
+        $statusTable->setPrimaryKey(['id']);
92
+        $statusTable->addUniqueIndex(['user_id'], 'user_status_uid_ix');
93
+        $statusTable->addIndex(['clear_at'], 'user_status_clr_ix');
94 94
 
95
-		return $schema;
96
-	}
95
+        return $schema;
96
+    }
97 97
 }
Please login to merge, or discard this patch.
apps/user_status/lib/Capabilities.php 1 patch
Indentation   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -34,27 +34,27 @@
 block discarded – undo
34 34
  */
35 35
 class Capabilities implements ICapability {
36 36
 
37
-	/** @var EmojiService */
38
-	private $emojiService;
37
+    /** @var EmojiService */
38
+    private $emojiService;
39 39
 
40
-	/**
41
-	 * Capabilities constructor.
42
-	 *
43
-	 * @param EmojiService $emojiService
44
-	 */
45
-	public function __construct(EmojiService $emojiService) {
46
-		$this->emojiService = $emojiService;
47
-	}
40
+    /**
41
+     * Capabilities constructor.
42
+     *
43
+     * @param EmojiService $emojiService
44
+     */
45
+    public function __construct(EmojiService $emojiService) {
46
+        $this->emojiService = $emojiService;
47
+    }
48 48
 
49
-	/**
50
-	 * @inheritDoc
51
-	 */
52
-	public function getCapabilities() {
53
-		return [
54
-			'user_status' => [
55
-				'enabled' => true,
56
-				'supports_emoji' => $this->emojiService->doesPlatformSupportEmoji(),
57
-			],
58
-		];
59
-	}
49
+    /**
50
+     * @inheritDoc
51
+     */
52
+    public function getCapabilities() {
53
+        return [
54
+            'user_status' => [
55
+                'enabled' => true,
56
+                'supports_emoji' => $this->emojiService->doesPlatformSupportEmoji(),
57
+            ],
58
+        ];
59
+    }
60 60
 }
Please login to merge, or discard this patch.
apps/user_status/lib/Db/UserStatus.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -53,38 +53,38 @@
 block discarded – undo
53 53
  */
54 54
 class UserStatus extends Entity {
55 55
 
56
-	/** @var string */
57
-	public $userId;
56
+    /** @var string */
57
+    public $userId;
58 58
 
59
-	/** @var string */
60
-	public $status;
59
+    /** @var string */
60
+    public $status;
61 61
 
62
-	/** @var int */
63
-	public $statusTimestamp;
62
+    /** @var int */
63
+    public $statusTimestamp;
64 64
 
65
-	/** @var boolean */
66
-	public $isUserDefined;
65
+    /** @var boolean */
66
+    public $isUserDefined;
67 67
 
68
-	/** @var string|null */
69
-	public $messageId;
68
+    /** @var string|null */
69
+    public $messageId;
70 70
 
71
-	/** @var string|null */
72
-	public $customIcon;
71
+    /** @var string|null */
72
+    public $customIcon;
73 73
 
74
-	/** @var string|null */
75
-	public $customMessage;
74
+    /** @var string|null */
75
+    public $customMessage;
76 76
 
77
-	/** @var int|null */
78
-	public $clearAt;
77
+    /** @var int|null */
78
+    public $clearAt;
79 79
 
80
-	public function __construct() {
81
-		$this->addType('userId', 'string');
82
-		$this->addType('status', 'string');
83
-		$this->addType('statusTimestamp', 'int');
84
-		$this->addType('isUserDefined', 'boolean');
85
-		$this->addType('messageId', 'string');
86
-		$this->addType('customIcon', 'string');
87
-		$this->addType('customMessage', 'string');
88
-		$this->addType('clearAt', 'int');
89
-	}
80
+    public function __construct() {
81
+        $this->addType('userId', 'string');
82
+        $this->addType('status', 'string');
83
+        $this->addType('statusTimestamp', 'int');
84
+        $this->addType('isUserDefined', 'boolean');
85
+        $this->addType('messageId', 'string');
86
+        $this->addType('customIcon', 'string');
87
+        $this->addType('customMessage', 'string');
88
+        $this->addType('clearAt', 'int');
89
+    }
90 90
 }
Please login to merge, or discard this patch.