created the follows model

This commit is contained in:
Ghostie 2025-01-04 10:31:20 -05:00
parent cd3cde3423
commit 4cd0dc96df
5 changed files with 95 additions and 27 deletions

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers\AP;
use App\Models\User; use App\Models\User;
use App\Models\Actor; use App\Models\Actor;
use App\Models\Activity; use App\Models\Activity;
use App\Models\Follow;
use App\Types\TypeActor; use App\Types\TypeActor;
use App\Types\TypeActivity; 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 // there's no follows model, it'll be handled with the activity model
$act = Activity::create ($activity); $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 // TODO: Users should be able to manually check this
$accept_activity = TypeActivity::craft_accept ($act); $accept_activity = TypeActivity::craft_accept ($act);
$response = TypeActivity::post_activity ($accept_activity, $target, $actor); $response = TypeActivity::post_activity ($accept_activity, $target, $actor);

View File

@ -3,19 +3,21 @@
namespace App\Http\Controllers\AP; namespace App\Http\Controllers\AP;
use App\Models\Note; use App\Models\Note;
use App\Models\NoteAttachment;
use App\Models\User; use App\Models\User;
use App\Models\Actor; use App\Models\Actor;
use App\Types\TypeNote;
use App\Models\Activity; use App\Models\Activity;
use App\Models\Instance; use App\Models\Instance;
use App\Models\Follow;
use App\Types\TypeActor; use App\Types\TypeActor;
use App\Types\TypeActivity; use App\Types\TypeActivity;
use App\Types\TypeNote;
use App\Actions\ActionsPost; use App\Actions\ActionsPost;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\NoteAttachment;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
@ -152,6 +154,12 @@ class APOutboxController extends Controller
$follow_activity = TypeActivity::craft_follow ($user->actor ()->first (), $object_actor); $follow_activity = TypeActivity::craft_follow ($user->actor ()->first (), $object_actor);
$response = TypeActivity::post_activity ($follow_activity, $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) if ($response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
return response ()->json ([ "error" => "failed to post activity" ], 500); return response ()->json ([ "error" => "failed to post activity" ], 500);

14
app/Models/Follow.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Follow extends Model
{
protected $fillable = [
"activity_id",
"actor",
"object"
];
}

View File

@ -75,44 +75,51 @@ class User extends Authenticatable
public function mutual_friends () public function mutual_friends ()
{ {
$followers = Activity::where ("type", "Follow")->where ("object", '"' . $this->actor->actor_id . '"')->pluck ("actor")->toArray (); $followers = Follow::where ("actor", $this->actor->id)->pluck ("object")->toArray ();
$following = Activity::where ("type", "Follow")->where ("actor", $this->actor->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 () public function received_requests ()
{ {
// users following me, where I am the object and I retrieve the actors // users following me, that I am not following them
$following = Activity::where ("type", "Follow") $followers = Follow::where ("object", $this->actor->id)->pluck ("actor")->toArray ();
->where ("object", '"' . $this->actor->actor_id . '"') // i am the object being followed $following = Follow::where ("actor", $this->actor->id)->pluck ("object")->toArray ();
->pluck ("actor")
->map (fn ($actor) => json_encode ($actor, JSON_UNESCAPED_SLASHES))
->toArray ();
// users i am following, where I am the actor and I retrieve the objects $diff = array_diff ($followers, $following);
$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); $actors = [];
foreach ($diff as $id)
{
$actors[] = Actor::where ("id", $id)->first ()->actor_id;
}
return $actors;
} }
public function sent_requests () public function sent_requests ()
{ {
// users i am following, where I am the actor and I retrieve the objects // users i am following
$followers = Activity::where ("type", "Follow") $followers = Follow::where ("actor", $this->actor->id)->pluck ("object")->toArray ();
->where ("actor", $this->actor->actor_id) // actors I follow $following = Follow::where ("object", $this->actor->id)->pluck ("actor")->toArray ();
->pluck ("object")->toArray ();
// users following me, where I am the object and I retrieve the actors $diff = array_diff ($followers, $following);
$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); $actors = [];
foreach ($diff as $id)
{
$actors[] = Actor::where ("id", $id)->first ()->actor_id;
}
return $actors;
} }
public function feed () public function feed ()

View File

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