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),
"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
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
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"])
do
statuses
else e ->
- IO.inspect(e)
+ IO.inspect(e)
[]
end
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
if is_integer(activity.data["statusnetConversationId"]) do
{:ok, activity}
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
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{
}
}
- content = "Some content"
+ content = "Some content mentioning @shp"
date = DateTime.utc_now() |> DateTime.to_iso8601
activity = %Activity{
"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" => %{
"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
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" => [
object = Repo.insert!(%Object{data: object_data})
input = %{
- "status" => "Hello again.",
+ "status" => "Hello again, @shp.<script></script>",
"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, <a href='shp'>@shp</a>."
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"]))
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."
}
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
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 = "<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