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\Network\HTTPException\NotFoundException;
29 use Friendica\Util\DateTimeFormat;
30 use Psr\Log\LoggerInterface;
32 class Report extends \Friendica\BaseRepository
34 protected static $table_name = 'report';
37 * @var \Friendica\Moderation\Factory\Report
41 public function __construct(Database $database, LoggerInterface $logger, \Friendica\Moderation\Factory\Report $factory)
43 parent::__construct($database, $logger, $factory);
45 $this->factory = $factory;
48 public function selectOneById(int $lastInsertId): \Friendica\Moderation\Entity\Report
50 return $this->_selectOne(['id' => $lastInsertId]);
53 public function save(\Friendica\Moderation\Entity\Report $Report)
56 'uid' => $Report->uid,
57 'reporter-id' => $Report->reporterId,
58 'cid' => $Report->cid,
59 'comment' => $Report->comment,
60 'category' => $Report->category,
61 'rules' => $Report->rules,
62 'forward' => $Report->forward,
65 $postUriIds = $Report->postUriIds;
68 $this->db->update(self::$table_name, $fields, ['id' => $Report->id]);
70 $fields['created'] = DateTimeFormat::utcNow();
71 $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
73 $Report = $this->selectOneById($this->db->lastInsertId());
76 $this->db->delete('report-post', ['rid' => $Report->id]);
78 foreach ($postUriIds as $uriId) {
79 if (Post::exists(['uri-id' => $uriId])) {
80 $this->db->insert('report-post', ['rid' => $Report->id, 'uri-id' => $uriId]);
82 Logger::notice('Post does not exist', ['uri-id' => $uriId, 'report' => $Report]);
89 protected function _selectOne(array $condition, array $params = []): BaseEntity
91 $fields = $this->db->selectFirst(static::$table_name, [], $condition, $params);
92 if (!$this->db->isResult($fields)) {
93 throw new NotFoundException();
96 $postUriIds = array_column($this->db->selectToArray('report-post', ['uri-id'], ['rid' => $condition['id'] ?? 0]), 'uri-id');
98 return $this->factory->createFromTableRow($fields, $postUriIds);