1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.UtilController do
6 use Pleroma.Web, :controller
12 alias Pleroma.Notification
13 alias Pleroma.PasswordResetToken
17 alias Pleroma.Web.ActivityPub.ActivityPub
18 alias Pleroma.Web.CommonAPI
19 alias Pleroma.Web.OStatus
20 alias Pleroma.Web.WebFinger
22 def show_password_reset(conn, %{"token" => token}) do
23 with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
24 %User{} = user <- Repo.get(User, token.user_id) do
25 render(conn, "password_reset.html", %{
30 _e -> render(conn, "invalid_token.html")
34 def password_reset(conn, %{"data" => data}) do
35 with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
36 render(conn, "password_reset_success.html")
38 _e -> render(conn, "password_reset_failed.html")
42 def help_test(conn, _params) do
46 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
47 with %User{} = user <- User.get_cached_by_nickname(nick), avatar = User.avatar_url(user) do
49 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
52 render(conn, "subscribe.html", %{
55 error: "Could not find user"
60 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
61 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
62 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
64 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
67 render(conn, "subscribe.html", %{
70 error: "Something went wrong."
75 def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
76 {err, followee} = OStatus.find_or_make_user(acct)
77 avatar = User.avatar_url(followee)
78 name = followee.nickname
83 |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id})
86 |> render("follow_login.html", %{
96 def do_remote_follow(conn, %{
97 "authorization" => %{"name" => username, "password" => password, "id" => id}
99 followee = Repo.get(User, id)
100 avatar = User.avatar_url(followee)
101 name = followee.nickname
103 with %User{} = user <- User.get_cached_by_nickname(username),
104 true <- Pbkdf2.checkpw(password, user.password_hash),
105 %User{} = _followed <- Repo.get(User, id),
106 {:ok, follower} <- User.follow(user, followee),
107 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
109 |> render("followed.html", %{error: false})
111 # Was already following user
112 {:error, "Could not follow user:" <> _rest} ->
113 render(conn, "followed.html", %{error: false})
117 |> render("follow_login.html", %{
118 error: "Wrong username or password",
126 def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do
127 with %User{} = followee <- Repo.get(User, id),
128 {:ok, follower} <- User.follow(user, followee),
129 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
131 |> render("followed.html", %{error: false})
133 # Was already following user
134 {:error, "Could not follow user:" <> _rest} ->
136 |> render("followed.html", %{error: false})
139 Logger.debug("Remote follow failed with error #{inspect(e)}")
142 |> render("followed.html", %{error: inspect(e)})
146 def notifications_read(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
147 with {:ok, _} <- Notification.read_one(user, notification_id) do
148 json(conn, %{status: "success"})
152 |> put_resp_content_type("application/json")
153 |> send_resp(403, Jason.encode!(%{"error" => message}))
157 def config(conn, _params) do
158 instance = Pleroma.Config.get(:instance)
159 instance_fe = Pleroma.Config.get(:fe)
160 instance_chat = Pleroma.Config.get(:chat)
162 case get_format(conn) do
167 <name>#{Keyword.get(instance, :name)}</name>
168 <site>#{Web.base_url()}</site>
169 <textlimit>#{Keyword.get(instance, :limit)}</textlimit>
170 <closed>#{!Keyword.get(instance, :registrations_open)}</closed>
176 |> put_resp_content_type("application/xml")
177 |> send_resp(200, response)
180 vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
183 uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
184 avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
185 backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
186 bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
190 name: Keyword.get(instance, :name),
191 description: Keyword.get(instance, :description),
192 server: Web.base_url(),
193 textlimit: to_string(Keyword.get(instance, :limit)),
194 uploadlimit: uploadlimit,
195 closed: if(Keyword.get(instance, :registrations_open), do: "0", else: "1"),
196 private: if(Keyword.get(instance, :public, true), do: "0", else: "1"),
197 vapidPublicKey: vapid_public_key,
198 accountActivationRequired:
199 if(Keyword.get(instance, :account_activation_required, false), do: "1", else: "0"),
200 invitesEnabled: if(Keyword.get(instance, :invites_enabled, false), do: "1", else: "0")
206 theme: Keyword.get(instance_fe, :theme),
207 background: Keyword.get(instance_fe, :background),
208 logo: Keyword.get(instance_fe, :logo),
209 logoMask: Keyword.get(instance_fe, :logo_mask),
210 logoMargin: Keyword.get(instance_fe, :logo_margin),
211 redirectRootNoLogin: Keyword.get(instance_fe, :redirect_root_no_login),
212 redirectRootLogin: Keyword.get(instance_fe, :redirect_root_login),
213 chatDisabled: !Keyword.get(instance_chat, :enabled),
214 showInstanceSpecificPanel: Keyword.get(instance_fe, :show_instance_panel),
215 scopeOptionsEnabled: Keyword.get(instance_fe, :scope_options_enabled),
216 formattingOptionsEnabled: Keyword.get(instance_fe, :formatting_options_enabled),
217 collapseMessageWithSubject:
218 Keyword.get(instance_fe, :collapse_message_with_subject),
219 hidePostStats: Keyword.get(instance_fe, :hide_post_stats),
220 hideUserStats: Keyword.get(instance_fe, :hide_user_stats),
221 scopeCopy: Keyword.get(instance_fe, :scope_copy),
222 subjectLineBehavior: Keyword.get(instance_fe, :subject_line_behavior),
223 alwaysShowSubjectInput: Keyword.get(instance_fe, :always_show_subject_input)
226 Pleroma.Config.get([:frontend_configurations, :pleroma_fe])
229 managed_config = Keyword.get(instance, :managed_config)
233 data |> Map.put("pleromafe", pleroma_fe)
238 json(conn, %{site: data})
242 def frontend_configurations(conn, _params) do
244 Pleroma.Config.get(:frontend_configurations, %{})
250 def version(conn, _params) do
251 version = Pleroma.Application.named_version()
253 case get_format(conn) do
255 response = "<version>#{version}</version>"
258 |> put_resp_content_type("application/xml")
259 |> send_resp(200, response)
266 def emoji(conn, _params) do
267 json(conn, Enum.into(Emoji.get_all(), %{}))
270 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
271 follow_import(conn, %{"list" => File.read!(listfile.path)})
274 def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
275 with followed_identifiers <- String.split(list),
276 {:ok, _} = Task.start(fn -> User.follow_import(follower, followed_identifiers) end) do
277 json(conn, "job started")
281 def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
282 blocks_import(conn, %{"list" => File.read!(listfile.path)})
285 def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
286 with blocked_identifiers <- String.split(list),
287 {:ok, _} = Task.start(fn -> User.blocks_import(blocker, blocked_identifiers) end) do
288 json(conn, "job started")
292 def change_password(%{assigns: %{user: user}} = conn, params) do
293 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
296 User.reset_password(user, %{
297 password: params["new_password"],
298 password_confirmation: params["new_password_confirmation"]
300 json(conn, %{status: "success"})
302 {:error, changeset} ->
303 {_, {error, _}} = Enum.at(changeset.errors, 0)
304 json(conn, %{error: "New password #{error}."})
307 json(conn, %{error: "Unable to change password."})
311 json(conn, %{error: msg})
315 def delete_account(%{assigns: %{user: user}} = conn, params) do
316 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
318 Task.start(fn -> User.delete(user) end)
319 json(conn, %{status: "success"})
322 json(conn, %{error: msg})
326 def captcha(conn, _params) do
327 json(conn, Pleroma.Captcha.new())