Add likes and shares to activitypub note

This commit is contained in:
Mirai Kumiko 2025-07-06 15:04:27 +02:00
parent 00ecbadeca
commit 60ca00e5d8
Signed by: miraikumiko
GPG key ID: 3F178B1B5E0CB278
4 changed files with 37 additions and 6 deletions

View file

@ -21,6 +21,12 @@ defmodule Nulla.Announces do
Repo.all(Announce) Repo.all(Announce)
end end
def list_announces_by(by) when is_map(by) or is_list(by) do
Announce
|> where(^by)
|> Repo.all()
end
@doc """ @doc """
Gets a single announce. Gets a single announce.

View file

@ -21,6 +21,12 @@ defmodule Nulla.Likes do
Repo.all(Like) Repo.all(Like)
end end
def list_likes_by(by) when is_map(by) or is_list(by) do
Like
|> where(^by)
|> Repo.all()
end
@doc """ @doc """
Gets a single like. Gets a single like.

View file

@ -1,8 +1,10 @@
defmodule NullaWeb.ActivityPub.NoteController do defmodule NullaWeb.ActivityPub.NoteController do
use NullaWeb, :controller use NullaWeb, :controller
alias Nulla.Repo alias Nulla.Repo
alias NullaWeb.ActivityPub.NoteJSON
alias Nulla.Notes alias Nulla.Notes
alias Nulla.Likes
alias Nulla.Announces
alias NullaWeb.ActivityPub.NoteJSON
def show(conn, %{"username" => username, "id" => id}) do def show(conn, %{"username" => username, "id" => id}) do
case Integer.parse(id) do case Integer.parse(id) do
@ -23,9 +25,12 @@ defmodule NullaWeb.ActivityPub.NoteController do
|> halt() |> halt()
true -> true ->
likes_count = length(Likes.list_likes_by(note_id: note.id))
shares_count = length(Announces.list_announces_by(note_id: note.id))
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(NoteJSON.show(note)) |> json(NoteJSON.show(note, likes_count, shares_count))
end end
_ -> _ ->

View file

@ -4,11 +4,11 @@ defmodule NullaWeb.ActivityPub.NoteJSON do
@doc """ @doc """
Renders a single note. Renders a single note.
""" """
def show(note) do def show(note, likes_count, shares_count) do
data(note) data(note, likes_count, shares_count)
end end
defp data(%Note{} = note) do defp data(%Note{} = note, likes_count, shares_count) do
attachment = attachment =
case note.media_attachments do case note.media_attachments do
[] -> [] ->
@ -44,7 +44,21 @@ defmodule NullaWeb.ActivityPub.NoteJSON do
sensitive: note.sensitive, sensitive: note.sensitive,
content: note.content, content: note.content,
contentMap: Jason.OrderedObject.new("#{note.language}": note.content), contentMap: Jason.OrderedObject.new("#{note.language}": note.content),
attachment: attachment attachment: attachment,
tag: note.tag,
replies: %{},
likes:
Jason.OrderedObject.new(
id: "#{note.ap_id}/likes",
type: "Collection",
totalItems: likes_count
),
shares:
Jason.OrderedObject.new(
id: "#{note.ap_id}/shares",
type: "Collection",
totalItems: shares_count
)
) )
end end
end end