]> git.mxchange.org Git - friendica.git/blob - src/Moderation/Repository/Report.php
We now store the violation as well
[friendica.git] / src / Moderation / Repository / Report.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Moderation\Repository;
23
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;
31
32 class Report extends \Friendica\BaseRepository
33 {
34         protected static $table_name = 'report';
35
36         /**
37          * @var \Friendica\Moderation\Factory\Report
38          */
39         protected $factory;
40
41         public function __construct(Database $database, LoggerInterface $logger, \Friendica\Moderation\Factory\Report $factory)
42         {
43                 parent::__construct($database, $logger, $factory);
44
45                 $this->factory = $factory;
46         }
47
48         public function selectOneById(int $lastInsertId): \Friendica\Moderation\Entity\Report
49         {
50                 return $this->_selectOne(['id' => $lastInsertId]);
51         }
52
53         public function save(\Friendica\Moderation\Entity\Report $Report)
54         {
55                 $fields = [
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,
63                 ];
64
65                 $postUriIds = $Report->postUriIds;
66
67                 if ($Report->id) {
68                         $this->db->update(self::$table_name, $fields, ['id' => $Report->id]);
69                 } else {
70                         $fields['created'] = DateTimeFormat::utcNow();
71                         $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
72
73                         $Report = $this->selectOneById($this->db->lastInsertId());
74                 }
75
76                 $this->db->delete('report-post', ['rid' => $Report->id]);
77
78                 foreach ($postUriIds as $uriId) {
79                         if (Post::exists(['uri-id' => $uriId])) {
80                                 $this->db->insert('report-post', ['rid' => $Report->id, 'uri-id' => $uriId]);
81                         } else {
82                                 Logger::notice('Post does not exist', ['uri-id' => $uriId, 'report' => $Report]);
83                         }
84                 }
85
86                 return $Report;
87         }
88
89         protected function _selectOne(array $condition, array $params = []): BaseEntity
90         {
91                 $fields = $this->db->selectFirst(static::$table_name, [], $condition, $params);
92                 if (!$this->db->isResult($fields)) {
93                         throw new NotFoundException();
94                 }
95
96                 $postUriIds = array_column($this->db->selectToArray('report-post', ['uri-id'], ['rid' => $condition['id'] ?? 0]), 'uri-id');
97
98                 return $this->factory->createFromTableRow($fields, $postUriIds);
99         }
100 }