]> git.mxchange.org Git - friendica.git/blob - src/Moderation/Factory/Report.php
1c1e9cfb7f77ee3146517bac90cad7af70f18b7a
[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\Moderation\Collection;
27 use Friendica\Moderation\Entity;
28 use Psr\Clock\ClockInterface;
29 use Psr\Log\LoggerInterface;
30
31 class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
32 {
33         /** @var ClockInterface */
34         private $clock;
35
36         public function __construct(LoggerInterface $logger, ClockInterface $clock)
37         {
38                 parent::__construct($logger);
39
40                 $this->clock = $clock;
41         }
42
43         /**
44          * @param array                        $row   `report` table row
45          * @param Collection\Report\Posts|null $posts List of posts attached to the report
46          * @param Collection\Report\Rules|null $rules List of rules from the terms of service, see System::getRules()
47          * @return Entity\Report
48          * @throws \Exception
49          */
50         public function createFromTableRow(array $row, Collection\Report\Posts $posts = null, Collection\Report\Rules $rules = null): Entity\Report
51         {
52                 return new Entity\Report(
53                         $row['reporter-id'],
54                         $row['cid'],
55                         $row['gsid'],
56                         new \DateTimeImmutable($row['created'], new \DateTimeZone('UTC')),
57                         $row['category-id'],
58                         $row['uid'],
59                         $row['comment'],
60                         $row['forward'],
61                         $posts ?? new Collection\Report\Posts(),
62                         $rules ?? new Collection\Report\Rules(),
63                         $row['public-remarks'],
64                         $row['private-remarks'],
65                         $row['edited'] ? new \DateTimeImmutable($row['edited'], new \DateTimeZone('UTC')) : null,
66                         $row['status'],
67                         $row['resolution'],
68                         $row['assigned-uid'],
69                         $row['last-editor-uid'],
70                         $row['id'],
71                 );
72         }
73
74         /**
75          * Creates a Report entity from a Mastodon API /reports request
76          *
77          * @param array  $rules      Line-number indexed node rules array, see System::getRules(true)
78          * @param int    $reporterId
79          * @param int    $cid
80          * @param int    $gsid
81          * @param string $comment
82          * @param string $category
83          * @param bool   $forward
84          * @param array  $postUriIds
85          * @param array  $ruleIds
86          * @param ?int   $uid
87          * @return Entity\Report
88          * @see \Friendica\Module\Api\Mastodon\Reports::post()
89          */
90         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
91         {
92                 if (count($ruleIds)) {
93                         $categoryId = Entity\Report::CATEGORY_VIOLATION;
94                 } elseif ($category == 'spam') {
95                         $categoryId = Entity\Report::CATEGORY_SPAM;
96                 } else {
97                         $categoryId = Entity\Report::CATEGORY_OTHER;
98                 }
99
100                 return new Entity\Report(
101                         $reporterId,
102                         $cid,
103                         $gsid,
104                         $this->clock->now(),
105                         $categoryId,
106                         $uid,
107                         $comment,
108                         $forward,
109                         new Collection\Report\Posts(array_map(function ($uriId) {
110                                 return new Entity\Report\Post($uriId);
111                         }, $postUriIds)),
112                         new Collection\Report\Rules(array_map(function ($lineId) use ($rules) {
113                                 return new Entity\Report\Rule($lineId, $rules[$lineId] ?? '');
114                         }, $ruleIds)),
115                 );
116         }
117 }