We now store the violation as well

This commit is contained in:
Michael 2022-12-25 10:44:06 +00:00
parent fd53cf824f
commit 5298cd73b3
10 changed files with 38 additions and 8 deletions

View file

@ -1678,6 +1678,7 @@ CREATE TABLE IF NOT EXISTS `report` (
`cid` int unsigned NOT NULL COMMENT 'Reported contact', `cid` int unsigned NOT NULL COMMENT 'Reported contact',
`comment` text COMMENT 'Report', `comment` text COMMENT 'Report',
`category` varchar(20) COMMENT 'Category of the report (spam, violation, other)', `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', `forward` boolean COMMENT 'Forward the report to the remote server',
`created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`status` tinyint unsigned COMMENT 'Status of the report', `status` tinyint unsigned COMMENT 'Status of the report',

View file

@ -14,6 +14,7 @@ Fields
| cid | Reported contact | int unsigned | NO | | NULL | | | cid | Reported contact | int unsigned | NO | | NULL | |
| comment | Report | text | YES | | NULL | | | comment | Report | text | YES | | NULL | |
| category | Category of the report (spam, violation, other) | varchar(20) | 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 | | | forward | Forward the report to the remote server | boolean | YES | | NULL | |
| created | | datetime | NO | | 0001-01-01 00:00:00 | | | created | | datetime | NO | | 0001-01-01 00:00:00 | |
| status | Status of the report | tinyint unsigned | YES | | NULL | | | status | Status of the report | tinyint unsigned | YES | | NULL | |

View file

@ -665,10 +665,11 @@ class System
/** /**
* Fetch the system rules * Fetch the system rules
* @param bool $numeric_id If set to "true", the rules are returned with a numeric id as key.
* *
* @return array * @return array
*/ */
public static function getRules(): array public static function getRules(bool $numeric_id = false): array
{ {
$rules = []; $rules = [];
$id = 0; $id = 0;
@ -681,7 +682,11 @@ class System
foreach (explode("\n", trim($msg)) as $line) { foreach (explode("\n", trim($msg)) as $line) {
$line = trim($line); $line = trim($line);
if ($line) { if ($line) {
$rules[] = ['id' => (string)++$id, 'text' => $line]; if ($numeric_id) {
$rules[++$id] = $line;
} else {
$rules[] = ['id' => (string)++$id, 'text' => $line];
}
} }
} }
} }

View file

@ -44,6 +44,8 @@ class Report extends \Friendica\BaseEntity
protected $comment; protected $comment;
/** @var string Optional category */ /** @var string Optional category */
protected $category; protected $category;
/** @var string Violated rules */
protected $rules;
/** @var bool Whether this report should be forwarded to the remote server */ /** @var bool Whether this report should be forwarded to the remote server */
protected $forward; protected $forward;
/** @var \DateTime|null When the report was created */ /** @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*/ /** @var int ID of the user making a moderation report*/
protected $uid; 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->reporterId = $reporterId;
$this->cid = $cid; $this->cid = $cid;
$this->created = $created; $this->created = $created;
$this->comment = $comment; $this->comment = $comment;
$this->category = $category; $this->category = $category;
$this->rules = $rules;
$this->forward = $forward; $this->forward = $forward;
$this->postUriIds = $postUriIds; $this->postUriIds = $postUriIds;
$this->uid = $uid; $this->uid = $uid;

View file

@ -40,6 +40,7 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')), new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')),
$row['comment'], $row['comment'],
$row['category'], $row['category'],
$row['rules'],
$row['forward'], $row['forward'],
$postUriIds, $postUriIds,
$row['uid'], $row['uid'],
@ -61,7 +62,7 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
* @return Entity\Report * @return Entity\Report
* @throws \Exception * @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( return new Entity\Report(
$reporterId, $reporterId,
@ -69,6 +70,7 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
new \DateTime('now', new \DateTimeZone('UTC')), new \DateTime('now', new \DateTimeZone('UTC')),
$comment, $comment,
$category, $category,
$rules,
$forward, $forward,
$postUriIds, $postUriIds,
$uid, $uid,

View file

@ -58,6 +58,7 @@ class Report extends \Friendica\BaseRepository
'cid' => $Report->cid, 'cid' => $Report->cid,
'comment' => $Report->comment, 'comment' => $Report->comment,
'category' => $Report->category, 'category' => $Report->category,
'rules' => $Report->rules,
'forward' => $Report->forward, 'forward' => $Report->forward,
]; ];

View file

@ -58,6 +58,7 @@ class Reports extends BaseApi
'status_ids' => [], // Array of Statuses to attach to the report, for context 'status_ids' => [], // Array of Statuses to attach to the report, for context
'comment' => '', // Reason for the report (default max 1000 characters) '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. '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? 'forward' => false, // If the account is remote, should the report be forwarded to the remote admin?
], $request); ], $request);
@ -66,7 +67,16 @@ class Reports extends BaseApi
throw new HTTPException\NotFoundException('Account ' . $request['account_id'] . ' not found'); 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); $this->reportRepo->save($report);

View file

@ -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); DI::report()->save($report);
Logger::info('Stored report', ['reporter' => $reporter_id, 'account_id' => $account_id, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]); Logger::info('Stored report', ['reporter' => $reporter_id, 'account_id' => $account_id, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]);

View file

@ -1677,6 +1677,7 @@ return [
"cid" => ["type" => "int unsigned", "not null" => "1", "foreign" => ["contact" => "id"], "comment" => "Reported contact"], "cid" => ["type" => "int unsigned", "not null" => "1", "foreign" => ["contact" => "id"], "comment" => "Reported contact"],
"comment" => ["type" => "text", "comment" => "Report"], "comment" => ["type" => "text", "comment" => "Report"],
"category" => ["type" => "varchar(20)", "comment" => "Category of the report (spam, violation, other)"], "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"], "forward" => ["type" => "boolean", "comment" => "Forward the report to the remote server"],
"created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
"status" => ["type" => "tinyint unsigned", "comment" => "Status of the report"], "status" => ["type" => "tinyint unsigned", "comment" => "Status of the report"],

View file

@ -49,6 +49,7 @@ class ReportTest extends MockedTest
new \DateTime('now', new \DateTimeZone('UTC')), new \DateTime('now', new \DateTimeZone('UTC')),
'', '',
null, null,
'',
false, false,
[], [],
12, 12,
@ -63,6 +64,7 @@ class ReportTest extends MockedTest
'cid' => 13, 'cid' => 13,
'comment' => 'Report', 'comment' => 'Report',
'category' => 'violation', 'category' => 'violation',
'rules' => 'Rules',
'forward' => true, 'forward' => true,
'created' => '2021-10-12 12:23:00' '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')), new \DateTime('2021-10-12 12:23:00', new \DateTimeZone('UTC')),
'Report', 'Report',
'violation', 'violation',
'Rules',
true, true,
[89, 90], [89, 90],
12, 12,
@ -117,6 +120,7 @@ class ReportTest extends MockedTest
'cid' => 13, 'cid' => 13,
'comment' => '', 'comment' => '',
'category' => null, 'category' => null,
'rules' => null,
'forward' => false, 'forward' => false,
'postUriIds' => [], 'postUriIds' => [],
'uid' => 12, 'uid' => 12,
@ -126,6 +130,7 @@ class ReportTest extends MockedTest
new \DateTime('now', new \DateTimeZone('UTC')), new \DateTime('now', new \DateTimeZone('UTC')),
'', '',
null, null,
'',
false, false,
[], [],
12, 12,
@ -146,6 +151,7 @@ class ReportTest extends MockedTest
new \DateTime('now', new \DateTimeZone('UTC')), new \DateTime('now', new \DateTimeZone('UTC')),
'Report', 'Report',
'violation', 'violation',
'Rules',
true, true,
[89, 90], [89, 90],
12, 12,
@ -158,10 +164,10 @@ class ReportTest extends MockedTest
/** /**
* @dataProvider dataCreateFromReportsRequest * @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()); $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);
} }
} }