1 defmodule Pleroma.Web.TwitterAPI.UtilController do
2 use Pleroma.Web, :controller
5 alias Pleroma.Web.OStatus
6 alias Pleroma.Web.WebFinger
7 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.{Repo, PasswordResetToken, User, Emoji}
12 def show_password_reset(conn, %{"token" => token}) do
13 with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
14 %User{} = user <- Repo.get(User, token.user_id) do
15 render(conn, "password_reset.html", %{
20 _e -> render(conn, "invalid_token.html")
24 def password_reset(conn, %{"data" => data}) do
25 with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
26 render(conn, "password_reset_success.html")
28 _e -> render(conn, "password_reset_failed.html")
32 def help_test(conn, _params) do
36 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
37 with %User{} = user <- User.get_cached_by_nickname(nick), avatar = User.avatar_url(user) do
39 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
42 render(conn, "subscribe.html", %{
45 error: "Could not find user"
50 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
51 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
52 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
54 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
57 render(conn, "subscribe.html", %{
60 error: "Something went wrong."
65 def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
66 {err, followee} = OStatus.find_or_make_user(acct)
67 avatar = User.avatar_url(followee)
68 name = followee.nickname
73 |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id})
76 |> render("follow_login.html", %{
86 def do_remote_follow(conn, %{
87 "authorization" => %{"name" => username, "password" => password, "id" => id}
89 followee = Repo.get(User, id)
90 avatar = User.avatar_url(followee)
91 name = followee.nickname
93 with %User{} = user <- User.get_cached_by_nickname(username),
94 true <- Pbkdf2.checkpw(password, user.password_hash),
95 %User{} = _followed <- Repo.get(User, id),
96 {:ok, follower} <- User.follow(user, followee),
97 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
99 |> render("followed.html", %{error: false})
101 # Was already following user
102 {:error, "Could not follow user:" <> _rest} ->
103 render(conn, "followed.html", %{error: false})
107 |> render("follow_login.html", %{
108 error: "Wrong username or password",
116 def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do
117 with %User{} = followee <- Repo.get(User, id),
118 {:ok, follower} <- User.follow(user, followee),
119 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
121 |> render("followed.html", %{error: false})
123 # Was already following user
124 {:error, "Could not follow user:" <> _rest} ->
126 |> render("followed.html", %{error: false})
129 Logger.debug("Remote follow failed with error #{inspect(e)}")
132 |> render("followed.html", %{error: inspect(e)})
136 def config(conn, _params) do
137 instance = Pleroma.Config.get(:instance)
138 instance_fe = Pleroma.Config.get(:fe)
139 instance_chat = Pleroma.Config.get(:chat)
141 case get_format(conn) do
146 <name>#{Keyword.get(instance, :name)}</name>
147 <site>#{Web.base_url()}</site>
148 <textlimit>#{Keyword.get(instance, :limit)}</textlimit>
149 <closed>#{!Keyword.get(instance, :registrations_open)}</closed>
155 |> put_resp_content_type("application/xml")
156 |> send_resp(200, response)
159 vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
162 uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
163 avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
164 backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
165 bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
169 name: Keyword.get(instance, :name),
170 description: Keyword.get(instance, :description),
171 server: Web.base_url(),
172 textlimit: to_string(Keyword.get(instance, :limit)),
173 uploadlimit: uploadlimit,
174 closed: if(Keyword.get(instance, :registrations_open), do: "0", else: "1"),
175 private: if(Keyword.get(instance, :public, true), do: "0", else: "1"),
176 vapidPublicKey: vapid_public_key,
177 invitesEnabled: if(Keyword.get(instance, :invites_enabled, false), do: "1", else: "0")
181 theme: Keyword.get(instance_fe, :theme),
182 background: Keyword.get(instance_fe, :background),
183 logo: Keyword.get(instance_fe, :logo),
184 logoMask: Keyword.get(instance_fe, :logo_mask),
185 logoMargin: Keyword.get(instance_fe, :logo_margin),
186 redirectRootNoLogin: Keyword.get(instance_fe, :redirect_root_no_login),
187 redirectRootLogin: Keyword.get(instance_fe, :redirect_root_login),
188 chatDisabled: !Keyword.get(instance_chat, :enabled),
189 showInstanceSpecificPanel: Keyword.get(instance_fe, :show_instance_panel),
190 scopeOptionsEnabled: Keyword.get(instance_fe, :scope_options_enabled),
191 formattingOptionsEnabled: Keyword.get(instance_fe, :formatting_options_enabled),
192 collapseMessageWithSubject: Keyword.get(instance_fe, :collapse_message_with_subject),
193 hidePostStats: Keyword.get(instance_fe, :hide_post_stats),
194 hideUserStats: Keyword.get(instance_fe, :hide_user_stats),
195 scopeCopy: Keyword.get(instance_fe, :scope_copy),
196 subjectLineBehavior: Keyword.get(instance_fe, :subject_line_behavior),
197 alwaysShowSubjectInput: Keyword.get(instance_fe, :always_show_subject_input)
200 managed_config = Keyword.get(instance, :managed_config)
204 data |> Map.put("pleromafe", pleroma_fe)
209 json(conn, %{site: data})
213 def version(conn, _params) do
214 version = Pleroma.Application.named_version()
216 case get_format(conn) do
218 response = "<version>#{version}</version>"
221 |> put_resp_content_type("application/xml")
222 |> send_resp(200, response)
229 def emoji(conn, _params) do
230 json(conn, Enum.into(Emoji.get_all(), %{}))
233 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
234 follow_import(conn, %{"list" => File.read!(listfile.path)})
237 def follow_import(%{assigns: %{user: user}} = conn, %{"list" => list}) do
240 |> Enum.map(fn account ->
241 with %User{} = follower <- User.get_cached_by_ap_id(user.ap_id),
242 %User{} = followed <- User.get_or_fetch(account),
243 {:ok, follower} <- User.maybe_direct_follow(follower, followed) do
244 ActivityPub.follow(follower, followed)
246 err -> Logger.debug("follow_import: following #{account} failed with #{inspect(err)}")
251 json(conn, "job started")
254 def change_password(%{assigns: %{user: user}} = conn, params) do
255 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
258 User.reset_password(user, %{
259 password: params["new_password"],
260 password_confirmation: params["new_password_confirmation"]
262 json(conn, %{status: "success"})
264 {:error, changeset} ->
265 {_, {error, _}} = Enum.at(changeset.errors, 0)
266 json(conn, %{error: "New password #{error}."})
269 json(conn, %{error: "Unable to change password."})
273 json(conn, %{error: msg})
277 def delete_account(%{assigns: %{user: user}} = conn, params) do
278 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
280 Task.start(fn -> User.delete(user) end)
281 json(conn, %{status: "success"})
284 json(conn, %{error: msg})