902bddc655279a630e379eeae3dd5e01a89d33dc
[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 =
59 fetch_manifest(if options[:manifest], do: options[:manifest], else: @default_manifest)
60
61 for pack_name <- pack_names do
62 if Map.has_key?(manifest, pack_name) do
63 pack = manifest[pack_name]
64 src_url = pack["src"]
65
66 IO.puts(
67 IO.ANSI.format([
68 "Downloading ",
69 :bright,
70 pack_name,
71 :normal,
72 " from ",
73 :underline,
74 src_url
75 ])
76 )
77
78 binary_archive = Tesla.get!(src_url).body
79
80 IO.puts(IO.ANSI.format(["Unpacking ", :bright, pack_name]))
81
82 static_path = Path.join(:code.priv_dir(:pleroma), "static")
83
84 pack_path =
85 Path.join([
86 static_path,
87 Pleroma.Config.get!([:instance, :static_dir]),
88 "emoji",
89 pack_name
90 ])
91
92 files_to_unzip =
93 Enum.map(
94 pack["files"],
95 fn {_, f} -> to_charlist(f) end
96 )
97
98 {:ok, _} =
99 :zip.unzip(binary_archive,
100 cwd: pack_path,
101 file_list: files_to_unzip
102 )
103
104 IO.puts(IO.ANSI.format(["Writing emoji.txt for ", :bright, pack_name]))
105
106 emoji_txt_str =
107 Enum.map(
108 pack["files"],
109 fn {shortcode, path} ->
110 "#{shortcode}, /instance/static/emoji/#{pack_name}/#{path}"
111 end
112 )
113 |> Enum.join("\n")
114
115 File.write!(Path.join(pack_path, "emoji.txt"), emoji_txt_str)
116 else
117 IO.puts(IO.ANSI.format([:bright, :red, "No pack named \"#{pack_name}\" found"]))
118 end
119 end
120 end
121 end