Add Federated Reports

This commit is contained in:
Daniel Supernault 2023-04-24 06:26:57 -06:00
parent c294368b8f
commit 383c6fe8ee
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 134 additions and 0 deletions

View file

@ -0,0 +1,17 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class RemoteReport extends Model
{
use HasFactory;
protected $casts = [
'status_ids' => 'array',
'action_taken_meta' => 'array',
'report_meta' => 'array'
];
}

View file

@ -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;
}
}

View file

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('remote_reports', function (Blueprint $table) {
$table->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');
}
};