CI: Bump lint stage to elixir-1.12
[akkoma] / lib / pleroma / web / activity_pub / mrf / steal_emoji_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
6 require Logger
7
8 alias Pleroma.Config
9
10 @moduledoc "Detect new emojis by their shortcode and steals them"
11 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
12
13 defp accept_host?(host), do: host in Config.get([:mrf_steal_emoji, :hosts], [])
14
15 defp steal_emoji({shortcode, url}, emoji_dir_path) do
16 url = Pleroma.Web.MediaProxy.url(url)
17
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)
20
21 if byte_size(response.body) <= size_limit do
22 extension =
23 url
24 |> URI.parse()
25 |> Map.get(:path)
26 |> Path.basename()
27 |> Path.extname()
28
29 file_path = Path.join(emoji_dir_path, shortcode <> (extension || ".png"))
30
31 case File.write(file_path, response.body) do
32 :ok ->
33 shortcode
34
35 e ->
36 Logger.warn("MRF.StealEmojiPolicy: Failed to write to #{file_path}: #{inspect(e)}")
37 nil
38 end
39 else
40 Logger.debug(
41 "MRF.StealEmojiPolicy: :#{shortcode}: at #{url} (#{byte_size(response.body)} B) over size limit (#{size_limit} B)"
42 )
43
44 nil
45 end
46 else
47 e ->
48 Logger.warn("MRF.StealEmojiPolicy: Failed to fetch #{url}: #{inspect(e)}")
49 nil
50 end
51 end
52
53 @impl true
54 def filter(%{"object" => %{"emoji" => foreign_emojis, "actor" => actor}} = message) do
55 host = URI.parse(actor).host
56
57 if host != Pleroma.Web.Endpoint.host() and accept_host?(host) do
58 installed_emoji = Pleroma.Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
59
60 emoji_dir_path =
61 Config.get(
62 [:mrf_steal_emoji, :path],
63 Path.join(Config.get([:instance, :static_dir]), "emoji/stolen")
64 )
65
66 File.mkdir_p(emoji_dir_path)
67
68 new_emojis =
69 foreign_emojis
70 |> Enum.reject(fn {shortcode, _url} -> shortcode in installed_emoji end)
71 |> Enum.filter(fn {shortcode, _url} ->
72 reject_emoji? =
73 [:mrf_steal_emoji, :rejected_shortcodes]
74 |> Config.get([])
75 |> Enum.find(false, fn regex -> String.match?(shortcode, regex) end)
76
77 !reject_emoji?
78 end)
79 |> Enum.map(&steal_emoji(&1, emoji_dir_path))
80 |> Enum.filter(& &1)
81
82 if !Enum.empty?(new_emojis) do
83 Logger.info("Stole new emojis: #{inspect(new_emojis)}")
84 Pleroma.Emoji.reload()
85 end
86 end
87
88 {:ok, message}
89 end
90
91 def filter(message), do: {:ok, message}
92
93 @impl true
94 @spec config_description :: %{
95 children: [
96 %{
97 description: <<_::272, _::_*256>>,
98 key: :hosts | :rejected_shortcodes | :size_limit,
99 suggestions: [any(), ...],
100 type: {:list, :string} | {:list, :string} | :integer
101 },
102 ...
103 ],
104 description: <<_::448>>,
105 key: :mrf_steal_emoji,
106 label: <<_::80>>,
107 related_policy: <<_::352>>
108 }
109 def config_description do
110 %{
111 key: :mrf_steal_emoji,
112 related_policy: "Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy",
113 label: "MRF Emojis",
114 description: "Steals emojis from selected instances when it sees them.",
115 children: [
116 %{
117 key: :hosts,
118 type: {:list, :string},
119 description: "List of hosts to steal emojis from",
120 suggestions: [""]
121 },
122 %{
123 key: :rejected_shortcodes,
124 type: {:list, :string},
125 description: "Regex-list of shortcodes to reject",
126 suggestions: [""]
127 },
128 %{
129 key: :size_limit,
130 type: :integer,
131 description: "File size limit (in bytes), checked before an emoji is saved to the disk",
132 suggestions: ["100000"]
133 }
134 ]
135 }
136 end
137
138 @impl true
139 def describe do
140 {:ok, %{}}
141 end
142 end