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
10 alias Pleroma.Activity
13 alias Pleroma.Healthcheck
14 alias Pleroma.Notification
15 alias Pleroma.Plugs.AuthenticationPlug
16 alias Pleroma.Plugs.OAuthScopesPlug
19 alias Pleroma.Web.CommonAPI
20 alias Pleroma.Web.WebFinger
24 %{scopes: ["follow", "write:follows"]}
25 when action == :follow_import
28 # Note: follower can submit the form (with password auth) not being signed in (having no token)
31 %{fallback: :proceed_unauthenticated, scopes: ["follow", "write:follows"]}
32 when action == :do_remote_follow
35 plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks_import)
39 %{scopes: ["write:accounts"]}
44 :update_notificaton_settings,
49 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action == :notifications_read)
51 plug(Pleroma.Plugs.SetFormatPlug when action in [:config, :version])
53 def help_test(conn, _params) do
57 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
58 with %User{} = user <- User.get_cached_by_nickname(nick),
59 avatar = User.avatar_url(user) do
61 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
64 render(conn, "subscribe.html", %{
67 error: "Could not find user"
72 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
73 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
74 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
76 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
79 render(conn, "subscribe.html", %{
82 error: "Something went wrong."
87 def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
88 if is_status?(acct) do
89 {:ok, object} = Pleroma.Object.Fetcher.fetch_object_from_id(acct)
90 %Activity{id: activity_id} = Activity.get_create_by_object_ap_id(object.data["id"])
91 redirect(conn, to: "/notice/#{activity_id}")
93 with {:ok, followee} <- User.get_or_fetch(acct) do
95 |> render(follow_template(user), %{
98 avatar: User.avatar_url(followee),
99 name: followee.nickname,
104 render(conn, follow_template(user), %{error: :error})
109 defp follow_template(%User{} = _user), do: "follow.html"
110 defp follow_template(_), do: "follow_login.html"
112 defp is_status?(acct) do
113 case Pleroma.Object.Fetcher.fetch_and_contain_remote_object_from_id(acct) do
114 {:ok, %{"type" => type}} when type in ["Article", "Note", "Video", "Page", "Question"] ->
122 def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}})
123 when not is_nil(user) do
124 with {:fetch_user, %User{} = followee} <- {:fetch_user, User.get_cached_by_id(id)},
125 {:ok, _follower, _followee, _activity} <- CommonAPI.follow(user, followee) do
127 |> render("followed.html", %{error: false})
129 # Was already following user
130 {:error, "Could not follow user:" <> _rest} ->
131 render(conn, "followed.html", %{error: "Error following account"})
133 {:fetch_user, error} ->
134 Logger.debug("Remote follow failed with error #{inspect(error)}")
135 render(conn, "followed.html", %{error: "Could not find user"})
138 Logger.debug("Remote follow failed with error #{inspect(e)}")
139 render(conn, "followed.html", %{error: "Something went wrong."})
143 # Note: "id" is the id of followee user, disregard incorrect placing under "authorization"
144 def do_remote_follow(conn, %{
145 "authorization" => %{"name" => username, "password" => password, "id" => id}
147 with %User{} = followee <- User.get_cached_by_id(id),
148 {_, %User{} = user, _} <- {:auth, User.get_cached_by_nickname(username), followee},
151 AuthenticationPlug.checkpw(password, user.password_hash),
154 {:ok, _follower, _followee, _activity} <- CommonAPI.follow(user, followee) do
156 |> render("followed.html", %{error: false})
158 # Was already following user
159 {:error, "Could not follow user:" <> _rest} ->
160 render(conn, "followed.html", %{error: "Error following account"})
162 {:auth, _, followee} ->
164 |> render("follow_login.html", %{
165 error: "Wrong username or password",
167 name: followee.nickname,
168 avatar: User.avatar_url(followee)
172 Logger.debug("Remote follow failed with error #{inspect(e)}")
173 render(conn, "followed.html", %{error: "Something went wrong."})
177 def do_remote_follow(%{assigns: %{user: nil}} = conn, _) do
178 render(conn, "followed.html", %{error: "Insufficient permissions: follow | write:follows."})
181 def do_remote_follow(conn, _) do
182 render(conn, "followed.html", %{error: "Something went wrong."})
185 def notifications_read(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
186 with {:ok, _} <- Notification.read_one(user, notification_id) do
187 json(conn, %{status: "success"})
191 |> put_resp_content_type("application/json")
192 |> send_resp(403, Jason.encode!(%{"error" => message}))
196 def config(%{assigns: %{format: "xml"}} = conn, _params) do
197 instance = Pleroma.Config.get(:instance)
202 <name>#{Keyword.get(instance, :name)}</name>
203 <site>#{Web.base_url()}</site>
204 <textlimit>#{Keyword.get(instance, :limit)}</textlimit>
205 <closed>#{!Keyword.get(instance, :registrations_open)}</closed>
211 |> put_resp_content_type("application/xml")
212 |> send_resp(200, response)
215 def config(conn, _params) do
216 instance = Pleroma.Config.get(:instance)
218 vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
221 uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
222 avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
223 backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
224 bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
228 name: Keyword.get(instance, :name),
229 description: Keyword.get(instance, :description),
230 server: Web.base_url(),
231 textlimit: to_string(Keyword.get(instance, :limit)),
232 uploadlimit: uploadlimit,
233 closed: bool_to_val(Keyword.get(instance, :registrations_open), "0", "1"),
234 private: bool_to_val(Keyword.get(instance, :public, true), "0", "1"),
235 vapidPublicKey: vapid_public_key,
236 accountActivationRequired:
237 bool_to_val(Keyword.get(instance, :account_activation_required, false)),
238 invitesEnabled: bool_to_val(Keyword.get(instance, :invites_enabled, false)),
239 safeDMMentionsEnabled: bool_to_val(Pleroma.Config.get([:instance, :safe_dm_mentions]))
242 managed_config = Keyword.get(instance, :managed_config)
246 pleroma_fe = Pleroma.Config.get([:frontend_configurations, :pleroma_fe])
247 Map.put(data, "pleromafe", pleroma_fe)
252 json(conn, %{site: data})
255 defp bool_to_val(true), do: "1"
256 defp bool_to_val(_), do: "0"
257 defp bool_to_val(true, val, _), do: val
258 defp bool_to_val(_, _, val), do: val
260 def frontend_configurations(conn, _params) do
262 Pleroma.Config.get(:frontend_configurations, %{})
268 def version(%{assigns: %{format: "xml"}} = conn, _params) do
269 version = Pleroma.Application.named_version()
272 |> put_resp_content_type("application/xml")
273 |> send_resp(200, "<version>#{version}</version>")
276 def version(conn, _params) do
277 json(conn, Pleroma.Application.named_version())
280 def emoji(conn, _params) do
282 Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
283 Map.put(acc, code, %{image_url: file, tags: tags})
289 def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
290 with {:ok, _} <- User.update_notification_settings(user, params) do
291 json(conn, %{status: "success"})
295 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
296 follow_import(conn, %{"list" => File.read!(listfile.path)})
299 def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
300 with lines <- String.split(list, "\n"),
301 followed_identifiers <-
302 Enum.map(lines, fn line ->
303 String.split(line, ",") |> List.first()
305 |> List.delete("Account address") do
306 User.follow_import(follower, followed_identifiers)
307 json(conn, "job started")
311 def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
312 blocks_import(conn, %{"list" => File.read!(listfile.path)})
315 def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
316 with blocked_identifiers <- String.split(list) do
317 User.blocks_import(blocker, blocked_identifiers)
318 json(conn, "job started")
322 def change_password(%{assigns: %{user: user}} = conn, params) do
323 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
326 User.reset_password(user, %{
327 password: params["new_password"],
328 password_confirmation: params["new_password_confirmation"]
330 json(conn, %{status: "success"})
332 {:error, changeset} ->
333 {_, {error, _}} = Enum.at(changeset.errors, 0)
334 json(conn, %{error: "New password #{error}."})
337 json(conn, %{error: "Unable to change password."})
341 json(conn, %{error: msg})
345 def change_email(%{assigns: %{user: user}} = conn, params) do
346 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
348 with {:ok, _user} <- User.change_email(user, params["email"]) do
349 json(conn, %{status: "success"})
351 {:error, changeset} ->
352 {_, {error, _}} = Enum.at(changeset.errors, 0)
353 json(conn, %{error: "Email #{error}."})
356 json(conn, %{error: "Unable to change email."})
360 json(conn, %{error: msg})
364 def delete_account(%{assigns: %{user: user}} = conn, params) do
365 password = params["password"] || ""
367 case CommonAPI.Utils.confirm_current_password(user, password) do
370 json(conn, %{status: "success"})
373 json(conn, %{error: msg})
377 def disable_account(%{assigns: %{user: user}} = conn, params) do
378 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
380 User.deactivate_async(user)
381 json(conn, %{status: "success"})
384 json(conn, %{error: msg})
388 def captcha(conn, _params) do
389 json(conn, Pleroma.Captcha.new())
392 def healthcheck(conn, _params) do
393 with true <- Config.get([:instance, :healthcheck]),
394 %{healthy: true} = info <- Healthcheck.system_info() do
397 %{healthy: false} = info ->
398 service_unavailable(conn, info)
401 service_unavailable(conn, %{})
405 defp service_unavailable(conn, info) do
407 |> put_status(:service_unavailable)