Merge branch 'feature/emoji-in-local-users' into 'develop'

Add emoji to local user profiles.

Closes #202

See merge request pleroma/pleroma!271
This commit is contained in:
kaniini 2018-08-26 23:24:41 +00:00
commit 63094cfd3e
5 changed files with 61 additions and 2 deletions

View file

@ -42,7 +42,8 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"image" => %{ "image" => %{
"type" => "Image", "type" => "Image",
"url" => User.banner_url(user) "url" => User.banner_url(user)
} },
"tag" => user.info["source_data"]["tag"] || []
} }
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Utils.make_json_ld_header())
end end

View file

@ -1,5 +1,5 @@
defmodule Pleroma.Web.CommonAPI do defmodule Pleroma.Web.CommonAPI do
alias Pleroma.{Repo, Activity, Object} alias Pleroma.{User, Repo, Activity, Object}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Formatter alias Pleroma.Formatter
@ -123,6 +123,18 @@ defmodule Pleroma.Web.CommonAPI do
end end
def update(user) do def update(user) do
user =
with emoji <- emoji_from_profile(user),
source_data <- (user.info["source_data"] || %{}) |> Map.put("tag", emoji),
new_info <- Map.put(user.info, "source_data", source_data),
change <- User.info_changeset(user, %{info: new_info}),
{:ok, user} <- User.update_and_set_cache(change) do
user
else
_e ->
user
end
ActivityPub.update(%{ ActivityPub.update(%{
local: true, local: true,
to: [user.follower_address], to: [user.follower_address],

View file

@ -1,6 +1,7 @@
defmodule Pleroma.Web.CommonAPI.Utils do defmodule Pleroma.Web.CommonAPI.Utils do
alias Pleroma.{Repo, Object, Formatter, Activity} alias Pleroma.{Repo, Object, Formatter, Activity}
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.Endpoint
alias Pleroma.User alias Pleroma.User
alias Calendar.Strftime alias Calendar.Strftime
alias Comeonin.Pbkdf2 alias Comeonin.Pbkdf2
@ -195,4 +196,15 @@ defmodule Pleroma.Web.CommonAPI.Utils do
_ -> {:error, "Invalid password."} _ -> {:error, "Invalid password."}
end end
end end
def emoji_from_profile(%{info: info} = user) do
(Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
|> Enum.map(fn {shortcode, url} ->
%{
"type" => "Emoji",
"icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
"name" => ":#{shortcode}:"
}
end)
end
end end

View file

@ -1,6 +1,7 @@
defmodule Pleroma.Web.CommonAPI.Test do defmodule Pleroma.Web.CommonAPI.Test do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.User
import Pleroma.Factory import Pleroma.Factory
@ -10,4 +11,14 @@ defmodule Pleroma.Web.CommonAPI.Test do
assert activity.data["object"]["tag"] == ["2hu"] assert activity.data["object"]["tag"] == ["2hu"]
end end
test "it adds emoji when updating profiles" do
user = insert(:user, %{name: ":karjalanpiirakka:"})
CommonAPI.update(user)
user = User.get_cached_by_ap_id(user.ap_id)
[karjalanpiirakka] = user.info["source_data"]["tag"]
assert karjalanpiirakka["name"] == ":karjalanpiirakka:"
end
end end

View file

@ -1,5 +1,6 @@
defmodule Pleroma.Web.CommonAPI.UtilsTest do defmodule Pleroma.Web.CommonAPI.UtilsTest do
alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.Endpoint
alias Pleroma.Builders.{UserBuilder} alias Pleroma.Builders.{UserBuilder}
use Pleroma.DataCase use Pleroma.DataCase
@ -29,4 +30,26 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
assert Utils.confirm_current_password(user, "test") == {:ok, user} assert Utils.confirm_current_password(user, "test") == {:ok, user}
end end
end end
test "parses emoji from name and bio" do
{:ok, user} = UserBuilder.insert(%{name: ":karjalanpiirakka:", bio: ":perkele:"})
expected = [
%{
"type" => "Emoji",
"icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/finmoji/128px/perkele-128.png"},
"name" => ":perkele:"
},
%{
"type" => "Emoji",
"icon" => %{
"type" => "Image",
"url" => "#{Endpoint.url()}/finmoji/128px/karjalanpiirakka-128.png"
},
"name" => ":karjalanpiirakka:"
}
]
assert expected == Utils.emoji_from_profile(user)
end
end end