ffe7336175523fef992ce4f9021700998ad835bf
[akkoma] / lib / mix / tasks / pleroma / emoji.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Emoji do
6 use Mix.Task
7
8 @shortdoc "Manages Pleroma instance"
9 @moduledoc """
10 """
11
12 defp fetch_manifest do
13 Tesla.get!("https://git.pleroma.social/vaartis/emoji-index/raw/master/index.json").body
14 |> Poison.decode!()
15 end
16
17 def run(["ls-packs"]) do
18 Application.ensure_all_started(:hackney)
19
20 manifest = fetch_manifest()
21
22 Enum.each(manifest, fn {name, info} ->
23 to_print = [
24 {"Name", name},
25 {"Homepage", info["homepage"]},
26 {"Description", info["description"]},
27 {"License", info["license"]},
28 {"Source", info["src"]}
29 ]
30
31 for {param, value} <- to_print do
32 IO.puts(IO.ANSI.format([:bright, param, :normal, ": ", value]))
33 end
34 end)
35 end
36
37 def run(["get-pack", pack_name]) do
38 Application.ensure_all_started(:hackney)
39
40 manifest = fetch_manifest()
41
42 if Map.has_key?(manifest, pack_name) do
43 pack = manifest[pack_name]
44 src_url = pack["src"]
45
46 IO.puts(
47 IO.ANSI.format([
48 "Downloading pack ",
49 :bright,
50 pack_name,
51 :normal,
52 " from ",
53 :underline,
54 src_url
55 ])
56 )
57
58 binary_archive = Tesla.get!(src_url).body
59
60 IO.puts("Unpacking #{pack_name} pack")
61
62 static_path = Path.join(:code.priv_dir(:pleroma), "static")
63
64 pack_path =
65 Path.join([
66 static_path,
67 Pleroma.Config.get!([:instance, :static_dir]),
68 "emoji",
69 pack_name
70 ])
71
72 files_to_unzip =
73 Enum.map(
74 pack["files"],
75 fn {_, f} -> to_charlist(f) end
76 )
77
78 {:ok, _} =
79 :zip.unzip(binary_archive,
80 cwd: pack_path,
81 file_list: files_to_unzip
82 )
83
84 IO.puts("Wriring emoji.txt for the #{pack_name} pack")
85
86 emoji_txt_str =
87 Enum.map(
88 pack["files"],
89 fn {shortcode, path} -> "#{shortcode}, /instance/static/emoji/#{pack_name}/#{path}" end
90 )
91 |> Enum.join("\n")
92
93 File.write!(Path.join(pack_path, "emoji.txt"), emoji_txt_str)
94 else
95 IO.puts(IO.ANSI.format([:bright, :red, "No pack named \"#{pack_name}\" found"]))
96 end
97 end
98 end