3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Moderation\Factory;
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;
32 class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
34 /** @var ClockInterface */
37 public function __construct(LoggerInterface $logger, ClockInterface $clock)
39 parent::__construct($logger);
41 $this->clock = $clock;
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
51 public function createFromTableRow(array $row, Collection\Report\Posts $posts = null, Collection\Report\Rules $rules = null): Entity\Report
53 return new Entity\Report(
57 new \DateTimeImmutable($row['created'], new \DateTimeZone('UTC')),
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,
70 $row['last-editor-uid'],
76 * Creates a Report entity from a Mastodon API /reports request
78 * @param array $rules Line-number indexed node rules array, see System::getRules(true)
79 * @param int $reporterId
82 * @param string $comment
83 * @param string $category
84 * @param bool $forward
85 * @param array $postUriIds
86 * @param array $ruleIds
88 * @return Entity\Report
89 * @see \Friendica\Module\Api\Mastodon\Reports::post()
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
93 if (count($ruleIds)) {
94 $categoryId = Entity\Report::CATEGORY_VIOLATION;
95 } elseif ($category == 'spam') {
96 $categoryId = Entity\Report::CATEGORY_SPAM;
98 $categoryId = Entity\Report::CATEGORY_OTHER;
101 return new Entity\Report(
110 new Collection\Report\Posts(array_map(function ($uriId) {
111 return new Entity\Report\Post($uriId);
113 new Collection\Report\Rules(array_map(function ($lineId) use ($rules) {
114 return new Entity\Report\Rule($lineId, $rules[$lineId] ?? '');
119 public function createFromForm(array $rules, int $cid, int $reporterId, int $categoryId, array $ruleIds, string $comment, array $uriIds, bool $forward): Entity\Report
121 $contact = Contact::getById($cid, ['gsid']);
123 throw new \InvalidArgumentException('Contact with id: ' . $cid . ' not found');
126 if (!in_array($categoryId, Entity\Report::CATEGORIES)) {
127 throw new \OutOfBoundsException('Category with id: ' . $categoryId . ' not found in set: [' . implode(', ', Entity\Report::CATEGORIES) . ']');
130 return new Entity\Report(
131 Contact::getPublicIdByUserId($reporterId),
139 new Collection\Report\Posts(array_map(function ($uriId) {
140 return new Entity\Report\Post($uriId);
142 new Collection\Report\Rules(array_map(function ($lineId) use ($rules) {
143 return new Entity\Report\Rule($lineId, $rules[$lineId] ?? '');