]> git.mxchange.org Git - friendica.git/blob - src/Moderation/Factory/Report.php
Merge pull request #13269 from MrPetovan/bug/fix-exception-return-code
[friendica.git] / src / Moderation / Factory / 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\Factory;
23
24 use Friendica\Capabilities\ICanCreateFromTableRow;
25 use Friendica\Core\System;
26 use Friendica\Model\Contact;
27 use Friendica\Moderation\Collection;
28 use Friendica\Moderation\Entity;
29 use Psr\Clock\ClockInterface;
30 use Psr\Log\LoggerInterface;
31
32 class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
33 {
34         /** @var ClockInterface */
35         private $clock;
36
37         public function __construct(LoggerInterface $logger, ClockInterface $clock)
38         {
39                 parent::__construct($logger);
40
41                 $this->clock = $clock;
42         }
43
44         /**
45          * @param array                        $row   `report` table row
46          * @param Collection\Report\Posts|null $posts List of posts attached to the report
47          * @param Collection\Report\Rules|null $rules List of rules from the terms of service, see System::getRules()
48          * @return Entity\Report
49          * @throws \Exception
50          */
51         public function createFromTableRow(array $row, Collection\Report\Posts $posts = null, Collection\Report\Rules $rules = null): Entity\Report
52         {
53                 return new Entity\Report(
54                         $row['reporter-id'],
55                         $row['cid'],
56                         $row['gsid'],
57                         new \DateTimeImmutable($row['created'], new \DateTimeZone('UTC')),
58                         $row['category-id'],
59                         $row['uid'],
60                         $row['comment'],
61                         $row['forward'],
62                         $posts ?? new Collection\Report\Posts(),
63                         $rules ?? new Collection\Report\Rules(),
64                         $row['public-remarks'],
65                         $row['private-remarks'],
66                         $row['edited'] ? new \DateTimeImmutable($row['edited'], new \DateTimeZone('UTC')) : null,
67                         $row['status'],
68                         $row['resolution'],
69                         $row['assigned-uid'],
70                         $row['last-editor-uid'],
71                         $row['id'],
72                 );
73         }
74
75         /**
76          * Creates a Report entity from a Mastodon API /reports request
77          *
78          * @param array  $rules      Line-number indexed node rules array, see System::getRules(true)
79          * @param int    $reporterId
80          * @param int    $cid
81          * @param int    $gsid
82          * @param string $comment
83          * @param string $category
84          * @param bool   $forward
85          * @param array  $postUriIds
86          * @param array  $ruleIds
87          * @param ?int   $uid
88          * @return Entity\Report
89          * @see \Friendica\Module\Api\Mastodon\Reports::post()
90          */
91         public function createFromReportsRequest(array $rules, int $reporterId, int $cid, int $gsid, string $comment = '', string $category = '', bool $forward = false, array $postUriIds = [], array $ruleIds = [], int $uid = null): Entity\Report
92         {
93                 if (count($ruleIds)) {
94                         $categoryId = Entity\Report::CATEGORY_VIOLATION;
95                 } elseif ($category == 'spam') {
96                         $categoryId = Entity\Report::CATEGORY_SPAM;
97                 } else {
98                         $categoryId = Entity\Report::CATEGORY_OTHER;
99                 }
100
101                 return new Entity\Report(
102                         $reporterId,
103                         $cid,
104                         $gsid,
105                         $this->clock->now(),
106                         $categoryId,
107                         $uid,
108                         $comment,
109                         $forward,
110                         new Collection\Report\Posts(array_map(function ($uriId) {
111                                 return new Entity\Report\Post($uriId);
112                         }, $postUriIds)),
113                         new Collection\Report\Rules(array_map(function ($lineId) use ($rules) {
114                                 return new Entity\Report\Rule($lineId, $rules[$lineId] ?? '');
115                         }, $ruleIds)),
116                 );
117         }
118
119         public function createFromForm(array $rules, int $cid, int $reporterId, int $categoryId, array $ruleIds, string $comment, array $uriIds, bool $forward): Entity\Report
120         {
121                 $contact = Contact::getById($cid, ['gsid']);
122                 if (!$contact) {
123                         throw new \InvalidArgumentException('Contact with id: ' . $cid . ' not found');
124                 }
125
126                 if (!in_array($categoryId, Entity\Report::CATEGORIES)) {
127                         throw new \OutOfBoundsException('Category with id: ' . $categoryId . ' not found in set: [' . implode(', ', Entity\Report::CATEGORIES) . ']');
128                 }
129
130                 return new Entity\Report(
131                         Contact::getPublicIdByUserId($reporterId),
132                         $cid,
133                         $contact['gsid'],
134                         $this->clock->now(),
135                         $categoryId,
136                         $reporterId,
137                         $comment,
138                         $forward,
139                         new Collection\Report\Posts(array_map(function ($uriId) {
140                                 return new Entity\Report\Post($uriId);
141                         }, $uriIds)),
142                         new Collection\Report\Rules(array_map(function ($lineId) use ($rules) {
143                                 return new Entity\Report\Rule($lineId, $rules[$lineId] ?? '');
144                         }, $ruleIds)),
145                 );
146         }
147 }