Separate emoji pack file lists in a different file
[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 @default_manifest "https://git.pleroma.social/vaartis/emoji-index/raw/master/index.json"
13
14 defp fetch_manifest(from) do
15 Tesla.get!(from).body |> Poison.decode!()
16 end
17
18 defp parse_global_opts(args) do
19 OptionParser.parse(
20 args,
21 strict: [
22 manifest: :string
23 ],
24 aliases: [
25 m: :manifest
26 ]
27 )
28 end
29
30 def run(["ls-packs" | args]) do
31 Application.ensure_all_started(:hackney)
32
33 {options, [], []} = parse_global_opts(args)
34
35 manifest =
36 fetch_manifest(if options[:manifest], do: options[:manifest], else: @default_manifest)
37
38 Enum.each(manifest, fn {name, info} ->
39 to_print = [
40 {"Name", name},
41 {"Homepage", info["homepage"]},
42 {"Description", info["description"]},
43 {"License", info["license"]},
44 {"Source", info["src"]}
45 ]
46
47 for {param, value} <- to_print do
48 IO.puts(IO.ANSI.format([:bright, param, :normal, ": ", value]))
49 end
50 end)
51 end
52
53 def run(["get-packs" | args]) do
54 Application.ensure_all_started(:hackney)
55
56 {options, pack_names, []} = parse_global_opts(args)
57
58 manifest_url = if options[:manifest], do: options[:manifest], else: @default_manifest
59
60 manifest = fetch_manifest(manifest_url)
61
62 for pack_name <- pack_names do
63 if Map.has_key?(manifest, pack_name) do
64 pack = manifest[pack_name]
65 src_url = pack["src"]
66
67 IO.puts(
68 IO.ANSI.format([
69 "Downloading ",
70 :bright,
71 pack_name,
72 :normal,
73 " from ",
74 :underline,
75 src_url
76 ])
77 )
78
79 binary_archive = Tesla.get!(src_url).body
80
81 # The url specified in files should be in the same directory
82 files_url = Path.join(Path.dirname(manifest_url), pack["files"])
83
84 IO.puts(
85 IO.ANSI.format([
86 "Fetching the file list for ",
87 :bright,
88 pack_name,
89 :normal,
90 " from ",
91 :underline,
92 files_url
93 ])
94 )
95
96 files = Tesla.get!(files_url).body |> Poison.decode!()
97
98 IO.puts(IO.ANSI.format(["Unpacking ", :bright, pack_name]))
99
100 static_path = Path.join(:code.priv_dir(:pleroma), "static")
101
102 pack_path =
103 Path.join([
104 static_path,
105 Pleroma.Config.get!([:instance, :static_dir]),
106 "emoji",
107 pack_name
108 ])
109
110 files_to_unzip =
111 Enum.map(
112 files,
113 fn {_, f} -> to_charlist(f) end
114 )
115
116 {:ok, _} =
117 :zip.unzip(binary_archive,
118 cwd: pack_path,
119 file_list: files_to_unzip
120 )
121
122 IO.puts(IO.ANSI.format(["Writing emoji.txt for ", :bright, pack_name]))
123
124 emoji_txt_str =
125 Enum.map(
126 files,
127 fn {shortcode, path} ->
128 "#{shortcode}, /instance/static/emoji/#{pack_name}/#{path}"
129 end
130 )
131 |> Enum.join("\n")
132
133 File.write!(Path.join(pack_path, "emoji.txt"), emoji_txt_str)
134 else
135 IO.puts(IO.ANSI.format([:bright, :red, "No pack named \"#{pack_name}\" found"]))
136 end
137 end
138 end
139 end