mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-27 08:16:16 +00:00
PleromaAPIController: Add endpoint to fetch emoji reactions.
This commit is contained in:
parent
99ea990a16
commit
05e9776517
|
@ -8,12 +8,40 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
|
|||
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 7]
|
||||
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Conversation.Participation
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Pleroma.Web.CommonAPI
|
||||
alias Pleroma.Web.MastodonAPI.AccountView
|
||||
alias Pleroma.Web.MastodonAPI.ConversationView
|
||||
alias Pleroma.Web.MastodonAPI.StatusView
|
||||
|
||||
def emoji_reactions_by(%{assigns: %{user: user}} = conn, %{"id" => activity_id}) do
|
||||
with %Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
|
||||
%Object{data: %{"reactions" => emoji_reactions}} <- Object.normalize(activity) do
|
||||
reactions =
|
||||
Enum.reduce(emoji_reactions, %{}, fn {emoji, users}, res ->
|
||||
users =
|
||||
users
|
||||
|> Enum.map(&User.get_cached_by_ap_id/1)
|
||||
|
||||
res
|
||||
|> Map.put(
|
||||
emoji,
|
||||
AccountView.render("accounts.json", %{users: users, for: user, as: :user})
|
||||
)
|
||||
end)
|
||||
|
||||
conn
|
||||
|> json(reactions)
|
||||
else
|
||||
_e ->
|
||||
conn
|
||||
|> json(%{})
|
||||
end
|
||||
end
|
||||
|
||||
def react_with_emoji(%{assigns: %{user: user}} = conn, %{"id" => activity_id, "emoji" => emoji}) do
|
||||
with {:ok, _activity, _object} <- CommonAPI.react_with_emoji(activity_id, user, emoji),
|
||||
activity = Activity.get_by_id(activity_id) do
|
||||
|
|
|
@ -265,6 +265,12 @@ defmodule Pleroma.Web.Router do
|
|||
end
|
||||
end
|
||||
|
||||
scope "/api/v1/pleroma", Pleroma.Web.PleromaAPI do
|
||||
pipe_through(:api)
|
||||
|
||||
get("/statuses/:id/emoji_reactions_by", PleromaAPIController, :emoji_reactions_by)
|
||||
end
|
||||
|
||||
scope "/api/v1/pleroma", Pleroma.Web.PleromaAPI do
|
||||
pipe_through(:authenticated_api)
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
|
|||
alias Pleroma.Conversation.Participation
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Web.CommonAPI
|
||||
alias Pleroma.Web.MastodonAPI.AccountView
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
|
@ -26,6 +27,30 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
|
|||
assert to_string(activity.id) == id
|
||||
end
|
||||
|
||||
test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
|
||||
{:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
|
||||
|
||||
result =
|
||||
conn
|
||||
|> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
|
||||
|> json_response(200)
|
||||
|
||||
assert result == %{}
|
||||
|
||||
{:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
|
||||
|
||||
result =
|
||||
conn
|
||||
|> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
|
||||
|> json_response(200)
|
||||
|
||||
[represented_user] = result["🎅"]
|
||||
assert represented_user["id"] == other_user.id
|
||||
end
|
||||
|
||||
test "/api/v1/pleroma/conversations/:id", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
|
|
Loading…
Reference in a new issue