A feature for shareable emoji packs, use it in download_from & tests

This commit is contained in:
Ekaterina Vaartis 2019-09-11 22:58:55 +03:00
parent 74fb6d8647
commit 36f2275dc9
3 changed files with 88 additions and 50 deletions

View file

@ -153,6 +153,15 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
from that instance, otherwise it will be downloaded from the fallback source, if there is one. 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 def download_from(conn, %{"instance_address" => address, "pack_name" => name} = data) do
shareable_packs_available =
"#{address}/nodeinfo/2.1.json"
|> Tesla.get!()
|> Map.get(:body)
|> Jason.decode!()
|> Map.get("features")
|> Enum.member?("shareable_emoji_packs")
if shareable_packs_available do
full_pack = full_pack =
"#{address}/api/pleroma/emoji/packs/list" "#{address}/api/pleroma/emoji/packs/list"
|> Tesla.get!() |> Tesla.get!()
@ -178,7 +187,8 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
}} }}
_ -> _ ->
{:error, "The pack was not set as shared and there is no fallback src to download from"} {:error,
"The pack was not set as shared and there is no fallback src to download from"}
end end
with {:ok, %{sha: sha, uri: uri} = pinfo} <- pack_info_res, with {:ok, %{sha: sha, uri: uri} = pinfo} <- pack_info_res,
@ -212,6 +222,11 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
|> put_status(:internal_server_error) |> put_status(:internal_server_error)
|> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"}) |> json(%{error: "SHA256 for the pack doesn't match the one sent by the server"})
end end
else
conn
|> put_status(:internal_server_error)
|> json(%{error: "The requested instance does not support sharing emoji packs"})
end
end end
@doc """ @doc """

View file

@ -57,6 +57,7 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
"mastodon_api_streaming", "mastodon_api_streaming",
"polls", "polls",
"pleroma_explicit_addressing", "pleroma_explicit_addressing",
"shareable_emoji_packs",
if Config.get([:media_proxy, :enabled]) do if Config.get([:media_proxy, :enabled]) do
"media_proxy" "media_proxy"
end, end,

View file

@ -54,6 +54,12 @@ defmodule Pleroma.Web.PleromaAPI.EmojiAPIControllerTest do
end) end)
mock(fn mock(fn
%{method: :get, url: "https://old-instance/nodeinfo/2.1.json"} ->
json(%{features: []})
%{method: :get, url: "https://example.com/nodeinfo/2.1.json"} ->
json(%{features: ["shareable_emoji_packs"]})
%{ %{
method: :get, method: :get,
url: "https://example.com/api/pleroma/emoji/packs/list" url: "https://example.com/api/pleroma/emoji/packs/list"
@ -87,6 +93,22 @@ defmodule Pleroma.Web.PleromaAPI.EmojiAPIControllerTest do
conn = build_conn() |> assign(:user, admin) conn = build_conn() |> assign(:user, admin)
assert (conn
|> put_req_header("content-type", "application/json")
|> post(
emoji_api_path(
conn,
:download_from
),
%{
instance_address: "https://old-instance",
pack_name: "test_pack",
as: "test_pack2"
}
|> Jason.encode!()
)
|> json_response(500))["error"] =~ "does not support"
assert conn assert conn
|> put_req_header("content-type", "application/json") |> put_req_header("content-type", "application/json")
|> post( |> post(