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
18 alias Pleroma.Web.CommonAPI
19 alias Pleroma.Web.WebFinger
21 plug(Pleroma.Plugs.SetFormatPlug when action in [:config, :version])
23 def help_test(conn, _params) do
27 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
28 with %User{} = user <- User.get_cached_by_nickname(nick),
29 avatar = User.avatar_url(user) do
31 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
34 render(conn, "subscribe.html", %{
37 error: "Could not find user"
42 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
43 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
44 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
46 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
49 render(conn, "subscribe.html", %{
52 error: "Something went wrong."
57 def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
58 if is_status?(acct) do
59 {:ok, object} = Pleroma.Object.Fetcher.fetch_object_from_id(acct)
60 %Activity{id: activity_id} = Activity.get_create_by_object_ap_id(object.data["id"])
61 redirect(conn, to: "/notice/#{activity_id}")
63 with {:ok, followee} <- User.get_or_fetch(acct) do
65 |> render(follow_template(user), %{
68 avatar: User.avatar_url(followee),
69 name: followee.nickname,
74 render(conn, follow_template(user), %{error: :error})
79 defp follow_template(%User{} = _user), do: "follow.html"
80 defp follow_template(_), do: "follow_login.html"
82 defp is_status?(acct) do
83 case Pleroma.Object.Fetcher.fetch_and_contain_remote_object_from_id(acct) do
84 {:ok, %{"type" => type}} when type in ["Article", "Note", "Video", "Page", "Question"] ->
92 def do_remote_follow(conn, %{
93 "authorization" => %{"name" => username, "password" => password, "id" => id}
95 with %User{} = followee <- User.get_cached_by_id(id),
96 {_, %User{} = user, _} <- {:auth, User.get_cached_by_nickname(username), followee},
99 AuthenticationPlug.checkpw(password, user.password_hash),
102 {:ok, _follower, _followee, _activity} <- CommonAPI.follow(user, followee) do
104 |> render("followed.html", %{error: false})
106 # Was already following user
107 {:error, "Could not follow user:" <> _rest} ->
108 render(conn, "followed.html", %{error: "Error following account"})
110 {:auth, _, followee} ->
112 |> render("follow_login.html", %{
113 error: "Wrong username or password",
115 name: followee.nickname,
116 avatar: User.avatar_url(followee)
120 Logger.debug("Remote follow failed with error #{inspect(e)}")
121 render(conn, "followed.html", %{error: "Something went wrong."})
125 def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do
126 with {:fetch_user, %User{} = followee} <- {:fetch_user, User.get_cached_by_id(id)},
127 {:ok, _follower, _followee, _activity} <- CommonAPI.follow(user, followee) do
129 |> render("followed.html", %{error: false})
131 # Was already following user
132 {:error, "Could not follow user:" <> _rest} ->
133 render(conn, "followed.html", %{error: "Error following account"})
135 {:fetch_user, error} ->
136 Logger.debug("Remote follow failed with error #{inspect(error)}")
137 render(conn, "followed.html", %{error: "Could not find user"})
140 Logger.debug("Remote follow failed with error #{inspect(e)}")
141 render(conn, "followed.html", %{error: "Something went wrong."})
145 def notifications_read(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
146 with {:ok, _} <- Notification.read_one(user, notification_id) do
147 json(conn, %{status: "success"})
151 |> put_resp_content_type("application/json")
152 |> send_resp(403, Jason.encode!(%{"error" => message}))
156 def config(%{assigns: %{format: "xml"}} = conn, _params) do
157 instance = Pleroma.Config.get(:instance)
162 <name>#{Keyword.get(instance, :name)}</name>
163 <site>#{Web.base_url()}</site>
164 <textlimit>#{Keyword.get(instance, :limit)}</textlimit>
165 <closed>#{!Keyword.get(instance, :registrations_open)}</closed>
171 |> put_resp_content_type("application/xml")
172 |> send_resp(200, response)
175 def config(conn, _params) do
176 instance = Pleroma.Config.get(:instance)
178 vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
181 uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
182 avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
183 backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
184 bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
188 name: Keyword.get(instance, :name),
189 description: Keyword.get(instance, :description),
190 server: Web.base_url(),
191 textlimit: to_string(Keyword.get(instance, :limit)),
192 uploadlimit: uploadlimit,
193 closed: bool_to_val(Keyword.get(instance, :registrations_open), "0", "1"),
194 private: bool_to_val(Keyword.get(instance, :public, true), "0", "1"),
195 vapidPublicKey: vapid_public_key,
196 accountActivationRequired:
197 bool_to_val(Keyword.get(instance, :account_activation_required, false)),
198 invitesEnabled: bool_to_val(Keyword.get(instance, :invites_enabled, false)),
199 safeDMMentionsEnabled: bool_to_val(Pleroma.Config.get([:instance, :safe_dm_mentions]))
202 managed_config = Keyword.get(instance, :managed_config)
206 pleroma_fe = Pleroma.Config.get([:frontend_configurations, :pleroma_fe])
207 Map.put(data, "pleromafe", pleroma_fe)
212 json(conn, %{site: data})
215 defp bool_to_val(true), do: "1"
216 defp bool_to_val(_), do: "0"
217 defp bool_to_val(true, val, _), do: val
218 defp bool_to_val(_, _, val), do: val
220 def frontend_configurations(conn, _params) do
222 Pleroma.Config.get(:frontend_configurations, %{})
228 def version(%{assigns: %{format: "xml"}} = conn, _params) do
229 version = Pleroma.Application.named_version()
232 |> put_resp_content_type("application/xml")
233 |> send_resp(200, "<version>#{version}</version>")
236 def version(conn, _params) do
237 json(conn, Pleroma.Application.named_version())
240 def emoji(conn, _params) do
242 Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
243 Map.put(acc, code, %{image_url: file, tags: tags})
249 def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
250 with {:ok, _} <- User.update_notification_settings(user, params) do
251 json(conn, %{status: "success"})
255 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
256 follow_import(conn, %{"list" => File.read!(listfile.path)})
259 def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
260 with lines <- String.split(list, "\n"),
261 followed_identifiers <-
262 Enum.map(lines, fn line ->
263 String.split(line, ",") |> List.first()
265 |> List.delete("Account address") do
266 User.follow_import(follower, followed_identifiers)
267 json(conn, "job started")
271 def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
272 blocks_import(conn, %{"list" => File.read!(listfile.path)})
275 def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
276 with blocked_identifiers <- String.split(list) do
277 User.blocks_import(blocker, blocked_identifiers)
278 json(conn, "job started")
282 def change_password(%{assigns: %{user: user}} = conn, params) do
283 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
286 User.reset_password(user, %{
287 password: params["new_password"],
288 password_confirmation: params["new_password_confirmation"]
290 json(conn, %{status: "success"})
292 {:error, changeset} ->
293 {_, {error, _}} = Enum.at(changeset.errors, 0)
294 json(conn, %{error: "New password #{error}."})
297 json(conn, %{error: "Unable to change password."})
301 json(conn, %{error: msg})
305 def change_email(%{assigns: %{user: user}} = conn, params) do
306 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
308 with {:ok, _user} <- User.change_email(user, params["email"]) do
309 json(conn, %{status: "success"})
311 {:error, changeset} ->
312 {_, {error, _}} = Enum.at(changeset.errors, 0)
313 json(conn, %{error: "Email #{error}."})
316 json(conn, %{error: "Unable to change email."})
320 json(conn, %{error: msg})
324 def delete_account(%{assigns: %{user: user}} = conn, params) do
325 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
328 json(conn, %{status: "success"})
331 json(conn, %{error: msg})
335 def disable_account(%{assigns: %{user: user}} = conn, params) do
336 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
338 User.deactivate_async(user)
339 json(conn, %{status: "success"})
342 json(conn, %{error: msg})
346 def captcha(conn, _params) do
347 json(conn, Pleroma.Captcha.new())
350 def healthcheck(conn, _params) do
351 with true <- Config.get([:instance, :healthcheck]),
352 %{healthy: true} = info <- Healthcheck.system_info() do
355 %{healthy: false} = info ->
356 service_unavailable(conn, info)
359 service_unavailable(conn, %{})
363 defp service_unavailable(conn, info) do
365 |> put_status(:service_unavailable)