now all users will be friend of the admin upon account creation

This commit is contained in:
Ghostie 2025-01-05 16:59:32 -05:00
parent f7b9ff9b8b
commit c578a4c56f
5 changed files with 79 additions and 3 deletions

View File

@ -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 ())

View File

@ -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 ([

View File

@ -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!");
}

View File

@ -0,0 +1,30 @@
<?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::table('users', function (Blueprint $table) {
$table->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");
});
}
};

View File

@ -3,7 +3,12 @@
<p>{{ $user->name }}</p>
</a>
<a href="{{ route ('users.show', [ 'user_name' => $user->local_actor_id ? $user->local_actor_id : $user->name ]) }}">
<img loading="lazy" src="{{ $user->avatar ? $user->avatar : $user->icon }}" alt="{{ $user->name }}'s profile picture"
class="pfp-fallback" style="width: 100%; max-height: 95px; aspect-ratio: 1/1">
@if ($user instanceof App\Models\User)
<img loading="lazy" src="{{ $user->avatar }}" alt="{{ $user->name }}'s profile picture"
class="pfp-fallback" style="width: 100%; max-height: 95px; aspect-ratio: 1/1">
@else
<img loading="lazy" src="{{ $user->user_id ? $user->user ()->first ()->avatar : $user->icon }}" alt="{{ $user->name }}'s profile picture"
class="pfp-fallback" style="width: 100%; max-height: 95px; aspect-ratio: 1/1">
@endif
</a>
</div>