Split steal_emoji function for better readability

This commit is contained in:
Oneric 2024-03-07 13:07:02 +01:00
parent af041db6dc
commit 111cdb0d86

View file

@ -33,13 +33,7 @@ defp reject_emoji?({shortcode, _url}, installed_emoji) do
!valid_shortcode? or rejected_shortcode? or emoji_installed? !valid_shortcode? or rejected_shortcode? or emoji_installed?
end end
defp steal_emoji({shortcode, url}, emoji_dir_path) do defp steal_emoji(%{} = response, {shortcode, url}, emoji_dir_path) do
url = Pleroma.Web.MediaProxy.url(url)
with {:ok, %{status: status} = response} when status in 200..299 <- Pleroma.HTTP.get(url) do
size_limit = Config.get([:mrf_steal_emoji, :size_limit], 50_000)
if byte_size(response.body) <= size_limit do
extension = extension =
url url
|> URI.parse() |> URI.parse()
@ -58,6 +52,16 @@ defp steal_emoji({shortcode, url}, emoji_dir_path) do
Logger.warning("MRF.StealEmojiPolicy: Failed to write to #{file_path}: #{inspect(e)}") Logger.warning("MRF.StealEmojiPolicy: Failed to write to #{file_path}: #{inspect(e)}")
nil nil
end end
end
defp maybe_steal_emoji({shortcode, url}, emoji_dir_path) do
url = Pleroma.Web.MediaProxy.url(url)
with {:ok, %{status: status} = response} when status in 200..299 <- Pleroma.HTTP.get(url) do
size_limit = Config.get([:mrf_steal_emoji, :size_limit], 50_000)
if byte_size(response.body) <= size_limit do
steal_emoji(response, {shortcode, url}, emoji_dir_path)
else else
Logger.debug( Logger.debug(
"MRF.StealEmojiPolicy: :#{shortcode}: at #{url} (#{byte_size(response.body)} B) over size limit (#{size_limit} B)" "MRF.StealEmojiPolicy: :#{shortcode}: at #{url} (#{byte_size(response.body)} B) over size limit (#{size_limit} B)"
@ -90,7 +94,7 @@ def filter(%{"object" => %{"emoji" => foreign_emojis, "actor" => actor}} = messa
new_emojis = new_emojis =
foreign_emojis foreign_emojis
|> Enum.reject(&reject_emoji?(&1, installed_emoji)) |> Enum.reject(&reject_emoji?(&1, installed_emoji))
|> Enum.map(&steal_emoji(&1, emoji_dir_path)) |> Enum.map(&maybe_steal_emoji(&1, emoji_dir_path))
|> Enum.filter(& &1) |> Enum.filter(& &1)
if !Enum.empty?(new_emojis) do if !Enum.empty?(new_emojis) do