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
243 |> Enum.map(fn {short_code, path, tags} ->
244 {short_code, %{image_url: path, tags: tags}}
251 def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
252 with {:ok, _} <- User.update_notification_settings(user, params) do
253 json(conn, %{status: "success"})
257 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
258 follow_import(conn, %{"list" => File.read!(listfile.path)})
261 def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
262 with lines <- String.split(list, "\n"),
263 followed_identifiers <-
264 Enum.map(lines, fn line ->
265 String.split(line, ",") |> List.first()
267 |> List.delete("Account address") do
268 User.follow_import(follower, followed_identifiers)
269 json(conn, "job started")
273 def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
274 blocks_import(conn, %{"list" => File.read!(listfile.path)})
277 def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
278 with blocked_identifiers <- String.split(list) do
279 User.blocks_import(blocker, blocked_identifiers)
280 json(conn, "job started")
284 def change_password(%{assigns: %{user: user}} = conn, params) do
285 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
288 User.reset_password(user, %{
289 password: params["new_password"],
290 password_confirmation: params["new_password_confirmation"]
292 json(conn, %{status: "success"})
294 {:error, changeset} ->
295 {_, {error, _}} = Enum.at(changeset.errors, 0)
296 json(conn, %{error: "New password #{error}."})
299 json(conn, %{error: "Unable to change password."})
303 json(conn, %{error: msg})
307 def change_email(%{assigns: %{user: user}} = conn, params) do
308 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
310 with {:ok, _user} <- User.change_email(user, params["email"]) do
311 json(conn, %{status: "success"})
313 {:error, changeset} ->
314 {_, {error, _}} = Enum.at(changeset.errors, 0)
315 json(conn, %{error: "Email #{error}."})
318 json(conn, %{error: "Unable to change email."})
322 json(conn, %{error: msg})
326 def delete_account(%{assigns: %{user: user}} = conn, params) do
327 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
330 json(conn, %{status: "success"})
333 json(conn, %{error: msg})
337 def disable_account(%{assigns: %{user: user}} = conn, params) do
338 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
340 User.deactivate_async(user)
341 json(conn, %{status: "success"})
344 json(conn, %{error: msg})
348 def captcha(conn, _params) do
349 json(conn, Pleroma.Captcha.new())
352 def healthcheck(conn, _params) do
353 with true <- Config.get([:instance, :healthcheck]),
354 %{healthy: true} = info <- Healthcheck.system_info() do
357 %{healthy: false} = info ->
358 service_unavailable(conn, info)
361 service_unavailable(conn, %{})
365 defp service_unavailable(conn, info) do
367 |> put_status(:service_unavailable)