From 149cf9dc299e15c25b71e9f1ce91bd6fd6bf1a8e Mon Sep 17 00:00:00 2001
From: Daniel Supernault <danielsupernault@gmail.com>
Date: Sat, 6 Aug 2022 03:23:58 -0600
Subject: [PATCH] Fix remote account post counts

---
 app/Console/Commands/FixRemotePostCount.php   | 53 +++++++++++++++++
 .../ProfilePipeline/DecrementPostCount.php    | 58 +++++++++++++++++++
 .../ProfilePipeline/IncrementPostCount.php    | 58 +++++++++++++++++++
 app/Util/ActivityPub/Helpers.php              |  4 ++
 app/Util/ActivityPub/Inbox.php                |  3 +
 5 files changed, 176 insertions(+)
 create mode 100644 app/Console/Commands/FixRemotePostCount.php
 create mode 100644 app/Jobs/ProfilePipeline/DecrementPostCount.php
 create mode 100644 app/Jobs/ProfilePipeline/IncrementPostCount.php

diff --git a/app/Console/Commands/FixRemotePostCount.php b/app/Console/Commands/FixRemotePostCount.php
new file mode 100644
index 000000000..71b20ecf1
--- /dev/null
+++ b/app/Console/Commands/FixRemotePostCount.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Profile;
+use App\Status;
+
+class FixRemotePostCount extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'fix:rpc';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Fix remote accounts post count';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        Profile::whereNotNull('domain')->chunk(50, function($profiles) {
+            foreach($profiles as $profile) {
+                $count = Status::whereNull(['in_reply_to_id', 'reblog_of_id'])->whereProfileId($profile->id)->count();
+                $this->info("Checking {$profile->id} {$profile->username} - found {$count} statuses");
+                $profile->status_count = $count;
+                $profile->save();
+            }
+        });
+
+        return 0;
+    }
+}
diff --git a/app/Jobs/ProfilePipeline/DecrementPostCount.php b/app/Jobs/ProfilePipeline/DecrementPostCount.php
new file mode 100644
index 000000000..d6781d7a5
--- /dev/null
+++ b/app/Jobs/ProfilePipeline/DecrementPostCount.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Jobs\ProfilePipeline;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeUnique;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use App\Profile;
+use App\Status;
+use App\Services\AccountService;
+
+class DecrementPostCount implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    public $id;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct($id)
+    {
+        $this->id = $id;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $id = $this->id;
+
+        $profile = Profile::find($id);
+
+        if(!$profile) {
+            return 1;
+        }
+
+        if($profile->updated_at && $profile->updated_at->lt(now()->subDays(30))) {
+            $profile->status_count = Status::whereProfileId($id)->whereNull(['in_reply_to_id', 'reblog_of_id'])->count();
+            $profile->save();
+            AccountService::del($id);
+        } else {
+            $profile->status_count = $profile->status_count ? $profile->status_count - 1 : 0;
+            $profile->save();
+            AccountService::del($id);
+        }
+
+        return 1;
+    }
+}
diff --git a/app/Jobs/ProfilePipeline/IncrementPostCount.php b/app/Jobs/ProfilePipeline/IncrementPostCount.php
new file mode 100644
index 000000000..554a4c149
--- /dev/null
+++ b/app/Jobs/ProfilePipeline/IncrementPostCount.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Jobs\ProfilePipeline;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeUnique;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use App\Profile;
+use App\Status;
+use App\Services\AccountService;
+
+class IncrementPostCount implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    public $id;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct($id)
+    {
+        $this->id = $id;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $id = $this->id;
+
+        $profile = Profile::find($id);
+
+        if(!$profile) {
+            return 1;
+        }
+
+        if($profile->updated_at && $profile->updated_at->lt(now()->subDays(30))) {
+            $profile->status_count = Status::whereProfileId($id)->whereNull(['in_reply_to_id', 'reblog_of_id'])->count();
+            $profile->save();
+            AccountService::del($id);
+        } else {
+            $profile->status_count = $profile->status_count + 1;
+            $profile->save();
+            AccountService::del($id);
+        }
+
+        return 1;
+    }
+}
diff --git a/app/Util/ActivityPub/Helpers.php b/app/Util/ActivityPub/Helpers.php
index b6fea3b29..e00f618ff 100644
--- a/app/Util/ActivityPub/Helpers.php
+++ b/app/Util/ActivityPub/Helpers.php
@@ -38,6 +38,8 @@ use App\Jobs\AvatarPipeline\RemoteAvatarFetch;
 use App\Util\Media\License;
 use App\Models\Poll;
 use Illuminate\Contracts\Cache\LockTimeoutException;
+use App\Jobs\ProfilePipeline\IncrementPostCount;
+use App\Jobs\ProfilePipeline\DecrementPostCount;
 
 class Helpers {
 
@@ -501,6 +503,8 @@ class Helpers {
 				NetworkTimelineService::add($status->id);
 			}
 
+			IncrementPostCount::dispatch($pid)->onQueue('low');
+
 			return $status;
 		});
 	}
diff --git a/app/Util/ActivityPub/Inbox.php b/app/Util/ActivityPub/Inbox.php
index e75a5228b..a5046a529 100644
--- a/app/Util/ActivityPub/Inbox.php
+++ b/app/Util/ActivityPub/Inbox.php
@@ -38,6 +38,8 @@ use App\Services\PollService;
 use App\Services\FollowerService;
 use App\Services\StatusService;
 use App\Models\Conversation;
+use App\Jobs\ProfilePipeline\IncrementPostCount;
+use App\Jobs\ProfilePipeline\DecrementPostCount;
 
 class Inbox
 {
@@ -655,6 +657,7 @@ class Inbox
 						$status->likes()->delete();
 						$status->shares()->delete();
 						$status->delete();
+                        DecrementPostCount::dispatch($profile->id)->onQueue('low');
 						return;
 					break;
 
-- 
GitLab