]> git.mxchange.org Git - friendica.git/commitdiff
We now store the violation as well
authorMichael <heluecht@pirati.ca>
Sun, 25 Dec 2022 10:44:06 +0000 (10:44 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 25 Dec 2022 10:44:06 +0000 (10:44 +0000)
database.sql
doc/database/db_report.md
src/Core/System.php
src/Moderation/Entity/Report.php
src/Moderation/Factory/Report.php
src/Moderation/Repository/Report.php
src/Module/Api/Mastodon/Reports.php
src/Protocol/ActivityPub/Processor.php
static/dbstructure.config.php
tests/src/Moderation/Factory/ReportTest.php

index 8c24dd4eb65d560a7b8f0eda3f0c268805a6ad4a..7eb756cf2a3df73aa5140d9cba7c98e18251c7b8 100644 (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',
index 56f51bdd416e49aeebe6e18cba9723f9efdc4ccd..92c0cced36c689b14dc4ca94d13223ad81924fad 100644 (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                |                |
index ec214fea9d26f613e260051903ac09fbbf48d43c..65dce1bc0e51f75e8f2bd6d5416c7dded18e3d61 100644 (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];
+                                       }
                                }
                        }
                }
index 2bb4350d485914cd3e93b822e11807d5d32826d7..3fbd77256857ccb4b00523935c43537f1f1d76ef 100644 (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;
index bbcbe8eb12461a9266f534a1ab1ede2b744540c1..8e66d8ad59f1beaa737d645c382b4c4d20c9f611 100644 (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,
index 78d88937888f31f093b48e64a44c826b4e34718d..5322a4ffba6bfc8974d0c9e5e0f05bfd52790049 100644 (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,
                ];
 
index 7d98a5e664d9d8bb3c8f04b3bed4072134711335..98213f9be8253a4df410c1c7a456282fc61f9986 100644 (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);
 
index 400a52b3271d843d7ab9ec28bf270eaf46ad95ff..e98fa4596c122b3ac21b638a55cad6b4118a9b64 100644 (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']]);
index 8971118147568f2517a16f4eaa6a5ca6672c83a1..1d573fa87ec8f896b4c270e457efeef3c1da0a64 100644 (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"],
index be406f499eddb99ee0be286a02a74e0a1ffab84a..8d1e8131ee96643e3fe7118027c7b8f8c5e789ad 100644 (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);
        }
 }