Add actual policy file

This commit is contained in:
Moon Man 2020-03-22 16:41:29 +00:00
parent 6506b03779
commit 8d5ce6a3df
1 changed files with 79 additions and 0 deletions

79
steal_emoji_policy.ex Normal file
View File

@ -0,0 +1,79 @@
defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
require Logger
@moduledoc "Detect new emoji and steal them"
@behaviour Pleroma.Web.ActivityPub.MRF
defp has_emojo(installed_emoji, str) do
result = for emojo <- installed_emoji, elem(emojo, 0) == str, do: true
length(result) == 1
end
@impl true
def filter(%{"object" => %{"emoji" => foreign_emoji}} = object) do
num_foreign_emoji = Kernel.map_size(foreign_emoji)
if num_foreign_emoji > 0 do
Logger.debug("#{num_foreign_emoji} EMOJI TO PROCESS")
installed_emoji = Pleroma.Emoji.get_all()
new_files =
for {emojo_shortcode, emojo_url} <- foreign_emoji,
!has_emojo(installed_emoji, emojo_shortcode) do
Logger.debug("HANDLING EMOJO")
cond do
String.contains?(emojo_shortcode, "/") ->
Logger.error("BAD EMOJO SHORTCODE: #{emojo_shortcode}")
!String.starts_with(emojo_url, "https://") ->
Logger.error("BAD EMOJO URL: #{emojo_url}")
true ->
Logger.debug("GOOD EMOJO")
remote_emojo = HTTPoison.get!(emojo_url)
if remote_emojo.status_code == 200 do
with {:ok, current_dir} <- File.cwd() do
new_file =
current_dir <>
"/priv/static/emoji/stolen/" <> emojo_shortcode <> Path.extname(emojo_url)
try do
{:ok, file} = File.open(new_file, [:write])
IO.binwrite(file, remote_emojo.body)
File.close(file)
Logger.debug("SAVED EMOJO")
rescue
RuntimeError ->
_ = "Error"
Logger.error("FAILED TO SAVE EMOJO: #{new_file}")
end
new_file
end
else
Logger.error("FAILED TO DOWNLOAD EMOJO: #{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
{:ok, object}
end
@impl true
def filter(object) do
{:ok, object}
end
@impl true
def describe, do: {:ok, %{}}
end