util_controller.ex 5.2 KB
Newer Older
1
# Pleroma: A lightweight social networking server
2
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

dtluna's avatar
dtluna committed
5
6
defmodule Pleroma.Web.TwitterAPI.UtilController do
  use Pleroma.Web, :controller
7

eal's avatar
eal committed
8
  require Logger
9

Maksim's avatar
Maksim committed
10
  alias Pleroma.Config
Haelwenn's avatar
Haelwenn committed
11
  alias Pleroma.Emoji
Maksim's avatar
Maksim committed
12
  alias Pleroma.Healthcheck
13
  alias Pleroma.Notification
14
  alias Pleroma.Web.Plugs.OAuthScopesPlug
15
  alias Pleroma.User
Haelwenn's avatar
Haelwenn committed
16
17
  alias Pleroma.Web.CommonAPI
  alias Pleroma.Web.WebFinger
Roger Braun's avatar
Roger Braun committed
18

19
20
  plug(Pleroma.Web.FederatingPlug when action == :remote_subscribe)

21
22
23
24
  plug(
    OAuthScopesPlug,
    %{scopes: ["write:accounts"]}
    when action in [
25
           :change_email,
26
27
28
29
30
31
32
           :change_password,
           :delete_account,
           :update_notificaton_settings,
           :disable_account
         ]
  )

33
34
  plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action == :notifications_read)

eal's avatar
eal committed
35
  def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
Maksim's avatar
Maksim committed
36
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 notifications_read(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
    with {:ok, _} <- Notification.read_one(user, notification_id) do
      json(conn, %{status: "success"})
    else
      {:error, message} ->
        conn
        |> put_resp_content_type("application/json")
        |> send_resp(403, Jason.encode!(%{"error" => message}))
    end
  end

lain's avatar
lain committed
76
  def frontend_configurations(conn, _params) do
77
    render(conn, "frontend_configurations.json")
lain's avatar
lain committed
78
79
  end

eal's avatar
eal committed
80
  def emoji(conn, _params) do
81
    emoji =
Maksim's avatar
Maksim committed
82
83
      Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
        Map.put(acc, code, %{image_url: file, tags: tags})
84
85
86
      end)

    json(conn, emoji)
eal's avatar
eal committed
87
  end
eal's avatar
eal committed
88

89
90
91
92
  def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
    with {:ok, _} <- User.update_notification_settings(user, params) do
      json(conn, %{status: "success"})
    end
eal's avatar
eal committed
93
  end
eal's avatar
eal committed
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  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

minibikini's avatar
minibikini committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  def change_email(%{assigns: %{user: user}} = conn, params) do
    case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
      {:ok, user} ->
        with {:ok, _user} <- User.change_email(user, params["email"]) do
          json(conn, %{status: "success"})
        else
          {:error, changeset} ->
            {_, {error, _}} = Enum.at(changeset.errors, 0)
            json(conn, %{error: "Email #{error}."})

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

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

137
  def delete_account(%{assigns: %{user: user}} = conn, params) do
138
139
140
    password = params["password"] || ""

    case CommonAPI.Utils.confirm_current_password(user, password) do
141
      {:ok, user} ->
142
        User.delete(user)
143
        json(conn, %{status: "success"})
144
145
146
147
148

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

150
151
152
  def disable_account(%{assigns: %{user: user}} = conn, params) do
    case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
      {:ok, user} ->
153
        User.deactivate_async(user)
154
155
156
157
158
159
160
        json(conn, %{status: "success"})

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

161
162
163
  def captcha(conn, _params) do
    json(conn, Pleroma.Captcha.new())
  end
164
165

  def healthcheck(conn, _params) do
Maksim's avatar
Maksim committed
166
167
168
169
170
171
    with true <- Config.get([:instance, :healthcheck]),
         %{healthy: true} = info <- Healthcheck.system_info() do
      json(conn, info)
    else
      %{healthy: false} = info ->
        service_unavailable(conn, info)
172

Maksim's avatar
Maksim committed
173
174
175
176
      _ ->
        service_unavailable(conn, %{})
    end
  end
177

Maksim's avatar
Maksim committed
178
179
180
181
  defp service_unavailable(conn, info) do
    conn
    |> put_status(:service_unavailable)
    |> json(info)
182
  end
dtluna's avatar
dtluna committed
183
end