1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
10 @moduledoc "Detect new emojis by their shortcode and steals them"
11 @behaviour Pleroma.Web.ActivityPub.MRF
13 defp accept_host?(host), do: host in Config.get([:mrf_steal_emoji, :hosts], [])
15 defp steal_emoji({shortcode, url}, emoji_dir_path) do
16 url = Pleroma.Web.MediaProxy.url(url)
18 with {:ok, %{status: status} = response} when status in 200..299 <- Pleroma.HTTP.get(url) do
19 size_limit = Config.get([:mrf_steal_emoji, :size_limit], 50_000)
21 if byte_size(response.body) <= size_limit do
29 file_path = Path.join(emoji_dir_path, shortcode <> (extension || ".png"))
31 case File.write(file_path, response.body) do
36 Logger.warn("MRF.StealEmojiPolicy: Failed to write to #{file_path}: #{inspect(e)}")
41 "MRF.StealEmojiPolicy: :#{shortcode}: at #{url} (#{byte_size(response.body)} B) over size limit (#{
50 Logger.warn("MRF.StealEmojiPolicy: Failed to fetch #{url}: #{inspect(e)}")
56 def filter(%{"object" => %{"emoji" => foreign_emojis, "actor" => actor}} = message) do
57 host = URI.parse(actor).host
59 if host != Pleroma.Web.Endpoint.host() and accept_host?(host) do
60 installed_emoji = Pleroma.Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
64 [:mrf_steal_emoji, :path],
65 Path.join(Config.get([:instance, :static_dir]), "emoji/stolen")
68 File.mkdir_p(emoji_dir_path)
72 |> Enum.reject(fn {shortcode, _url} -> shortcode in installed_emoji end)
73 |> Enum.filter(fn {shortcode, _url} ->
75 [:mrf_steal_emoji, :rejected_shortcodes]
77 |> Enum.find(false, fn regex -> String.match?(shortcode, regex) end)
81 |> Enum.map(&steal_emoji(&1, emoji_dir_path))
84 if !Enum.empty?(new_emojis) do
85 Logger.info("Stole new emojis: #{inspect(new_emojis)}")
86 Pleroma.Emoji.reload()
93 def filter(message), do: {:ok, message}