Merge pull request 'emoji reaction standardisation' (#14) from emoji-reaction-standardisation into develop

Reviewed-on: AkkomaGang/akkoma#14
This commit is contained in:
floatingghost 2022-06-25 14:46:32 +00:00
commit c977a27043
3 changed files with 76 additions and 12 deletions

View file

@ -158,10 +158,14 @@ def stripped_name(name), do: name
def maybe_quote(name) when is_binary(name) do def maybe_quote(name) when is_binary(name) do
if is_unicode_emoji?(name) do if is_unicode_emoji?(name) do
name name
else
if String.starts_with?(name, ":") do
name
else else
":#{name}:" ":#{name}:"
end end
end end
end
def maybe_quote(name), do: name def maybe_quote(name), do: name

View file

@ -43,10 +43,14 @@ def index_operation do
def create_operation do def create_operation do
%Operation{ %Operation{
tags: ["Emoji reactions"], tags: ["Emoji reactions"],
summary: "React to a post with a unicode emoji", summary: "React to a post with either a unicode or custom emoji",
parameters: [ parameters: [
Operation.parameter(:id, :path, FlakeID, "Status ID", required: true), Operation.parameter(:id, :path, FlakeID, "Status ID", required: true),
Operation.parameter(:emoji, :path, :string, "A single character unicode emoji", Operation.parameter(
:emoji,
:path,
:string,
"A single character unicode emoji, or a \:shortcode\: format emoji name",
required: true required: true
) )
], ],
@ -62,10 +66,14 @@ def create_operation do
def delete_operation do def delete_operation do
%Operation{ %Operation{
tags: ["Emoji reactions"], tags: ["Emoji reactions"],
summary: "Remove a reaction to a post with a unicode emoji", summary: "Remove a reaction to a post with either a unicode or custom emoji",
parameters: [ parameters: [
Operation.parameter(:id, :path, FlakeID, "Status ID", required: true), Operation.parameter(:id, :path, FlakeID, "Status ID", required: true),
Operation.parameter(:emoji, :path, :string, "A single character unicode emoji", Operation.parameter(
:emoji,
:path,
:string,
"A single character unicode emoji, or a \:shortcode\: format emoji name",
required: true required: true
) )
], ],
@ -81,7 +89,7 @@ defp array_of_reactions_response do
Operation.response("Array of Emoji reactions", "application/json", %Schema{ Operation.response("Array of Emoji reactions", "application/json", %Schema{
type: :array, type: :array,
items: emoji_reaction(), items: emoji_reaction(),
example: [emoji_reaction().example] example: emoji_reaction().example
}) })
end end
@ -93,18 +101,34 @@ defp emoji_reaction do
name: %Schema{type: :string, description: "Emoji"}, name: %Schema{type: :string, description: "Emoji"},
count: %Schema{type: :integer, description: "Count of reactions with this emoji"}, count: %Schema{type: :integer, description: "Count of reactions with this emoji"},
me: %Schema{type: :boolean, description: "Did I react with this emoji?"}, me: %Schema{type: :boolean, description: "Did I react with this emoji?"},
url: %Schema{
type: :string,
description: "URL of the emoji if it's custom - otherwise null",
nullable: true,
format: "url"
},
accounts: %Schema{ accounts: %Schema{
type: :array, type: :array,
items: Account, items: Account,
description: "Array of accounts reacted with this emoji" description: "Array of accounts reacted with this emoji"
} }
}, },
example: %{ example: [
%{
"name" => "😱", "name" => "😱",
"count" => 1, "count" => 1,
"me" => false, "me" => false,
"url" => nil,
"accounts" => [Account.schema().example]
},
%{
"name" => "dinosaur",
"count" => 1,
"me" => false,
"url" => "https://akkoma.dev/emoji/dinosaur.png",
"accounts" => [Account.schema().example] "accounts" => [Account.schema().example]
} }
]
} }
end end
end end

View file

@ -34,6 +34,30 @@ test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
%{"name" => "", "count" => 1, "me" => true, "url" => nil} %{"name" => "", "count" => 1, "me" => true, "url" => nil}
] ]
{:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
ObanHelpers.perform_all()
# Reacting with a custom emoji
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/:dinosaur:")
|> json_response_and_validate_schema(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" => "dinosaur",
"count" => 1,
"me" => true,
"url" => "http://localhost:4001/emoji/dino walking.gif"
}
]
# Reacting with a non-emoji # Reacting with a non-emoji
assert conn assert conn
|> assign(:user, other_user) |> assign(:user, other_user)
@ -48,6 +72,7 @@ test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
{:ok, activity} = CommonAPI.post(user, %{status: "#cofe"}) {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
{:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "") {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "")
{:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
ObanHelpers.perform_all() ObanHelpers.perform_all()
@ -60,6 +85,17 @@ test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
assert %{"id" => id} = json_response_and_validate_schema(result, 200) assert %{"id" => id} = json_response_and_validate_schema(result, 200)
assert to_string(activity.id) == id assert to_string(activity.id) == id
# Remove custom emoji
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/:dinosaur:")
assert %{"id" => id} = json_response_and_validate_schema(result, 200)
assert to_string(activity.id) == id
ObanHelpers.perform_all() ObanHelpers.perform_all()
object = Object.get_by_ap_id(activity.data["object"]) object = Object.get_by_ap_id(activity.data["object"])