defmodule NullaWeb.ActivityPub.NoteController do use NullaWeb, :controller alias Nulla.Repo alias Nulla.Notes alias Nulla.Likes alias Nulla.Announces alias NullaWeb.ActivityPub.NoteJSON def show(conn, %{"username" => username, "id" => id}) do case Integer.parse(id) do {int_id, ""} -> note = Notes.get_note!(int_id) |> Repo.preload([:actor, :media_attachments]) cond do is_nil(note) -> conn |> put_status(:not_found) |> json(%{error: "Not Found"}) |> halt() username != note.actor.preferredUsername -> conn |> put_status(:not_found) |> json(%{error: "Not Found"}) |> halt() true -> likes_count = length(Likes.list_likes_by(note_id: note.id)) shares_count = length(Announces.list_announces_by(note_id: note.id)) conn |> put_resp_content_type("application/activity+json") |> json(NoteJSON.show(note, likes_count, shares_count)) end _ -> conn |> put_status(:not_found) |> json(%{error: "Not Found"}) |> halt() end end end