From a83fa053de4c95cab0b54f3b5fc9ee3622fca63b Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 3 Apr 2017 18:28:19 +0200 Subject: [PATCH] Parse mentions, save them, output them in TwAPI. --- .../representers/activity_representer.ex | 11 ++++- lib/pleroma/web/twitter_api/twitter_api.ex | 48 ++++++++++++++++++---- .../representers/activity_representer_test.exs | 11 +++-- test/web/twitter_api/twitter_api_test.exs | 40 +++++++++++++++--- 4 files changed, 93 insertions(+), 17 deletions(-) diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 0cf20dc45..32c6a48e9 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -6,6 +6,14 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do def to_map(%Activity{} = activity, %{user: user} = opts) do content = get_in(activity.data, ["object", "content"]) 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, "user" => UserRepresenter.to_map(user, opts), @@ -17,7 +25,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do "created_at" => published, "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"], "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 diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index c07c7cfbf..2aaa73b78 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -13,16 +13,28 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end) 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 = %{ "type" => "Create", - "to" => [ - User.ap_followers(user), - "https://www.w3.org/ns/activitystreams#Public" - ], - "actor" => User.ap_id(user), + "to" => to, + "actor" => user.ap_id, "object" => %{ "type" => "Note", - "content" => data["status"], + "to" => to, + "content" => content_html, "published" => date, "context" => context, "attachment" => attachments @@ -36,7 +48,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do inReplyTo <- Repo.get(Activity, inReplyToId), context <- inReplyTo.data["context"] do + + to = activity["to"] ++ [inReplyTo.data["actor"]] + activity + |> put_in(["to"], to) |> put_in(["context"], context) |> put_in(["object", "context"], context) |> put_in(["object", "inReplyTo"], inReplyTo.data["object"]["id"]) @@ -74,7 +90,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do do statuses else e -> - IO.inspect(e) + IO.inspect(e) [] end end @@ -122,6 +138,21 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do """ 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, "#{match}") end) + end + defp add_conversation_id(activity) do if is_integer(activity.data["statusnetConversationId"]) do {:ok, activity} @@ -144,6 +175,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do defp activity_to_status(activity, opts) do actor = get_in(activity.data, ["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 diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index 91300f229..256d920c0 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -6,6 +6,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do test "an activity" do {:ok, user} = UserBuilder.insert + {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"}) {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]}) 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 activity = %Activity{ @@ -31,7 +32,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "type" => "Create", "to" => [ 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), "object" => %{ @@ -62,9 +64,12 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "statusnet_conversation_id" => 4711, "attachments" => [ 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 diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 8d123ff4e..99b6a6cb2 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -6,7 +6,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter 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 = %{ "type" => "Image", "url" => [ @@ -22,17 +24,18 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do object = Repo.insert!(%Object{data: object_data}) input = %{ - "status" => "Hello again.", + "status" => "Hello again, @shp.", "media_ids" => [object.id] } { :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, @shp." 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"]), "https://www.w3.org/ns/activitystreams#Public") + assert Enum.member?(get_in(activity.data, ["to"]), "shp") # Add a context + 'statusnet_conversation_id' assert is_binary(get_in(activity.data, ["context"])) @@ -44,7 +47,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "create a status that is a reply" do - user = UserBuilder.build + user = UserBuilder.build(%{ap_id: "some_cool_id"}) input = %{ "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", "inReplyTo"]) == get_in(activity.data, ["object", "id"]) assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id + assert Enum.member?(get_in(reply.data, ["to"]), "some_cool_id") end test "fetch public statuses" do @@ -141,4 +145,30 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert is_binary(response) 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 = "@gsimg According to @archaeme , that is @daggsy." + + assert TwitterAPI.add_user_links(text, mentions) == expected_text + end end