Переглянути джерело

Add migrations to fix webfinger profiles

Daniel Supernault 2 роки тому
батько
коміт
66aa8bf9de

+ 59 - 0
database/migrations/2022_09_01_000000_fix_webfinger_profile_duplicate_accounts.php

@@ -0,0 +1,59 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+use App\Profile;
+use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
+
+class FixWebfingerProfileDuplicateAccounts extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        if(Profile::count() === 0) {
+            return;
+        }
+
+        Profile::whereNotNull('domain')
+            ->where('username', 'not like', '@%')
+            ->chunk(200, function($profiles) {
+                foreach($profiles as $profile) {
+                    $exists = Profile::whereUsername("@{$profile->username}@{$profile->domain}")->first();
+                    if($exists) {
+                        $exists->username = null;
+                        $exists->domain = null;
+                        $exists->webfinger = null;
+                        $exists->save();
+                        DeleteRemoteProfilePipeline::dispatch($exists);
+
+                        $profile->username = "@{$profile->username}@{$profile->domain}";
+                        if(!$profile->webfinger) {
+                            $profile->webfinger = "@{$profile->username}@{$profile->domain}";
+                        }
+                        $profile->save();
+                    } else {
+                        $profile->username = "@{$profile->username}@{$profile->domain}";
+                        if(!$profile->webfinger) {
+                            $profile->webfinger = "@{$profile->username}@{$profile->domain}";
+                        }
+                        $profile->save();
+                    }
+                }
+            });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        //
+    }
+}

+ 38 - 0
database/migrations/2022_09_01_043002_generate_missing_profile_webfinger.php

@@ -0,0 +1,38 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+use App\Profile;
+
+class GenerateMissingProfileWebfinger extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Profile::whereNotNull('domain')
+            ->whereNull('webfinger')
+            ->chunk(200, function($profiles) {
+                foreach($profiles as $profile) {
+                    if(substr($profile->username, 0, 1) === "@") {
+                        $profile->webfinger = $profile->username;
+                        $profile->save();
+                    }
+                }
+            });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        //
+    }
+}