Add fields to Report entity

- Add clock dependency to Moderation\Factory\Report
- Change DateTime field to DateTimeImmutable to satisfy Clock return type
- Add category, status and resolution constants
This commit is contained in:
Hypolite Petovan 2022-12-10 09:08:12 -05:00
parent d29a84ae46
commit 76de49a25c
13 changed files with 543 additions and 222 deletions

2
.gitignore vendored
View file

@ -19,7 +19,7 @@ robots.txt
/doc/cache /doc/cache
#ignore reports, should be generated with every build #ignore reports, should be generated with every build
report/ /report/
#ignore config files from eclipse, we don't want IDE files in our repository #ignore config files from eclipse, we don't want IDE files in our repository
.project .project

View file

@ -1695,19 +1695,33 @@ CREATE TABLE IF NOT EXISTS `report` (
`uid` mediumint unsigned COMMENT 'Reporting user', `uid` mediumint unsigned COMMENT 'Reporting user',
`reporter-id` int unsigned COMMENT 'Reporting contact', `reporter-id` int unsigned COMMENT 'Reporting contact',
`cid` int unsigned NOT NULL COMMENT 'Reported contact', `cid` int unsigned NOT NULL COMMENT 'Reported contact',
`gsid` int unsigned NOT NULL COMMENT 'Reported contact server',
`comment` text COMMENT 'Report', `comment` text COMMENT 'Report',
`category` varchar(20) COMMENT 'Category of the report (spam, violation, other)', `category-id` int unsigned NOT NULL DEFAULT 1 COMMENT 'Report category, one of Entity\Report::CATEGORY_*',
`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 '', `public-remarks` text COMMENT 'Remarks shared with the reporter',
`status` tinyint unsigned COMMENT 'Status of the report', `private-remarks` text COMMENT 'Remarks shared with the moderation team',
`last-editor-uid` mediumint unsigned COMMENT 'Last editor user',
`assigned-uid` mediumint unsigned COMMENT 'Assigned moderator user',
`status` tinyint unsigned NOT NULL COMMENT 'Status of the report, one of Entity\Report::STATUS_*',
`resolution` tinyint unsigned COMMENT 'Resolution of the report, one of Entity\Report::RESOLUTION_*',
`created` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`edited` datetime(6) COMMENT 'Last time the report has been edited',
PRIMARY KEY(`id`), PRIMARY KEY(`id`),
INDEX `uid` (`uid`), INDEX `uid` (`uid`),
INDEX `cid` (`cid`), INDEX `cid` (`cid`),
INDEX `reporter-id` (`reporter-id`), INDEX `reporter-id` (`reporter-id`),
INDEX `gsid` (`gsid`),
INDEX `assigned-uid` (`assigned-uid`),
INDEX `status-resolution` (`status`,`resolution`),
INDEX `created` (`created`),
INDEX `edited` (`edited`),
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 (`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,
FOREIGN KEY (`gsid`) REFERENCES `gserver` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`last-editor-uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`assigned-uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT=''; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='';
-- --
@ -1721,7 +1735,18 @@ CREATE TABLE IF NOT EXISTS `report-post` (
INDEX `uri-id` (`uri-id`), INDEX `uri-id` (`uri-id`),
FOREIGN KEY (`rid`) REFERENCES `report` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, FOREIGN KEY (`rid`) REFERENCES `report` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT=''; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Individual posts attached to a moderation report';
--
-- TABLE report-rule
--
CREATE TABLE IF NOT EXISTS `report-rule` (
`rid` int unsigned NOT NULL COMMENT 'Report id',
`line-id` int unsigned NOT NULL COMMENT 'Terms of service rule line number, may become invalid after a TOS change.',
`text` text NOT NULL COMMENT 'Terms of service rule text recorded at the time of the report',
PRIMARY KEY(`rid`,`line-id`),
FOREIGN KEY (`rid`) REFERENCES `report` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Terms of service rule lines relevant to a moderation report';
-- --
-- TABLE search -- TABLE search

View file

@ -77,7 +77,8 @@ Database Tables
| [push_subscriber](help/database/db_push_subscriber) | Used for OStatus: Contains feed subscribers | | [push_subscriber](help/database/db_push_subscriber) | Used for OStatus: Contains feed subscribers |
| [register](help/database/db_register) | registrations requiring admin approval | | [register](help/database/db_register) | registrations requiring admin approval |
| [report](help/database/db_report) | | | [report](help/database/db_report) | |
| [report-post](help/database/db_report-post) | | | [report-post](help/database/db_report-post) | Individual posts attached to a moderation report |
| [report-rule](help/database/db_report-rule) | Terms of service rule lines relevant to a moderation report |
| [search](help/database/db_search) | | | [search](help/database/db_search) | |
| [session](help/database/db_session) | web session storage | | [session](help/database/db_session) | web session storage |
| [storage](help/database/db_storage) | Data stored by Database storage backend | | [storage](help/database/db_storage) | Data stored by Database storage backend |

View file

@ -1,7 +1,7 @@
Table report-post Table report-post
=========== ===========
Individual posts attached to a moderation report
Fields Fields
------ ------

View file

@ -0,0 +1,29 @@
Table report-rule
===========
Terms of service rule lines relevant to a moderation report
Fields
------
| Field | Description | Type | Null | Key | Default | Extra |
| ------- | ------------------------------------------------------------------------- | ------------ | ---- | --- | ------- | ----- |
| rid | Report id | int unsigned | NO | PRI | NULL | |
| line-id | Terms of service rule line number, may become invalid after a TOS change. | int unsigned | NO | PRI | NULL | |
| text | Terms of service rule text recorded at the time of the report | text | NO | | NULL | |
Indexes
------------
| Name | Fields |
| ------- | ------------ |
| PRIMARY | rid, line-id |
Foreign Keys
------------
| Field | Target Table | Target Field |
|-------|--------------|--------------|
| rid | [report](help/database/db_report) | id |
Return to [database documentation](help/database)

View file

@ -6,28 +6,39 @@ 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 | |
| reporter-id | Reporting contact | int unsigned | YES | | NULL | | | reporter-id | Reporting contact | int unsigned | YES | | NULL | |
| cid | Reported contact | int unsigned | NO | | NULL | | | cid | Reported contact | int unsigned | NO | | NULL | |
| comment | Report | text | YES | | NULL | | | gsid | Reported contact server | int unsigned | NO | | NULL | |
| category | Category of the report (spam, violation, other) | varchar(20) | YES | | NULL | | | comment | Report | text | YES | | NULL | |
| rules | Violated rules | text | YES | | NULL | | | category-id | Report category, one of Entity\Report::CATEGORY_* | int unsigned | NO | | 1 | |
| 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 | | | public-remarks | Remarks shared with the reporter | text | YES | | NULL | |
| status | Status of the report | tinyint unsigned | YES | | NULL | | | private-remarks | Remarks shared with the moderation team | text | YES | | NULL | |
| last-editor-uid | Last editor user | mediumint unsigned | YES | | NULL | |
| assigned-uid | Assigned moderator user | mediumint unsigned | YES | | NULL | |
| status | Status of the report, one of Entity\Report::STATUS_* | tinyint unsigned | NO | | NULL | |
| resolution | Resolution of the report, one of Entity\Report::RESOLUTION_* | tinyint unsigned | YES | | NULL | |
| created | | datetime(6) | NO | | 0001-01-01 00:00:00 | |
| edited | Last time the report has been edited | datetime(6) | YES | | NULL | |
Indexes Indexes
------------ ------------
| Name | Fields | | Name | Fields |
| ----------- | ----------- | | ----------------- | ------------------ |
| PRIMARY | id | | PRIMARY | id |
| uid | uid | | uid | uid |
| cid | cid | | cid | cid |
| reporter-id | reporter-id | | reporter-id | reporter-id |
| gsid | gsid |
| assigned-uid | assigned-uid |
| status-resolution | status, resolution |
| created | created |
| edited | edited |
Foreign Keys Foreign Keys
------------ ------------
@ -37,5 +48,8 @@ Foreign Keys
| uid | [user](help/database/db_user) | uid | | uid | [user](help/database/db_user) | uid |
| reporter-id | [contact](help/database/db_contact) | id | | reporter-id | [contact](help/database/db_contact) | id |
| cid | [contact](help/database/db_contact) | id | | cid | [contact](help/database/db_contact) | id |
| gsid | [gserver](help/database/db_gserver) | id |
| last-editor-uid | [user](help/database/db_user) | uid |
| assigned-uid | [user](help/database/db_user) | uid |
Return to [database documentation](help/database) Return to [database documentation](help/database)

View file

@ -21,51 +21,126 @@
namespace Friendica\Moderation\Entity; namespace Friendica\Moderation\Entity;
use Friendica\Moderation\Collection;
/** /**
* @property-read int $id * @property-read int $id
* @property-read int $reporterId * @property-read int $reporterCid
* @property-read int $cid * @property-read int $cid
* @property-read string $comment * @property-read int $gsid
* @property-read string|null $category * @property-read string $comment
* @property-read bool $forward * @property-read string $publicRemarks
* @property-read array $postUriIds * @property-read string $privateRemarks
* @property-read int $uid * @property-read bool $forward
* @property-read \DateTime|null $created * @property-read int $category
* @property-read int $status
* @property-read int|null $resolution
* @property-read int $reporterUid
* @property-read int|null $lastEditorUid
* @property-read int|null $assignedUid
* @property-read \DateTimeImmutable $created
* @property-read \DateTimeImmutable|null $edited
* @property-read Collection\Report\Posts $posts
* @property-read Collection\Report\Rules $rules
*/ */
class Report extends \Friendica\BaseEntity final class Report extends \Friendica\BaseEntity
{ {
const CATEGORY_OTHER = 1;
const CATEGORY_SPAM = 2;
const CATEGORY_ILLEGAL = 4;
const CATEGORY_SAFETY = 8;
const CATEGORY_UNWANTED = 16;
const CATEGORY_VIOLATION = 32;
const CATEGORIES = [
self::CATEGORY_OTHER,
self::CATEGORY_SPAM,
self::CATEGORY_ILLEGAL,
self::CATEGORY_SAFETY,
self::CATEGORY_UNWANTED,
self::CATEGORY_VIOLATION,
];
const STATUS_CLOSED = 0;
const STATUS_OPEN = 1;
const RESOLUTION_ACCEPTED = 0;
const RESOLUTION_REJECTED = 1;
/** @var int|null */ /** @var int|null */
protected $id; protected $id;
/** @var int ID of the contact making a moderation report*/ /** @var int ID of the contact making a moderation report */
protected $reporterId; protected $reporterCid;
/** @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 int ID of the gserver of the contact being reported */
protected $gsid;
/** @var string Reporter comment */
protected $comment; protected $comment;
/** @var string Optional category */ /** @var int One of CATEGORY_* */
protected $category; protected $category;
/** @var string Violated rules */ /** @var int ID of the user making a moderation report, null in case of an incoming forwarded report */
protected $rules; protected $reporterUid;
/** @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 \DateTimeImmutable When the report was created */
protected $created; protected $created;
/** @var array Optional list of URI IDs of posts supporting the report*/ /** @var Collection\Report\Rules List of terms of service rule lines being possibly violated */
protected $postUriIds; protected $rules;
/** @var int ID of the user making a moderation report*/ /** @var Collection\Report\Posts List of URI IDs of posts supporting the report */
protected $uid; protected $posts;
/** @var string Remarks shared with the reporter */
protected $publicRemarks;
/** @var string Remarks shared with the moderation team */
protected $privateRemarks;
/** @var \DateTimeImmutable|null When the report was last edited */
protected $edited;
/** @var int One of STATUS_* */
protected $status;
/** @var int|null One of RESOLUTION_* if any */
protected $resolution;
/** @var int|null Assigned moderator user id if any */
protected $assignedUid;
/** @var int|null Last editor user ID if any */
protected $lastEditorUid;
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) public function __construct(
{ int $reporterCid,
$this->reporterId = $reporterId; int $cid,
$this->cid = $cid; int $gsid,
$this->created = $created; \DateTimeImmutable $created,
$this->comment = $comment; int $category,
$this->category = $category; int $reporterUid = null,
$this->rules = $rules; string $comment = '',
$this->forward = $forward; bool $forward = false,
$this->postUriIds = $postUriIds; Collection\Report\Posts $posts = null,
$this->uid = $uid; Collection\Report\Rules $rules = null,
$this->id = $id; string $publicRemarks = '',
string $privateRemarks = '',
\DateTimeImmutable $edited = null,
int $status = self::STATUS_OPEN,
int $resolution = null,
int $assignedUid = null,
int $lastEditorUid = null,
int $id = null
) {
$this->reporterCid = $reporterCid;
$this->cid = $cid;
$this->gsid = $gsid;
$this->created = $created;
$this->category = $category;
$this->reporterUid = $reporterUid;
$this->comment = $comment;
$this->forward = $forward;
$this->posts = $posts ?? new Collection\Report\Posts();
$this->rules = $rules ?? new Collection\Report\Rules();
$this->publicRemarks = $publicRemarks;
$this->privateRemarks = $privateRemarks;
$this->edited = $edited;
$this->status = $status;
$this->resolution = $resolution;
$this->assignedUid = $assignedUid;
$this->lastEditorUid = $lastEditorUid;
$this->id = $id;
} }
} }

View file

@ -22,28 +22,51 @@
namespace Friendica\Moderation\Factory; namespace Friendica\Moderation\Factory;
use Friendica\Capabilities\ICanCreateFromTableRow; use Friendica\Capabilities\ICanCreateFromTableRow;
use Friendica\Core\System;
use Friendica\Moderation\Collection;
use Friendica\Moderation\Entity; use Friendica\Moderation\Entity;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
{ {
/** @var ClockInterface */
private $clock;
public function __construct(LoggerInterface $logger, ClockInterface $clock)
{
parent::__construct($logger);
$this->clock = $clock;
}
/** /**
* @param array $row `report` table row * @param array $row `report` table row
* @param array $postUriIds List of post URI ids from the `report-post` table * @param Collection\Report\Posts|null $posts List of posts attached to the report
* @param Collection\Report\Rules|null $rules List of rules from the terms of service, see System::getRules()
* @return Entity\Report * @return Entity\Report
* @throws \Exception * @throws \Exception
*/ */
public function createFromTableRow(array $row, array $postUriIds = []): Entity\Report public function createFromTableRow(array $row, Collection\Report\Posts $posts = null, Collection\Report\Rules $rules = null): Entity\Report
{ {
return new Entity\Report( return new Entity\Report(
$row['reporter-id'], $row['reporter-id'],
$row['cid'], $row['cid'],
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')), $row['gsid'],
$row['comment'], new \DateTimeImmutable($row['created'], new \DateTimeZone('UTC')),
$row['category'], $row['category-id'],
$row['rules'],
$row['forward'],
$postUriIds,
$row['uid'], $row['uid'],
$row['comment'],
$row['forward'],
$posts ?? new Collection\Report\Posts(),
$rules ?? new Collection\Report\Rules(),
$row['public-remarks'],
$row['private-remarks'],
$row['edited'] ? new \DateTimeImmutable($row['edited'], new \DateTimeZone('UTC')) : null,
$row['status'],
$row['resolution'],
$row['assigned-uid'],
$row['last-editor-uid'],
$row['id'], $row['id'],
); );
} }
@ -51,29 +74,44 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
/** /**
* Creates a Report entity from a Mastodon API /reports request * Creates a Report entity from a Mastodon API /reports request
* *
* @see \Friendica\Module\Api\Mastodon\Reports::post() * @param array $rules Line-number indexed node rules array, see System::getRules(true)
*
* @param int $uid
* @param int $reporterId * @param int $reporterId
* @param int $cid * @param int $cid
* @param int $gsid
* @param string $comment * @param string $comment
* @param string $category
* @param bool $forward * @param bool $forward
* @param array $postUriIds * @param array $postUriIds
* @param array $ruleIds
* @param ?int $uid
* @return Entity\Report * @return Entity\Report
* @throws \Exception * @see \Friendica\Module\Api\Mastodon\Reports::post()
*/ */
public function createFromReportsRequest(int $reporterId, int $cid, string $comment = '', string $category = null, string $rules = '', bool $forward = false, array $postUriIds = [], int $uid = null): Entity\Report 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
{ {
if (count($ruleIds)) {
$categoryId = Entity\Report::CATEGORY_VIOLATION;
} elseif ($category == 'spam') {
$categoryId = Entity\Report::CATEGORY_SPAM;
} else {
$categoryId = Entity\Report::CATEGORY_OTHER;
}
return new Entity\Report( return new Entity\Report(
$reporterId, $reporterId,
$cid, $cid,
new \DateTime('now', new \DateTimeZone('UTC')), $gsid,
$comment, $this->clock->now(),
$category, $categoryId,
$rules,
$forward,
$postUriIds,
$uid, $uid,
$comment,
$forward,
new Collection\Report\Posts(array_map(function ($uriId) {
return new Entity\Report\Post($uriId);
}, $postUriIds)),
new Collection\Report\Rules(array_map(function ($lineId) use ($rules) {
return new Entity\Report\Rule($lineId, $rules[$lineId] ?? '');
}, $ruleIds)),
); );
} }
} }

View file

@ -25,24 +25,30 @@ use Friendica\BaseEntity;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Moderation\Factory;
use Friendica\Moderation\Collection;
use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class Report extends \Friendica\BaseRepository final class Report extends \Friendica\BaseRepository
{ {
protected static $table_name = 'report'; protected static $table_name = 'report';
/** /** @var Factory\Report */
* @var \Friendica\Moderation\Factory\Report
*/
protected $factory; protected $factory;
/** @var Factory\Report\Post */
protected $postFactory;
/** @var Factory\Report\Rule */
protected $ruleFactory;
public function __construct(Database $database, LoggerInterface $logger, \Friendica\Moderation\Factory\Report $factory) public function __construct(Database $database, LoggerInterface $logger, Factory\Report $factory, Factory\Report\Post $postFactory, Factory\Report\Rule $ruleFactory)
{ {
parent::__construct($database, $logger, $factory); parent::__construct($database, $logger, $factory);
$this->factory = $factory; $this->factory = $factory;
$this->postFactory = $postFactory;
$this->ruleFactory = $postFactory;
} }
public function selectOneById(int $lastInsertId): \Friendica\Moderation\Entity\Report public function selectOneById(int $lastInsertId): \Friendica\Moderation\Entity\Report
@ -53,34 +59,43 @@ 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, 'reporter-id' => $Report->reporterCid,
'reporter-id' => $Report->reporterId, 'uid' => $Report->reporterUid,
'cid' => $Report->cid, 'cid' => $Report->cid,
'comment' => $Report->comment, 'gsid' => $Report->gsid,
'category' => $Report->category, 'comment' => $Report->comment,
'rules' => $Report->rules, 'forward' => $Report->forward,
'forward' => $Report->forward, 'category-id' => $Report->category,
'public-remarks' => $Report->publicRemarks,
'private-remarks' => $Report->privateRemarks,
'last-editor-uid' => $Report->lastEditorUid,
'assigned-uid' => $Report->assignedUid,
'status' => $Report->status,
'resolution' => $Report->resolution,
'created' => $Report->created->format(DateTimeFormat::MYSQL),
'edited' => $Report->edited ? $Report->edited->format(DateTimeFormat::MYSQL) : null,
]; ];
$postUriIds = $Report->postUriIds;
if ($Report->id) { if ($Report->id) {
$this->db->update(self::$table_name, $fields, ['id' => $Report->id]); $this->db->update(self::$table_name, $fields, ['id' => $Report->id]);
} else { } else {
$fields['created'] = DateTimeFormat::utcNow();
$this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE); $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
$Report = $this->selectOneById($this->db->lastInsertId()); $newReportId = $this->db->lastInsertId();
}
$this->db->delete('report-post', ['rid' => $Report->id]); foreach ($Report->posts as $post) {
if (Post::exists(['uri-id' => $post->uriId])) {
foreach ($postUriIds as $uriId) { $this->db->insert('report-post', ['rid' => $newReportId, 'uri-id' => $post->uriId, 'status' => $post->status]);
if (Post::exists(['uri-id' => $uriId])) { } else {
$this->db->insert('report-post', ['rid' => $Report->id, 'uri-id' => $uriId]); Logger::notice('Post does not exist', ['uri-id' => $post->uriId, 'report' => $Report]);
} else { }
Logger::notice('Post does not exist', ['uri-id' => $uriId, 'report' => $Report]);
} }
foreach ($Report->rules as $rule) {
$this->db->insert('report-rule', ['rid' => $newReportId, 'line-id' => $rule->lineId, 'text' => $rule->text]);
}
$Report = $this->selectOneById($this->db->lastInsertId());
} }
return $Report; return $Report;
@ -88,13 +103,14 @@ class Report extends \Friendica\BaseRepository
protected function _selectOne(array $condition, array $params = []): BaseEntity protected function _selectOne(array $condition, array $params = []): BaseEntity
{ {
$fields = $this->db->selectFirst(static::$table_name, [], $condition, $params); $fields = $this->db->selectFirst(self::$table_name, [], $condition, $params);
if (!$this->db->isResult($fields)) { if (!$this->db->isResult($fields)) {
throw new NotFoundException(); throw new NotFoundException();
} }
$postUriIds = array_column($this->db->selectToArray('report-post', ['uri-id'], ['rid' => $condition['id'] ?? 0]), 'uri-id'); $reportPosts = new Collection\Report\Posts(array_map([$this->postFactory, 'createFromTableRow'], $this->db->selectToArray('report-post', ['uri-id', 'status'], ['rid' => $condition['id'] ?? 0])));
$reportRules = new Collection\Report\Rules(array_map([$this->ruleFactory, 'createFromTableRow'], $this->db->selectToArray('report-rule', ['line-id', 'line-text'], ['rid' => $condition['id'] ?? 0])));
return $this->factory->createFromTableRow($fields, $postUriIds); return $this->factory->createFromTableRow($fields, $reportPosts, $reportRules);
} }
} }

View file

@ -62,21 +62,23 @@ class Reports extends BaseApi
'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);
$contact = Contact::getById($request['account_id'], ['id']); $contact = Contact::getById($request['account_id'], ['id', 'gsid']);
if (empty($contact)) { if (empty($contact)) {
throw new HTTPException\NotFoundException('Account ' . $request['account_id'] . ' not found'); throw new HTTPException\NotFoundException('Account ' . $request['account_id'] . ' not found');
} }
$violation = ''; $report = $this->reportFactory->createFromReportsRequest(
$rules = System::getRules(true); System::getRules(),
Contact::getPublicIdByUserId(self::getCurrentUserID()),
foreach ($request['rule_ids'] as $key) { $contact['id'],
if (!empty($rules[$key])) { $contact['gsid'],
$violation .= $rules[$key] . "\n"; $request['comment'],
} $request['category'],
} $request['forward'],
$request['status_ids'],
$report = $this->reportFactory->createFromReportsRequest(Contact::getPublicIdByUserId(self::getCurrentUserID()), $request['account_id'], $request['comment'], $request['category'], trim($violation), $request['forward'], $request['status_ids'], self::getCurrentUserID()); $request['rule_ids'],
self::getCurrentUserID()
);
$this->reportRepo->save($report); $this->reportRepo->save($report);

View file

@ -42,6 +42,7 @@ use Friendica\Model\Mail;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Moderation\Entity\Report;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Delivery; use Friendica\Protocol\Delivery;
@ -1893,8 +1894,8 @@ class Processor
*/ */
public static function ReportAccount(array $activity) public static function ReportAccount(array $activity)
{ {
$account_id = Contact::getIdForURL($activity['object_id']); $account = Contact::getByURL($activity['object_id'], null, ['id', 'gsid']);
if (empty($account_id)) { if (empty($account)) {
Logger::info('Unknown account', ['activity' => $activity]); Logger::info('Unknown account', ['activity' => $activity]);
Queue::remove($activity); Queue::remove($activity);
return; return;
@ -1915,10 +1916,10 @@ class Processor
} }
} }
$report = DI::reportFactory()->createFromReportsRequest($reporter_id, $account_id, $activity['content'], null, '', false, $uri_ids); $report = DI::reportFactory()->createFromReportsRequest(System::getRules(true), $reporter_id, $account['id'], $account['gsid'], $activity['content'], 'other', 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' => $account, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]);
} }
/** /**

View file

@ -1691,22 +1691,33 @@ return [
"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"], "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"],
"gsid" => ["type" => "int unsigned", "not null" => "1", "foreign" => ["gserver" => "id"], "comment" => "Reported contact server"],
"comment" => ["type" => "text", "comment" => "Report"], "comment" => ["type" => "text", "comment" => "Report"],
"category" => ["type" => "varchar(20)", "comment" => "Category of the report (spam, violation, other)"], "category-id" => ["type" => "int unsigned", "not null" => 1, "default" => \Friendica\Moderation\Entity\Report::CATEGORY_OTHER, "comment" => "Report category, one of Entity\Report::CATEGORY_*"],
"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" => ""], "public-remarks" => ["type" => "text", "comment" => "Remarks shared with the reporter"],
"status" => ["type" => "tinyint unsigned", "comment" => "Status of the report"], "private-remarks" => ["type" => "text", "comment" => "Remarks shared with the moderation team"],
"last-editor-uid" => ["type" => "mediumint unsigned", "foreign" => ["user" => "uid"], "comment" => "Last editor user"],
"assigned-uid" => ["type" => "mediumint unsigned", "foreign" => ["user" => "uid"], "comment" => "Assigned moderator user"],
"status" => ["type" => "tinyint unsigned", "not null" => "1", "comment" => "Status of the report, one of Entity\Report::STATUS_*"],
"resolution" => ["type" => "tinyint unsigned", "comment" => "Resolution of the report, one of Entity\Report::RESOLUTION_*"],
"created" => ["type" => "datetime(6)", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
"edited" => ["type" => "datetime(6)", "comment" => "Last time the report has been edited"],
], ],
"indexes" => [ "indexes" => [
"PRIMARY" => ["id"], "PRIMARY" => ["id"],
"uid" => ["uid"], "uid" => ["uid"],
"cid" => ["cid"], "cid" => ["cid"],
"reporter-id" => ["reporter-id"], "reporter-id" => ["reporter-id"],
"gsid" => ["gsid"],
"assigned-uid" => ["assigned-uid"],
"status-resolution" => ["status", "resolution"],
"created" => ["created"],
"edited" => ["edited"],
] ]
], ],
"report-post" => [ "report-post" => [
"comment" => "", "comment" => "Individual posts attached to a moderation report",
"fields" => [ "fields" => [
"rid" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["report" => "id"], "comment" => "Report id"], "rid" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["report" => "id"], "comment" => "Report id"],
"uri-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["item-uri" => "id"], "comment" => "Uri-id of the reported post"], "uri-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["item-uri" => "id"], "comment" => "Uri-id of the reported post"],
@ -1717,6 +1728,17 @@ return [
"uri-id" => ["uri-id"], "uri-id" => ["uri-id"],
] ]
], ],
"report-rule" => [
"comment" => "Terms of service rule lines relevant to a moderation report",
"fields" => [
"rid" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["report" => "id"], "comment" => "Report id"],
"line-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "comment" => "Terms of service rule line number, may become invalid after a TOS change."],
"text" => ["type" => "text", "not null" => "1", "comment" => "Terms of service rule text recorded at the time of the report"],
],
"indexes" => [
"PRIMARY" => ["rid", "line-id"],
]
],
"search" => [ "search" => [
"comment" => "", "comment" => "",
"fields" => [ "fields" => [

View file

@ -21,144 +21,242 @@
namespace Friendica\Test\src\Moderation\Factory; namespace Friendica\Test\src\Moderation\Factory;
use Friendica\Moderation\Collection;
use Friendica\Moderation\Factory; use Friendica\Moderation\Factory;
use Friendica\Moderation\Entity; use Friendica\Moderation\Entity;
use Friendica\Test\MockedTest; use Friendica\Test\MockedTest;
use Friendica\Util\Clock\FrozenClock;
use Friendica\Util\DateTimeFormat;
use Psr\Clock\ClockInterface;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
class ReportTest extends MockedTest class ReportTest extends MockedTest
{ {
public function dataCreateFromTableRow(): array public function dataCreateFromTableRow(): array
{ {
$clock = new FrozenClock();
// We need to strip the microseconds part to match database stored timestamps
$nowSeconds = $clock->now()->setTime(
$clock->now()->format('H'),
$clock->now()->format('i'),
$clock->now()->format('s')
);
return [ return [
'default' => [ 'default' => [
'clock' => $clock,
'row' => [ 'row' => [
'id' => 11, 'id' => 11,
'uid' => 12, 'reporter-id' => 12,
'reporter-id' => 14, 'uid' => null,
'cid' => 13, 'cid' => 13,
'comment' => '', 'gsid' => 14,
'category' => null, 'comment' => '',
'rules' => '', 'forward' => false,
'forward' => false, 'category-id' => Entity\Report::CATEGORY_SPAM,
'created' => null 'public-remarks' => '',
'private-remarks' => '',
'last-editor-uid' => null,
'assigned-uid' => null,
'status' => Entity\Report::STATUS_OPEN,
'resolution' => null,
'created' => $nowSeconds->format(DateTimeFormat::MYSQL),
'edited' => null,
], ],
'postUriIds' => [], 'posts' => new Collection\Report\Posts(),
'rules' => new Collection\Report\Rules(),
'assertion' => new Entity\Report( 'assertion' => new Entity\Report(
14, 12,
13, 13,
new \DateTime('now', new \DateTimeZone('UTC')), 14,
'', $nowSeconds,
Entity\Report::CATEGORY_SPAM,
null, null,
'', '',
false, false,
[], new Collection\Report\Posts(),
12, new Collection\Report\Rules(),
11, '',
'',
null,
Entity\Report::STATUS_OPEN,
null,
null,
null,
11
), ),
], ],
'full' => [ 'full' => [
'clock' => $clock,
'row' => [ 'row' => [
'id' => 11, 'id' => 11,
'uid' => 12, 'reporter-id' => 42,
'reporter-id' => 14, 'uid' => 12,
'cid' => 13, 'cid' => 13,
'comment' => 'Report', 'gsid' => 14,
'category' => 'violation', 'comment' => 'Report',
'rules' => 'Rules', 'forward' => true,
'forward' => true, 'category-id' => Entity\Report::CATEGORY_VIOLATION,
'created' => '2021-10-12 12:23:00' 'public-remarks' => 'Public remarks',
'private-remarks' => 'Private remarks',
'last-editor-uid' => 15,
'assigned-uid' => 16,
'status' => Entity\Report::STATUS_CLOSED,
'resolution' => Entity\Report::RESOLUTION_ACCEPTED,
'created' => '2021-10-12 12:23:00',
'edited' => '2021-12-10 21:08:00',
], ],
'postUriIds' => [89, 90], 'posts' => new Collection\Report\Posts([
new Entity\Report\Post(89),
new Entity\Report\Post(90),
]),
'rules' => new Collection\Report\Rules([
new Entity\Report\Rule(1, 'No hate speech'),
new Entity\Report\Rule(3, 'No commercial promotion'),
]),
'assertion' => new Entity\Report( 'assertion' => new Entity\Report(
14, 42,
13, 13,
new \DateTime('2021-10-12 12:23:00', new \DateTimeZone('UTC')), 14,
'Report', new \DateTimeImmutable('2021-10-12 12:23:00', new \DateTimeZone('UTC')),
'violation', Entity\Report::CATEGORY_VIOLATION,
'Rules',
true,
[89, 90],
12, 12,
'Report',
true,
new Collection\Report\Posts([
new Entity\Report\Post(89),
new Entity\Report\Post(90),
]),
new Collection\Report\Rules([
new Entity\Report\Rule(1, 'No hate speech'),
new Entity\Report\Rule(3, 'No commercial promotion'),
]),
'Public remarks',
'Private remarks',
new \DateTimeImmutable('2021-12-10 21:08:00', new \DateTimeZone('UTC')),
Entity\Report::STATUS_CLOSED,
Entity\Report::RESOLUTION_ACCEPTED,
16,
15,
11 11
), ),
], ],
]; ];
} }
public function assertReport(Entity\Report $assertion, Entity\Report $report)
{
self::assertEquals(
$assertion->id,
$report->id
);
self::assertEquals($assertion->uid, $report->uid);
self::assertEquals($assertion->reporterId, $report->reporterId);
self::assertEquals($assertion->cid, $report->cid);
self::assertEquals($assertion->comment, $report->comment);
self::assertEquals($assertion->category, $report->category);
self::assertEquals($assertion->rules, $report->rules);
self::assertEquals($assertion->forward, $report->forward);
// No way to test "now" at the moment
//self::assertEquals($assertion->created, $report->created);
self::assertEquals($assertion->postUriIds, $report->postUriIds);
}
/** /**
* @dataProvider dataCreateFromTableRow * @dataProvider dataCreateFromTableRow
*/ */
public function testCreateFromTableRow(array $row, array $postUriIds, Entity\Report $assertion) public function testCreateFromTableRow(ClockInterface $clock, array $row, Collection\Report\Posts $posts, Collection\Report\Rules $rules, Entity\Report $assertion)
{ {
$factory = new Factory\Report(new NullLogger()); $factory = new Factory\Report(new NullLogger(), $clock);
$this->assertReport($factory->createFromTableRow($row, $postUriIds), $assertion); $this->assertEquals($factory->createFromTableRow($row, $posts, $rules), $assertion);
} }
public function dataCreateFromReportsRequest(): array public function dataCreateFromReportsRequest(): array
{ {
$clock = new FrozenClock();
return [ return [
'default' => [ 'default' => [
'reporter-id' => 14, 'clock' => $clock,
'cid' => 13, 'rules' => [],
'comment' => '', 'reporterId' => 12,
'category' => null, 'cid' => 13,
'rules' => '', 'gsid' => 14,
'forward' => false, 'comment' => '',
'postUriIds' => [], 'category' => 'spam',
'uid' => 12, 'forward' => false,
'assertion' => new Entity\Report( 'postUriIds' => [],
14, 'ruleIds' => [],
13, 'uid' => null,
new \DateTime('now', new \DateTimeZone('UTC')), 'assertion' => new Entity\Report(
'',
null,
'',
false,
[],
12, 12,
null 13,
14,
$clock->now(),
Entity\Report::CATEGORY_SPAM,
), ),
], ],
'full' => [ 'full' => [
'reporter-id' => 14, 'clock' => $clock,
'cid' => 13, 'rules' => ['', 'Rule 1', 'Rule 2', 'Rule 3'],
'comment' => 'Report', 'reporterId' => 12,
'category' => 'violation', 'cid' => 13,
'rules' => 'Rules', 'gsid' => 14,
'forward' => true, 'comment' => 'Report',
'postUriIds' => [89, 90], 'category' => 'violation',
'uid' => 12, 'forward' => true,
'assertion' => new Entity\Report( 'postUriIds' => [89, 90],
14, 'ruleIds' => [1, 3],
13, 'uid' => 42,
new \DateTime('now', new \DateTimeZone('UTC')), 'assertion' => new Entity\Report(
'Report',
'violation',
'Rules',
true,
[89, 90],
12, 12,
null 13,
14,
$clock->now(),
Entity\Report::CATEGORY_VIOLATION,
42,
'Report',
true,
new Collection\Report\Posts([
new Entity\Report\Post(89),
new Entity\Report\Post(90)
]),
new Collection\Report\Rules([
new Entity\Report\Rule(1, 'Rule 1'),
new Entity\Report\Rule(3, 'Rule 3'),
]),
),
],
'forced-violation' => [
'clock' => $clock,
'rules' => ['', 'Rule 1', 'Rule 2', 'Rule 3'],
'reporterId' => 12,
'cid' => 13,
'gsid' => 14,
'comment' => 'Report',
'category' => 'other',
'forward' => false,
'postUriIds' => [],
'ruleIds' => [2, 3],
'uid' => null,
'assertion' => new Entity\Report(
12,
13,
14,
$clock->now(),
Entity\Report::CATEGORY_VIOLATION,
null,
'Report',
false,
new Collection\Report\Posts(),
new Collection\Report\Rules([
new Entity\Report\Rule(2, 'Rule 2'),
new Entity\Report\Rule(3, 'Rule 3'),
]),
),
],
'unknown-category' => [
'clock' => $clock,
'rules' => ['', 'Rule 1', 'Rule 2', 'Rule 3'],
'reporterId' => 12,
'cid' => 13,
'gsid' => 14,
'comment' => '',
'category' => 'unknown',
'forward' => false,
'postUriIds' => [],
'ruleIds' => [],
'uid' => null,
'assertion' => new Entity\Report(
12,
13,
14,
$clock->now(),
Entity\Report::CATEGORY_OTHER,
), ),
], ],
]; ];
@ -167,10 +265,10 @@ class ReportTest extends MockedTest
/** /**
* @dataProvider dataCreateFromReportsRequest * @dataProvider dataCreateFromReportsRequest
*/ */
public function testCreateFromReportsRequest(int $reporter, int $cid, string $comment, string $category = null, string $rules = '', bool $forward, array $postUriIds, int $uid, Entity\Report $assertion) public function testCreateFromReportsRequest(ClockInterface $clock, array $rules, int $reporterId, int $cid, int $gsid, string $comment, string $category, bool $forward, array $postUriIds, array $ruleIds, int $uid = null, Entity\Report $assertion)
{ {
$factory = new Factory\Report(new NullLogger()); $factory = new Factory\Report(new NullLogger(), $clock);
$this->assertReport($factory->createFromReportsRequest($reporter, $cid, $comment, $category, $rules, $forward, $postUriIds, $uid), $assertion); $this->assertEquals($factory->createFromReportsRequest($rules, $reporterId, $cid, $gsid, $comment, $category, $forward, $postUriIds, $ruleIds, $uid), $assertion);
} }
} }