]> git.mxchange.org Git - friendica.git/blob - src/Moderation/Repository/Report.php
Merge pull request #12158 from annando/api-report
[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\Factory\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                         'cid'     => $Report->cid,
58                         'comment' => $Report->comment,
59                         'forward' => $Report->forward,
60                 ];
61
62                 if ($Report->id) {
63                         $this->db->update(self::$table_name, $fields, ['id' => $Report->id]);
64                 } else {
65                         $fields['created'] = DateTimeFormat::utcNow();
66                         $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
67
68                         $Report = $this->selectOneById($this->db->lastInsertId());
69                 }
70
71                 $this->db->delete('report-post', ['rid' => $Report->id]);
72
73                 foreach ($Report->postUriIds as $uriId) {
74                         if (Post::exists(['uri-id' => $uriId])) {
75                                 $this->db->insert('report-post', ['rid' => $Report->id, 'uri-id' => $uriId]);
76                         } else {
77                                 Logger::notice('Post does not exist', ['uri-id' => $uriId, 'report' => $Report]);
78                         }
79                 }
80
81                 return $Report;
82         }
83
84         protected function _selectOne(array $condition, array $params = []): BaseEntity
85         {
86                 $fields = $this->db->selectFirst(static::$table_name, [], $condition, $params);
87                 if (!$this->db->isResult($fields)) {
88                         throw new NotFoundException();
89                 }
90
91                 $postUriIds = array_column($this->db->selectToArray('report-post', ['uri-id'], ['rid' => $condition['id'] ?? 0]), 'uri-id');
92
93                 return $this->factory->createFromTableRow($fields, $postUriIds);
94         }
95 }