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 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
!valid_shortcode? or rejected_shortcode? or emoji_installed?
end
defp 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
defp steal_emoji(%{} = response, {shortcode, url}, emoji_dir_path) do
extension =
url
|> URI.parse()
@ -58,6 +52,16 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
Logger.warning("MRF.StealEmojiPolicy: Failed to write to #{file_path}: #{inspect(e)}")
nil
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
Logger.debug(
"MRF.StealEmojiPolicy: :#{shortcode}: at #{url} (#{byte_size(response.body)} B) over size limit (#{size_limit} B)"
@ -90,7 +94,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
new_emojis =
foreign_emojis
|> 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)
if !Enum.empty?(new_emojis) do