From d38da1722a289fa55915cfaf2710d0d8381144b5 Mon Sep 17 00:00:00 2001 From: Ghostie Date: Sat, 4 Jan 2025 13:12:50 -0500 Subject: [PATCH] friends page --- README.md | 1 + app/Http/Controllers/AP/APInboxController.php | 9 ++++- app/Http/Controllers/ProfileController.php | 29 ++++++++++++++ app/Types/TypeActor.php | 38 +++++++++++++------ .../views/components/user_block.blade.php | 6 +-- resources/views/users/friends.blade.php | 34 +++++++++++++++++ resources/views/users/profile.blade.php | 18 ++++----- resources/views/users/requests.blade.php | 2 +- routes/web.php | 1 + 9 files changed, 113 insertions(+), 25 deletions(-) create mode 100644 resources/views/users/friends.blade.php diff --git a/README.md b/README.md index 29edb38..1858cc5 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Notice that the styles were taken from [AnySpace](https://anyspace.3to.moe/about - [x] Follows - [x] I cannot follow myself - [ ] Check when waiting for approval + - [ ] Handle Rejection - [ ] Likes - [ ] Comments diff --git a/app/Http/Controllers/AP/APInboxController.php b/app/Http/Controllers/AP/APInboxController.php index 0b6ff82..04c0fa7 100644 --- a/app/Http/Controllers/AP/APInboxController.php +++ b/app/Http/Controllers/AP/APInboxController.php @@ -46,6 +46,13 @@ class APInboxController extends Controller if (!$target || !$target->user) return response ()->json (["error" => "Target not found",], 404); + // check follow doesn't exist + $follow_exists = Follow::where ("actor", $actor->id) + ->where ("object", $target->id) + ->first (); + if ($follow_exists) + return response ()->json (["error" => "Follow already exists",], 409); + $activity ["activity_id"] = $activity ["id"]; // there's no follows model, it'll be handled with the activity model @@ -84,7 +91,7 @@ class APInboxController extends Controller $child_activity_id = $child_activity; if (!TypeActivity::activity_exists ($child_activity_id)) - return response ()->json (["error" => "Child activity not found",], 404); + return response ()->json (["error" => "Child activity doesn't exist",], 404); $child_activity = Activity::where ("activity_id", $child_activity_id)->first (); $child_activity->delete (); diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 2b21e59..f9d10d6 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -106,4 +106,33 @@ class ProfileController extends Controller return back ()->with ("success", "Profile updated successfully!"); } + + public function friends ($user_name) + { + // only local users + if (str_starts_with ($user_name, "@")) + { + return redirect ()->route ("users.show", [ "user_name" => $user_name ]); + } + + $user = User::where ("name", $user_name)->first (); + if (!$user) + return redirect ()->route ("home"); + + $actor = $user->actor; + + $ids = $user->mutual_friends (); + if (request ()->get ("query")) + { + $friends = Actor::whereIn ("actor_id", $ids) + ->where ("preferredUsername", "like", "%" . request ()->get ("query") . "%") + ->get (); + } + else + { + $friends = Actor::whereIn ("actor_id", $ids)->get (); + } + + return view ("users.friends", compact ("actor", "user", "friends")); + } } diff --git a/app/Types/TypeActor.php b/app/Types/TypeActor.php index ef14fe2..60ddd44 100644 --- a/app/Types/TypeActor.php +++ b/app/Types/TypeActor.php @@ -199,22 +199,38 @@ class TypeActor { $well_known = TypeActor::query_wellknown ($actor_name, $parsed_url ["host"]); - foreach ($well_known->links as $link) + if (isset ($well_known->links)) { - if ($link->rel == "self") + foreach ($well_known->links as $link) { - $client = new Client (); - $res = $client->request ("GET", $link->href, [ - "headers" => [ - "Accept" => "application/json" - ] - ]); - $actor = json_decode ($res->getBody ()->getContents (), true); + if ($link->rel == "self") + { + $client = new Client (); + $res = $client->request ("GET", $link->href, [ + "headers" => [ + "Accept" => "application/json" + ] + ]); + $actor = json_decode ($res->getBody ()->getContents (), true); - $result = TypeActor::create_from_request ($actor); - return $result; + $result = TypeActor::create_from_request ($actor); + return $result; + } } } + else + { + $client = new Client (); + $res = $client->request ("GET", $actor_id, [ + "headers" => [ + "Accept" => "application/activity+json" + ] + ]); + $actor = json_decode ($res->getBody ()->getContents (), true); + + $result = TypeActor::create_from_request ($actor); + return $result; + } return null; } diff --git a/resources/views/components/user_block.blade.php b/resources/views/components/user_block.blade.php index f8879f6..884b92b 100644 --- a/resources/views/components/user_block.blade.php +++ b/resources/views/components/user_block.blade.php @@ -1,9 +1,9 @@
- +

{{ $user->name }}

- - {{ $user->name }}'s profile picture + {{ $user->name }}'s profile picture
diff --git a/resources/views/users/friends.blade.php b/resources/views/users/friends.blade.php new file mode 100644 index 0000000..9a1acdb --- /dev/null +++ b/resources/views/users/friends.blade.php @@ -0,0 +1,34 @@ +@extends ("partials.layout") + +@section ("title", $user->name . "'s Friends") + +@section ("content") +
+

{{ $user->name }}'s Friends

+

+ « Back to profile +

+ +
+ +
+ + +
+ +
+ +
+
+

Friends

+
+
+ @forelse ($friends as $friend) + + @empty +

No friends found.

+ @endforelse +
+
+
+@endsection diff --git a/resources/views/users/profile.blade.php b/resources/views/users/profile.blade.php index 62bca0d..9f5fdf4 100644 --- a/resources/views/users/profile.blade.php +++ b/resources/views/users/profile.blade.php @@ -1,13 +1,13 @@ @extends ("partials.layout") -@section('title', "$actor->preferredUsername's Profile") +@section('title', "$actor->name's Profile") @section('content')
-

{{ $actor->preferredUsername }}

+

{{ $actor->name }}

@@ -49,7 +49,7 @@
-

Contacting {{ $actor->preferredUsername }}

+

Contacting {{ $actor->name }}

@auth @@ -250,7 +250,7 @@

- {{ $actor->preferredUsername }}'s Bio + {{ $actor->name }}'s Bio

@@ -264,16 +264,16 @@

- {{ $actor->preferredUsername }}'s Friend Space + {{ $actor->name }}'s Friend Space

- [view all] + [view all]

- {{ $actor->preferredUsername }} has {{ count ($user->mutual_friends ()) }} friends. + {{ $actor->name }} has {{ count ($user->mutual_friends ()) }} friends.

@@ -285,11 +285,11 @@
-

{{ $actor->preferredUsername }}'s Posts

+

{{ $actor->name }}'s Posts

- {{ $actor->preferredUsername }} has {{ count ($actor->posts) }} posts. + {{ $actor->name }} has {{ count ($actor->posts) }} posts.

@if (auth ()->user () && auth ()->user ()->is ($user)) diff --git a/resources/views/users/requests.blade.php b/resources/views/users/requests.blade.php index bda8353..98de8ea 100644 --- a/resources/views/users/requests.blade.php +++ b/resources/views/users/requests.blade.php @@ -31,7 +31,7 @@

- {{ $frequest->name }} + {{ $frequest->name ? $frequest->name : $frequest->preferredUsername }}

diff --git a/routes/web.php b/routes/web.php index 0e40da5..c00c212 100644 --- a/routes/web.php +++ b/routes/web.php @@ -25,6 +25,7 @@ Route::post ("/user/action/post/new", [ UserActionController::class, "post_new" // user routes Route::get ("/user/edit", [ ProfileController::class, "edit" ])->name ("users.edit")->middleware ("auth"); Route::post ("/user/edit", [ ProfileController::class, "update" ])->middleware ("auth"); +Route::get ("/user/{user_name}/friends", [ ProfileController::class, "friends" ])->name ("users.friends"); Route::get ("/user/{user_name}", [ ProfileController::class, "show" ])->name ("users.show"); // posts routes