From 60ca00e5d8e835e9c9aecdad8e825b561e103744 Mon Sep 17 00:00:00 2001 From: miraikumiko Date: Sun, 6 Jul 2025 15:04:27 +0200 Subject: [PATCH] Add likes and shares to activitypub note --- lib/nulla/announces.ex | 6 +++++ lib/nulla/likes.ex | 6 +++++ .../activitypub/note_controller.ex | 9 ++++++-- .../controllers/activitypub/note_json.ex | 22 +++++++++++++++---- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/nulla/announces.ex b/lib/nulla/announces.ex index c8d5fda..7a7ef0b 100644 --- a/lib/nulla/announces.ex +++ b/lib/nulla/announces.ex @@ -21,6 +21,12 @@ defmodule Nulla.Announces do Repo.all(Announce) end + def list_announces_by(by) when is_map(by) or is_list(by) do + Announce + |> where(^by) + |> Repo.all() + end + @doc """ Gets a single announce. diff --git a/lib/nulla/likes.ex b/lib/nulla/likes.ex index c89ee6b..5a8f456 100644 --- a/lib/nulla/likes.ex +++ b/lib/nulla/likes.ex @@ -21,6 +21,12 @@ defmodule Nulla.Likes do Repo.all(Like) end + def list_likes_by(by) when is_map(by) or is_list(by) do + Like + |> where(^by) + |> Repo.all() + end + @doc """ Gets a single like. diff --git a/lib/nulla_web/controllers/activitypub/note_controller.ex b/lib/nulla_web/controllers/activitypub/note_controller.ex index 58a1d27..166b149 100644 --- a/lib/nulla_web/controllers/activitypub/note_controller.ex +++ b/lib/nulla_web/controllers/activitypub/note_controller.ex @@ -1,8 +1,10 @@ defmodule NullaWeb.ActivityPub.NoteController do use NullaWeb, :controller alias Nulla.Repo - alias NullaWeb.ActivityPub.NoteJSON 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 @@ -23,9 +25,12 @@ defmodule NullaWeb.ActivityPub.NoteController do |> 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)) + |> json(NoteJSON.show(note, likes_count, shares_count)) end _ -> diff --git a/lib/nulla_web/controllers/activitypub/note_json.ex b/lib/nulla_web/controllers/activitypub/note_json.ex index a37c7b4..d7b3d0b 100644 --- a/lib/nulla_web/controllers/activitypub/note_json.ex +++ b/lib/nulla_web/controllers/activitypub/note_json.ex @@ -4,11 +4,11 @@ defmodule NullaWeb.ActivityPub.NoteJSON do @doc """ Renders a single note. """ - def show(note) do - data(note) + def show(note, likes_count, shares_count) do + data(note, likes_count, shares_count) end - defp data(%Note{} = note) do + defp data(%Note{} = note, likes_count, shares_count) do attachment = case note.media_attachments do [] -> @@ -44,7 +44,21 @@ defmodule NullaWeb.ActivityPub.NoteJSON do sensitive: note.sensitive, content: 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