minor refactoring to friends

This commit is contained in:
Ghostie 2025-01-02 21:43:56 -05:00
parent 031b805e5b
commit d6b0affcb4
10 changed files with 123 additions and 30 deletions

View File

@ -22,8 +22,7 @@ class APActorController extends Controller
public function followers (User $user)
{
$actor_id = '"' . str_replace ("/", "\/", $user->actor->actor_id) . '"';
$followers = Activity::where ("type", "Follow")->where ("object", $actor_id);
$followers = Activity::where ("type", "Follow")->where ("object", $user->actor->actor_id);
$ordered_collection = new TypeOrderedCollection ();
$ordered_collection->collection = $followers->get ()->pluck ("actor")->toArray ();
$ordered_collection->url = route ("ap.followers", $user->name);

View File

@ -165,14 +165,13 @@ class APOutboxController extends Controller
$object_actor = Actor::where ("actor_id", $object)->first ();
if (!$object_actor)
return response ()->json ([ "error" => "object not found" ], 404);
$object_id = '"' . str_replace ("/", "\/", $object_actor->actor_id) . '"';
$follow_activity = Activity::where ("actor", $user->actor ()->first ()->actor_id)
->where ("object", $object_id)
->where ("object", json_encode ($object_actor->actor_id, JSON_UNESCAPED_SLASHES))
->where ("type", "Follow")
->first ();
if (!$follow_activity)
return response ()->json ([ "error" => "no follow activity found" ], 404);
return response ()->json ([ "error" => "no follow activity found. " . $user->actor ()->first ()->actor_id . " unfollowing " . $object_actor->actor_id ], 404);
$unfollow_activity = TypeActivity::craft_undo ($follow_activity, $user->actor ()->first ());
$response = TypeActivity::post_activity ($unfollow_activity, $user->actor ()->first (), $object_actor);

View File

@ -47,18 +47,28 @@ class HomeController extends Controller
public function requests ()
{
$user = auth ()->user ();
$requests = [];
$received_requests = [];
$sent_requests = [];
foreach ($user->friend_requests () as $request)
foreach ($user->received_requests () as $request)
{
$actor = Actor::where ("actor_id", $request)->first ();
if (!$actor)
continue;
$requests[] = $actor;
$received_requests[] = $actor;
}
return view ("users.requests", compact ("user", "requests"));
foreach ($user->sent_requests () as $request)
{
$actor = Actor::where ("actor_id", $request)->first ();
if (!$actor)
continue;
$sent_requests[] = $actor;
}
return view ("users.requests", compact ("user", "received_requests", "sent_requests"));
}
public function requests_accept (Request $request)

View File

@ -20,6 +20,11 @@ class Activity extends Model
"target" => "array"
];
public function setObjectAttribute ($value)
{
$this->attributes["object"] = json_encode ($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION);
}
public function actor ()
{
return $this->belongsTo (Actor::class);

View File

@ -71,11 +71,8 @@ class Actor extends Model
public function friends_with (Actor $actor)
{
$self_id = '"' . str_replace ("/", "\/", $this->actor_id) . '"';
$other_id = '"' . str_replace ("/", "\/", $actor->actor_id) . '"';
$following = Activity::where ("actor", $this->actor_id)->where ("type", "Follow")->where ("object", $other_id)->first ();
$followers = Activity::where ("actor", $actor->actor_id)->where ("type", "Follow")->where ("object", $self_id)->first ();
$following = Activity::where ("actor", $this->actor_id)->where ("type", "Follow")->where ("object", $actor->actor_id)->first ();
$followers = Activity::where ("actor", $actor->actor_id)->where ("type", "Follow")->where ("object", $this->actor_id)->first ();
return $following && $followers;
}

View File

@ -75,20 +75,42 @@ class User extends Authenticatable
public function mutual_friends ()
{
$actor_id = '"' . str_replace ("/", "\/", $this->actor->actor_id) . '"';
$followers = Activity::where ("type", "Follow")->where ("object", $actor_id)->pluck ("actor")->toArray ();
$followers = Activity::where ("type", "Follow")->where ("object", '"' . $this->actor->actor_id . '"')->pluck ("actor")->toArray ();
$following = Activity::where ("type", "Follow")->where ("actor", $this->actor->actor_id)->pluck ("object")->toArray ();
return array_intersect ($followers, $following);
}
public function friend_requests ()
public function received_requests ()
{
$actor_id = '"' . str_replace ("/", "\/", $this->actor->actor_id) . '"';
// 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 ();
$followers = Activity::where ("type", "Follow")->where ("object", $actor_id)->pluck ("actor")->toArray ();
$following = Activity::where ("type", "Follow")->where ("actor", $this->actor->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 ();
return array_diff ($following, $followers);
}
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 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 ();
return array_diff ($followers, $following);
}

View File

@ -15,10 +15,10 @@
<div class="inner">
@foreach ($latest_users as $user)
<div class="person">
<a href="{{ route ('users.show', [ 'user_name' => $user ]) }}">
<a href="{{ route ('users.show', [ 'user_name' => $user->name ]) }}">
<p>{{ $user->name }}</p>
</a>
<a href="#">
<a href="{{ route ('users.show', [ 'user_name' => $user->name ]) }}">
<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">
</a>

View File

@ -1,3 +1,7 @@
@php
@endphp
<div class="row profile user-home">
<div class="col w-40 left">
<div class="general-about home-actions">
@ -103,7 +107,7 @@
<div class="inner">
<p>
<b>
<span class="count">{{ count (auth ()->user ()->friend_requests ()) }}</span>
<span class="count">{{ count (auth ()->user ()->received_requests ()) }}</span>
Open Friend Requests
</b>
</p>

View File

@ -24,9 +24,15 @@
<div class="details">
<p>{{ $user->status }}</p>
<p>{{ $user->about_you }}</p>
<p class="online">
<img loading="lazy" src="/resources/img/green_person.png" alt="online"> ONLINE!
</p>
@if (auth ()->user () && auth ()->user ()->actor ()->first ()->is ($actor))
<p class="online">
<img loading="lazy" src="/resources/img/green_person.png" alt="online"> YOU!
</p>
@else
<p class="online">
<img loading="lazy" src="/resources/img/green_person.png" alt="online"> ONLINE!
</p>
@endif
</div>
@endif
@ -57,6 +63,18 @@
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
<img loading="lazy" src="/resources/icons/delete.png" alt=""> Remove Friend
</form>
@elseif (in_array ($actor->actor_id, auth ()->user ()->received_requests ()))
<form action="{{ route ('user.friend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
@csrf
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
<img loading="lazy" src="/resources/icons/add.png" alt=""> Accept Friend Request
</form>
@elseif (in_array ($actor->actor_id, auth ()->user ()->sent_requests ()))
<form action="{{ route ('user.unfriend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
@csrf
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
<img loading="lazy" src="/resources/icons/hourglass.png" alt=""> Cancel Request
</form>
@else
<form action="{{ route ('user.friend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
@csrf
@ -275,7 +293,7 @@
</p>
@if (auth ()->user () && auth ()->user ()->is ($user))
<form action="{{ route ('user.post.new') }}" method="post" enctype="multipart/form-data">
<form action="{{ route ('user.post.new') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="text" name="summary" placeholder="Title" size="60">
<br>

View File

@ -14,7 +14,7 @@
<br>
<p>
<b>
<span class="count">{{ count ($user->friend_requests ()) }}</span>
<span class="count">{{ count ($user->received_requests ()) }}</span>
Open Friend Requests
</b>
@ -26,7 +26,7 @@
<table class="comments-table" cellspacing="0" cellpadding="3" bordercolor="ffffff" border="1">
<tbody>
@foreach ($requests as $frequest)
@foreach ($received_requests as $frequest)
<tr>
<td>
<a href="{{ route ('users.show', [ 'user_name' => $frequest->local_actor_id ? $frequest->local_actor_id : $frequest->preferredUsername ]) }}">
@ -54,6 +54,45 @@
</tbody>
</table>
</p>
<br>
<p>
<b>
<span class="count">{{ count ($user->sent_requests ()) }}</span>
Pending Friend Requests
</b>
<br>
<table class="comments-table" cellspacing="0" cellpadding="3" bordercolor="ffffff" border="1">
<tbody>
@foreach ($sent_requests as $frequest)
<tr>
<td>
<a href="{{ route ('users.show', [ 'user_name' => $frequest->local_actor_id ? $frequest->local_actor_id : $frequest->preferredUsername ]) }}">
<p>
{{ $frequest->name }}
</p>
</a>
<a href="{{ route ('users.show', [ 'user_name' => $frequest->local_actor_id ? $frequest->local_actor_id : $frequest->preferredUsername ]) }}">
<img src="{{ $frequest->user ? $frequest->user->avatar : $frequest->icon }}" alt="{{ $frequest->name }}" class="avatar">
</a>
</td>
<td>
<p>
<b>Sent Request</b>
</p>
<form method="POST">
@csrf
<input type="hidden" name="cancel" value="{{ $frequest->actor_id }}">
<input type="submit" name="submit" value="Cancel">
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</p>
</div>
</div>