diff --git a/app/Models/RemoteReport.php b/app/Models/RemoteReport.php new file mode 100644 index 000000000..5eff3ab92 --- /dev/null +++ b/app/Models/RemoteReport.php @@ -0,0 +1,17 @@ + 'array', + 'action_taken_meta' => 'array', + 'report_meta' => 'array' + ]; +} diff --git a/app/Util/ActivityPub/Inbox.php b/app/Util/ActivityPub/Inbox.php index 7bc34beac..9b42e53b8 100644 --- a/app/Util/ActivityPub/Inbox.php +++ b/app/Util/ActivityPub/Inbox.php @@ -42,6 +42,7 @@ use App\Services\StatusService; use App\Services\UserFilterService; use App\Services\NetworkTimelineService; use App\Models\Conversation; +use App\Models\RemoteReport; use App\Jobs\ProfilePipeline\IncrementPostCount; use App\Jobs\ProfilePipeline\DecrementPostCount; @@ -122,6 +123,10 @@ class Inbox $this->handleStoryReplyActivity(); break; + case 'Flag': + $this->handleFlagActivity(); + break + // case 'Update': // (new UpdateActivity($this->payload, $this->profile))->handle(); // break; @@ -1140,4 +1145,80 @@ class Inbox return; } + + public function handleFlagActivity() + { + if(!isset( + $this->payload['id'], + $this->payload['type'], + $this->payload['actor'], + $this->payload['content'], + $this->payload['object'] + )) { + return; + } + + $id = $this->payload['id']; + $actor = $this->payload['actor']; + $content = Purify::clean($this->payload['content']); + $object = $this->payload['object']; + + if(Helpers::validateLocalUrl($id) || parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) { + return; + } + + if(!is_array($object) || empty($object)) { + return; + } + + if(count($object) > 40) { + return; + } + + $objects = collect([]); + $accountId = null; + + foreach($object as $objectUrl) { + if(!Helpers::validateLocalUrl($objectUrl)) { + return; + } + + if(str_contains($objectUrl, '/users/')) { + $username = last(explode('/', $objectUrl)); + $profileId = Profile::whereUsername($username)->first(); + if($profileId) { + $accountId = $profileId->id; + } + } else if(str_contains($object_url, '/p/')) { + $postId = last(explode('/', $objectUrl)); + $objects->push($postId); + } else { + return; + } + } + + if(!$accountId || !$objects->count()) { + return; + } + + $instanceHost = parse_url($id, PHP_URL_HOST); + + $instance = Instance::updateOrCreate([ + 'domain' => $instanceHost + ]); + + $report = new RemoteReport; + $report->status_ids = $objects->toArray(); + $report->comment = $content; + $report->account_id = $accountId; + $report->uri = $id; + $report->instance_id = $instance->id; + $report->report_meta = [ + 'actor' => $actor, + 'object' => $object + ]; + $report->save(); + + return; + } } diff --git a/database/migrations/2023_04_24_101904_create_remote_reports_table.php b/database/migrations/2023_04_24_101904_create_remote_reports_table.php new file mode 100644 index 000000000..de16bbcc4 --- /dev/null +++ b/database/migrations/2023_04_24_101904_create_remote_reports_table.php @@ -0,0 +1,36 @@ +bigIncrements('id'); + $table->json('status_ids')->nullable(); + $table->text('comment')->nullable(); + $table->bigInteger('account_id')->unsigned()->nullable(); + $table->string('uri')->nullable(); + $table->unsignedInteger('instance_id')->nullable(); + $table->timestamp('action_taken_at')->nullable()->index(); + $table->json('report_meta')->nullable(); + $table->json('action_taken_meta')->nullable(); + $table->bigInteger('action_taken_by_account_id')->unsigned()->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('remote_reports'); + } +};