now local posts can be deleted as well

This commit is contained in:
Ghostie 2025-01-04 13:42:28 -05:00
parent d38da1722a
commit d32c81575f
5 changed files with 86 additions and 26 deletions

View File

@ -37,6 +37,10 @@ class APOutboxController extends Controller
return $this->handle_update_note ($user, $request);
break;
case "DeleteNote":
return $this->handle_delete_note ($user, $request);
break;
case "Follow":
return $this->handle_follow ($user, $request->get ("object"));
break;
@ -62,15 +66,7 @@ class APOutboxController extends Controller
$actor_response = TypeActor::build_response ($actor);
$update_activity = TypeActivity::craft_update ($actor, $actor_response);
$instances = Instance::all ();
foreach ($instances as $instance)
{
$response = TypeActivity::post_activity ($update_activity, $actor, $instance->inbox, true);
if ($response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
{
Log::info ("failed to post activity to " . $instance->inbox);
}
}
$response = TypeActivity::post_to_instances ($update_activity, $actor);
return response ()->json ("success", 200);
}
@ -121,16 +117,27 @@ class APOutboxController extends Controller
$note_response = TypeNote::build_response ($note);
$update_activity = TypeActivity::craft_update ($actor, $note_response);
$instances = Instance::all ();
foreach ($instances as $instance)
{
$response = TypeActivity::post_activity ($update_activity, $actor, $instance->inbox, true);
if ($response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
{
Log::info ("failed to post activity to " . $instance->inbox);
}
$response = TypeActivity::post_to_instances ($update_activity, $actor);
return response ()->json ("success", 200);
}
public function handle_delete_note (User $user, $request)
{
$actor = $user->actor ()->first ();
$note = Note::where ("id", $request ["note"])->first ();
if (!$note)
return response ()->json ([ "error" => "note not found" ], 404);
$note_actor = $note->get_actor ()->first ();
if ($actor != $note_actor)
return response ()->json ([ "error" => "not allowed" ], 403);
$note->delete ();
$delete_activity = TypeActivity::craft_delete ($actor, $note->note_id);
$response = TypeActivity::post_to_instances ($delete_activity, $actor);
return response ()->json ("success", 200);
}
@ -208,13 +215,7 @@ class APOutboxController extends Controller
$note->activity_id = $create_activity->id;
$note->save ();
$instances = Instance::all ();
foreach ($instances as $instance)
{
$response = TypeActivity::post_activity ($create_activity, $actor, $instance->inbox);
if ($response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
continue;
}
$response = TypeActivity::post_to_instances ($create_activity, $actor);
return response ()->json ("success", 200);
}
}

View File

@ -69,4 +69,30 @@ class PostController extends Controller
Log::error ($e->getMessage ());
}
}
public function delete (Note $note)
{
$actor = auth ()->user ()->actor ()->first ();
$note_user = $actor->user ()->first ();
if (!auth ()->user ()->is ($note_user)) {
return back ()->with ("error", "You are not allowed to delete this post.");
}
try {
$client = new Client ();
$client->request ("POST", $note->get_actor ()->first ()->outbox, [
"json" => [
"type" => "DeleteNote",
"note" => $note->id
]
]);
return redirect ()->route ("home")->with ("success", "Post deleted successfully.");
} catch (\Exception $e) {
return back ()->with ("error", "An error occurred while deleting the post.");
Log::error ("An error occurred while deleting the post.");
Log::error ($e->getMessage ());
}
}
}

View File

@ -4,6 +4,7 @@ namespace App\Types;
use App\Models\Actor;
use App\Models\Activity;
use App\Models\Instance;
use GuzzleHttp\Client;
@ -101,6 +102,23 @@ class TypeActivity {
return $create_activity;
}
public static function craft_delete (Actor $actor, $id)
{
$delete_activity = new Activity ();
$delete_activity->activity_id = env ("APP_URL") . "/activity/" . uniqid ();
$delete_activity->type = "Delete";
$delete_activity->actor = $actor->actor_id;
$delete_activity->object = [
"id" => $id,
"type" => "Tombstone"
];
$delete_activity->save ();
return $delete_activity;
}
public static function get_private_key (Actor $actor)
{
return openssl_get_privatekey ($actor->private_key);
@ -234,6 +252,19 @@ class TypeActivity {
return $response;
}
public static function post_to_instances (Activity $activity, Actor $source)
{
$instances = Instance::all ();
foreach ($instances as $instance)
{
$response = TypeActivity::post_activity ($activity, $source, $instance->inbox, true);
if ($response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
{
Log::info ("failed to post activity to " . $instance->inbox);
}
}
}
// some little functions
public static function activity_exists ($activity_id)
{

View File

@ -42,6 +42,7 @@
@if (auth ()->check () && auth ()->user ()->is ($actor->user))
<form action="#" method="POST">
@csrf
@method("DELETE")
<a href="{{ route ('posts.edit', [ 'note' => $note ]) }}">
<button type="button">Edit</button>
</a>

View File

@ -32,6 +32,7 @@ Route::get ("/user/{user_name}", [ ProfileController::class, "show" ])->name ("u
Route::get ("/post/{note}/edit", [ PostController::class, "edit" ])->name ("posts.edit")->middleware ("auth");
Route::post ("/post/{note}/edit", [ PostController::class, "update" ])->middleware ("auth");
Route::get ("/post/{note}", [ PostController::class, "show" ])->name ("posts.show");
Route::delete ("/post/{note}", [ PostController::class, "delete" ])->name ("posts.delete")->middleware ("auth");
// other routes
Route::get ("/search", [ HomeController::class, "search" ])->name ("search");