Merge pull request #12518 from annando/reporter

Reports: The reporting contact id is added
This commit is contained in:
Hypolite Petovan 2022-12-25 07:34:21 -05:00 committed by GitHub
commit 08bfd37cac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 152 additions and 71 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2023.03-dev (Giant Rhubarb) -- Friendica 2023.03-dev (Giant Rhubarb)
-- DB_UPDATE_VERSION 1503 -- DB_UPDATE_VERSION 1504
-- ------------------------------------------ -- ------------------------------------------
@ -1674,15 +1674,20 @@ CREATE TABLE IF NOT EXISTS `register` (
CREATE TABLE IF NOT EXISTS `report` ( CREATE TABLE IF NOT EXISTS `report` (
`id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID', `id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID',
`uid` mediumint unsigned COMMENT 'Reporting user', `uid` mediumint unsigned COMMENT 'Reporting user',
`reporter-id` int unsigned COMMENT 'Reporting contact',
`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)',
`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',
PRIMARY KEY(`id`), PRIMARY KEY(`id`),
INDEX `uid` (`uid`), INDEX `uid` (`uid`),
INDEX `cid` (`cid`), INDEX `cid` (`cid`),
INDEX `reporter-id` (`reporter-id`),
FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE, FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`reporter-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`cid`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE FOREIGN KEY (`cid`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT=''; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='';

View file

@ -6,24 +6,28 @@ Table report
Fields Fields
------ ------
| Field | Description | Type | Null | Key | Default | Extra | | Field | Description | Type | Null | Key | Default | Extra |
| ------- | --------------------------------------- | ------------------ | ---- | --- | ------------------- | -------------- | | ----------- | ----------------------------------------------- | ------------------ | ---- | --- | ------------------- | -------------- |
| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | | id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment |
| uid | Reporting user | mediumint unsigned | YES | | NULL | | | uid | Reporting user | mediumint unsigned | YES | | NULL | |
| cid | Reported contact | int unsigned | NO | | NULL | | | reporter-id | Reporting contact | int unsigned | YES | | NULL | |
| comment | Report | text | YES | | NULL | | | cid | Reported contact | int unsigned | NO | | NULL | |
| forward | Forward the report to the remote server | boolean | YES | | NULL | | | comment | Report | text | YES | | NULL | |
| created | | datetime | NO | | 0001-01-01 00:00:00 | | | category | Category of the report (spam, violation, other) | varchar(20) | YES | | NULL | |
| status | Status of the report | tinyint unsigned | 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 | |
Indexes Indexes
------------ ------------
| Name | Fields | | Name | Fields |
| ------- | ------ | | ----------- | ----------- |
| PRIMARY | id | | PRIMARY | id |
| uid | uid | | uid | uid |
| cid | cid | | cid | cid |
| reporter-id | reporter-id |
Foreign Keys Foreign Keys
------------ ------------
@ -31,6 +35,7 @@ Foreign Keys
| Field | Target Table | Target Field | | Field | Target Table | Target Field |
|-------|--------------|--------------| |-------|--------------|--------------|
| uid | [user](help/database/db_user) | uid | | uid | [user](help/database/db_user) | uid |
| reporter-id | [contact](help/database/db_contact) | id |
| cid | [contact](help/database/db_contact) | id | | cid | [contact](help/database/db_contact) | id |
Return to [database documentation](help/database) Return to [database documentation](help/database)

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

@ -23,38 +23,49 @@ namespace Friendica\Moderation\Entity;
/** /**
* @property-read int $id * @property-read int $id
* @property-read int $uid * @property-read int $reporterId
* @property-read int $cid * @property-read int $cid
* @property-read string $comment * @property-read string $comment
* @property-read string|null $category
* @property-read bool $forward * @property-read bool $forward
* @property-read array $postUriIds * @property-read array $postUriIds
* @property-read int $uid
* @property-read \DateTime|null $created * @property-read \DateTime|null $created
*/ */
class Report extends \Friendica\BaseEntity class Report extends \Friendica\BaseEntity
{ {
/** @var int|null */ /** @var int|null */
protected $id; protected $id;
/** @var int ID of the user making a moderation report*/ /** @var int ID of the contact making a moderation report*/
protected $uid; protected $reporterId;
/** @var int ID of the contact being reported*/ /** @var int ID of the contact being reported*/
protected $cid; protected $cid;
/** @var string Optional comment */ /** @var string Optional comment */
protected $comment; 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 */ /** @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 */
protected $created; protected $created;
/** @var array Optional list of URI IDs of posts supporting the report*/ /** @var array Optional list of URI IDs of posts supporting the report*/
protected $postUriIds; protected $postUriIds;
/** @var int ID of the user making a moderation report*/
protected $uid;
public function __construct(int $uid, int $cid, \DateTime $created, string $comment = '', bool $forward = false, array $postUriIds = [], 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->uid = $uid; $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->rules = $rules;
$this->forward = $forward; $this->forward = $forward;
$this->postUriIds = $postUriIds; $this->postUriIds = $postUriIds;
$this->uid = $uid;
$this->id = $id; $this->id = $id;
} }
} }

View file

@ -35,12 +35,15 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
public function createFromTableRow(array $row, array $postUriIds = []): Entity\Report public function createFromTableRow(array $row, array $postUriIds = []): Entity\Report
{ {
return new Entity\Report( return new Entity\Report(
$row['uid'], $row['reporter-id'],
$row['cid'], $row['cid'],
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')), new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')),
$row['comment'], $row['comment'],
$row['category'],
$row['rules'],
$row['forward'], $row['forward'],
$postUriIds, $postUriIds,
$row['uid'],
$row['id'], $row['id'],
); );
} }
@ -51,6 +54,7 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
* @see \Friendica\Module\Api\Mastodon\Reports::post() * @see \Friendica\Module\Api\Mastodon\Reports::post()
* *
* @param int $uid * @param int $uid
* @param int $reporterId
* @param int $cid * @param int $cid
* @param string $comment * @param string $comment
* @param bool $forward * @param bool $forward
@ -58,15 +62,18 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
* @return Entity\Report * @return Entity\Report
* @throws \Exception * @throws \Exception
*/ */
public function createFromReportsRequest(int $uid, int $cid, string $comment = '', bool $forward = false, array $postUriIds = []): 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(
$uid, $reporterId,
$cid, $cid,
new \DateTime('now', new \DateTimeZone('UTC')), new \DateTime('now', new \DateTimeZone('UTC')),
$comment, $comment,
$category,
$rules,
$forward, $forward,
$postUriIds, $postUriIds,
$uid,
); );
} }
} }

View file

@ -53,10 +53,13 @@ class Report extends \Friendica\BaseRepository
public function save(\Friendica\Moderation\Entity\Report $Report) public function save(\Friendica\Moderation\Entity\Report $Report)
{ {
$fields = [ $fields = [
'uid' => $Report->uid, 'uid' => $Report->uid,
'cid' => $Report->cid, 'reporter-id' => $Report->reporterId,
'comment' => $Report->comment, 'cid' => $Report->cid,
'forward' => $Report->forward, 'comment' => $Report->comment,
'category' => $Report->category,
'rules' => $Report->rules,
'forward' => $Report->forward,
]; ];
$postUriIds = $Report->postUriIds; $postUriIds = $Report->postUriIds;

View file

@ -54,10 +54,12 @@ class Reports extends BaseApi
self::checkAllowedScope(self::SCOPE_WRITE); self::checkAllowedScope(self::SCOPE_WRITE);
$request = $this->getRequest([ $request = $this->getRequest([
'account_id' => '', // ID of the account to report 'account_id' => '', // ID of the account to report
'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)
'forward' => false, // If the account is remote, should the report be forwarded to the remote admin? '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); ], $request);
$contact = Contact::getById($request['account_id'], ['id']); $contact = Contact::getById($request['account_id'], ['id']);
@ -65,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(self::getCurrentUserID(), $request['account_id'], $request['comment'], $request['forward'], $request['status_ids']); $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

@ -1837,22 +1837,25 @@ class Processor
return; return;
} }
$status_ids = $activity['object_ids']; $reporter_id = Contact::getIdForURL($activity['actor']);
array_shift($status_ids); if (empty($reporter_id)) {
Logger::info('Unknown actor', ['activity' => $activity]);
Queue::remove($activity);
return;
}
$uri_ids = []; $uri_ids = [];
foreach ($status_ids as $status_id) { foreach ($activity['object_ids'] as $status_id) {
$post = Post::selectFirst(['uri-id'], ['uri' => $status_id]); $post = Post::selectFirst(['uri-id'], ['uri' => $status_id]);
if (!empty($post['uri-id'])) { if (!empty($post['uri-id'])) {
$uri_ids[] = $post['uri-id']; $uri_ids[] = $post['uri-id'];
} }
} }
// @todo We should store the actor $report = DI::reportFactory()->createFromReportsRequest($reporter_id, $account_id, $activity['content'], null, '', false, $uri_ids);
$report = DI::reportFactory()->createFromReportsRequest(0, $account_id, $activity['content'], false, $uri_ids);
DI::report()->save($report); DI::report()->save($report);
Logger::info('Stored report', ['account_id' => $account_id, 'comment' => $activity['content'], 'status_ids' => $status_ids]); Logger::info('Stored report', ['reporter' => $reporter_id, 'account_id' => $account_id, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]);
} }
/** /**

View file

@ -55,7 +55,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1503); define('DB_UPDATE_VERSION', 1504);
} }
return [ return [
@ -1673,8 +1673,11 @@ return [
"fields" => [ "fields" => [
"id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"],
"uid" => ["type" => "mediumint unsigned", "foreign" => ["user" => "uid"], "comment" => "Reporting user"], "uid" => ["type" => "mediumint unsigned", "foreign" => ["user" => "uid"], "comment" => "Reporting user"],
"reporter-id" => ["type" => "int unsigned", "foreign" => ["contact" => "id"], "comment" => "Reporting contact"],
"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)"],
"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"],
@ -1683,6 +1686,7 @@ return [
"PRIMARY" => ["id"], "PRIMARY" => ["id"],
"uid" => ["uid"], "uid" => ["uid"],
"cid" => ["cid"], "cid" => ["cid"],
"reporter-id" => ["reporter-id"],
] ]
], ],
"report-post" => [ "report-post" => [

View file

@ -33,41 +33,53 @@ class ReportTest extends MockedTest
return [ return [
'default' => [ 'default' => [
'row' => [ 'row' => [
'id' => 11, 'id' => 11,
'uid' => 12, 'uid' => 12,
'cid' => 13, 'reporter-id' => 14,
'comment' => '', 'cid' => 13,
'forward' => false, 'comment' => '',
'created' => null 'category' => null,
'rules' => '',
'forward' => false,
'created' => null
], ],
'postUriIds' => [], 'postUriIds' => [],
'assertion' => new Entity\Report( 'assertion' => new Entity\Report(
12, 14,
13, 13,
new \DateTime('now', new \DateTimeZone('UTC')), new \DateTime('now', new \DateTimeZone('UTC')),
'', '',
null,
'',
false, false,
[], [],
12,
11, 11,
), ),
], ],
'full' => [ 'full' => [
'row' => [ 'row' => [
'id' => 11, 'id' => 11,
'uid' => 12, 'uid' => 12,
'cid' => 13, 'reporter-id' => 14,
'comment' => 'Report', 'cid' => 13,
'forward' => true, 'comment' => 'Report',
'created' => '2021-10-12 12:23:00' 'category' => 'violation',
'rules' => 'Rules',
'forward' => true,
'created' => '2021-10-12 12:23:00'
], ],
'postUriIds' => [89, 90], 'postUriIds' => [89, 90],
'assertion' => new Entity\Report( 'assertion' => new Entity\Report(
12, 14,
13, 13,
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',
'Rules',
true, true,
[89, 90], [89, 90],
12,
11 11
), ),
], ],
@ -81,8 +93,11 @@ class ReportTest extends MockedTest
$report->id $report->id
); );
self::assertEquals($assertion->uid, $report->uid); self::assertEquals($assertion->uid, $report->uid);
self::assertEquals($assertion->reporterId, $report->reporterId);
self::assertEquals($assertion->cid, $report->cid); self::assertEquals($assertion->cid, $report->cid);
self::assertEquals($assertion->comment, $report->comment); self::assertEquals($assertion->comment, $report->comment);
self::assertEquals($assertion->category, $report->category);
self::assertEquals($assertion->rules, $report->rules);
self::assertEquals($assertion->forward, $report->forward); self::assertEquals($assertion->forward, $report->forward);
// No way to test "now" at the moment // No way to test "now" at the moment
//self::assertEquals($assertion->created, $report->created); //self::assertEquals($assertion->created, $report->created);
@ -103,34 +118,46 @@ class ReportTest extends MockedTest
{ {
return [ return [
'default' => [ 'default' => [
'uid' => 12, 'reporter-id' => 14,
'cid' => 13, 'cid' => 13,
'comment' => '', 'comment' => '',
'forward' => false, 'category' => null,
'postUriIds' => [], 'rules' => '',
'assertion' => new Entity\Report( 'forward' => false,
12, 'postUriIds' => [],
'uid' => 12,
'assertion' => new Entity\Report(
14,
13, 13,
new \DateTime('now', new \DateTimeZone('UTC')), new \DateTime('now', new \DateTimeZone('UTC')),
'', '',
null,
'',
false, false,
[], [],
12,
null null
), ),
], ],
'full' => [ 'full' => [
'uid' => 12, 'reporter-id' => 14,
'cid' => 13, 'cid' => 13,
'comment' => 'Report', 'comment' => 'Report',
'forward' => true, 'category' => 'violation',
'postUriIds' => [89, 90], 'rules' => 'Rules',
'assertion' => new Entity\Report( 'forward' => true,
12, 'postUriIds' => [89, 90],
'uid' => 12,
'assertion' => new Entity\Report(
14,
13, 13,
new \DateTime('now', new \DateTimeZone('UTC')), new \DateTime('now', new \DateTimeZone('UTC')),
'Report', 'Report',
'violation',
'Rules',
true, true,
[89, 90], [89, 90],
12,
null null
), ),
], ],
@ -140,10 +167,10 @@ class ReportTest extends MockedTest
/** /**
* @dataProvider dataCreateFromReportsRequest * @dataProvider dataCreateFromReportsRequest
*/ */
public function testCreateFromReportsRequest(int $uid, int $cid, string $comment, bool $forward, array $postUriIds, Entity\Report $assertion) public function testCreateFromReportsRequest(int $reporter, int $cid, string $comment, string $category = null, string $rules = '', 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($uid, $cid, $comment, $forward, $postUriIds), $assertion); $this->assertReport($factory->createFromReportsRequest($reporter, $cid, $comment, $category, $rules, $forward, $postUriIds, $uid), $assertion);
} }
} }