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',
`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',

View file

@ -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 | |

View file

@ -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];
}
}
}
}

View file

@ -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;

View file

@ -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,

View file

@ -58,6 +58,7 @@ class Report extends \Friendica\BaseRepository
'cid' => $Report->cid,
'comment' => $Report->comment,
'category' => $Report->category,
'rules' => $Report->rules,
'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
'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);

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);
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"],
"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"],

View file

@ -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);
}
}