]> git.mxchange.org Git - friendica.git/blob - src/Moderation/Repository/Report.php
Merge pull request #13269 from MrPetovan/bug/fix-exception-return-code
[friendica.git] / src / Moderation / Repository / Report.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Moderation\Factory;
29 use Friendica\Moderation\Collection;
30 use Friendica\Network\HTTPException\NotFoundException;
31 use Friendica\Util\DateTimeFormat;
32 use Psr\Log\LoggerInterface;
33
34 final class Report extends \Friendica\BaseRepository
35 {
36         protected static $table_name = 'report';
37
38         /** @var Factory\Report */
39         protected $factory;
40         /** @var Factory\Report\Post */
41         protected $postFactory;
42         /** @var Factory\Report\Rule */
43         protected $ruleFactory;
44
45         public function __construct(Database $database, LoggerInterface $logger, Factory\Report $factory, Factory\Report\Post $postFactory, Factory\Report\Rule $ruleFactory)
46         {
47                 parent::__construct($database, $logger, $factory);
48
49                 $this->factory     = $factory;
50                 $this->postFactory = $postFactory;
51                 $this->ruleFactory = $postFactory;
52         }
53
54         public function selectOneById(int $lastInsertId): \Friendica\Moderation\Entity\Report
55         {
56                 return $this->_selectOne(['id' => $lastInsertId]);
57         }
58
59         public function save(\Friendica\Moderation\Entity\Report $Report): \Friendica\Moderation\Entity\Report
60         {
61                 $fields = [
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,
77                 ];
78
79                 if ($Report->id) {
80                         $this->db->update(self::$table_name, $fields, ['id' => $Report->id]);
81                 } else {
82                         $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
83
84                         $newReportId = $this->db->lastInsertId();
85
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]);
89                                 } else {
90                                         Logger::notice('Post does not exist', ['uri-id' => $post->uriId, 'report' => $Report]);
91                                 }
92                         }
93
94                         foreach ($Report->rules as $rule) {
95                                 $this->db->insert('report-rule', ['rid' => $newReportId, 'line-id' => $rule->lineId, 'text' => $rule->text]);
96                         }
97
98                         $Report = $this->selectOneById($newReportId);
99                 }
100
101                 return $Report;
102         }
103
104         protected function _selectOne(array $condition, array $params = []): BaseEntity
105         {
106                 $fields = $this->db->selectFirst(self::$table_name, [], $condition, $params);
107                 if (!$this->db->isResult($fields)) {
108                         throw new NotFoundException();
109                 }
110
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', 'line-text'], ['rid' => $condition['id'] ?? 0])));
113
114                 return $this->factory->createFromTableRow($fields, $reportPosts, $reportRules);
115         }
116 }