aboutsummaryrefslogtreecommitdiff
path: root/lib/mix/tasks/pleroma/frontend.ex
blob: 8130a71ea61af8dc3551b0d1fd39929a0db7d71e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Mix.Tasks.Pleroma.Frontend do
  use Mix.Task

  import Mix.Pleroma

  # alias Pleroma.Config

  @shortdoc "Manages bundled Pleroma frontends"
  @moduledoc File.read!("docs/administration/CLI_tasks/frontend.md")

  @pleroma_gitlab_host "git.pleroma.social"
  @frontends %{
    # TODO stable
    "admin" => %{"project" => "pleroma/admin-fe"},
    # TODO
    "kenoma" => %{"project" => "lambadalambda/kenoma"},
    # TODO
    "mastodon" => %{"project" => "pleroma/mastofe"},
    # OK
    "pleroma" => %{"project" => "pleroma/pleroma-fe"}
  }
  @known_frontends Map.keys(@frontends)

  def run(["install", "none" | _args]) do
    shell_info("Skipping frontend installation because none was requested")
  end

  def run(["install", unknown_fe | _args]) when unknown_fe not in @known_frontends do
    shell_error(
      "Frontend \"#{unknown_fe}\" is not known. Known frontends are: #{
        Enum.join(@known_frontends, ", ")
      }"
    )
  end

  def run(["install", frontend | args]) do
    log_level = Logger.level()
    Logger.configure(level: :warn)
    {:ok, _} = Application.ensure_all_started(:pleroma)

    {options, [], []} =
      OptionParser.parse(
        args,
        strict: [
          ref: :string
        ]
      )

    ref = suggest_ref(options, frontend)

    %{"name" => bundle_name, "url" => bundle_url} =
      get_bundle_meta(ref, @pleroma_gitlab_host, @frontends[frontend]["project"])

    shell_info("Installing frontend #{frontend}, version: #{bundle_name}")

    dest = Path.join([Pleroma.Config.get!([:instance, :static_dir]), "frontends", frontend, ref])

    with :ok <- install_bundle(bundle_url, dest),
         :ok <- post_install_bundle(frontend, dest) do
      shell_info("Installed!")
    else
      {:error, error} ->
        shell_error("Error: #{inspect(error)}")
    end

    Logger.configure(level: log_level)
  end

  defp post_install_bundle("mastodon", path) do
    with :ok <- File.rename("#{path}/public/assets/sw.js", "#{path}/sw.js"),
         :ok <- File.rename("#{path}/public/packs", "#{path}/packs"),
         {:ok, _deleted_files} <- File.rm_rf("#{path}/public") do
      :ok
    else
      error ->
        {:error, error}
    end
  end

  defp post_install_bundle(_fe_name, _path), do: :ok

  defp suggest_ref(options, frontend) do
    case Pleroma.Config.get([:frontends, String.to_atom(frontend)]) do
      nil ->
        primary_fe_config = Pleroma.Config.get([:frontends, :primary])

        if primary_fe_config["name"] == frontend do
          primary_fe_config["ref"]
        else
          nil
        end

      val ->
        val
    end
    |> case do
      nil ->
        stable_pleroma? = Pleroma.Application.stable?()

        current_stable_out =
          if stable_pleroma? do
            "stable"
          else
            "develop"
          end

        get_option(
          options,
          :ref,
          "You are currently running #{current_stable_out} version of Pleroma backend. What version of \"#{
            frontend
          }\" frontend you want to install? (\"stable\", \"develop\" or specific ref)",
          current_stable_out
        )

      config_value ->
        current_ref =
          case config_value do
            %{"ref" => ref} -> ref
            ref -> ref
          end

        get_option(
          options,
          :ref,
          "You are currently running \"#{current_ref}\" version of \"#{frontend}\" frontend. What version do you want to install? (\"stable\", \"develop\" or specific ref)",
          current_ref
        )
    end
  end

  defp get_bundle_meta("develop", gitlab_base_url, project) do
    url = "#{gitlab_api_url(gitlab_base_url, project)}/repository/branches"

    %{status: 200, body: json} = Tesla.get!(http_client(), url)

    %{"name" => name, "commit" => %{"short_id" => last_commit_ref}} =
      Enum.find(json, & &1["default"])

    %{
      "name" => name,
      "url" => build_url(gitlab_base_url, project, last_commit_ref)
    }
  end

  defp get_bundle_meta("stable", gitlab_base_url, project) do
    url = "#{gitlab_api_url(gitlab_base_url, project)}/releases"
    %{status: 200, body: json} = Tesla.get!(http_client(), url)

    [%{"commit" => %{"short_id" => commit_id}, "name" => name} = data | _] =
      Enum.sort(json, fn r1, r2 ->
        {:ok, date1, _offset} = DateTime.from_iso8601(r1["created_at"])
        {:ok, date2, _offset} = DateTime.from_iso8601(r2["created_at"])
        DateTime.compare(date1, date2) != :lt
      end)

    IO.inspect(data)

    %{
      "name" => name,
      "url" => build_url(gitlab_base_url, project, commit_id)
    }
  end

  defp get_bundle_meta(ref, gitlab_base_url, project) do
    %{
      "name" => ref,
      "url" => build_url(gitlab_base_url, project, ref)
    }
  end

  defp install_bundle(bundle_url, dir) do
    http_client = http_client()

    with {:ok, %{status: 200, body: zip_body}} <- Tesla.get(http_client, bundle_url),
         {:ok, unzipped} <- :zip.unzip(zip_body, [:memory]) do
      File.rm_rf!(dir)

      Enum.each(unzipped, fn {path, data} ->
        path =
          path
          |> to_string()
          |> String.replace(~r/^dist\//, "")

        file_path = Path.join(dir, path)

        file_path
        |> Path.dirname()
        |> File.mkdir_p!()

        File.write!(file_path, data)
      end)
    else
      {:ok, %{status: 404}} ->
        {:error, "Bundle not found"}

      error ->
        {:error, error}
    end
  end

  defp gitlab_api_url(gitlab_base_url, project),
    do: "https://#{gitlab_base_url}/api/v4/projects/#{URI.encode_www_form(project)}"

  defp build_url(gitlab_base_url, project, ref),
    do:
      "https://#{gitlab_base_url}/#{project}/-/jobs/artifacts/#{ref}/download?job=build"
      |> IO.inspect()

  defp http_client do
    middleware = [
      Tesla.Middleware.FollowRedirects,
      Tesla.Middleware.JSON
    ]

    Tesla.client(middleware)
  end
end