1<?php
2
3declare(strict_types=1);
4/**
5 * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
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
24namespace OCA\Talk\Chat\Parser;
25
26use OCA\Talk\Model\Attendee;
27use OCA\Talk\Model\Message;
28
29class Command {
30	/**
31	 * @param Message $message
32	 * @throws \OutOfBoundsException
33	 */
34	public function parseMessage(Message $message): void {
35		$comment = $message->getComment();
36		$data = json_decode($comment->getMessage(), true);
37		if (!\is_array($data)) {
38			throw new \OutOfBoundsException('Invalid message');
39		}
40
41		if ($data['visibility'] === \OCA\Talk\Model\Command::RESPONSE_NONE) {
42			$message->setVisibility(false);
43			return;
44		}
45
46		$participant = $message->getParticipant();
47		if ($data['visibility'] !== \OCA\Talk\Model\Command::RESPONSE_ALL &&
48			($participant->getAttendee()->getActorType() !== Attendee::ACTOR_USERS
49				|| $data['user'] !== $participant->getAttendee()->getActorId())) {
50			$message->setVisibility(false);
51			return;
52		}
53
54		$message->setMessage($data['output'], []);
55	}
56}
57