Support OpenSearch protocol

This commit is contained in:
itepechi 2023-10-22 04:32:28 +09:00
parent 93cf428e2a
commit 9104458dd9
6 changed files with 66 additions and 0 deletions

View File

@ -151,6 +151,7 @@ config :logger, :ex_syslogger,
config :mime, :types, %{
"application/xml" => ["xml"],
"application/xrd+xml" => ["xrd+xml"],
"application/opensearchdescription+xml" => ["xml"],
"application/jrd+json" => ["jrd+json"],
"application/activity+json" => ["activity+json"],
"application/ld+json" => ["activity+json"]

View File

@ -0,0 +1,16 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OpenSearchController do
use Pleroma.Web, :controller
plug(:skip_auth when action == :show)
@doc "GET /opensearch.xml"
def show(conn, _params) do
conn
|> put_resp_content_type("application/opensearchdescription+xml")
|> render("opensearch.xml")
end
end

View File

@ -156,6 +156,10 @@ defmodule Pleroma.Web.Router do
plug(Pleroma.Web.Plugs.StaticFEPlug)
end
pipeline :accepts_opensearch do
plug(:accepts, ["xml"])
end
scope "/api/v1/pleroma", Pleroma.Web.TwitterAPI do
pipe_through(:pleroma_api)
@ -865,6 +869,12 @@ defmodule Pleroma.Web.Router do
get("/web/manifest.json", MastoFEController, :manifest)
end
scope "/", Pleroma.Web do
pipe_through(:accepts_opensearch)
get("/opensearch.xml", OpenSearchController, :show)
end
scope "/", Pleroma.Web do
pipe_through(:mastodon_html)

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName><%= Config.get([:instance, :name]) %></ShortName>
<Description>Search through <%= Config.get([:instance, :name]) %></Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16" type="image/x-icon"><%= Endpoint.url() %>/favicon.ico</Image>
<Image type="image/png"><%= Endpoint.url() %>/favicon.png</Image>
<Url type="text/html" method="get" template="<%= Endpoint.url() %>/search?query={searchTerms}"/>
</OpenSearchDescription>

View File

@ -0,0 +1,9 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OpenSearchView do
use Pleroma.Web, :view
alias Pleroma.Config
alias Pleroma.Web.Endpoint
end

View File

@ -0,0 +1,21 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OpenSearchControllerTest do
use Pleroma.Web.ConnCase, async: false
alias Pleroma.Config
alias Pleroma.Web.Endpoint
test "opensearch.xml", %{conn: conn} do
resp =
conn
|> put_req_header("accept", "application/opensearchdescription+xml")
|> get("/opensearch.xml")
|> response(200)
assert resp =~ "<ShortName>#{Config.get([:instance, :name])}</ShortName>"
assert resp =~ "<Description>Search through #{Config.get([:instance, :name])}</Description>"
assert resp =~ "template=\"#{Endpoint.url()}/search?query={searchTerms}\""
end
end