Add an API endpoint for listing remote packs
This commit is contained in:
parent
a6e85215e1
commit
ba9d35a904
|
@ -10,6 +10,27 @@ def emoji_dir_path do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Lists packs from the remote instance.
|
||||||
|
|
||||||
|
Since JS cannot ask remote instances for their packs due to CPS, it has to
|
||||||
|
be done by the server
|
||||||
|
"""
|
||||||
|
def list_from(conn, %{"instance_address" => address}) do
|
||||||
|
address = String.trim(address)
|
||||||
|
|
||||||
|
if shareable_packs_available(address) do
|
||||||
|
list_resp =
|
||||||
|
"#{address}/api/pleroma/emoji/packs" |> Tesla.get!() |> Map.get(:body) |> Jason.decode!()
|
||||||
|
|
||||||
|
json(conn, list_resp)
|
||||||
|
else
|
||||||
|
conn
|
||||||
|
|> put_status(:internal_server_error)
|
||||||
|
|> json(%{error: "The requested instance does not support sharing emoji packs"})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Lists the packs available on the instance as JSON.
|
Lists the packs available on the instance as JSON.
|
||||||
|
|
||||||
|
@ -156,15 +177,7 @@ def download_shared(conn, %{"name" => name}) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
defp shareable_packs_available(address) do
|
||||||
An admin endpoint to request downloading a pack named `pack_name` from the instance
|
|
||||||
`instance_address`.
|
|
||||||
|
|
||||||
If the requested instance's admin chose to share the pack, it will be downloaded
|
|
||||||
from that instance, otherwise it will be downloaded from the fallback source, if there is one.
|
|
||||||
"""
|
|
||||||
def download_from(conn, %{"instance_address" => address, "pack_name" => name} = data) do
|
|
||||||
shareable_packs_available =
|
|
||||||
"#{address}/.well-known/nodeinfo"
|
"#{address}/.well-known/nodeinfo"
|
||||||
|> Tesla.get!()
|
|> Tesla.get!()
|
||||||
|> Map.get(:body)
|
|> Map.get(:body)
|
||||||
|
@ -177,8 +190,19 @@ def download_from(conn, %{"instance_address" => address, "pack_name" => name} =
|
||||||
|> Jason.decode!()
|
|> Jason.decode!()
|
||||||
|> get_in(["metadata", "features"])
|
|> get_in(["metadata", "features"])
|
||||||
|> Enum.member?("shareable_emoji_packs")
|
|> Enum.member?("shareable_emoji_packs")
|
||||||
|
end
|
||||||
|
|
||||||
if shareable_packs_available do
|
@doc """
|
||||||
|
An admin endpoint to request downloading a pack named `pack_name` from the instance
|
||||||
|
`instance_address`.
|
||||||
|
|
||||||
|
If the requested instance's admin chose to share the pack, it will be downloaded
|
||||||
|
from that instance, otherwise it will be downloaded from the fallback source, if there is one.
|
||||||
|
"""
|
||||||
|
def download_from(conn, %{"instance_address" => address, "pack_name" => name} = data) do
|
||||||
|
address = String.trim(address)
|
||||||
|
|
||||||
|
if shareable_packs_available(address) do
|
||||||
full_pack =
|
full_pack =
|
||||||
"#{address}/api/pleroma/emoji/packs/list"
|
"#{address}/api/pleroma/emoji/packs/list"
|
||||||
|> Tesla.get!()
|
|> Tesla.get!()
|
||||||
|
|
|
@ -222,6 +222,7 @@ defmodule Pleroma.Web.Router do
|
||||||
put("/:name", EmojiAPIController, :create)
|
put("/:name", EmojiAPIController, :create)
|
||||||
delete("/:name", EmojiAPIController, :delete)
|
delete("/:name", EmojiAPIController, :delete)
|
||||||
post("/download_from", EmojiAPIController, :download_from)
|
post("/download_from", EmojiAPIController, :download_from)
|
||||||
|
post("/list_from", EmojiAPIController, :list_from)
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/packs" do
|
scope "/packs" do
|
||||||
|
|
|
@ -33,6 +33,28 @@ test "shared & non-shared pack information in list_packs is ok" do
|
||||||
refute pack["pack"]["can-download"]
|
refute pack["pack"]["can-download"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "listing remote packs" do
|
||||||
|
admin = insert(:user, info: %{is_admin: true})
|
||||||
|
conn = build_conn() |> assign(:user, admin)
|
||||||
|
|
||||||
|
resp = conn |> get(emoji_api_path(conn, :list_packs)) |> json_response(200)
|
||||||
|
|
||||||
|
mock(fn
|
||||||
|
%{method: :get, url: "https://example.com/.well-known/nodeinfo"} ->
|
||||||
|
json([%{href: "https://example.com/nodeinfo/2.1.json"}])
|
||||||
|
|
||||||
|
%{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
|
||||||
|
json(%{metadata: %{features: ["shareable_emoji_packs"]}})
|
||||||
|
|
||||||
|
%{method: :get, url: "https://example.com/api/pleroma/emoji/packs"} ->
|
||||||
|
json(resp)
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert conn
|
||||||
|
|> post(emoji_api_path(conn, :list_from), %{instance_address: "https://example.com"})
|
||||||
|
|> json_response(200) == resp
|
||||||
|
end
|
||||||
|
|
||||||
test "downloading a shared pack from download_shared" do
|
test "downloading a shared pack from download_shared" do
|
||||||
conn = build_conn()
|
conn = build_conn()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue