Merge branch 'display-name' into 'develop'

Add optional `display-name` parameter for custom emoji packs

See merge request pleroma/pleroma!3092
This commit is contained in:
Sean King 2021-10-10 11:12:57 +00:00
commit 7408f004aa
18 changed files with 142 additions and 20 deletions

View File

@ -418,6 +418,7 @@ The status posting endpoint takes an additional parameter, `in_reply_to_conversa
* Params: * Params:
* `name`: pack name * `name`: pack name
* `metadata`: metadata to replace the old one * `metadata`: metadata to replace the old one
* `display-name`: Pack name that returns in `category` for each emoji in its pack whenever called from `/api/v1/custom_emojis`
* `license`: Pack license * `license`: Pack license
* `homepage`: Pack home page url * `homepage`: Pack home page url
* `description`: Pack description * `description`: Pack description

View File

@ -20,6 +20,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
Enum.each(manifest, fn {name, info} -> Enum.each(manifest, fn {name, info} ->
to_print = [ to_print = [
{"Name", name}, {"Name", name},
{"Display name", info["display-name"]},
{"Homepage", info["homepage"]}, {"Homepage", info["homepage"]},
{"Description", info["description"]}, {"Description", info["description"]},
{"License", info["license"]}, {"License", info["license"]},
@ -119,6 +120,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
pack_json = %{ pack_json = %{
pack: %{ pack: %{
"display-name" => pack["display_name"],
"license" => pack["license"], "license" => pack["license"],
"homepage" => pack["homepage"], "homepage" => pack["homepage"],
"description" => pack["description"], "description" => pack["description"],
@ -144,6 +146,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
args, args,
strict: [ strict: [
name: :string, name: :string,
display_name: :string,
license: :string, license: :string,
homepage: :string, homepage: :string,
description: :string, description: :string,
@ -154,6 +157,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
proposed_name = Path.basename(src) |> Path.rootname() proposed_name = Path.basename(src) |> Path.rootname()
name = get_option(opts, :name, "Pack name:", proposed_name) name = get_option(opts, :name, "Pack name:", proposed_name)
display_name = get_option(opts, :display_name, "Display name:")
license = get_option(opts, :license, "License:") license = get_option(opts, :license, "License:")
homepage = get_option(opts, :homepage, "Homepage:") homepage = get_option(opts, :homepage, "Homepage:")
description = get_option(opts, :description, "Description:") description = get_option(opts, :description, "Description:")
@ -190,6 +194,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
pack_json = %{ pack_json = %{
name => %{ name => %{
display_name: display_name,
license: license, license: license,
homepage: homepage, homepage: homepage,
description: description, description: description,

View File

@ -105,10 +105,12 @@ defmodule Pleroma.Emoji.Loader do
if File.exists?(pack_file) do if File.exists?(pack_file) do
contents = Jason.decode!(File.read!(pack_file)) contents = Jason.decode!(File.read!(pack_file))
display_name = contents["pack"]["display-name"] || "pack:#{pack_name}"
contents["files"] contents["files"]
|> Enum.map(fn {name, rel_file} -> |> Enum.map(fn {name, rel_file} ->
filename = Path.join("/emoji/#{pack_name}", rel_file) filename = Path.join("/emoji/#{pack_name}", rel_file)
{name, filename, ["pack:#{pack_name}"]} {name, filename, ["#{display_name}"]}
end) end)
else else
# Load from emoji.txt / all files # Load from emoji.txt / all files

View File

@ -275,10 +275,15 @@ defmodule Pleroma.Emoji.Pack do
@spec update_metadata(String.t(), map()) :: {:ok, t()} | {:error, File.posix()} @spec update_metadata(String.t(), map()) :: {:ok, t()} | {:error, File.posix()}
def update_metadata(name, data) do def update_metadata(name, data) do
with {:ok, pack} <- load_pack(name) do with {:ok, pack} <- load_pack(name) do
if fallback_sha_changed?(pack, data) do cond do
update_sha_and_save_metadata(pack, data) fallback_sha_changed?(pack, data) ->
else update_sha_and_save_metadata(pack, data)
save_metadata(data, pack)
display_name_blank?(data) ->
remove_display_name_and_save_metadata(pack, data)
true ->
save_metadata(data, pack)
end end
end end
end end
@ -630,14 +635,31 @@ defmodule Pleroma.Emoji.Pack do
is_binary(data[:"fallback-src"]) and data[:"fallback-src"] != pack.pack["fallback-src"] is_binary(data[:"fallback-src"]) and data[:"fallback-src"] != pack.pack["fallback-src"]
end end
defp display_name_blank?(data) do
data[:"display-name"] == "" or data[:"display-name"] == nil
end
defp remove_display_name_and_save_metadata(pack, data) do
data
|> Map.delete(:"display-name")
|> save_metadata(pack)
end
defp update_sha_and_save_metadata(pack, data) do defp update_sha_and_save_metadata(pack, data) do
with {:ok, %{body: zip}} <- Pleroma.HTTP.get(data[:"fallback-src"]), with {:ok, %{body: zip}} <- Pleroma.HTTP.get(data[:"fallback-src"]),
:ok <- validate_has_all_files(pack, zip) do :ok <- validate_has_all_files(pack, zip) do
fallback_sha = :sha256 |> :crypto.hash(zip) |> Base.encode16() fallback_sha = :sha256 |> :crypto.hash(zip) |> Base.encode16()
data if display_name_blank?(data) do
|> Map.put("fallback-src-sha256", fallback_sha) data
|> save_metadata(pack) |> Map.put("fallback-src-sha256", fallback_sha)
remove_display_name_and_save_metadata(pack, data)
else
data
|> Map.put("fallback-src-sha256", fallback_sha)
|> save_metadata(pack)
end
end end
end end

View File

@ -248,6 +248,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiPackOperation do
pack: %Schema{ pack: %Schema{
type: :object, type: :object,
properties: %{ properties: %{
"display-name": %Schema{type: :string},
license: %Schema{type: :string}, license: %Schema{type: :string},
homepage: %Schema{type: :string, format: :uri}, homepage: %Schema{type: :string, format: :uri},
description: %Schema{type: :string}, description: %Schema{type: :string},
@ -260,6 +261,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiPackOperation do
example: %{ example: %{
"files" => %{"emacs" => "emacs.png", "guix" => "guix.png"}, "files" => %{"emacs" => "emacs.png", "guix" => "guix.png"},
"pack" => %{ "pack" => %{
"display-name" => "Test display name",
"license" => "Test license", "license" => "Test license",
"homepage" => "https://pleroma.social", "homepage" => "https://pleroma.social",
"description" => "Test description", "description" => "Test description",
@ -287,6 +289,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiPackOperation do
type: :object, type: :object,
description: "Metadata to replace the old one", description: "Metadata to replace the old one",
properties: %{ properties: %{
"display-name": %Schema{type: :string},
license: %Schema{type: :string}, license: %Schema{type: :string},
homepage: %Schema{type: :string, format: :uri}, homepage: %Schema{type: :string, format: :uri},
description: %Schema{type: :string}, description: %Schema{type: :string},
@ -310,6 +313,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiPackOperation do
%Schema{ %Schema{
type: :object, type: :object,
properties: %{ properties: %{
"display-name": %Schema{type: :string},
license: %Schema{type: :string}, license: %Schema{type: :string},
homepage: %Schema{type: :string, format: :uri}, homepage: %Schema{type: :string, format: :uri},
description: %Schema{type: :string}, description: %Schema{type: :string},

View File

@ -1,5 +1,6 @@
{ {
"finmoji": { "finmoji": {
"display-name": "Finmoji",
"license": "CC BY-NC-ND 4.0", "license": "CC BY-NC-ND 4.0",
"homepage": "https://finland.fi/emoji/", "homepage": "https://finland.fi/emoji/",
"description": "Finland is the first country in the world to publish its own set of country themed emojis. The Finland emoji collection contains 56 tongue-in-cheek emotions, which were created to explain some hard-to-describe Finnish emotions, Finnish words and customs.", "description": "Finland is the first country in the world to publish its own set of country themed emojis. The Finland emoji collection contains 56 tongue-in-cheek emotions, which were created to explain some hard-to-describe Finnish emotions, Finnish words and customs.",

View File

@ -1,5 +1,6 @@
{ {
"blobs.gg": { "blobs.gg": {
"display-name": "Blob Emoji",
"src_sha256": "3a12f3a181678d5b3584a62095411b0d60a335118135910d879920f8ade5a57f", "src_sha256": "3a12f3a181678d5b3584a62095411b0d60a335118135910d879920f8ade5a57f",
"src": "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip", "src": "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip",
"license": "Apache 2.0", "license": "Apache 2.0",

View File

@ -3,6 +3,7 @@
"blank": "blank.png" "blank": "blank.png"
}, },
"pack": { "pack": {
"display-name": "Test display name",
"description": "Test description", "description": "Test description",
"homepage": "https://pleroma.social", "homepage": "https://pleroma.social",
"license": "Test license", "license": "Test license",

View File

@ -1,5 +1,6 @@
{ {
"pack": { "pack": {
"display-name": "Test display name",
"license": "Test license", "license": "Test license",
"homepage": "https://pleroma.social", "homepage": "https://pleroma.social",
"description": "Test description", "description": "Test description",

View File

@ -4,6 +4,7 @@
"blank2": "blank2.png" "blank2": "blank2.png"
}, },
"pack": { "pack": {
"display-name": "Test display name",
"description": "Test description", "description": "Test description",
"homepage": "https://pleroma.social", "homepage": "https://pleroma.social",
"license": "Test license", "license": "Test license",

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 B

View File

@ -0,0 +1,11 @@
{
"files": {
"blank5": "blank5.png"
},
"pack": {
"description": "Test description",
"homepage": "https://pleroma.social",
"license": "Test license",
"share-files": true
}
}

View File

@ -1,5 +1,6 @@
{ {
"pack": { "pack": {
"display-name": "Test display name",
"license": "Test license", "license": "Test license",
"homepage": "https://pleroma.social", "homepage": "https://pleroma.social",
"description": "Test description", "description": "Test description",

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 B

View File

@ -0,0 +1,12 @@
{
"files": {
"blank6": "blank6.png"
},
"pack": {
"display-name": "Test display name",
"description": "Test description",
"homepage": "https://pleroma.social",
"license": "Test license",
"share-files": true
}
}

View File

@ -154,6 +154,8 @@ defmodule Mix.Tasks.Pleroma.EmojiTest do
url, url,
"--name", "--name",
name, name,
"--display-name",
"display_name",
"--license", "--license",
"license", "license",
"--homepage", "--homepage",
@ -193,6 +195,8 @@ defmodule Mix.Tasks.Pleroma.EmojiTest do
url, url,
"--name", "--name",
name, name,
"--display-name",
"display_name",
"--license", "--license",
"license", "license",
"--homepage", "--homepage",
@ -219,6 +223,8 @@ defmodule Mix.Tasks.Pleroma.EmojiTest do
url, url,
"--name", "--name",
name, name,
"--display-name",
"display_name",
"--license", "--license",
"license", "license",
"--homepage", "--homepage",

View File

@ -20,4 +20,33 @@ defmodule Pleroma.Web.MastodonAPI.CustomEmojiControllerTest do
assert Map.has_key?(emoji, "url") assert Map.has_key?(emoji, "url")
assert Map.has_key?(emoji, "visible_in_picker") assert Map.has_key?(emoji, "visible_in_picker")
end end
test "with display name", %{conn: conn} do
assert resp =
conn
|> get("/api/v1/custom_emojis")
|> json_response_and_validate_schema(200)
assert test_pack =
conn
|> get("/api/pleroma/emoji/pack?name=test_pack_with_display_name")
|> json_response_and_validate_schema(200)
assert emoji = Enum.at(Enum.filter(resp, fn x -> x["shortcode"] == "blank6" end), 0)
assert emoji["category"] == test_pack["pack"]["display-name"]
assert Enum.at(emoji["tags"], 0) == test_pack["pack"]["display-name"]
end
test "without display name", %{conn: conn} do
assert resp =
conn
|> get("/api/v1/custom_emojis")
|> json_response_and_validate_schema(200)
assert test_pack_name = "test_pack_no_display_name"
assert emoji = Enum.at(Enum.filter(resp, fn x -> x["shortcode"] == "blank5" end), 0)
assert emoji["category"] == "pack:#{test_pack_name}"
assert Enum.at(emoji["tags"], 0) == "pack:#{test_pack_name}"
end
end end

View File

@ -37,11 +37,11 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
test "GET /api/pleroma/emoji/packs", %{conn: conn} do test "GET /api/pleroma/emoji/packs", %{conn: conn} do
resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200) resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 6
assert resp["packs"] assert resp["packs"]
|> Map.keys() |> Map.keys()
|> length() == 4 |> length() == 6
shared = resp["packs"]["test_pack"] shared = resp["packs"]["test_pack"]
assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"} assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"}
@ -53,12 +53,15 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
assert non_shared["pack"]["share-files"] == false assert non_shared["pack"]["share-files"] == false
assert non_shared["pack"]["can-download"] == false assert non_shared["pack"]["can-download"] == false
no_display_name = resp["packs"]["test_pack_no_display_name"]
assert no_display_name["pack"]["display-name"] == nil
resp = resp =
conn conn
|> get("/api/pleroma/emoji/packs?page_size=1") |> get("/api/pleroma/emoji/packs?page_size=1")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 6
packs = Map.keys(resp["packs"]) packs = Map.keys(resp["packs"])
@ -71,7 +74,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> get("/api/pleroma/emoji/packs?page_size=1&page=2") |> get("/api/pleroma/emoji/packs?page_size=1&page=2")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 6
packs = Map.keys(resp["packs"]) packs = Map.keys(resp["packs"])
assert length(packs) == 1 assert length(packs) == 1
[pack2] = packs [pack2] = packs
@ -81,7 +84,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> get("/api/pleroma/emoji/packs?page_size=1&page=3") |> get("/api/pleroma/emoji/packs?page_size=1&page=3")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 6
packs = Map.keys(resp["packs"]) packs = Map.keys(resp["packs"])
assert length(packs) == 1 assert length(packs) == 1
[pack3] = packs [pack3] = packs
@ -91,7 +94,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> get("/api/pleroma/emoji/packs?page_size=1&page=4") |> get("/api/pleroma/emoji/packs?page_size=1&page=4")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 6
packs = Map.keys(resp["packs"]) packs = Map.keys(resp["packs"])
assert length(packs) == 1 assert length(packs) == 1
[pack4] = packs [pack4] = packs
@ -358,6 +361,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
{:ok, {:ok,
pack_file: pack_file, pack_file: pack_file,
new_data: %{ new_data: %{
"display-name" => "Test display name changed",
"license" => "Test license changed", "license" => "Test license changed",
"homepage" => "https://pleroma.social", "homepage" => "https://pleroma.social",
"description" => "Test description", "description" => "Test description",
@ -587,6 +591,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
"files_count" => 2, "files_count" => 2,
"pack" => %{ "pack" => %{
"can-download" => true, "can-download" => true,
"display-name" => "Test display name",
"description" => "Test description", "description" => "Test description",
"download-sha256" => _, "download-sha256" => _,
"homepage" => "https://pleroma.social", "homepage" => "https://pleroma.social",
@ -627,6 +632,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
"files_count" => 1, "files_count" => 1,
"pack" => %{ "pack" => %{
"can-download" => true, "can-download" => true,
"display-name" => "Test display name",
"description" => "Test description", "description" => "Test description",
"download-sha256" => _, "download-sha256" => _,
"homepage" => "https://pleroma.social", "homepage" => "https://pleroma.social",
@ -647,6 +653,24 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
} }
end end
test "for pack with no display name", %{conn: conn} do
assert %{
"files" => _files,
"files_count" => 1,
"pack" => %{
"can-download" => true,
"description" => "Test description",
"download-sha256" => _,
"homepage" => "https://pleroma.social",
"license" => "Test license",
"share-files" => true
}
} =
conn
|> get("/api/pleroma/emoji/pack?name=test_pack_no_display_name")
|> json_response_and_validate_schema(200)
end
test "error name", %{conn: conn} do test "error name", %{conn: conn} do
assert conn assert conn
|> get("/api/pleroma/emoji/pack?name= ") |> get("/api/pleroma/emoji/pack?name= ")