From 4cd0dc96dff0735e58e5d7418751ca1b8064054b Mon Sep 17 00:00:00 2001 From: Ghostie Date: Sat, 4 Jan 2025 10:31:20 -0500 Subject: [PATCH] created the follows model --- app/Http/Controllers/AP/APInboxController.php | 7 +++ .../Controllers/AP/APOutboxController.php | 12 +++- app/Models/Follow.php | 14 +++++ app/Models/User.php | 57 +++++++++++-------- ...2025_01_04_151745_create_follows_table.php | 32 +++++++++++ 5 files changed, 95 insertions(+), 27 deletions(-) create mode 100644 app/Models/Follow.php create mode 100644 database/migrations/2025_01_04_151745_create_follows_table.php diff --git a/app/Http/Controllers/AP/APInboxController.php b/app/Http/Controllers/AP/APInboxController.php index 0261b88..0b6ff82 100644 --- a/app/Http/Controllers/AP/APInboxController.php +++ b/app/Http/Controllers/AP/APInboxController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\AP; use App\Models\User; use App\Models\Actor; use App\Models\Activity; +use App\Models\Follow; use App\Types\TypeActor; use App\Types\TypeActivity; @@ -50,6 +51,12 @@ class APInboxController extends Controller // there's no follows model, it'll be handled with the activity model $act = Activity::create ($activity); + $follow = Follow::create ([ + "activity_id" => $act->id, + "actor" => $actor->id, + "object" => $target->id, + ]); + // TODO: Users should be able to manually check this $accept_activity = TypeActivity::craft_accept ($act); $response = TypeActivity::post_activity ($accept_activity, $target, $actor); diff --git a/app/Http/Controllers/AP/APOutboxController.php b/app/Http/Controllers/AP/APOutboxController.php index 541e43b..c9d9be6 100644 --- a/app/Http/Controllers/AP/APOutboxController.php +++ b/app/Http/Controllers/AP/APOutboxController.php @@ -3,19 +3,21 @@ namespace App\Http\Controllers\AP; use App\Models\Note; +use App\Models\NoteAttachment; use App\Models\User; use App\Models\Actor; -use App\Types\TypeNote; use App\Models\Activity; use App\Models\Instance; +use App\Models\Follow; use App\Types\TypeActor; use App\Types\TypeActivity; + +use App\Types\TypeNote; use App\Actions\ActionsPost; use Illuminate\Http\Request; -use App\Models\NoteAttachment; use Illuminate\Support\Facades\Log; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Storage; @@ -152,6 +154,12 @@ class APOutboxController extends Controller $follow_activity = TypeActivity::craft_follow ($user->actor ()->first (), $object_actor); $response = TypeActivity::post_activity ($follow_activity, $user->actor ()->first (), $object_actor); + $follow = Follow::create ([ + "activity_id" => $follow_activity->id, + "actor" => $user->actor ()->first ()->id, + "object" => $object_actor->id, + ]); + if ($response->getStatusCode () < 200 || $response->getStatusCode () >= 300) return response ()->json ([ "error" => "failed to post activity" ], 500); diff --git a/app/Models/Follow.php b/app/Models/Follow.php new file mode 100644 index 0000000..c7135c3 --- /dev/null +++ b/app/Models/Follow.php @@ -0,0 +1,14 @@ +where ("object", '"' . $this->actor->actor_id . '"')->pluck ("actor")->toArray (); - $following = Activity::where ("type", "Follow")->where ("actor", $this->actor->actor_id)->pluck ("object")->toArray (); + $followers = Follow::where ("actor", $this->actor->id)->pluck ("object")->toArray (); + $following = Follow::where ("object", $this->actor->id)->pluck ("actor")->toArray (); - return array_intersect ($followers, $following); + $mutuals = array_intersect ($followers, $following); + $actors = []; + + foreach ($mutuals as $id) + { + $actors[] = Actor::where ("id", $id)->first ()->actor_id; + } + + return $actors; } public function received_requests () { - // users following me, where I am the object and I retrieve the actors - $following = Activity::where ("type", "Follow") - ->where ("object", '"' . $this->actor->actor_id . '"') // i am the object being followed - ->pluck ("actor") - ->map (fn ($actor) => json_encode ($actor, JSON_UNESCAPED_SLASHES)) - ->toArray (); + // users following me, that I am not following them + $followers = Follow::where ("object", $this->actor->id)->pluck ("actor")->toArray (); + $following = Follow::where ("actor", $this->actor->id)->pluck ("object")->toArray (); - // users i am following, where I am the actor and I retrieve the objects - $followers = Activity::where ("type", "Follow") - ->whereIn ("object", $following) // actors - ->where ("actor", $this->actor->actor_id) // following me - ->pluck ("actor")->toArray (); + $diff = array_diff ($followers, $following); - return array_diff ($following, $followers); + $actors = []; + foreach ($diff as $id) + { + $actors[] = Actor::where ("id", $id)->first ()->actor_id; + } + return $actors; } public function sent_requests () { - // users i am following, where I am the actor and I retrieve the objects - $followers = Activity::where ("type", "Follow") - ->where ("actor", $this->actor->actor_id) // actors I follow - ->pluck ("object")->toArray (); + // users i am following + $followers = Follow::where ("actor", $this->actor->id)->pluck ("object")->toArray (); + $following = Follow::where ("object", $this->actor->id)->pluck ("actor")->toArray (); - // users following me, where I am the object and I retrieve the actors - $following = Activity::where ("type", "Follow") - ->whereIn ("actor", $followers) // actors - ->where ("object", '"' . $this->actor->actor_id . '"') // that following me - ->pluck ("actor")->toArray (); + $diff = array_diff ($followers, $following); - return array_diff ($followers, $following); + $actors = []; + foreach ($diff as $id) + { + $actors[] = Actor::where ("id", $id)->first ()->actor_id; + } + + return $actors; } public function feed () diff --git a/database/migrations/2025_01_04_151745_create_follows_table.php b/database/migrations/2025_01_04_151745_create_follows_table.php new file mode 100644 index 0000000..895cb54 --- /dev/null +++ b/database/migrations/2025_01_04_151745_create_follows_table.php @@ -0,0 +1,32 @@ +id(); + + $table->foreignId ("activity_id")->constrained()->onDelete("cascade"); + $table->foreignId ("actor")->constrained("actors")->onDelete("cascade"); + $table->foreignId ("object")->constrained("actors")->onDelete("cascade"); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('follows'); + } +};