From d09db3114516bf87970a9d5232d8cddf5d068dc9 Mon Sep 17 00:00:00 2001 From: Ghostie Date: Sun, 29 Dec 2024 15:21:34 -0500 Subject: [PATCH] now we can receive posts --- README.md | 6 +- app/Http/Controllers/AP/APInboxController.php | 1 + .../AP/APInstanceInboxController.php | 60 ++++++++++++++++++- app/Http/Controllers/ProfileController.php | 2 + app/Models/Note.php | 37 ++++++++++++ app/Models/NoteAttachment.php | 13 ++++ app/Types/TypeNote.php | 55 +++++++++++++++++ .../2024_12_29_193742_create_notes_table.php | 40 +++++++++++++ ...9_193934_create_note_attachments_table.php | 31 ++++++++++ 9 files changed, 242 insertions(+), 3 deletions(-) create mode 100644 app/Models/Note.php create mode 100644 app/Models/NoteAttachment.php create mode 100644 app/Types/TypeNote.php create mode 100644 database/migrations/2024_12_29_193742_create_notes_table.php create mode 100644 database/migrations/2024_12_29_193934_create_note_attachments_table.php diff --git a/README.md b/README.md index e2a71b8..bb9420e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/Http/Controllers/AP/APInboxController.php b/app/Http/Controllers/AP/APInboxController.php index b75605e..b2a8716 100644 --- a/app/Http/Controllers/AP/APInboxController.php +++ b/app/Http/Controllers/AP/APInboxController.php @@ -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); diff --git a/app/Http/Controllers/AP/APInstanceInboxController.php b/app/Http/Controllers/AP/APInstanceInboxController.php index c196e6e..be3d491 100644 --- a/app/Http/Controllers/AP/APInstanceInboxController.php +++ b/app/Http/Controllers/AP/APInstanceInboxController.php @@ -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); diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index ab6d9de..2b21e59 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -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 { diff --git a/app/Models/Note.php b/app/Models/Note.php new file mode 100644 index 0000000..54e8308 --- /dev/null +++ b/app/Models/Note.php @@ -0,0 +1,37 @@ +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); + } +} diff --git a/app/Models/NoteAttachment.php b/app/Models/NoteAttachment.php new file mode 100644 index 0000000..b65f6e6 --- /dev/null +++ b/app/Models/NoteAttachment.php @@ -0,0 +1,13 @@ +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 (); + } +} diff --git a/database/migrations/2024_12_29_193742_create_notes_table.php b/database/migrations/2024_12_29_193742_create_notes_table.php new file mode 100644 index 0000000..1ecfcc3 --- /dev/null +++ b/database/migrations/2024_12_29_193742_create_notes_table.php @@ -0,0 +1,40 @@ +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'); + } +}; diff --git a/database/migrations/2024_12_29_193934_create_note_attachments_table.php b/database/migrations/2024_12_29_193934_create_note_attachments_table.php new file mode 100644 index 0000000..9e353ff --- /dev/null +++ b/database/migrations/2024_12_29_193934_create_note_attachments_table.php @@ -0,0 +1,31 @@ +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'); + } +};