From ed58a6b0701475740690b5ec39c16cd2a907feba Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Fri, 10 Jun 2022 13:41:08 +0100 Subject: [PATCH] run mix format --- lib/pleroma/emoji.ex | 15 ++--- lib/pleroma/web/activity_pub/builder.ex | 39 +++++++------ .../emoji_react_validator.ex | 8 ++- .../web/activity_pub/transmogrifier.ex | 2 +- lib/pleroma/web/activity_pub/utils.ex | 56 +++++++++++-------- .../web/mastodon_api/views/status_view.ex | 1 + .../pleroma_api/views/emoji_reaction_view.ex | 1 + mix.exs | 4 +- 8 files changed, 73 insertions(+), 53 deletions(-) diff --git a/lib/pleroma/emoji.ex b/lib/pleroma/emoji.ex index f484f96a0..a52df0de3 100644 --- a/lib/pleroma/emoji.ex +++ b/lib/pleroma/emoji.ex @@ -50,13 +50,14 @@ defmodule Pleroma.Emoji do @doc "Returns the path of the emoji `name`." @spec get(String.t()) :: String.t() | nil def get(name) do - name = if String.starts_with?(name, ":") do - name - |> String.replace_leading(":", "") - |> String.replace_trailing(":", "") - else - name - end + name = + if String.starts_with?(name, ":") do + name + |> String.replace_leading(":", "") + |> String.replace_trailing(":", "") + else + name + end case :ets.lookup(@ets, name) do [{_, path}] -> path diff --git a/lib/pleroma/web/activity_pub/builder.ex b/lib/pleroma/web/activity_pub/builder.ex index 0ba17a4d5..652c25721 100644 --- a/lib/pleroma/web/activity_pub/builder.ex +++ b/lib/pleroma/web/activity_pub/builder.ex @@ -58,29 +58,32 @@ defmodule Pleroma.Web.ActivityPub.Builder do @spec emoji_react(User.t(), Object.t(), String.t()) :: {:ok, map(), keyword()} def emoji_react(actor, object, emoji) do with {:ok, data, meta} <- object_action(actor, object) do - data = if Emoji.is_unicode_emoji?(emoji) do - data - |> Map.put("content", emoji) - |> Map.put("type", "EmojiReact") - else - emojo = Emoji.get(emoji) - path = emojo |> Map.get(:file) - url = "#{Endpoint.url()}#{path}" - data - |> Map.put("content", emoji) - |> Map.put("type", "EmojiReact") - |> Map.put("tag", [ + data = + if Emoji.is_unicode_emoji?(emoji) do + data + |> Map.put("content", emoji) + |> Map.put("type", "EmojiReact") + else + emojo = Emoji.get(emoji) + path = emojo |> Map.get(:file) + url = "#{Endpoint.url()}#{path}" + + data + |> Map.put("content", emoji) + |> Map.put("type", "EmojiReact") + |> Map.put("tag", [ %{} |> Map.put("id", url) |> Map.put("type", "Emoji") |> Map.put("name", emojo.code) - |> Map.put("icon", - %{} - |> Map.put("type", "Image") - |> Map.put("url", url) + |> Map.put( + "icon", + %{} + |> Map.put("type", "Image") + |> Map.put("url", url) ) - ]) - end + ]) + end {:ok, data, meta} end diff --git a/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex b/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex index f0eb5417c..422ab594a 100644 --- a/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex @@ -55,11 +55,12 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do |> CommonFixes.fix_actor() |> CommonFixes.fix_activity_addressing() - data = if Map.has_key?(data, "tag") do + data = + if Map.has_key?(data, "tag") do data - else + else Map.put(data, "tag", []) - end + end with %Object{} = object <- Object.normalize(data["object"]) do data @@ -72,6 +73,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do defp validate_emoji(cng) do content = get_field(cng, :content) + if Pleroma.Emoji.is_unicode_emoji?(content) || Regex.match?(@emoji_regex, content) do cng else diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 25aaba1a6..cf0c9a9e2 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -433,7 +433,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do def handle_incoming( %{ "type" => "Like", - "_misskey_reaction" => reaction, + "_misskey_reaction" => reaction } = data, options ) do diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index cca58e0da..37f7a9173 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -350,14 +350,15 @@ defmodule Pleroma.Web.ActivityPub.Utils do reactions = get_cached_emoji_reactions(object) emoji = stripped_emoji_name(emoji) url = emoji_url(emoji, activity) + new_reactions = case Enum.find_index(reactions, fn [candidate, _, candidate_url] -> - if is_nil(candidate_url) do - emoji == candidate - else - url == candidate_url - end - end) do + if is_nil(candidate_url) do + emoji == candidate + else + url == candidate_url + end + end) do nil -> reactions ++ [[emoji, [actor], url]] @@ -380,12 +381,18 @@ defmodule Pleroma.Web.ActivityPub.Utils do |> String.replace_trailing(":", "") end - defp emoji_url(name, - %Activity{ - data: %{"tag" => [ - %{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}} - ]} - }), do: url + defp emoji_url( + name, + %Activity{ + data: %{ + "tag" => [ + %{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}} + ] + } + } + ), + do: url + defp emoji_url(_, _), do: nil def emoji_count(reactions_list) do @@ -399,14 +406,15 @@ defmodule Pleroma.Web.ActivityPub.Utils do emoji = stripped_emoji_name(emoji) reactions = get_cached_emoji_reactions(object) url = emoji_url(emoji, activity) + new_reactions = case Enum.find_index(reactions, fn [candidate, _, candidate_url] -> - if is_nil(candidate_url) do - emoji == candidate - else - url == candidate_url - end - end) do + if is_nil(candidate_url) do + emoji == candidate + else + url == candidate_url + end + end) do nil -> reactions @@ -536,11 +544,13 @@ defmodule Pleroma.Web.ActivityPub.Utils do def get_latest_reaction(internal_activity_id, %{ap_id: ap_id}, emoji) do %{data: %{"object" => object_ap_id}} = Activity.get_by_id(internal_activity_id) - emoji = if String.starts_with?(emoji, ":") do - emoji - else - ":#{emoji}:" - end + + emoji = + if String.starts_with?(emoji, ":") do + emoji + else + ":#{emoji}:" + end "EmojiReact" |> Activity.Queries.by_type() diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 90f6f8469..008040866 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -304,6 +304,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do _e -> nil end + emoji_reactions = object.data |> Map.get("reactions", []) diff --git a/lib/pleroma/web/pleroma_api/views/emoji_reaction_view.ex b/lib/pleroma/web/pleroma_api/views/emoji_reaction_view.ex index 85eb09d25..9993480db 100644 --- a/lib/pleroma/web/pleroma_api/views/emoji_reaction_view.ex +++ b/lib/pleroma/web/pleroma_api/views/emoji_reaction_view.ex @@ -14,6 +14,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionView do def render("show.json", %{emoji_reaction: {emoji, user_ap_ids, url}, user: user}) do users = fetch_users(user_ap_ids) + %{ name: emoji, count: length(users), diff --git a/mix.exs b/mix.exs index 848c51c66..95c9f7f61 100644 --- a/mix.exs +++ b/mix.exs @@ -157,7 +157,9 @@ defmodule Pleroma.Mixfile do {:floki, "~> 0.27"}, {:timex, "~> 3.6"}, {:ueberauth, "~> 0.4"}, - {:linkify, git: "https://git.ihatebeinga.live/floatingghost/linkify.git", branch: "bugfix/line-ending-buffer"}, + {:linkify, + git: "https://git.ihatebeinga.live/floatingghost/linkify.git", + branch: "bugfix/line-ending-buffer"}, {:http_signatures, "~> 0.1.1"}, {:telemetry, "~> 0.3"}, {:poolboy, "~> 1.5"},