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

View file

@ -21,51 +21,126 @@
namespace Friendica\Moderation\Entity;
use Friendica\Moderation\Collection;
/**
* @property-read int $id
* @property-read int $reporterId
* @property-read int $cid
* @property-read string $comment
* @property-read string|null $category
* @property-read bool $forward
* @property-read array $postUriIds
* @property-read int $uid
* @property-read \DateTime|null $created
* @property-read int $id
* @property-read int $reporterCid
* @property-read int $cid
* @property-read int $gsid
* @property-read string $comment
* @property-read string $publicRemarks
* @property-read string $privateRemarks
* @property-read bool $forward
* @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 */
protected $id;
/** @var int ID of the contact making a moderation report*/
protected $reporterId;
/** @var int ID of the contact being reported*/
/** @var int ID of the contact making a moderation report */
protected $reporterCid;
/** @var int ID of the contact being reported */
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;
/** @var string Optional category */
/** @var int One of CATEGORY_* */
protected $category;
/** @var string Violated rules */
protected $rules;
/** @var int ID of the user making a moderation report, null in case of an incoming forwarded report */
protected $reporterUid;
/** @var bool Whether this report should be forwarded to the remote server */
protected $forward;
/** @var \DateTime|null When the report was created */
/** @var \DateTimeImmutable When the report was created */
protected $created;
/** @var array Optional list of URI IDs of posts supporting the report*/
protected $postUriIds;
/** @var int ID of the user making a moderation report*/
protected $uid;
/** @var Collection\Report\Rules List of terms of service rule lines being possibly violated */
protected $rules;
/** @var Collection\Report\Posts List of URI IDs of posts supporting the report */
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)
{
$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;
$this->id = $id;
public function __construct(
int $reporterCid,
int $cid,
int $gsid,
\DateTimeImmutable $created,
int $category,
int $reporterUid = null,
string $comment = '',
bool $forward = false,
Collection\Report\Posts $posts = null,
Collection\Report\Rules $rules = null,
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;
use Friendica\Capabilities\ICanCreateFromTableRow;
use Friendica\Core\System;
use Friendica\Moderation\Collection;
use Friendica\Moderation\Entity;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
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 $postUriIds List of post URI ids from the `report-post` table
* @param array $row `report` table row
* @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
* @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(
$row['reporter-id'],
$row['cid'],
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')),
$row['comment'],
$row['category'],
$row['rules'],
$row['forward'],
$postUriIds,
$row['gsid'],
new \DateTimeImmutable($row['created'], new \DateTimeZone('UTC')),
$row['category-id'],
$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'],
);
}
@ -51,29 +74,44 @@ class Report extends \Friendica\BaseFactory implements ICanCreateFromTableRow
/**
* Creates a Report entity from a Mastodon API /reports request
*
* @see \Friendica\Module\Api\Mastodon\Reports::post()
*
* @param int $uid
* @param array $rules Line-number indexed node rules array, see System::getRules(true)
* @param int $reporterId
* @param int $cid
* @param int $gsid
* @param string $comment
* @param string $category
* @param bool $forward
* @param array $postUriIds
* @param array $ruleIds
* @param ?int $uid
* @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(
$reporterId,
$cid,
new \DateTime('now', new \DateTimeZone('UTC')),
$comment,
$category,
$rules,
$forward,
$postUriIds,
$gsid,
$this->clock->now(),
$categoryId,
$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\Database\Database;
use Friendica\Model\Post;
use Friendica\Moderation\Factory;
use Friendica\Moderation\Collection;
use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Util\DateTimeFormat;
use Psr\Log\LoggerInterface;
class Report extends \Friendica\BaseRepository
final class Report extends \Friendica\BaseRepository
{
protected static $table_name = 'report';
/**
* @var \Friendica\Moderation\Factory\Report
*/
/** @var Factory\Report */
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);
$this->factory = $factory;
$this->factory = $factory;
$this->postFactory = $postFactory;
$this->ruleFactory = $postFactory;
}
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)
{
$fields = [
'uid' => $Report->uid,
'reporter-id' => $Report->reporterId,
'cid' => $Report->cid,
'comment' => $Report->comment,
'category' => $Report->category,
'rules' => $Report->rules,
'forward' => $Report->forward,
'reporter-id' => $Report->reporterCid,
'uid' => $Report->reporterUid,
'cid' => $Report->cid,
'gsid' => $Report->gsid,
'comment' => $Report->comment,
'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) {
$this->db->update(self::$table_name, $fields, ['id' => $Report->id]);
} else {
$fields['created'] = DateTimeFormat::utcNow();
$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 ($postUriIds as $uriId) {
if (Post::exists(['uri-id' => $uriId])) {
$this->db->insert('report-post', ['rid' => $Report->id, 'uri-id' => $uriId]);
} else {
Logger::notice('Post does not exist', ['uri-id' => $uriId, 'report' => $Report]);
foreach ($Report->posts as $post) {
if (Post::exists(['uri-id' => $post->uriId])) {
$this->db->insert('report-post', ['rid' => $newReportId, 'uri-id' => $post->uriId, 'status' => $post->status]);
} else {
Logger::notice('Post does not exist', ['uri-id' => $post->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;
@ -88,13 +103,14 @@ class Report extends \Friendica\BaseRepository
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)) {
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);
}
}