Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CommentService.php « Service « lib - github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4dc3c696c86aea12ef007cc9bf432cf4a5f80ed9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
 *
 * @author René Gieling <github@dartcafe.de>
 *
 * @license GNU AGPL version 3 or any later version
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\Polls\Service;

use OCA\Polls\Db\Comment;
use OCA\Polls\Db\CommentMapper;
use OCA\Polls\Db\Watch;
use OCA\Polls\Model\Acl;

class CommentService {

	/** @var Acl */
	private $acl;

	/** @var AnonymizeService */
	private $anonymizer;

	/** @var CommentMapper */
	private $commentMapper;

	/** @var Comment */
	private $comment;

	/** @var WatchService */
	private $watchService;

	public function __construct(
		Acl $acl,
		AnonymizeService $anonymizer,
		CommentMapper $commentMapper,
		Comment $comment,
		WatchService $watchService
	) {
		$this->acl = $acl;
		$this->anonymizer = $anonymizer;
		$this->commentMapper = $commentMapper;
		$this->comment = $comment;
		$this->watchService = $watchService;
	}

	/**
	 * Get comments
	 * Read all comments of a poll based on the poll id and return list as array
	 */
	public function list(?int $pollId = 0, string $token = ''): array {
		if ($token) {
			$this->acl->setToken($token);
		} else {
			$this->acl->setPollId($pollId);
		}

		if ($this->acl->getIsAllowed(Acl::PERMISSION_POLL_USERNAMES_VIEW)) {
			return $this->commentMapper->findByPoll($this->acl->getPollId());
		} else {
			$this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId());
			return $this->anonymizer->getComments();
		}
	}

	/**
	 * Add comment
	 */
	public function add(int $pollId = 0, ?string $token = '', string $message = ''): Comment {
		if ($token) {
			$this->acl->setToken($token, Acl::PERMISSION_COMMENT_ADD);
		} else {
			$this->acl->setPollId($pollId, Acl::PERMISSION_COMMENT_ADD);
		}

		$this->comment = new Comment();
		$this->comment->setPollId($this->acl->getPollId());
		$this->comment->setUserId($this->acl->getUserId());
		$this->comment->setComment($message);
		$this->comment->setDt(date('Y-m-d H:i:s'));
		$this->comment->setTimestamp(time());
		$this->comment = $this->commentMapper->insert($this->comment);
		$this->watchService->writeUpdate($this->comment->getPollId(), Watch::OBJECT_COMMENTS);
		return $this->comment;
	}

	/**
	 * Delete comment
	 */
	public function delete(int $commentId, string $token = ''): Comment {
		$this->comment = $this->commentMapper->find($commentId);

		if ($token) {
			$this->acl->setToken($token, Acl::PERMISSION_COMMENT_ADD, $this->comment->getPollId());
		} else {
			$this->acl->setPollId($this->comment->getPollId());
		}

		if (!$this->acl->getIsOwner()) {
			$this->acl->validateUserId($this->comment->getUserId());
		}

		$this->commentMapper->delete($this->comment);
		$this->watchService->writeUpdate($this->comment->getPollId(), Watch::OBJECT_COMMENTS);
		return $this->comment;
	}
}