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 in [:do_remote_follow, :follow_import]
28 plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks_import)
32 %{scopes: ["write:accounts"]}
37 :update_notificaton_settings,
42 plug(Pleroma.Plugs.SetFormatPlug when action in [:config, :version])
44 def help_test(conn, _params) do
48 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
49 with %User{} = user <- User.get_cached_by_nickname(nick),
50 avatar = User.avatar_url(user) do
52 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
55 render(conn, "subscribe.html", %{
58 error: "Could not find user"
63 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
64 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
65 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
67 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
70 render(conn, "subscribe.html", %{
73 error: "Something went wrong."
78 def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
79 if is_status?(acct) do
80 {:ok, object} = Pleroma.Object.Fetcher.fetch_object_from_id(acct)
81 %Activity{id: activity_id} = Activity.get_create_by_object_ap_id(object.data["id"])
82 redirect(conn, to: "/notice/#{activity_id}")
84 with {:ok, followee} <- User.get_or_fetch(acct) do
86 |> render(follow_template(user), %{
89 avatar: User.avatar_url(followee),
90 name: followee.nickname,
95 render(conn, follow_template(user), %{error: :error})
100 defp follow_template(%User{} = _user), do: "follow.html"
101 defp follow_template(_), do: "follow_login.html"
103 defp is_status?(acct) do
104 case Pleroma.Object.Fetcher.fetch_and_contain_remote_object_from_id(acct) do
105 {:ok, %{"type" => type}} when type in ["Article", "Note", "Video", "Page", "Question"] ->
113 def do_remote_follow(conn, %{
114 "authorization" => %{"name" => username, "password" => password, "id" => id}
116 with %User{} = followee <- User.get_cached_by_id(id),
117 {_, %User{} = user, _} <- {:auth, User.get_cached_by_nickname(username), followee},
120 AuthenticationPlug.checkpw(password, user.password_hash),
123 {:ok, _follower, _followee, _activity} <- CommonAPI.follow(user, followee) do
125 |> render("followed.html", %{error: false})
127 # Was already following user
128 {:error, "Could not follow user:" <> _rest} ->
129 render(conn, "followed.html", %{error: "Error following account"})
131 {:auth, _, followee} ->
133 |> render("follow_login.html", %{
134 error: "Wrong username or password",
136 name: followee.nickname,
137 avatar: User.avatar_url(followee)
141 Logger.debug("Remote follow failed with error #{inspect(e)}")
142 render(conn, "followed.html", %{error: "Something went wrong."})
146 def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do
147 with {:fetch_user, %User{} = followee} <- {:fetch_user, User.get_cached_by_id(id)},
148 {:ok, _follower, _followee, _activity} <- CommonAPI.follow(user, followee) do
150 |> render("followed.html", %{error: false})
152 # Was already following user
153 {:error, "Could not follow user:" <> _rest} ->
154 render(conn, "followed.html", %{error: "Error following account"})
156 {:fetch_user, error} ->
157 Logger.debug("Remote follow failed with error #{inspect(error)}")
158 render(conn, "followed.html", %{error: "Could not find user"})
161 Logger.debug("Remote follow failed with error #{inspect(e)}")
162 render(conn, "followed.html", %{error: "Something went wrong."})
166 def notifications_read(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
167 with {:ok, _} <- Notification.read_one(user, notification_id) do
168 json(conn, %{status: "success"})
172 |> put_resp_content_type("application/json")
173 |> send_resp(403, Jason.encode!(%{"error" => message}))
177 def config(%{assigns: %{format: "xml"}} = conn, _params) do
178 instance = Pleroma.Config.get(:instance)
183 <name>#{Keyword.get(instance, :name)}</name>
184 <site>#{Web.base_url()}</site>
185 <textlimit>#{Keyword.get(instance, :limit)}</textlimit>
186 <closed>#{!Keyword.get(instance, :registrations_open)}</closed>
192 |> put_resp_content_type("application/xml")
193 |> send_resp(200, response)
196 def config(conn, _params) do
197 instance = Pleroma.Config.get(:instance)
199 vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
202 uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
203 avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
204 backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
205 bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
209 name: Keyword.get(instance, :name),
210 description: Keyword.get(instance, :description),
211 server: Web.base_url(),
212 textlimit: to_string(Keyword.get(instance, :limit)),
213 uploadlimit: uploadlimit,
214 closed: bool_to_val(Keyword.get(instance, :registrations_open), "0", "1"),
215 private: bool_to_val(Keyword.get(instance, :public, true), "0", "1"),
216 vapidPublicKey: vapid_public_key,
217 accountActivationRequired:
218 bool_to_val(Keyword.get(instance, :account_activation_required, false)),
219 invitesEnabled: bool_to_val(Keyword.get(instance, :invites_enabled, false)),
220 safeDMMentionsEnabled: bool_to_val(Pleroma.Config.get([:instance, :safe_dm_mentions]))
223 managed_config = Keyword.get(instance, :managed_config)
227 pleroma_fe = Pleroma.Config.get([:frontend_configurations, :pleroma_fe])
228 Map.put(data, "pleromafe", pleroma_fe)
233 json(conn, %{site: data})
236 defp bool_to_val(true), do: "1"
237 defp bool_to_val(_), do: "0"
238 defp bool_to_val(true, val, _), do: val
239 defp bool_to_val(_, _, val), do: val
241 def frontend_configurations(conn, _params) do
243 Pleroma.Config.get(:frontend_configurations, %{})
249 def version(%{assigns: %{format: "xml"}} = conn, _params) do
250 version = Pleroma.Application.named_version()
253 |> put_resp_content_type("application/xml")
254 |> send_resp(200, "<version>#{version}</version>")
257 def version(conn, _params) do
258 json(conn, Pleroma.Application.named_version())
261 def emoji(conn, _params) do
263 Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
264 Map.put(acc, code, %{image_url: file, tags: tags})
270 def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
271 with {:ok, _} <- User.update_notification_settings(user, params) do
272 json(conn, %{status: "success"})
276 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
277 follow_import(conn, %{"list" => File.read!(listfile.path)})
280 def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
281 with lines <- String.split(list, "\n"),
282 followed_identifiers <-
283 Enum.map(lines, fn line ->
284 String.split(line, ",") |> List.first()
286 |> List.delete("Account address") do
287 User.follow_import(follower, followed_identifiers)
288 json(conn, "job started")
292 def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
293 blocks_import(conn, %{"list" => File.read!(listfile.path)})
296 def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
297 with blocked_identifiers <- String.split(list) do
298 User.blocks_import(blocker, blocked_identifiers)
299 json(conn, "job started")
303 def change_password(%{assigns: %{user: user}} = conn, params) do
304 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
307 User.reset_password(user, %{
308 password: params["new_password"],
309 password_confirmation: params["new_password_confirmation"]
311 json(conn, %{status: "success"})
313 {:error, changeset} ->
314 {_, {error, _}} = Enum.at(changeset.errors, 0)
315 json(conn, %{error: "New password #{error}."})
318 json(conn, %{error: "Unable to change password."})
322 json(conn, %{error: msg})
326 def change_email(%{assigns: %{user: user}} = conn, params) do
327 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
329 with {:ok, _user} <- User.change_email(user, params["email"]) do
330 json(conn, %{status: "success"})
332 {:error, changeset} ->
333 {_, {error, _}} = Enum.at(changeset.errors, 0)
334 json(conn, %{error: "Email #{error}."})
337 json(conn, %{error: "Unable to change email."})
341 json(conn, %{error: msg})
345 def delete_account(%{assigns: %{user: user}} = conn, params) do
346 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
349 json(conn, %{status: "success"})
352 json(conn, %{error: msg})
356 def disable_account(%{assigns: %{user: user}} = conn, params) do
357 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
359 User.deactivate_async(user)
360 json(conn, %{status: "success"})
363 json(conn, %{error: msg})
367 def captcha(conn, _params) do
368 json(conn, Pleroma.Captcha.new())
371 def healthcheck(conn, _params) do
372 with true <- Config.get([:instance, :healthcheck]),
373 %{healthy: true} = info <- Healthcheck.system_info() do
376 %{healthy: false} = info ->
377 service_unavailable(conn, info)
380 service_unavailable(conn, %{})
384 defp service_unavailable(conn, info) do
386 |> put_status(:service_unavailable)