util_controller.ex 9.42 KB
Newer Older
dtluna's avatar
dtluna committed
1
2
defmodule Pleroma.Web.TwitterAPI.UtilController do
  use Pleroma.Web, :controller
eal's avatar
eal committed
3
  require Logger
dtluna's avatar
dtluna committed
4
  alias Pleroma.Web
5
  alias Pleroma.Web.OStatus
eal's avatar
eal committed
6
  alias Pleroma.Web.WebFinger
7
  alias Pleroma.Web.CommonAPI
8
  alias Comeonin.Pbkdf2
eal's avatar
eal committed
9
  alias Pleroma.Web.ActivityPub.ActivityPub
Maksim's avatar
Maksim committed
10
  alias Pleroma.{Repo, PasswordResetToken, User, Emoji}
Roger Braun's avatar
Roger Braun committed
11
12
13

  def show_password_reset(conn, %{"token" => token}) do
    with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
lain's avatar
lain committed
14
15
         %User{} = user <- Repo.get(User, token.user_id) do
      render(conn, "password_reset.html", %{
Roger Braun's avatar
Roger Braun committed
16
17
        token: token,
        user: user
lain's avatar
lain committed
18
      })
Roger Braun's avatar
Roger Braun committed
19
    else
lain's avatar
lain committed
20
      _e -> render(conn, "invalid_token.html")
Roger Braun's avatar
Roger Braun committed
21
22
23
24
25
    end
  end

  def password_reset(conn, %{"data" => data}) do
    with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
lain's avatar
lain committed
26
      render(conn, "password_reset_success.html")
Roger Braun's avatar
Roger Braun committed
27
    else
lain's avatar
lain committed
28
      _e -> render(conn, "password_reset_failed.html")
Roger Braun's avatar
Roger Braun committed
29
30
31
    end
  end

dtluna's avatar
dtluna committed
32
33
34
35
  def help_test(conn, _params) do
    json(conn, "ok")
  end

eal's avatar
eal committed
36
  def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
lain's avatar
lain committed
37
    with %User{} = user <- User.get_cached_by_nickname(nick), avatar = User.avatar_url(user) do
eal's avatar
eal committed
38
39
40
      conn
      |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
    else
lain's avatar
lain committed
41
42
43
44
45
46
      _e ->
        render(conn, "subscribe.html", %{
          nickname: nick,
          avatar: nil,
          error: "Could not find user"
        })
eal's avatar
eal committed
47
48
    end
  end
lain's avatar
lain committed
49

eal's avatar
eal committed
50
51
52
53
54
55
56
  def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
    with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
         %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
      conn
      |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
    else
      _e ->
lain's avatar
lain committed
57
58
59
60
61
        render(conn, "subscribe.html", %{
          nickname: nick,
          avatar: nil,
          error: "Something went wrong."
        })
eal's avatar
eal committed
62
63
64
    end
  end

65
66
67
68
69
70
71
72
73
74
75
  def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
    {err, followee} = OStatus.find_or_make_user(acct)
    avatar = User.avatar_url(followee)
    name = followee.nickname
    id = followee.id

    if !!user do
      conn
      |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id})
    else
      conn
lain's avatar
lain committed
76
77
78
79
80
81
82
      |> render("follow_login.html", %{
        error: false,
        acct: acct,
        avatar: avatar,
        name: name,
        id: id
      })
83
84
85
    end
  end

lain's avatar
lain committed
86
87
88
  def do_remote_follow(conn, %{
        "authorization" => %{"name" => username, "password" => password, "id" => id}
      }) do
89
90
91
    followee = Repo.get(User, id)
    avatar = User.avatar_url(followee)
    name = followee.nickname
lain's avatar
lain committed
92

93
94
    with %User{} = user <- User.get_cached_by_nickname(username),
         true <- Pbkdf2.checkpw(password, user.password_hash),
95
         %User{} = _followed <- Repo.get(User, id),
96
97
98
99
100
         {:ok, follower} <- User.follow(user, followee),
         {:ok, _activity} <- ActivityPub.follow(follower, followee) do
      conn
      |> render("followed.html", %{error: false})
    else
101
102
103
104
      # Was already following user
      {:error, "Could not follow user:" <> _rest} ->
        render(conn, "followed.html", %{error: false})

105
106
      _e ->
        conn
lain's avatar
lain committed
107
108
109
110
111
112
        |> render("follow_login.html", %{
          error: "Wrong username or password",
          id: id,
          name: name,
          avatar: avatar
        })
113
114
    end
  end
lain's avatar
lain committed
115

116
117
118
119
120
121
122
  def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do
    with %User{} = followee <- Repo.get(User, id),
         {:ok, follower} <- User.follow(user, followee),
         {:ok, _activity} <- ActivityPub.follow(follower, followee) do
      conn
      |> render("followed.html", %{error: false})
    else
123
124
125
126
127
      # Was already following user
      {:error, "Could not follow user:" <> _rest} ->
        conn
        |> render("followed.html", %{error: false})

128
      e ->
lain's avatar
lain committed
129
130
131
132
        Logger.debug("Remote follow failed with error #{inspect(e)}")

        conn
        |> render("followed.html", %{error: inspect(e)})
133
134
135
    end
  end

dtluna's avatar
dtluna committed
136
  def config(conn, _params) do
href's avatar
href committed
137
138
139
140
    instance = Pleroma.Config.get(:instance)
    instance_fe = Pleroma.Config.get(:fe)
    instance_chat = Pleroma.Config.get(:chat)

lain's avatar
lain committed
141
142
143
144
145
    case get_format(conn) do
      "xml" ->
        response = """
        <config>
          <site>
href's avatar
href committed
146
            <name>#{Keyword.get(instance, :name)}</name>
lain's avatar
lain committed
147
            <site>#{Web.base_url()}</site>
href's avatar
href committed
148
149
            <textlimit>#{Keyword.get(instance, :limit)}</textlimit>
            <closed>#{!Keyword.get(instance, :registrations_open)}</closed>
lain's avatar
lain committed
150
151
152
          </site>
        </config>
        """
lain's avatar
lain committed
153

lain's avatar
lain committed
154
155
156
        conn
        |> put_resp_content_type("application/xml")
        |> send_resp(200, response)
lain's avatar
lain committed
157

lain's avatar
lain committed
158
      _ ->
159
        vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
minibikini's avatar
minibikini committed
160

161
162
163
164
165
166
167
        uploadlimit = %{
          uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
          avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
          backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
          bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
        }

168
        data = %{
href's avatar
href committed
169
170
          name: Keyword.get(instance, :name),
          description: Keyword.get(instance, :description),
171
          server: Web.base_url(),
href's avatar
href committed
172
          textlimit: to_string(Keyword.get(instance, :limit)),
173
          uploadlimit: uploadlimit,
href's avatar
href committed
174
          closed: if(Keyword.get(instance, :registrations_open), do: "0", else: "1"),
175
          private: if(Keyword.get(instance, :public, true), do: "0", else: "1"),
176
177
          vapidPublicKey: vapid_public_key,
          invitesEnabled: if(Keyword.get(instance, :invites_enabled, false), do: "1", else: "0")
178
179
180
        }

        pleroma_fe = %{
href's avatar
href committed
181
182
183
184
185
186
187
188
189
190
191
192
193
          theme: Keyword.get(instance_fe, :theme),
          background: Keyword.get(instance_fe, :background),
          logo: Keyword.get(instance_fe, :logo),
          logoMask: Keyword.get(instance_fe, :logo_mask),
          logoMargin: Keyword.get(instance_fe, :logo_margin),
          redirectRootNoLogin: Keyword.get(instance_fe, :redirect_root_no_login),
          redirectRootLogin: Keyword.get(instance_fe, :redirect_root_login),
          chatDisabled: !Keyword.get(instance_chat, :enabled),
          showInstanceSpecificPanel: Keyword.get(instance_fe, :show_instance_panel),
          scopeOptionsEnabled: Keyword.get(instance_fe, :scope_options_enabled),
          formattingOptionsEnabled: Keyword.get(instance_fe, :formatting_options_enabled),
          collapseMessageWithSubject: Keyword.get(instance_fe, :collapse_message_with_subject),
          hidePostStats: Keyword.get(instance_fe, :hide_post_stats),
194
195
196
197
          hideUserStats: Keyword.get(instance_fe, :hide_user_stats),
          scopeCopy: Keyword.get(instance_fe, :scope_copy),
          subjectLineBehavior: Keyword.get(instance_fe, :subject_line_behavior),
          alwaysShowSubjectInput: Keyword.get(instance_fe, :always_show_subject_input)
198
199
        }

href's avatar
href committed
200
        managed_config = Keyword.get(instance, :managed_config)
kaniini's avatar
kaniini committed
201

202
203
204
205
206
207
208
209
        data =
          if managed_config do
            data |> Map.put("pleromafe", pleroma_fe)
          else
            data
          end

        json(conn, %{site: data})
lain's avatar
lain committed
210
211
212
213
    end
  end

  def version(conn, _params) do
href's avatar
href committed
214
    version = Pleroma.Application.named_version()
lain's avatar
lain committed
215

lain's avatar
lain committed
216
217
    case get_format(conn) do
      "xml" ->
218
        response = "<version>#{version}</version>"
lain's avatar
lain committed
219

lain's avatar
lain committed
220
221
222
        conn
        |> put_resp_content_type("application/xml")
        |> send_resp(200, response)
lain's avatar
lain committed
223
224
225

      _ ->
        json(conn, version)
lain's avatar
lain committed
226
    end
dtluna's avatar
dtluna committed
227
  end
eal's avatar
eal committed
228
229

  def emoji(conn, _params) do
href's avatar
href committed
230
    json(conn, Enum.into(Emoji.get_all(), %{}))
eal's avatar
eal committed
231
  end
eal's avatar
eal committed
232

233
234
235
  def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
    follow_import(conn, %{"list" => File.read!(listfile.path)})
  end
lain's avatar
lain committed
236

eal's avatar
eal committed
237
  def follow_import(%{assigns: %{user: user}} = conn, %{"list" => list}) do
238
    Task.start(fn ->
lain's avatar
lain committed
239
      String.split(list)
240
      |> Enum.map(fn account ->
eal's avatar
eal committed
241
        with %User{} = follower <- User.get_cached_by_ap_id(user.ap_id),
242
             %User{} = followed <- User.get_or_fetch(account),
243
             {:ok, follower} <- User.maybe_direct_follow(follower, followed) do
eal's avatar
eal committed
244
245
          ActivityPub.follow(follower, followed)
        else
Rachel H's avatar
Rachel H committed
246
          err -> Logger.debug("follow_import: following #{account} failed with #{inspect(err)}")
eal's avatar
eal committed
247
248
        end
      end)
eal's avatar
eal committed
249
250
    end)

lain's avatar
lain committed
251
    json(conn, "job started")
eal's avatar
eal committed
252
  end
253

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  def change_password(%{assigns: %{user: user}} = conn, params) do
    case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
      {:ok, user} ->
        with {:ok, _user} <-
               User.reset_password(user, %{
                 password: params["new_password"],
                 password_confirmation: params["new_password_confirmation"]
               }) do
          json(conn, %{status: "success"})
        else
          {:error, changeset} ->
            {_, {error, _}} = Enum.at(changeset.errors, 0)
            json(conn, %{error: "New password #{error}."})

          _ ->
            json(conn, %{error: "Unable to change password."})
        end

      {:error, msg} ->
        json(conn, %{error: msg})
    end
  end

277
  def delete_account(%{assigns: %{user: user}} = conn, params) do
278
    case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
279
      {:ok, user} ->
280
281
        Task.start(fn -> User.delete(user) end)
        json(conn, %{status: "success"})
282
283
284
285
286

      {:error, msg} ->
        json(conn, %{error: msg})
    end
  end
287
288
289
290

  def captcha(conn, _params) do
    json(conn, Pleroma.Captcha.new())
  end
dtluna's avatar
dtluna committed
291
end