Update
This commit is contained in:
parent
f963620cf0
commit
43ee272ca1
3 changed files with 87 additions and 14 deletions
|
@ -15,6 +15,7 @@
|
|||
</div>
|
||||
</div>
|
||||
<div class="relative border border-gray-300 shadow-md mt-5 rounded-t-xl overflow-hidden">
|
||||
{@note.content}
|
||||
</div>
|
||||
<div class="flex flex-col items-center mt-5 gap-5">
|
||||
<div class="text-sm rounded-xl border border-gray-300 p-4 w-[90%] h-[300px]"></div>
|
||||
|
|
|
@ -5,22 +5,40 @@ defmodule NullaWeb.NoteController do
|
|||
alias Nulla.Models.Note
|
||||
|
||||
def show(conn, %{"username" => username, "id" => id}) do
|
||||
accept = List.first(get_req_header(conn, "accept"))
|
||||
note = Note.get_note(id) |> Repo.preload([:actor, :media_attachments])
|
||||
case Integer.parse(id) do
|
||||
{int_id, ""} ->
|
||||
note = Note.get_note(int_id) |> Repo.preload([:actor, :media_attachments])
|
||||
|
||||
if username != note.actor.preferredUsername do
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> json(%{error: "Not Found"})
|
||||
|> halt()
|
||||
end
|
||||
cond do
|
||||
is_nil(note) ->
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> json(%{error: "Not Found"})
|
||||
|> halt()
|
||||
|
||||
if accept in ["application/activity+json", "application/ld+json"] do
|
||||
conn
|
||||
|> put_resp_content_type("application/activity+json")
|
||||
|> json(ActivityPub.note(note))
|
||||
else
|
||||
render(conn, :show, note: note, layout: false)
|
||||
username != note.actor.preferredUsername ->
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> json(%{error: "Not Found"})
|
||||
|> halt()
|
||||
|
||||
true ->
|
||||
accept = List.first(get_req_header(conn, "accept"))
|
||||
|
||||
if accept in ["application/activity+json", "application/ld+json"] do
|
||||
conn
|
||||
|> put_resp_content_type("application/activity+json")
|
||||
|> json(ActivityPub.note(note))
|
||||
else
|
||||
render(conn, :show, note: note, layout: false)
|
||||
end
|
||||
end
|
||||
|
||||
_ ->
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> json(%{error: "Not Found"})
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -71,5 +71,59 @@ defmodule NullaWeb.NoteControllerTest do
|
|||
assert is_map(response["contentMap"])
|
||||
assert is_list(response["attachment"])
|
||||
end
|
||||
|
||||
test "renders HTML if Accept header is regular", %{conn: conn} do
|
||||
{publicKeyPem, _privateKeyPem} = KeyGen.gen()
|
||||
|
||||
{:ok, actor} =
|
||||
Actor.create_actor(%{
|
||||
domain: "localhost",
|
||||
ap_id: "http://localhost/users/test",
|
||||
type: "Person",
|
||||
following: "http://localhost/users/test/following",
|
||||
followers: "http://localhost/users/test/followers",
|
||||
inbox: "http://localhost/users/test/inbox",
|
||||
outbox: "http://localhost/users/test/outbox",
|
||||
featured: "http://localhost/users/test/collections/featured",
|
||||
featuredTags: "http://localhost/users/test/collections/tags",
|
||||
preferredUsername: "test",
|
||||
name: "Test",
|
||||
summary: "Test User",
|
||||
url: "http://localhost/@test",
|
||||
manuallyApprovesFollowers: false,
|
||||
discoverable: true,
|
||||
indexable: true,
|
||||
published: DateTime.utc_now(),
|
||||
memorial: false,
|
||||
publicKey:
|
||||
Jason.OrderedObject.new(
|
||||
id: "http://localhost/users/test#main-key",
|
||||
owner: "http://localhost/users/test",
|
||||
publicKeyPem: publicKeyPem
|
||||
),
|
||||
endpoints: Jason.OrderedObject.new(sharedInbox: "http://localhost/inbox")
|
||||
})
|
||||
|
||||
note_id = Snowflake.next_id()
|
||||
|
||||
{:ok, note} =
|
||||
Note.create_note(%{
|
||||
id: note_id,
|
||||
url: "#{actor.url}/#{note_id}",
|
||||
content: "Hello World from Nulla!",
|
||||
language: "en",
|
||||
actor_id: actor.id
|
||||
})
|
||||
|
||||
conn = get(conn, ~p"/users/test/notes/#{note.id}")
|
||||
|
||||
assert html_response(conn, 200) =~ note.content
|
||||
end
|
||||
|
||||
test "returns 404 if note not found", %{conn: conn} do
|
||||
conn = get(conn, ~p"/users/test/notes/nonexistent")
|
||||
|
||||
assert json_response(conn, 404)["error"] == "Not Found"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue