Add GET endpoints for remote subscription forms

There are two reasons for adding a GET endpoint:

0: Barely displaying the form does not change anything on the server.

1: It makes frontend development easier as they can now use a link,
instead of a form, to allow remote users to interact with local ones.
This commit is contained in:
Tusooa Zhu 2021-12-28 16:41:46 -05:00
parent a243a217a7
commit 779457d9a4
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
4 changed files with 55 additions and 4 deletions

View file

@ -405,6 +405,16 @@ defmodule Pleroma.Web.ApiSpec.TwitterUtilOperation do
}
end
def show_subscribe_form_operation do
%Operation{
tags: ["Accounts"],
summary: "Show remote subscribe form",
operationId: "UtilController.show_subscribe_form",
parameters: [],
responses: %{200 => Operation.response("Web Page", "test/html", %Schema{type: :string})}
}
end
defp delete_account_request do
%Schema{
title: "AccountDeleteRequest",

View file

@ -337,6 +337,7 @@ defmodule Pleroma.Web.Router do
pipe_through(:pleroma_html)
post("/main/ostatus", UtilController, :remote_subscribe)
get("/main/ostatus", UtilController, :show_subscribe_form)
get("/ostatus_subscribe", RemoteFollowController, :follow)
post("/ostatus_subscribe", RemoteFollowController, :do_follow)
end

View file

@ -17,8 +17,8 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
alias Pleroma.Web.Plugs.OAuthScopesPlug
alias Pleroma.Web.WebFinger
plug(Pleroma.Web.ApiSpec.CastAndValidate when action != :remote_subscribe)
plug(Pleroma.Web.Plugs.FederatingPlug when action == :remote_subscribe)
plug(Pleroma.Web.ApiSpec.CastAndValidate when action != :remote_subscribe and action != :show_subscribe_form)
plug(Pleroma.Web.Plugs.FederatingPlug when action == :remote_subscribe when action == :show_subscribe_form)
plug(
OAuthScopesPlug,
@ -45,7 +45,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TwitterUtilOperation
def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
def show_subscribe_form(conn, %{"nickname" => nick}) do
with %User{} = user <- User.get_cached_by_nickname(nick),
avatar = User.avatar_url(user) do
conn
@ -60,7 +60,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
end
end
def remote_subscribe(conn, %{"status_id" => id, "profile" => _}) do
def show_subscribe_form(conn, %{"status_id" => id}) do
with %Activity{} = activity <- Activity.get_by_id(id),
%User{} = user <- User.get_cached_by_ap_id(activity.actor),
avatar = User.avatar_url(user) do
@ -81,6 +81,14 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
end
end
def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
show_subscribe_form(conn, %{"nickname" => nick})
end
def remote_subscribe(conn, %{"status_id" => id, "profile" => _}) do
show_subscribe_form(conn, %{"status_id" => id})
end
def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
%User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do

View file

@ -297,6 +297,38 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
end
end
describe "GET /main/ostatus - show_subscribe_form/2" do
setup do: clear_config([:instance, :federating], true)
test "it works with users", %{conn: conn} do
user = insert(:user)
response =
conn
|> get("/main/ostatus", %{"nickname" => user.nickname})
|> response(:ok)
refute response =~ "Could not find user"
assert response =~ "Remotely follow #{user.nickname}"
end
test "it works with statuses", %{conn: conn} do
user = insert(:user)
status = insert(:note_activity, %{user: user})
status_id = status.id
assert is_binary(status_id)
response =
conn
|> get("/main/ostatus", %{"status_id" => status_id})
|> response(:ok)
refute response =~ "Could not find status"
assert response =~ "Interacting with"
end
end
test "it returns new captcha", %{conn: conn} do
with_mock Pleroma.Captcha,
new: fn -> "test_captcha" end do