Merge branch 'develop' into 'develop'
[akkoma] / lib / pleroma / web / twitter_api / controllers / util_controller.ex
1 defmodule Pleroma.Web.TwitterAPI.UtilController do
2 use Pleroma.Web, :controller
3 require Logger
4 alias Pleroma.Web
5 alias Pleroma.Web.OStatus
6 alias Pleroma.Web.WebFinger
7 alias Pleroma.Web.CommonAPI
8 alias Comeonin.Pbkdf2
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.{Repo, PasswordResetToken, User, Emoji}
11
12 def show_password_reset(conn, %{"token" => token}) do
13 with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
14 %User{} = user <- Repo.get(User, token.user_id) do
15 render(conn, "password_reset.html", %{
16 token: token,
17 user: user
18 })
19 else
20 _e -> render(conn, "invalid_token.html")
21 end
22 end
23
24 def password_reset(conn, %{"data" => data}) do
25 with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
26 render(conn, "password_reset_success.html")
27 else
28 _e -> render(conn, "password_reset_failed.html")
29 end
30 end
31
32 def help_test(conn, _params) do
33 json(conn, "ok")
34 end
35
36 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
37 with %User{} = user <- User.get_cached_by_nickname(nick), avatar = User.avatar_url(user) do
38 conn
39 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
40 else
41 _e ->
42 render(conn, "subscribe.html", %{
43 nickname: nick,
44 avatar: nil,
45 error: "Could not find user"
46 })
47 end
48 end
49
50 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
51 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
52 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
53 conn
54 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
55 else
56 _e ->
57 render(conn, "subscribe.html", %{
58 nickname: nick,
59 avatar: nil,
60 error: "Something went wrong."
61 })
62 end
63 end
64
65 def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
66 {err, followee} = OStatus.find_or_make_user(acct)
67 avatar = User.avatar_url(followee)
68 name = followee.nickname
69 id = followee.id
70
71 if !!user do
72 conn
73 |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id})
74 else
75 conn
76 |> render("follow_login.html", %{
77 error: false,
78 acct: acct,
79 avatar: avatar,
80 name: name,
81 id: id
82 })
83 end
84 end
85
86 def do_remote_follow(conn, %{
87 "authorization" => %{"name" => username, "password" => password, "id" => id}
88 }) do
89 followee = Repo.get(User, id)
90 avatar = User.avatar_url(followee)
91 name = followee.nickname
92
93 with %User{} = user <- User.get_cached_by_nickname(username),
94 true <- Pbkdf2.checkpw(password, user.password_hash),
95 %User{} = _followed <- Repo.get(User, id),
96 {:ok, follower} <- User.follow(user, followee),
97 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
98 conn
99 |> render("followed.html", %{error: false})
100 else
101 # Was already following user
102 {:error, "Could not follow user:" <> _rest} ->
103 render(conn, "followed.html", %{error: false})
104
105 _e ->
106 conn
107 |> render("follow_login.html", %{
108 error: "Wrong username or password",
109 id: id,
110 name: name,
111 avatar: avatar
112 })
113 end
114 end
115
116 def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do
117 with %User{} = followee <- Repo.get(User, id),
118 {:ok, follower} <- User.follow(user, followee),
119 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
120 conn
121 |> render("followed.html", %{error: false})
122 else
123 # Was already following user
124 {:error, "Could not follow user:" <> _rest} ->
125 conn
126 |> render("followed.html", %{error: false})
127
128 e ->
129 Logger.debug("Remote follow failed with error #{inspect(e)}")
130
131 conn
132 |> render("followed.html", %{error: inspect(e)})
133 end
134 end
135
136 def config(conn, _params) do
137 instance = Pleroma.Config.get(:instance)
138 instance_fe = Pleroma.Config.get(:fe)
139 instance_chat = Pleroma.Config.get(:chat)
140
141 case get_format(conn) do
142 "xml" ->
143 response = """
144 <config>
145 <site>
146 <name>#{Keyword.get(instance, :name)}</name>
147 <site>#{Web.base_url()}</site>
148 <textlimit>#{Keyword.get(instance, :limit)}</textlimit>
149 <closed>#{!Keyword.get(instance, :registrations_open)}</closed>
150 </site>
151 </config>
152 """
153
154 conn
155 |> put_resp_content_type("application/xml")
156 |> send_resp(200, response)
157
158 _ ->
159 vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
160
161 uploadlimit = %{
162 uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
163 avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
164 backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
165 bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
166 }
167
168 data = %{
169 name: Keyword.get(instance, :name),
170 description: Keyword.get(instance, :description),
171 server: Web.base_url(),
172 textlimit: to_string(Keyword.get(instance, :limit)),
173 uploadlimit: uploadlimit,
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 vapidPublicKey: vapid_public_key,
177 invitesEnabled: if(Keyword.get(instance, :invites_enabled, false), do: "1", else: "0")
178 }
179
180 pleroma_fe = %{
181 theme: Keyword.get(instance_fe, :theme),
182 background: Keyword.get(instance_fe, :background),
183 logo: Keyword.get(instance_fe, :logo),
184 logoMask: Keyword.get(instance_fe, :logo_mask),
185 logoMargin: Keyword.get(instance_fe, :logo_margin),
186 redirectRootNoLogin: Keyword.get(instance_fe, :redirect_root_no_login),
187 redirectRootLogin: Keyword.get(instance_fe, :redirect_root_login),
188 chatDisabled: !Keyword.get(instance_chat, :enabled),
189 showInstanceSpecificPanel: Keyword.get(instance_fe, :show_instance_panel),
190 scopeOptionsEnabled: Keyword.get(instance_fe, :scope_options_enabled),
191 formattingOptionsEnabled: Keyword.get(instance_fe, :formatting_options_enabled),
192 collapseMessageWithSubject: Keyword.get(instance_fe, :collapse_message_with_subject),
193 hidePostStats: Keyword.get(instance_fe, :hide_post_stats),
194 hideUserStats: Keyword.get(instance_fe, :hide_user_stats),
195 scopeCopy: Keyword.get(instance_fe, :scope_copy),
196 subjectLineBehavior: Keyword.get(instance_fe, :subject_line_behavior),
197 alwaysShowSubjectInput: Keyword.get(instance_fe, :always_show_subject_input)
198 }
199
200 managed_config = Keyword.get(instance, :managed_config)
201
202 data =
203 if managed_config do
204 data |> Map.put("pleromafe", pleroma_fe)
205 else
206 data
207 end
208
209 json(conn, %{site: data})
210 end
211 end
212
213 def version(conn, _params) do
214 version = Pleroma.Application.named_version()
215
216 case get_format(conn) do
217 "xml" ->
218 response = "<version>#{version}</version>"
219
220 conn
221 |> put_resp_content_type("application/xml")
222 |> send_resp(200, response)
223
224 _ ->
225 json(conn, version)
226 end
227 end
228
229 def emoji(conn, _params) do
230 json(conn, Enum.into(Emoji.get_all(), %{}))
231 end
232
233 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
234 follow_import(conn, %{"list" => File.read!(listfile.path)})
235 end
236
237 def follow_import(%{assigns: %{user: user}} = conn, %{"list" => list}) do
238 Task.start(fn ->
239 String.split(list)
240 |> Enum.map(fn account ->
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
244 ActivityPub.follow(follower, followed)
245 else
246 err -> Logger.debug("follow_import: following #{account} failed with #{inspect(err)}")
247 end
248 end)
249 end)
250
251 json(conn, "job started")
252 end
253
254 def change_password(%{assigns: %{user: user}} = conn, params) do
255 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
256 {:ok, user} ->
257 with {:ok, _user} <-
258 User.reset_password(user, %{
259 password: params["new_password"],
260 password_confirmation: params["new_password_confirmation"]
261 }) do
262 json(conn, %{status: "success"})
263 else
264 {:error, changeset} ->
265 {_, {error, _}} = Enum.at(changeset.errors, 0)
266 json(conn, %{error: "New password #{error}."})
267
268 _ ->
269 json(conn, %{error: "Unable to change password."})
270 end
271
272 {:error, msg} ->
273 json(conn, %{error: msg})
274 end
275 end
276
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 Task.start(fn -> User.delete(user) end)
281 json(conn, %{status: "success"})
282
283 {:error, msg} ->
284 json(conn, %{error: msg})
285 end
286 end
287 end