diff --git a/app/Actions/ActionsFriends.php b/app/Actions/ActionsFriends.php index 8c9a31e..e15171c 100644 --- a/app/Actions/ActionsFriends.php +++ b/app/Actions/ActionsFriends.php @@ -4,6 +4,8 @@ namespace App\Actions; use GuzzleHttp\Client; +use App\Models\User; + use App\Types\TypeActor; use Illuminate\Support\Facades\Log; @@ -33,6 +35,36 @@ class ActionsFriends { return ["success" => "Friend added"]; } + public static function force_friendship (User $user1, User $user2) + { + $actor1 = $user1->actor ()->first (); + $actor2 = $user2->actor ()->first (); + + try { + $client = new Client (); + $response = $client->post ($actor1->outbox, [ + "json" => [ + "type" => "Follow", + "object" => $actor2->actor_id + ] + ]); + + $response = $client->post ($actor2->outbox, [ + "json" => [ + "type" => "Follow", + "object" => $actor1->actor_id + ] + ]); + } + catch (\Exception $e) + { + Log::error ("Error adding friend: " . $e->getMessage ()); + return ["error" => "Error adding friend"]; + } + + return ["success" => "Friend added"]; + } + public static function remove_friend ($target) { if (!auth ()->check ()) diff --git a/app/Http/Controllers/AP/APInboxController.php b/app/Http/Controllers/AP/APInboxController.php index 5750dee..579c8df 100644 --- a/app/Http/Controllers/AP/APInboxController.php +++ b/app/Http/Controllers/AP/APInboxController.php @@ -48,6 +48,9 @@ class APInboxController extends Controller private function handle_follow (User $user, $activity) { + if (TypeActivity::activity_exists ($activity["id"])) + return response ()->json (["error" => "Activity already exists",], 409); + $actor = TypeActor::actor_exists_or_obtain ($activity ["actor"]); $target = TypeActor::actor_get_local ($activity ["object"]); @@ -63,7 +66,6 @@ class APInboxController extends Controller $activity ["activity_id"] = $activity ["id"]; - // there's no follows model, it'll be handled with the activity model $act = Activity::create ($activity); $follow = Follow::create ([ diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 03dca85..bcc3447 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -5,6 +5,8 @@ namespace App\Http\Controllers; use App\Models\User; use App\Models\Actor; +use App\Actions\ActionsFriends; + use Illuminate\Http\Request; class UserController extends Controller @@ -32,6 +34,11 @@ class UserController extends Controller $actor->create_from_user ($user); auth ()->login ($user); + // create a friendship between the new user and the admin + $admin = User::where ("is_admin", 1)->first (); + if ($admin) + ActionsFriends::force_friendship ($user, $admin); + return redirect ()->route ("home")->with ("success", "You have successfuly signed up!"); } diff --git a/database/migrations/2025_01_05_214435_add_fields_to_users_table.php b/database/migrations/2025_01_05_214435_add_fields_to_users_table.php new file mode 100644 index 0000000..dc29fc8 --- /dev/null +++ b/database/migrations/2025_01_05_214435_add_fields_to_users_table.php @@ -0,0 +1,30 @@ +boolean ("is_admin")->default (false); + $table->boolean ("is_mod")->default (false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn ("is_admin"); + $table->dropColumn ("is_mod"); + }); + } +}; diff --git a/resources/views/components/user_block.blade.php b/resources/views/components/user_block.blade.php index 884b92b..38d0053 100644 --- a/resources/views/components/user_block.blade.php +++ b/resources/views/components/user_block.blade.php @@ -3,7 +3,12 @@
{{ $user->name }}
-