Parse mentions, save them, output them in TwAPI.
This commit is contained in:
parent
2e88cc4cbd
commit
a83fa053de
@ -6,6 +6,14 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
|
|||||||
def to_map(%Activity{} = activity, %{user: user} = opts) do
|
def to_map(%Activity{} = activity, %{user: user} = opts) do
|
||||||
content = get_in(activity.data, ["object", "content"])
|
content = get_in(activity.data, ["object", "content"])
|
||||||
published = get_in(activity.data, ["object", "published"])
|
published = get_in(activity.data, ["object", "published"])
|
||||||
|
|
||||||
|
mentions = opts[:mentioned] || []
|
||||||
|
|
||||||
|
attentions = activity.data["to"]
|
||||||
|
|> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
|
||||||
|
|> Enum.filter(&(&1))
|
||||||
|
|> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
|
||||||
|
|
||||||
%{
|
%{
|
||||||
"id" => activity.id,
|
"id" => activity.id,
|
||||||
"user" => UserRepresenter.to_map(user, opts),
|
"user" => UserRepresenter.to_map(user, opts),
|
||||||
@ -17,7 +25,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
|
|||||||
"created_at" => published,
|
"created_at" => published,
|
||||||
"in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
|
"in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
|
||||||
"statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
|
"statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
|
||||||
"attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts)
|
"attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
|
||||||
|
"attentions" => attentions
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -13,16 +13,28 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
context = ActivityPub.generate_context_id
|
context = ActivityPub.generate_context_id
|
||||||
|
|
||||||
|
content = HtmlSanitizeEx.strip_tags(data["status"])
|
||||||
|
|
||||||
|
mentions = parse_mentions(content)
|
||||||
|
|
||||||
|
default_to = [
|
||||||
|
User.ap_followers(user),
|
||||||
|
"https://www.w3.org/ns/activitystreams#Public"
|
||||||
|
]
|
||||||
|
|
||||||
|
to = default_to ++ Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
|
||||||
|
|
||||||
|
content_html = add_user_links(content, mentions)
|
||||||
|
|
||||||
activity = %{
|
activity = %{
|
||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
"to" => [
|
"to" => to,
|
||||||
User.ap_followers(user),
|
"actor" => user.ap_id,
|
||||||
"https://www.w3.org/ns/activitystreams#Public"
|
|
||||||
],
|
|
||||||
"actor" => User.ap_id(user),
|
|
||||||
"object" => %{
|
"object" => %{
|
||||||
"type" => "Note",
|
"type" => "Note",
|
||||||
"content" => data["status"],
|
"to" => to,
|
||||||
|
"content" => content_html,
|
||||||
"published" => date,
|
"published" => date,
|
||||||
"context" => context,
|
"context" => context,
|
||||||
"attachment" => attachments
|
"attachment" => attachments
|
||||||
@ -36,7 +48,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||||||
inReplyTo <- Repo.get(Activity, inReplyToId),
|
inReplyTo <- Repo.get(Activity, inReplyToId),
|
||||||
context <- inReplyTo.data["context"]
|
context <- inReplyTo.data["context"]
|
||||||
do
|
do
|
||||||
|
|
||||||
|
to = activity["to"] ++ [inReplyTo.data["actor"]]
|
||||||
|
|
||||||
activity
|
activity
|
||||||
|
|> put_in(["to"], to)
|
||||||
|> put_in(["context"], context)
|
|> put_in(["context"], context)
|
||||||
|> put_in(["object", "context"], context)
|
|> put_in(["object", "context"], context)
|
||||||
|> put_in(["object", "inReplyTo"], inReplyTo.data["object"]["id"])
|
|> put_in(["object", "inReplyTo"], inReplyTo.data["object"]["id"])
|
||||||
@ -74,7 +90,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||||||
do
|
do
|
||||||
statuses
|
statuses
|
||||||
else e ->
|
else e ->
|
||||||
IO.inspect(e)
|
IO.inspect(e)
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -122,6 +138,21 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parse_mentions(text) do
|
||||||
|
# Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
|
||||||
|
regex = ~r/@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/
|
||||||
|
|
||||||
|
Regex.scan(regex, text)
|
||||||
|
|> List.flatten
|
||||||
|
|> Enum.uniq
|
||||||
|
|> Enum.map(fn ("@" <> match = full_match) -> {full_match, Repo.get_by(User, nickname: match)} end)
|
||||||
|
|> Enum.filter(fn ({_match, user}) -> user end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_user_links(text, mentions) do
|
||||||
|
Enum.reduce(mentions, text, fn ({match, %User{ap_id: ap_id}}, text) -> String.replace(text, match, "<a href='#{ap_id}'>#{match}</a>") end)
|
||||||
|
end
|
||||||
|
|
||||||
defp add_conversation_id(activity) do
|
defp add_conversation_id(activity) do
|
||||||
if is_integer(activity.data["statusnetConversationId"]) do
|
if is_integer(activity.data["statusnetConversationId"]) do
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
@ -144,6 +175,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||||||
defp activity_to_status(activity, opts) do
|
defp activity_to_status(activity, opts) do
|
||||||
actor = get_in(activity.data, ["actor"])
|
actor = get_in(activity.data, ["actor"])
|
||||||
user = Repo.get_by!(User, ap_id: actor)
|
user = Repo.get_by!(User, ap_id: actor)
|
||||||
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
|
mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
|
||||||
|
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,6 +6,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||||||
|
|
||||||
test "an activity" do
|
test "an activity" do
|
||||||
{:ok, user} = UserBuilder.insert
|
{:ok, user} = UserBuilder.insert
|
||||||
|
{:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
|
||||||
{:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
|
{:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
|
||||||
|
|
||||||
object = %Object{
|
object = %Object{
|
||||||
@ -22,7 +23,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
content = "Some content"
|
content = "Some content mentioning @shp"
|
||||||
date = DateTime.utc_now() |> DateTime.to_iso8601
|
date = DateTime.utc_now() |> DateTime.to_iso8601
|
||||||
|
|
||||||
activity = %Activity{
|
activity = %Activity{
|
||||||
@ -31,7 +32,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
"to" => [
|
"to" => [
|
||||||
User.ap_followers(user),
|
User.ap_followers(user),
|
||||||
"https://www.w3.org/ns/activitystreams#Public"
|
"https://www.w3.org/ns/activitystreams#Public",
|
||||||
|
mentioned_user.ap_id
|
||||||
],
|
],
|
||||||
"actor" => User.ap_id(user),
|
"actor" => User.ap_id(user),
|
||||||
"object" => %{
|
"object" => %{
|
||||||
@ -62,9 +64,12 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||||||
"statusnet_conversation_id" => 4711,
|
"statusnet_conversation_id" => 4711,
|
||||||
"attachments" => [
|
"attachments" => [
|
||||||
ObjectRepresenter.to_map(object)
|
ObjectRepresenter.to_map(object)
|
||||||
|
],
|
||||||
|
"attentions" => [
|
||||||
|
UserRepresenter.to_map(mentioned_user, %{for: follower})
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
assert ActivityRepresenter.to_map(activity, %{user: user, for: follower}) == expected_status
|
assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,7 +6,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||||||
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
||||||
|
|
||||||
test "create a status" do
|
test "create a status" do
|
||||||
user = UserBuilder.build
|
user = UserBuilder.build(%{ap_id: "142344"})
|
||||||
|
_mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
|
||||||
|
|
||||||
object_data = %{
|
object_data = %{
|
||||||
"type" => "Image",
|
"type" => "Image",
|
||||||
"url" => [
|
"url" => [
|
||||||
@ -22,17 +24,18 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||||||
object = Repo.insert!(%Object{data: object_data})
|
object = Repo.insert!(%Object{data: object_data})
|
||||||
|
|
||||||
input = %{
|
input = %{
|
||||||
"status" => "Hello again.",
|
"status" => "Hello again, @shp.<script></script>",
|
||||||
"media_ids" => [object.id]
|
"media_ids" => [object.id]
|
||||||
}
|
}
|
||||||
|
|
||||||
{ :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
|
{ :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
|
||||||
|
|
||||||
assert get_in(activity.data, ["object", "content"]) == "Hello again."
|
assert get_in(activity.data, ["object", "content"]) == "Hello again, <a href='shp'>@shp</a>."
|
||||||
assert get_in(activity.data, ["object", "type"]) == "Note"
|
assert get_in(activity.data, ["object", "type"]) == "Note"
|
||||||
assert get_in(activity.data, ["actor"]) == User.ap_id(user)
|
assert get_in(activity.data, ["actor"]) == user.ap_id
|
||||||
assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user))
|
assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user))
|
||||||
assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
|
assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
|
||||||
|
assert Enum.member?(get_in(activity.data, ["to"]), "shp")
|
||||||
|
|
||||||
# Add a context + 'statusnet_conversation_id'
|
# Add a context + 'statusnet_conversation_id'
|
||||||
assert is_binary(get_in(activity.data, ["context"]))
|
assert is_binary(get_in(activity.data, ["context"]))
|
||||||
@ -44,7 +47,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "create a status that is a reply" do
|
test "create a status that is a reply" do
|
||||||
user = UserBuilder.build
|
user = UserBuilder.build(%{ap_id: "some_cool_id"})
|
||||||
input = %{
|
input = %{
|
||||||
"status" => "Hello again."
|
"status" => "Hello again."
|
||||||
}
|
}
|
||||||
@ -64,6 +67,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||||||
assert get_in(reply.data, ["object", "statusnetConversationId"]) == get_in(activity.data, ["object", "statusnetConversationId"])
|
assert get_in(reply.data, ["object", "statusnetConversationId"]) == get_in(activity.data, ["object", "statusnetConversationId"])
|
||||||
assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
|
assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
|
||||||
assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
|
assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
|
||||||
|
assert Enum.member?(get_in(reply.data, ["to"]), "some_cool_id")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "fetch public statuses" do
|
test "fetch public statuses" do
|
||||||
@ -141,4 +145,30 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||||||
|
|
||||||
assert is_binary(response)
|
assert is_binary(response)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it can parse mentions and return the relevant users" do
|
||||||
|
text = "@gsimg According to @archaeme , that is @daggsy."
|
||||||
|
|
||||||
|
{:ok, gsimg} = UserBuilder.insert(%{nickname: "gsimg"})
|
||||||
|
{:ok, archaeme} = UserBuilder.insert(%{nickname: "archaeme"})
|
||||||
|
|
||||||
|
expected_result = [
|
||||||
|
{"@gsimg", gsimg},
|
||||||
|
{"@archaeme", archaeme}
|
||||||
|
]
|
||||||
|
|
||||||
|
assert TwitterAPI.parse_mentions(text) == expected_result
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it adds user links to an existing text" do
|
||||||
|
text = "@gsimg According to @archaeme , that is @daggsy."
|
||||||
|
|
||||||
|
{:ok, _gsimg} = UserBuilder.insert(%{nickname: "gsimg", ap_id: "first_link" })
|
||||||
|
{:ok, _archaeme} = UserBuilder.insert(%{nickname: "archaeme", ap_id: "second_link"})
|
||||||
|
|
||||||
|
mentions = TwitterAPI.parse_mentions(text)
|
||||||
|
expected_text = "<a href='first_link'>@gsimg</a> According to <a href='second_link'>@archaeme</a> , that is @daggsy."
|
||||||
|
|
||||||
|
assert TwitterAPI.add_user_links(text, mentions) == expected_text
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user