now we can receive posts

This commit is contained in:
Ghostie 2024-12-29 15:21:34 -05:00
parent ac00afca07
commit d09db31145
9 changed files with 242 additions and 3 deletions

View File

@ -13,9 +13,11 @@ Notice that the styles were taken from [AnySpace](https://anyspace.3to.moe/about
- [-] Activitypub
- [x] Accounts
- [ ] Posts
- [-] Posts
- [ ] Local posts should be federated
- [ ] Remote posts should be fetched
- [ ] Local posts should be deleted
- [x] Remote posts should be fetched
- [x] Remote posts should be deleted
- [x] Follows
- [x] I cannot follow myself
- [ ] Check when waiting for approval

View File

@ -63,6 +63,7 @@ class APInboxController extends Controller
public function handle_undo (User $user, $activity)
{
// TODO: This needs to be refactored
if (TypeActivity::activity_exists ($activity ["id"]))
return response ()->json (["error" => "Activity already exists",], 409);

View File

@ -10,6 +10,7 @@ use App\Models\Activity;
use App\Types\TypeActor;
use App\Types\TypeActivity;
use App\Types\TypeNote;
use App\Http\Controllers\Controller;
@ -22,6 +23,14 @@ class APInstanceInboxController extends Controller
switch ($activity_type)
{
case "Create":
return $this->handle_create ($activity);
break;
case "Delete":
return $this->handle_delete ($activity);
break;
case "Update":
return $this->handle_update ($activity);
break;
@ -35,6 +44,39 @@ class APInstanceInboxController extends Controller
return response ()->json (["status" => "ok"]);
}
public function handle_create ($activity)
{
if (TypeActivity::activity_exists ($activity ["id"]))
return response ()->json (["status" => "ok"]);
$activity ["activity_id"] = $activity ["id"];
$new_activity = Activity::create ($activity);
$object = $activity ["object"];
switch ($object ["type"])
{
case "Note":
return $this->handle_create_note ($object, $new_activity);
break;
}
return response ()->json (["status" => "ok"]);
}
public function handle_delete ($activity)
{
// we suppose that we are deleting a note
$note = TypeNote::note_exists ($activity ["object"]["id"]);
if (!$note)
return response ()->json (["status" => "ok"]);
$activity = $note->get_activity ();
$activity->delete ();
return response ()->json (["status" => "ok"]);
}
public function handle_update ($activity)
{
if (TypeActivity::activity_exists ($activity ["id"]))
@ -55,9 +97,25 @@ class APInstanceInboxController extends Controller
return response ()->json (["status" => "ok"]);
}
// create related functions
public function handle_create_note ($note, Activity $activity)
{
$exists = TypeNote::note_exists ($note ["id"]);
if ($exists)
return response ()->json (["status" => "ok"]);
$actor = TypeActor::actor_exists_or_obtain ($activity ["actor"]);
if (!$actor)
return response ()->json (["status" => "error"]);
$created_note = TypeNote::create_from_request ($note, $activity, $actor);
return response ()->json (["status" => "ok"]);
}
// update related functions
public function handle_update_person ($person)
{
$actor = Actor::where ("actor_id", $person ["id"])->first ();
$actor = TypeActor::actor_exists ($person ["id"]);
if (!$actor)
$actor = TypeActor::create_from_request ($person);

View File

@ -22,6 +22,8 @@ class ProfileController extends Controller
if (str_starts_with ($user_name, "@")) {
$actor = Actor::where ("local_actor_id", $user_name)->first ();
if (!$actor)
return redirect ()->route ("home");
}
else
{

37
app/Models/Note.php Normal file
View File

@ -0,0 +1,37 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Note extends Model
{
protected $fillable = [
"activity_id",
"actor_id",
"note_id",
"in_reply_to",
"type",
"summary",
"url",
"attributedTo",
"content",
"tags",
];
public function get_activity ()
{
return $this->hasOne (Activity::class, "id", "activity_id");
}
public function get_actor ()
{
return $this->hasOne (Actor::class, "id", "actor_id");
}
public function attachments ()
{
return $this->hasMany (NoteAttachment::class);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class NoteAttachment extends Model
{
protected $fillable = [
"note_id",
"url"
];
}

55
app/Types/TypeNote.php Normal file
View File

@ -0,0 +1,55 @@
<?php
namespace App\Types;
use App\Models\Note;
use App\Models\Actor;
use App\Models\Activity;
use App\Models\NoteAttachment;
use Illuminate\Support\Facades\Log;
class TypeNote
{
public static function update_from_request (Note $note, $request, Activity $activity, Actor $actor)
{
$note->activity_id = $activity->id;
$note->actor_id = $actor->id;
$note->note_id = $request["id"] ?? null;
$note->in_reply_to = $request["inReplyTo"] ?? null;
$note->summary = $request["summary"] ?? null;
$note->url = $request["url"] ?? null;
$note->attributedTo = $request["attributedTo"] ?? null;
$note->content = $request["content"] ?? null;
$note->tag = $request["tag"] ?? null;
if ($request ["attachment"])
{
foreach ($request ["attachment"] as $attachment)
{
$note_attachment = NoteAttachment::create ([
"note_id" => $note->id,
"url" => $attachment ["url"]
]);
}
}
}
public static function create_from_request ($request, Activity $activity, Actor $actor)
{
$note = Note::create ([
"note_id" => $request["id"]
]);
TypeNote::update_from_request ($note, $request, $activity, $actor);
$note->save ();
return $note;
}
// some little functions
public static function note_exists ($note_id)
{
return Note::where ("note_id", $note_id)->first ();
}
}

View File

@ -0,0 +1,40 @@
<?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('notes', function (Blueprint $table) {
$table->id();
$table->foreignId ("activity_id")->nullable ()->constrained ()->onDelete ("cascade");
$table->foreignId ("actor_id")->nullable ()->constrained ()->onDelete ("cascade");
$table->string ("note_id")->unique ();
$table->string ("in_reply_to")->nullable ();
$table->string ("type")->default ("Note");
$table->string ("summary")->nullable ();
$table->string ("url")->nullable ();
$table->string ("attributedTo")->nullable ();
$table->text ("content")->nullable ();
$table->string ("tag")->nullable ();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('notes');
}
};

View File

@ -0,0 +1,31 @@
<?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('note_attachments', function (Blueprint $table) {
$table->id();
$table->foreignId('note_id')->constrained()->onDelete('cascade');
$table->string ("url")->nullable ();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('note_attachments');
}
};