3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Moderation\Repository;
24 use Friendica\BaseEntity;
25 use Friendica\Core\Logger;
26 use Friendica\Database\Database;
27 use Friendica\Model\Post;
28 use Friendica\Moderation\Factory;
29 use Friendica\Moderation\Collection;
30 use Friendica\Network\HTTPException\NotFoundException;
31 use Friendica\Util\DateTimeFormat;
32 use Psr\Log\LoggerInterface;
34 final class Report extends \Friendica\BaseRepository
36 protected static $table_name = 'report';
38 /** @var Factory\Report */
40 /** @var Factory\Report\Post */
41 protected $postFactory;
42 /** @var Factory\Report\Rule */
43 protected $ruleFactory;
45 public function __construct(Database $database, LoggerInterface $logger, Factory\Report $factory, Factory\Report\Post $postFactory, Factory\Report\Rule $ruleFactory)
47 parent::__construct($database, $logger, $factory);
49 $this->factory = $factory;
50 $this->postFactory = $postFactory;
51 $this->ruleFactory = $ruleFactory;
54 public function selectOneById(int $lastInsertId): \Friendica\Moderation\Entity\Report
56 return $this->_selectOne(['id' => $lastInsertId]);
59 public function save(\Friendica\Moderation\Entity\Report $Report): \Friendica\Moderation\Entity\Report
62 'reporter-id' => $Report->reporterCid,
63 'uid' => $Report->reporterUid,
64 'cid' => $Report->cid,
65 'gsid' => $Report->gsid,
66 'comment' => $Report->comment,
67 'forward' => $Report->forward,
68 'category-id' => $Report->category,
69 'public-remarks' => $Report->publicRemarks,
70 'private-remarks' => $Report->privateRemarks,
71 'last-editor-uid' => $Report->lastEditorUid,
72 'assigned-uid' => $Report->assignedUid,
73 'status' => $Report->status,
74 'resolution' => $Report->resolution,
75 'created' => $Report->created->format(DateTimeFormat::MYSQL),
76 'edited' => $Report->edited ? $Report->edited->format(DateTimeFormat::MYSQL) : null,
80 $this->db->update(self::$table_name, $fields, ['id' => $Report->id]);
82 $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
84 $newReportId = $this->db->lastInsertId();
86 foreach ($Report->posts as $post) {
87 if (Post::exists(['uri-id' => $post->uriId])) {
88 $this->db->insert('report-post', ['rid' => $newReportId, 'uri-id' => $post->uriId, 'status' => $post->status]);
90 Logger::notice('Post does not exist', ['uri-id' => $post->uriId, 'report' => $Report]);
94 foreach ($Report->rules as $rule) {
95 $this->db->insert('report-rule', ['rid' => $newReportId, 'line-id' => $rule->lineId, 'text' => $rule->text]);
98 $Report = $this->selectOneById($newReportId);
104 protected function _selectOne(array $condition, array $params = []): BaseEntity
106 $fields = $this->db->selectFirst(self::$table_name, [], $condition, $params);
107 if (!$this->db->isResult($fields)) {
108 throw new NotFoundException();
111 $reportPosts = new Collection\Report\Posts(array_map([$this->postFactory, 'createFromTableRow'], $this->db->selectToArray('report-post', ['uri-id', 'status'], ['rid' => $condition['id'] ?? 0])));
112 $reportRules = new Collection\Report\Rules(array_map([$this->ruleFactory, 'createFromTableRow'], $this->db->selectToArray('report-rule', ['line-id', 'text'], ['rid' => $condition['id'] ?? 0])));
114 return $this->factory->createFromTableRow($fields, $reportPosts, $reportRules);