don't check local objects

This commit is contained in:
Moon Man 2020-03-22 21:41:17 +00:00
parent 0996474508
commit 596bf5882d
1 changed files with 45 additions and 37 deletions

View File

@ -10,56 +10,64 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
length(result) == 1 length(result) == 1
end end
defp local_actor(actor) do
host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
prefix = "https://" <> host <> "/"
String.starts_with?(actor, prefix)
end
@impl true @impl true
def filter(%{"object" => %{"emoji" => foreign_emoji}} = object) do def filter(%{"object" => %{"emoji" => foreign_emoji, "actor" => actor}} = object) do
num_foreign_emoji = Kernel.map_size(foreign_emoji) unless local_actor(actor) do
num_foreign_emoji = Kernel.map_size(foreign_emoji)
if num_foreign_emoji > 0 do if num_foreign_emoji > 0 do
Logger.debug("#{num_foreign_emoji} EMOJI TO PROCESS") Logger.debug("#{num_foreign_emoji} EMOJI TO PROCESS")
installed_emoji = Pleroma.Emoji.get_all() installed_emoji = Pleroma.Emoji.get_all()
new_files = new_files =
for {emojo_shortcode, emojo_url} <- foreign_emoji, for {emojo_shortcode, emojo_url} <- foreign_emoji,
!has_emojo(installed_emoji, emojo_shortcode) do !has_emojo(installed_emoji, emojo_shortcode) do
Logger.debug("HANDLING EMOJO") Logger.debug("HANDLING EMOJO")
cond do cond do
String.contains?(emojo_shortcode, "/") -> String.contains?(emojo_shortcode, "/") ->
Logger.error("BAD EMOJO SHORTCODE: #{emojo_shortcode}") Logger.error("BAD EMOJO SHORTCODE: #{emojo_shortcode}")
String.starts_with?(emojo_url, "https://") -> String.starts_with?(emojo_url, "https://") ->
try do try do
remote_emojo = HTTPoison.get!(emojo_url) remote_emojo = HTTPoison.get!(emojo_url)
if remote_emojo.status_code == 200 do if remote_emojo.status_code == 200 do
with {:ok, current_dir} <- File.cwd() do with {:ok, current_dir} <- File.cwd() do
new_file = current_dir <> "/priv/static/emoji/stolen/" <> emojo_shortcode <> Path.extname(emojo_url) new_file = current_dir <> "/priv/static/emoji/stolen/" <> emojo_shortcode <> Path.extname(emojo_url)
{:ok, file} = File.open(new_file, [:write]) {:ok, file} = File.open(new_file, [:write])
IO.binwrite(file, remote_emojo.body) IO.binwrite(file, remote_emojo.body)
File.close(file) File.close(file)
Logger.debug("SAVED EMOJO") Logger.debug("SAVED EMOJO")
new_file new_file
end
end end
rescue
e in RuntimeError -> Logger.error("FAILED TO QUERY REMOTE EMOJO #{emojo_url} #{e}")
end end
rescue
e in RuntimeError -> Logger.error("FAILED TO QUERY REMOTE EMOJO #{emojo_url} #{e}")
end
true -> true ->
Logger.error("EMOJO COND FAIL #{emojo_shortcode} #{emojo_url}") Logger.error("EMOJO COND FAIL #{emojo_shortcode} #{emojo_url}")
end
end end
Logger.debug("EMOJI FILES: #{inspect(new_files)}")
if length(new_files) > 0 do
Logger.debug("RELOADING EMOJI")
Pleroma.Emoji.reload()
else
Logger.debug("NO EMOJI TO LOAD")
end end
Logger.debug("EMOJI FILES: #{inspect(new_files)}")
if length(new_files) > 0 do
Logger.debug("RELOADING EMOJI")
Pleroma.Emoji.reload()
else
Logger.debug("NO EMOJI TO LOAD")
end end
end end