EmojiReactions: Add Mastodon-aligned reaction endpoints, change response

This commit is contained in:
Lain Soykaf 2020-02-07 14:52:13 +01:00
parent f13b7878b4
commit f875b9650a
5 changed files with 96 additions and 12 deletions

View File

@ -242,9 +242,9 @@ def render("show.json", %{activity: %{data: %{"object" => _object}} = activity}
with %{data: %{"reactions" => emoji_reactions}} <- object do with %{data: %{"reactions" => emoji_reactions}} <- object do
Enum.map(emoji_reactions, fn [emoji, users] -> Enum.map(emoji_reactions, fn [emoji, users] ->
%{ %{
emoji: emoji, name: emoji,
count: length(users), count: length(users),
reacted: !!(opts[:for] && opts[:for].ap_id in users) me: !!(opts[:for] && opts[:for].ap_id in users)
} }
end) end)
else else

View File

@ -53,10 +53,10 @@ def emoji_reactions_by(%{assigns: %{user: user}} = conn, %{"id" => activity_id})
|> Enum.filter(& &1) |> Enum.filter(& &1)
%{ %{
emoji: emoji, name: emoji,
count: length(users), count: length(users),
accounts: AccountView.render("index.json", %{users: users, for: user, as: :user}), accounts: AccountView.render("index.json", %{users: users, for: user, as: :user}),
reacted: !!(user && user.ap_id in user_ap_ids) me: !!(user && user.ap_id in user_ap_ids)
} }
end) end)

View File

@ -272,6 +272,7 @@ defmodule Pleroma.Web.Router do
pipe_through(:api) pipe_through(:api)
get("/statuses/:id/emoji_reactions_by", PleromaAPIController, :emoji_reactions_by) get("/statuses/:id/emoji_reactions_by", PleromaAPIController, :emoji_reactions_by)
get("/statuses/:id/reactions", PleromaAPIController, :emoji_reactions_by)
end end
scope "/api/v1/pleroma", Pleroma.Web.PleromaAPI do scope "/api/v1/pleroma", Pleroma.Web.PleromaAPI do
@ -289,6 +290,8 @@ defmodule Pleroma.Web.Router do
patch("/conversations/:id", PleromaAPIController, :update_conversation) patch("/conversations/:id", PleromaAPIController, :update_conversation)
post("/statuses/:id/react_with_emoji", PleromaAPIController, :react_with_emoji) post("/statuses/:id/react_with_emoji", PleromaAPIController, :react_with_emoji)
post("/statuses/:id/unreact_with_emoji", PleromaAPIController, :unreact_with_emoji) post("/statuses/:id/unreact_with_emoji", PleromaAPIController, :unreact_with_emoji)
put("/statuses/:id/reactions/:emoji", PleromaAPIController, :react_with_emoji)
delete("/statuses/:id/reactions/:emoji", PleromaAPIController, :unreact_with_emoji)
post("/notifications/read", PleromaAPIController, :read_notification) post("/notifications/read", PleromaAPIController, :read_notification)
patch("/accounts/update_avatar", AccountController, :update_avatar) patch("/accounts/update_avatar", AccountController, :update_avatar)

View File

@ -37,15 +37,15 @@ test "has an emoji reaction list" do
status = StatusView.render("show.json", activity: activity) status = StatusView.render("show.json", activity: activity)
assert status[:pleroma][:emoji_reactions] == [ assert status[:pleroma][:emoji_reactions] == [
%{emoji: "β˜•", count: 2, reacted: false}, %{name: "β˜•", count: 2, me: false},
%{emoji: "🍡", count: 1, reacted: false} %{name: "🍡", count: 1, me: false}
] ]
status = StatusView.render("show.json", activity: activity, for: user) status = StatusView.render("show.json", activity: activity, for: user)
assert status[:pleroma][:emoji_reactions] == [ assert status[:pleroma][:emoji_reactions] == [
%{emoji: "β˜•", count: 2, reacted: true}, %{name: "β˜•", count: 2, me: true},
%{emoji: "🍡", count: 1, reacted: false} %{name: "🍡", count: 1, me: false}
] ]
end end

View File

@ -31,7 +31,29 @@ test "POST /api/v1/pleroma/statuses/:id/react_with_emoji", %{conn: conn} do
assert to_string(activity.id) == id assert to_string(activity.id) == id
assert result["pleroma"]["emoji_reactions"] == [ assert result["pleroma"]["emoji_reactions"] == [
%{"emoji" => "β˜•", "count" => 1, "reacted" => true} %{"name" => "β˜•", "count" => 1, "me" => true}
]
end
test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
result =
conn
|> assign(:user, other_user)
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
|> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/β˜•")
|> json_response(200)
# We return the status, but this our implementation detail.
assert %{"id" => id} = result
assert to_string(activity.id) == id
assert result["pleroma"]["emoji_reactions"] == [
%{"name" => "β˜•", "count" => 1, "me" => true}
] ]
end end
@ -56,6 +78,27 @@ test "POST /api/v1/pleroma/statuses/:id/unreact_with_emoji", %{conn: conn} do
assert object.data["reaction_count"] == 0 assert object.data["reaction_count"] == 0
end end
test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
{:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "β˜•")
result =
conn
|> assign(:user, other_user)
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
|> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/β˜•")
assert %{"id" => id} = json_response(result, 200)
assert to_string(activity.id) == id
object = Object.normalize(activity)
assert object.data["reaction_count"] == 0
end
test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
user = insert(:user) user = insert(:user)
other_user = insert(:user) other_user = insert(:user)
@ -80,8 +123,7 @@ test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
|> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by") |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
|> json_response(200) |> json_response(200)
[%{"emoji" => "πŸŽ…", "count" => 1, "accounts" => [represented_user], "reacted" => false}] = [%{"name" => "πŸŽ…", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
result
assert represented_user["id"] == other_user.id assert represented_user["id"] == other_user.id
@ -92,7 +134,46 @@ test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
|> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by") |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
|> json_response(200) |> json_response(200)
assert [%{"emoji" => "πŸŽ…", "count" => 1, "accounts" => [_represented_user], "reacted" => true}] = assert [%{"name" => "πŸŽ…", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
result
end
test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)
doomed_user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
result =
conn
|> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
|> json_response(200)
assert result == []
{:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "πŸŽ…")
{:ok, _, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "πŸŽ…")
User.perform(:delete, doomed_user)
result =
conn
|> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
|> json_response(200)
[%{"name" => "πŸŽ…", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
assert represented_user["id"] == other_user.id
result =
conn
|> assign(:user, other_user)
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
|> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
|> json_response(200)
assert [%{"name" => "πŸŽ…", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
result result
end end