1 defmodule Pleroma.Web.TwitterAPI.UtilController do
2 use Pleroma.Web, :controller
5 alias Pleroma.Web.OStatus
7 alias Pleroma.Formatter
8 alias Pleroma.Web.ActivityPub.ActivityPub
9 alias Pleroma.{Repo, PasswordResetToken, User}
11 def show_password_reset(conn, %{"token" => token}) do
12 with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
13 %User{} = user <- Repo.get(User, token.user_id) do
14 render conn, "password_reset.html", %{
19 _e -> render conn, "invalid_token.html"
23 def password_reset(conn, %{"data" => data}) do
24 with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
25 render conn, "password_reset_success.html"
27 _e -> render conn, "password_reset_failed.html"
31 def help_test(conn, _params) do
35 def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
36 {err, followee} = OStatus.find_or_make_user(acct)
37 avatar = User.avatar_url(followee)
38 name = followee.nickname
43 |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id})
46 |> render("follow_login.html", %{error: false, acct: acct, avatar: avatar, name: name, id: id})
50 def do_remote_follow(conn, %{"authorization" => %{"name" => username, "password" => password, "id" => id}}) do
51 followee = Repo.get(User, id)
52 avatar = User.avatar_url(followee)
53 name = followee.nickname
54 with %User{} = user <- User.get_cached_by_nickname(username),
55 true <- Pbkdf2.checkpw(password, user.password_hash),
56 %User{} = followed <- Repo.get(User, id),
57 {:ok, follower} <- User.follow(user, followee),
58 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
60 |> render("followed.html", %{error: false})
64 |> render("follow_login.html", %{error: "Wrong username or password", id: id, name: name, avatar: avatar})
67 def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do
68 with %User{} = followee <- Repo.get(User, id),
69 {:ok, follower} <- User.follow(user, followee),
70 {:ok, _activity} <- ActivityPub.follow(follower, followee) do
72 |> render("followed.html", %{error: false})
75 Logger.debug("Remote follow failed with error #{inspect e}")
77 |> render("followed.html", %{error: inspect(e)})
81 @instance Application.get_env(:pleroma, :instance)
82 def config(conn, _params) do
83 case get_format(conn) do
88 <name>#{Keyword.get(@instance, :name)}</name>
89 <site>#{Web.base_url}</site>
90 <textlimit>#{Keyword.get(@instance, :limit)}</textlimit>
91 <closed>#{!Keyword.get(@instance, :registrations_open)}</closed>
96 |> put_resp_content_type("application/xml")
97 |> send_resp(200, response)
101 name: Keyword.get(@instance, :name),
102 server: Web.base_url,
103 textlimit: Keyword.get(@instance, :limit),
104 closed: if(Keyword.get(@instance, :registrations_open), do: "0", else: "1")
110 def version(conn, _params) do
111 version = Keyword.get(@instance, :version)
112 case get_format(conn) do
114 response = "<version>#{version}</version>"
116 |> put_resp_content_type("application/xml")
117 |> send_resp(200, response)
118 _ -> json(conn, version)
122 def emoji(conn, _params) do
123 json conn, Enum.into(Formatter.get_custom_emoji(), %{})
126 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
127 follow_import(conn, %{"list" => File.read!(listfile.path)})
129 def follow_import(%{assigns: %{user: user}} = conn, %{"list" => list}) do
132 |> Enum.map(fn nick ->
133 with %User{} = follower <- User.get_cached_by_ap_id(user.ap_id),
134 %User{} = followed <- User.get_or_fetch_by_nickname(nick),
135 {:ok, follower} <- User.follow(follower, followed) do
136 ActivityPub.follow(follower, followed)
138 _e -> Logger.debug "follow_import: following #{nick} failed"
143 json conn, "job started"