From 5298cd73b3a798fcf2cc0a1a41139839d3dbe63d Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 25 Dec 2022 10:44:06 +0000 Subject: [PATCH] We now store the violation as well --- database.sql | 1 + doc/database/db_report.md | 1 + src/Core/System.php | 9 +++++++-- src/Moderation/Entity/Report.php | 5 ++++- src/Moderation/Factory/Report.php | 4 +++- src/Moderation/Repository/Report.php | 1 + src/Module/Api/Mastodon/Reports.php | 12 +++++++++++- src/Protocol/ActivityPub/Processor.php | 2 +- static/dbstructure.config.php | 1 + tests/src/Moderation/Factory/ReportTest.php | 10 ++++++++-- 10 files changed, 38 insertions(+), 8 deletions(-) diff --git a/database.sql b/database.sql index 8c24dd4eb..7eb756cf2 100644 --- a/database.sql +++ b/database.sql @@ -1678,6 +1678,7 @@ CREATE TABLE IF NOT EXISTS `report` ( `cid` int unsigned NOT NULL COMMENT 'Reported contact', `comment` text COMMENT 'Report', `category` varchar(20) COMMENT 'Category of the report (spam, violation, other)', + `rules` text COMMENT 'Violated rules', `forward` boolean COMMENT 'Forward the report to the remote server', `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', `status` tinyint unsigned COMMENT 'Status of the report', diff --git a/doc/database/db_report.md b/doc/database/db_report.md index 56f51bdd4..92c0cced3 100644 --- a/doc/database/db_report.md +++ b/doc/database/db_report.md @@ -14,6 +14,7 @@ Fields | cid | Reported contact | int unsigned | NO | | NULL | | | comment | Report | text | YES | | NULL | | | category | Category of the report (spam, violation, other) | varchar(20) | YES | | NULL | | +| rules | Violated rules | text | YES | | NULL | | | forward | Forward the report to the remote server | boolean | YES | | NULL | | | created | | datetime | NO | | 0001-01-01 00:00:00 | | | status | Status of the report | tinyint unsigned | YES | | NULL | | diff --git a/src/Core/System.php b/src/Core/System.php index ec214fea9..65dce1bc0 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -665,10 +665,11 @@ class System /** * Fetch the system rules + * @param bool $numeric_id If set to "true", the rules are returned with a numeric id as key. * * @return array */ - public static function getRules(): array + public static function getRules(bool $numeric_id = false): array { $rules = []; $id = 0; @@ -681,7 +682,11 @@ class System foreach (explode("\n", trim($msg)) as $line) { $line = trim($line); if ($line) { - $rules[] = ['id' => (string)++$id, 'text' => $line]; + if ($numeric_id) { + $rules[++$id] = $line; + } else { + $rules[] = ['id' => (string)++$id, 'text' => $line]; + } } } } diff --git a/src/Moderation/Entity/Report.php b/src/Moderation/Entity/Report.php index 2bb4350d4..3fbd77256 100644 --- a/src/Moderation/Entity/Report.php +++ b/src/Moderation/Entity/Report.php @@ -44,6 +44,8 @@ class Report extends \Friendica\BaseEntity protected $comment; /** @var string Optional category */ protected $category; + /** @var string Violated rules */ + protected $rules; /** @var bool Whether this report should be forwarded to the remote server */ protected $forward; /** @var \DateTime|null When the report was created */ @@ -53,13 +55,14 @@ class Report extends \Friendica\BaseEntity /** @var int ID of the user making a moderation report*/ protected $uid; - public function __construct(int $reporterId, int $cid, \DateTime $created, string $comment = '', string $category = null, bool $forward = false, array $postUriIds = [], int $uid = null, int $id = null) + public function __construct(int $reporterId, int $cid, \DateTime $created, string $comment = '', string $category = null, string $rules = '', bool $forward = false, array $postUriIds = [], int $uid = null, int $id = null) { $this->reporterId = $reporterId; $this->cid = $cid; $this->created = $created; $this->comment = $comment; $this->category = $category; + $this->rules = $rules; $this->forward = $forward; $this->postUriIds = $postUriIds; $this->uid = $uid; diff --git a/src/Moderation/Factory/Report.php b/src/Moderation/Factory/Report.php index bbcbe8eb1..8e66d8ad5 100644 --- a/src/Moderation/Factory/Report.php +++ b/src/Moderation/Factory/Report.php @@ -40,6 +40,7 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')), $row['comment'], $row['category'], + $row['rules'], $row['forward'], $postUriIds, $row['uid'], @@ -61,7 +62,7 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow * @return Entity\Report * @throws \Exception */ - public function createFromReportsRequest(int $reporterId, int $cid, string $comment = '', string $category = null, bool $forward = false, array $postUriIds = [], int $uid = null): Entity\Report + public function createFromReportsRequest(int $reporterId, int $cid, string $comment = '', string $category = null, string $rules = '', bool $forward = false, array $postUriIds = [], int $uid = null): Entity\Report { return new Entity\Report( $reporterId, @@ -69,6 +70,7 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow new \DateTime('now', new \DateTimeZone('UTC')), $comment, $category, + $rules, $forward, $postUriIds, $uid, diff --git a/src/Moderation/Repository/Report.php b/src/Moderation/Repository/Report.php index 78d889378..5322a4ffb 100644 --- a/src/Moderation/Repository/Report.php +++ b/src/Moderation/Repository/Report.php @@ -58,6 +58,7 @@ class Report extends \Friendica\BaseRepository 'cid' => $Report->cid, 'comment' => $Report->comment, 'category' => $Report->category, + 'rules' => $Report->rules, 'forward' => $Report->forward, ]; diff --git a/src/Module/Api/Mastodon/Reports.php b/src/Module/Api/Mastodon/Reports.php index 7d98a5e66..98213f9be 100644 --- a/src/Module/Api/Mastodon/Reports.php +++ b/src/Module/Api/Mastodon/Reports.php @@ -58,6 +58,7 @@ class Reports extends BaseApi 'status_ids' => [], // Array of Statuses to attach to the report, for context 'comment' => '', // Reason for the report (default max 1000 characters) 'category' => 'other', // Specify if the report is due to spam, violation of enumerated instance rules, or some other reason. + 'rule_ids' => [], // For violation category reports, specify the ID of the exact rules broken. 'forward' => false, // If the account is remote, should the report be forwarded to the remote admin? ], $request); @@ -66,7 +67,16 @@ class Reports extends BaseApi throw new HTTPException\NotFoundException('Account ' . $request['account_id'] . ' not found'); } - $report = $this->reportFactory->createFromReportsRequest(Contact::getPublicIdByUserId(self::getCurrentUserID()), $request['account_id'], $request['comment'], $request['category'], $request['forward'], $request['status_ids'], self::getCurrentUserID()); + $violation = ''; + $rules = System::getRules(true); + + foreach ($request['rule_ids'] as $key) { + if (!empty($rules[$key])) { + $violation .= $rules[$key] . "\n"; + } + } + + $report = $this->reportFactory->createFromReportsRequest(Contact::getPublicIdByUserId(self::getCurrentUserID()), $request['account_id'], $request['comment'], $request['category'], trim($violation), $request['forward'], $request['status_ids'], self::getCurrentUserID()); $this->reportRepo->save($report); diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 400a52b32..e98fa4596 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -1852,7 +1852,7 @@ class Processor } } - $report = DI::reportFactory()->createFromReportsRequest($reporter_id, $account_id, $activity['content'], null, false, $uri_ids); + $report = DI::reportFactory()->createFromReportsRequest($reporter_id, $account_id, $activity['content'], null, '', false, $uri_ids); DI::report()->save($report); Logger::info('Stored report', ['reporter' => $reporter_id, 'account_id' => $account_id, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]); diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 897111814..1d573fa87 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -1677,6 +1677,7 @@ return [ "cid" => ["type" => "int unsigned", "not null" => "1", "foreign" => ["contact" => "id"], "comment" => "Reported contact"], "comment" => ["type" => "text", "comment" => "Report"], "category" => ["type" => "varchar(20)", "comment" => "Category of the report (spam, violation, other)"], + "rules" => ["type" => "text", "comment" => "Violated rules"], "forward" => ["type" => "boolean", "comment" => "Forward the report to the remote server"], "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], "status" => ["type" => "tinyint unsigned", "comment" => "Status of the report"], diff --git a/tests/src/Moderation/Factory/ReportTest.php b/tests/src/Moderation/Factory/ReportTest.php index be406f499..8d1e8131e 100644 --- a/tests/src/Moderation/Factory/ReportTest.php +++ b/tests/src/Moderation/Factory/ReportTest.php @@ -49,6 +49,7 @@ class ReportTest extends MockedTest new \DateTime('now', new \DateTimeZone('UTC')), '', null, + '', false, [], 12, @@ -63,6 +64,7 @@ class ReportTest extends MockedTest 'cid' => 13, 'comment' => 'Report', 'category' => 'violation', + 'rules' => 'Rules', 'forward' => true, 'created' => '2021-10-12 12:23:00' ], @@ -73,6 +75,7 @@ class ReportTest extends MockedTest new \DateTime('2021-10-12 12:23:00', new \DateTimeZone('UTC')), 'Report', 'violation', + 'Rules', true, [89, 90], 12, @@ -117,6 +120,7 @@ class ReportTest extends MockedTest 'cid' => 13, 'comment' => '', 'category' => null, + 'rules' => null, 'forward' => false, 'postUriIds' => [], 'uid' => 12, @@ -126,6 +130,7 @@ class ReportTest extends MockedTest new \DateTime('now', new \DateTimeZone('UTC')), '', null, + '', false, [], 12, @@ -146,6 +151,7 @@ class ReportTest extends MockedTest new \DateTime('now', new \DateTimeZone('UTC')), 'Report', 'violation', + 'Rules', true, [89, 90], 12, @@ -158,10 +164,10 @@ class ReportTest extends MockedTest /** * @dataProvider dataCreateFromReportsRequest */ - public function testCreateFromReportsRequest(int $reporter, int $cid, string $comment, string $category = null, bool $forward, array $postUriIds, int $uid, Entity\Report $assertion) + public function testCreateFromReportsRequest(int $reporter, int $cid, string $comment, string $category = null, string $rules = null, bool $forward, array $postUriIds, int $uid, Entity\Report $assertion) { $factory = new Factory\Report(new NullLogger()); - $this->assertReport($factory->createFromReportsRequest($reporter, $cid, $comment, $category, $forward, $postUriIds, $uid), $assertion); + $this->assertReport($factory->createFromReportsRequest($reporter, $cid, $comment, $category, $rules, $forward, $postUriIds, $uid), $assertion); } }