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
11 alias Pleroma.Activity
13 alias Pleroma.Notification
14 alias Pleroma.PasswordResetToken
18 alias Pleroma.Web.ActivityPub.ActivityPub
19 alias Pleroma.Web.CommonAPI
20 alias Pleroma.Web.OStatus
21 alias Pleroma.Web.WebFinger
23 def show_password_reset(conn, %{"token" => token}) do
24 with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
25 %User{} = user <- User.get_by_id(token.user_id) do
26 render(conn, "password_reset.html", %{
31 _e -> render(conn, "invalid_token.html")
35 def password_reset(conn, %{"data" => data}) do
36 with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
37 render(conn, "password_reset_success.html")
39 _e -> render(conn, "password_reset_failed.html")
43 def help_test(conn, _params) do
47 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
48 with %User{} = user <- User.get_cached_by_nickname(nick), avatar = User.avatar_url(user) do
50 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
53 render(conn, "subscribe.html", %{
56 error: "Could not find user"
61 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
62 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
63 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
65 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
68 render(conn, "subscribe.html", %{
71 error: "Something went wrong."
76 def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
77 if is_status?(acct) do
78 {:ok, object} = ActivityPub.fetch_object_from_id(acct)
79 %Activity{id: activity_id} = Activity.get_create_by_object_ap_id(object.data["id"])
80 redirect(conn, to: "/notice/#{activity_id}")
82 {err, followee} = OStatus.find_or_make_user(acct)
83 avatar = User.avatar_url(followee)
84 name = followee.nickname
89 |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id})
92 |> render("follow_login.html", %{
103 defp is_status?(acct) do
104 case ActivityPub.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 followee = User.get_by_id(id)
117 avatar = User.avatar_url(followee)
118 name = followee.nickname
120 with %User{} = user <- User.get_cached_by_nickname(username),
121 true <- Pbkdf2.checkpw(password, user.password_hash),
122 %User{} = _followed <- User.get_by_id(id),
123 {:ok, follower} <- User.follow(user, followee),
124 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
126 |> render("followed.html", %{error: false})
128 # Was already following user
129 {:error, "Could not follow user:" <> _rest} ->
130 render(conn, "followed.html", %{error: false})
134 |> render("follow_login.html", %{
135 error: "Wrong username or password",
143 def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do
144 with %User{} = followee <- User.get_by_id(id),
145 {:ok, follower} <- User.follow(user, followee),
146 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
148 |> render("followed.html", %{error: false})
150 # Was already following user
151 {:error, "Could not follow user:" <> _rest} ->
153 |> render("followed.html", %{error: false})
156 Logger.debug("Remote follow failed with error #{inspect(e)}")
159 |> render("followed.html", %{error: inspect(e)})
163 def notifications_read(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
164 with {:ok, _} <- Notification.read_one(user, notification_id) do
165 json(conn, %{status: "success"})
169 |> put_resp_content_type("application/json")
170 |> send_resp(403, Jason.encode!(%{"error" => message}))
174 def config(conn, _params) do
175 instance = Pleroma.Config.get(:instance)
176 instance_fe = Pleroma.Config.get(:fe)
177 instance_chat = Pleroma.Config.get(:chat)
179 case get_format(conn) do
184 <name>#{Keyword.get(instance, :name)}</name>
185 <site>#{Web.base_url()}</site>
186 <textlimit>#{Keyword.get(instance, :limit)}</textlimit>
187 <closed>#{!Keyword.get(instance, :registrations_open)}</closed>
193 |> put_resp_content_type("application/xml")
194 |> send_resp(200, response)
197 vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
200 uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
201 avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
202 backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
203 bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
207 name: Keyword.get(instance, :name),
208 description: Keyword.get(instance, :description),
209 server: Web.base_url(),
210 textlimit: to_string(Keyword.get(instance, :limit)),
211 uploadlimit: uploadlimit,
212 closed: if(Keyword.get(instance, :registrations_open), do: "0", else: "1"),
213 private: if(Keyword.get(instance, :public, true), do: "0", else: "1"),
214 vapidPublicKey: vapid_public_key,
215 accountActivationRequired:
216 if(Keyword.get(instance, :account_activation_required, false), do: "1", else: "0"),
217 invitesEnabled: if(Keyword.get(instance, :invites_enabled, false), do: "1", else: "0"),
218 safeDMMentionsEnabled:
219 if(Pleroma.Config.get([:instance, :safe_dm_mentions]), do: "1", else: "0")
225 theme: Keyword.get(instance_fe, :theme),
226 background: Keyword.get(instance_fe, :background),
227 logo: Keyword.get(instance_fe, :logo),
228 logoMask: Keyword.get(instance_fe, :logo_mask),
229 logoMargin: Keyword.get(instance_fe, :logo_margin),
230 redirectRootNoLogin: Keyword.get(instance_fe, :redirect_root_no_login),
231 redirectRootLogin: Keyword.get(instance_fe, :redirect_root_login),
232 chatDisabled: !Keyword.get(instance_chat, :enabled),
233 showInstanceSpecificPanel: Keyword.get(instance_fe, :show_instance_panel),
234 scopeOptionsEnabled: Keyword.get(instance_fe, :scope_options_enabled),
235 formattingOptionsEnabled: Keyword.get(instance_fe, :formatting_options_enabled),
236 collapseMessageWithSubject:
237 Keyword.get(instance_fe, :collapse_message_with_subject),
238 hidePostStats: Keyword.get(instance_fe, :hide_post_stats),
239 hideUserStats: Keyword.get(instance_fe, :hide_user_stats),
240 scopeCopy: Keyword.get(instance_fe, :scope_copy),
241 subjectLineBehavior: Keyword.get(instance_fe, :subject_line_behavior),
242 alwaysShowSubjectInput: Keyword.get(instance_fe, :always_show_subject_input)
245 Pleroma.Config.get([:frontend_configurations, :pleroma_fe])
248 managed_config = Keyword.get(instance, :managed_config)
252 data |> Map.put("pleromafe", pleroma_fe)
257 json(conn, %{site: data})
261 def frontend_configurations(conn, _params) do
263 Pleroma.Config.get(:frontend_configurations, %{})
269 def version(conn, _params) do
270 version = Pleroma.Application.named_version()
272 case get_format(conn) do
274 response = "<version>#{version}</version>"
277 |> put_resp_content_type("application/xml")
278 |> send_resp(200, response)
285 def emoji(conn, _params) do
288 |> Enum.map(fn {short_code, path, tags} ->
289 {short_code, %{image_url: path, tags: String.split(tags, ",")}}
296 def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
297 with {:ok, _} <- User.update_notification_settings(user, params) do
298 json(conn, %{status: "success"})
302 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
303 follow_import(conn, %{"list" => File.read!(listfile.path)})
306 def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
307 with followed_identifiers <- String.split(list),
308 {:ok, _} = Task.start(fn -> User.follow_import(follower, followed_identifiers) end) do
309 json(conn, "job started")
313 def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
314 blocks_import(conn, %{"list" => File.read!(listfile.path)})
317 def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
318 with blocked_identifiers <- String.split(list),
319 {:ok, _} = Task.start(fn -> User.blocks_import(blocker, blocked_identifiers) end) do
320 json(conn, "job started")
324 def change_password(%{assigns: %{user: user}} = conn, params) do
325 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
328 User.reset_password(user, %{
329 password: params["new_password"],
330 password_confirmation: params["new_password_confirmation"]
332 json(conn, %{status: "success"})
334 {:error, changeset} ->
335 {_, {error, _}} = Enum.at(changeset.errors, 0)
336 json(conn, %{error: "New password #{error}."})
339 json(conn, %{error: "Unable to change password."})
343 json(conn, %{error: msg})
347 def delete_account(%{assigns: %{user: user}} = conn, params) do
348 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
350 Task.start(fn -> User.delete(user) end)
351 json(conn, %{status: "success"})
354 json(conn, %{error: msg})
358 def captcha(conn, _params) do
359 json(conn, Pleroma.Captcha.new())