2018-12-23 21:11:29 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2019-01-09 13:54:19 +01:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:11:29 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-03-30 12:57:55 +02:00
|
|
|
defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
|
2019-03-05 03:52:23 +01:00
|
|
|
alias Pleroma.Activity
|
2019-04-17 11:22:32 +02:00
|
|
|
alias Pleroma.Object
|
2019-03-05 03:52:23 +01:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2018-03-30 12:57:55 +02:00
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
alias Pleroma.Web.CommonAPI.Utils
|
|
|
|
alias Pleroma.Web.TwitterAPI.ActivityView
|
|
|
|
alias Pleroma.Web.TwitterAPI.UserView
|
2018-04-02 14:46:56 +02:00
|
|
|
|
2018-03-30 12:57:55 +02:00
|
|
|
import Pleroma.Factory
|
2018-12-23 14:55:08 +01:00
|
|
|
import Tesla.Mock
|
|
|
|
|
|
|
|
setup do
|
|
|
|
mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2018-04-02 14:46:56 +02:00
|
|
|
import Mock
|
2018-03-30 12:57:55 +02:00
|
|
|
|
2019-01-07 20:59:30 +01:00
|
|
|
test "returns a temporary ap_id based user for activities missing db users" do
|
2019-01-07 12:41:31 +01:00
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
|
|
|
|
|
|
|
|
Repo.delete(user)
|
|
|
|
Cachex.clear(:user_cache)
|
|
|
|
|
2019-01-07 20:59:30 +01:00
|
|
|
%{"user" => tw_user} = ActivityView.render("activity.json", activity: activity)
|
|
|
|
|
|
|
|
assert tw_user["screen_name"] == "erroruser@example.com"
|
|
|
|
assert tw_user["name"] == user.ap_id
|
|
|
|
assert tw_user["statusnet_profile_url"] == user.ap_id
|
2019-01-07 12:41:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "tries to get a user by nickname if fetching by ap_id doesn't work" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
|
|
|
|
|
|
|
|
{:ok, user} =
|
|
|
|
user
|
|
|
|
|> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
|
|
|
|
|> Repo.update()
|
|
|
|
|
|
|
|
Cachex.clear(:user_cache)
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: activity)
|
|
|
|
assert result["user"]["id"] == user.id
|
|
|
|
end
|
|
|
|
|
2019-02-27 16:52:03 +01:00
|
|
|
test "tells if the message is muted for some reason" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, user} = User.mute(user, other_user)
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
|
|
|
|
status = ActivityView.render("activity.json", %{activity: activity})
|
|
|
|
|
|
|
|
assert status["muted"] == false
|
|
|
|
|
|
|
|
status = ActivityView.render("activity.json", %{activity: activity, for: user})
|
|
|
|
|
|
|
|
assert status["muted"] == true
|
|
|
|
end
|
|
|
|
|
2018-12-17 20:41:36 +01:00
|
|
|
test "a create activity with a html status" do
|
|
|
|
text = """
|
|
|
|
#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
|
|
|
|
"""
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: activity)
|
|
|
|
|
|
|
|
assert result["statusnet_html"] ==
|
2019-03-17 14:46:46 +01:00
|
|
|
"<a class=\"hashtag\" data-tag=\"bike\" href=\"http://localhost:4001/tag/bike\" rel=\"tag\">#Bike</a> log - Commute Tuesday<br /><a href=\"https://pla.bike/posts/20181211/\">https://pla.bike/posts/20181211/</a><br /><a class=\"hashtag\" data-tag=\"cycling\" href=\"http://localhost:4001/tag/cycling\" rel=\"tag\">#cycling</a> <a class=\"hashtag\" data-tag=\"chscycling\" href=\"http://localhost:4001/tag/chscycling\" rel=\"tag\">#CHScycling</a> <a class=\"hashtag\" data-tag=\"commute\" href=\"http://localhost:4001/tag/commute\" rel=\"tag\">#commute</a><br />MVIMG_20181211_054020.jpg"
|
2018-12-17 20:41:36 +01:00
|
|
|
|
|
|
|
assert result["text"] ==
|
|
|
|
"#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg"
|
|
|
|
end
|
|
|
|
|
2019-01-05 22:46:42 +01:00
|
|
|
test "a create activity with a summary containing emoji" do
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(insert(:user), %{
|
2019-04-18 21:04:37 +02:00
|
|
|
"spoiler_text" => ":firefox: meow",
|
2019-01-05 22:46:42 +01:00
|
|
|
"status" => "."
|
|
|
|
})
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: activity)
|
|
|
|
|
2019-04-18 21:04:37 +02:00
|
|
|
expected = ":firefox: meow"
|
2019-01-06 11:16:40 +01:00
|
|
|
|
|
|
|
expected_html =
|
2019-02-07 16:41:20 +01:00
|
|
|
"<img class=\"emoji\" alt=\"firefox\" title=\"firefox\" src=\"http://localhost:4001/emoji/Firefox.gif\" /> meow"
|
2019-01-05 22:46:42 +01:00
|
|
|
|
|
|
|
assert result["summary"] == expected
|
2019-01-06 11:16:40 +01:00
|
|
|
assert result["summary_html"] == expected_html
|
2019-01-05 22:46:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a create activity with a summary containing invalid HTML" do
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(insert(:user), %{
|
|
|
|
"spoiler_text" => "<span style=\"color: magenta; font-size: 32px;\">meow</span>",
|
|
|
|
"status" => "."
|
|
|
|
})
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: activity)
|
|
|
|
|
|
|
|
expected = "meow"
|
|
|
|
|
|
|
|
assert result["summary"] == expected
|
2019-01-06 11:16:40 +01:00
|
|
|
assert result["summary_html"] == expected
|
2019-01-05 22:46:42 +01:00
|
|
|
end
|
|
|
|
|
2018-03-30 12:57:55 +02:00
|
|
|
test "a create activity with a note" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user, %{nickname: "shp"})
|
|
|
|
|
2018-05-20 18:01:24 +02:00
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
|
2018-11-25 23:49:39 +01:00
|
|
|
object = Object.normalize(activity.data["object"])
|
2018-03-30 12:57:55 +02:00
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: activity)
|
|
|
|
|
2019-04-17 11:22:32 +02:00
|
|
|
convo_id = Utils.context_to_conversation_id(object.data["context"])
|
2018-03-30 12:57:55 +02:00
|
|
|
|
|
|
|
expected = %{
|
|
|
|
"activity_type" => "post",
|
|
|
|
"attachments" => [],
|
|
|
|
"attentions" => [
|
|
|
|
UserView.render("show.json", %{user: other_user})
|
|
|
|
],
|
2018-11-25 23:49:39 +01:00
|
|
|
"created_at" => object.data["published"] |> Utils.date_to_asctime(),
|
|
|
|
"external_url" => object.data["id"],
|
2018-03-30 12:57:55 +02:00
|
|
|
"fave_num" => 0,
|
|
|
|
"favorited" => false,
|
|
|
|
"id" => activity.id,
|
|
|
|
"in_reply_to_status_id" => nil,
|
2018-10-25 05:03:44 +02:00
|
|
|
"in_reply_to_screen_name" => nil,
|
2018-10-25 06:06:42 +02:00
|
|
|
"in_reply_to_user_id" => nil,
|
|
|
|
"in_reply_to_profileurl" => nil,
|
|
|
|
"in_reply_to_ostatus_uri" => nil,
|
2018-03-30 12:57:55 +02:00
|
|
|
"is_local" => true,
|
|
|
|
"is_post_verb" => true,
|
|
|
|
"possibly_sensitive" => false,
|
|
|
|
"repeat_num" => 0,
|
|
|
|
"repeated" => false,
|
2019-01-09 13:54:19 +01:00
|
|
|
"pinned" => false,
|
2018-03-30 12:57:55 +02:00
|
|
|
"statusnet_conversation_id" => convo_id,
|
2019-01-05 19:20:42 +01:00
|
|
|
"summary" => "",
|
2019-01-06 11:16:40 +01:00
|
|
|
"summary_html" => "",
|
2018-03-30 12:57:55 +02:00
|
|
|
"statusnet_html" =>
|
2019-01-16 05:09:01 +01:00
|
|
|
"Hey <span class=\"h-card\"><a data-user=\"#{other_user.id}\" class=\"u-url mention\" href=\"#{
|
|
|
|
other_user.ap_id
|
|
|
|
}\">@<span>shp</span></a></span>!",
|
2018-03-30 12:57:55 +02:00
|
|
|
"tags" => [],
|
|
|
|
"text" => "Hey @shp!",
|
2018-11-25 23:49:39 +01:00
|
|
|
"uri" => object.data["id"],
|
2018-05-20 18:01:24 +02:00
|
|
|
"user" => UserView.render("show.json", %{user: user}),
|
2018-06-24 08:23:19 +02:00
|
|
|
"visibility" => "direct",
|
2019-02-27 16:52:03 +01:00
|
|
|
"card" => nil,
|
|
|
|
"muted" => false
|
2018-03-30 12:57:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert result == expected
|
|
|
|
end
|
2018-03-30 13:49:09 +02:00
|
|
|
|
2018-04-02 14:46:56 +02:00
|
|
|
test "a list of activities" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user, %{nickname: "shp"})
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
|
2018-11-25 23:49:39 +01:00
|
|
|
object = Object.normalize(activity.data["object"])
|
2018-04-02 14:46:56 +02:00
|
|
|
|
2019-04-17 11:22:32 +02:00
|
|
|
convo_id = Utils.context_to_conversation_id(object.data["context"])
|
2018-04-02 14:46:56 +02:00
|
|
|
|
|
|
|
mocks = [
|
|
|
|
{
|
2019-03-22 00:37:00 +01:00
|
|
|
Utils,
|
|
|
|
[:passthrough],
|
2018-04-02 16:27:36 +02:00
|
|
|
[context_to_conversation_id: fn _ -> false end]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
User,
|
|
|
|
[:passthrough],
|
|
|
|
[get_cached_by_ap_id: fn _ -> nil end]
|
2018-04-02 14:46:56 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
with_mocks mocks do
|
|
|
|
[result] = ActivityView.render("index.json", activities: [activity])
|
|
|
|
|
|
|
|
assert result["statusnet_conversation_id"] == convo_id
|
|
|
|
assert result["user"]
|
2019-03-22 00:17:53 +01:00
|
|
|
refute called(Utils.context_to_conversation_id(:_))
|
2018-04-02 16:27:36 +02:00
|
|
|
refute called(User.get_cached_by_ap_id(user.ap_id))
|
|
|
|
refute called(User.get_cached_by_ap_id(other_user.ap_id))
|
2018-04-02 14:46:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-30 13:49:09 +02:00
|
|
|
test "an activity that is a reply" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user, %{nickname: "shp"})
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
|
|
|
|
|
|
|
|
{:ok, answer} =
|
|
|
|
CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", %{activity: answer})
|
|
|
|
|
|
|
|
assert result["in_reply_to_status_id"] == activity.id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a like activity" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user, %{nickname: "shp"})
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
|
|
|
|
{:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: like)
|
2018-12-14 07:24:18 +01:00
|
|
|
activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
|
2018-03-30 13:49:09 +02:00
|
|
|
|
|
|
|
expected = %{
|
|
|
|
"activity_type" => "like",
|
|
|
|
"created_at" => like.data["published"] |> Utils.date_to_asctime(),
|
|
|
|
"external_url" => like.data["id"],
|
|
|
|
"id" => like.id,
|
|
|
|
"in_reply_to_status_id" => activity.id,
|
|
|
|
"is_local" => true,
|
2018-08-27 16:07:26 +02:00
|
|
|
"is_post_verb" => false,
|
2018-12-14 07:24:18 +01:00
|
|
|
"favorited_status" => ActivityView.render("activity.json", activity: activity),
|
2018-08-27 16:07:26 +02:00
|
|
|
"statusnet_html" => "shp favorited a status.",
|
|
|
|
"text" => "shp favorited a status.",
|
|
|
|
"uri" => "tag:#{like.data["id"]}:objectType=Favourite",
|
|
|
|
"user" => UserView.render("show.json", user: other_user)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result == expected
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a like activity for deleted post" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user, %{nickname: "shp"})
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
|
|
|
|
{:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
|
|
|
|
CommonAPI.delete(activity.id, user)
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: like)
|
|
|
|
|
|
|
|
expected = %{
|
|
|
|
"activity_type" => "like",
|
|
|
|
"created_at" => like.data["published"] |> Utils.date_to_asctime(),
|
|
|
|
"external_url" => like.data["id"],
|
|
|
|
"id" => like.id,
|
|
|
|
"in_reply_to_status_id" => nil,
|
|
|
|
"is_local" => true,
|
2018-03-30 13:49:09 +02:00
|
|
|
"is_post_verb" => false,
|
2018-12-14 07:24:18 +01:00
|
|
|
"favorited_status" => nil,
|
2018-03-30 13:49:09 +02:00
|
|
|
"statusnet_html" => "shp favorited a status.",
|
|
|
|
"text" => "shp favorited a status.",
|
|
|
|
"uri" => "tag:#{like.data["id"]}:objectType=Favourite",
|
|
|
|
"user" => UserView.render("show.json", user: other_user)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result == expected
|
|
|
|
end
|
2018-03-30 14:46:36 +02:00
|
|
|
|
|
|
|
test "an announce activity" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user, %{nickname: "shp"})
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
|
2018-11-25 23:49:39 +01:00
|
|
|
{:ok, announce, object} = CommonAPI.repeat(activity.id, other_user)
|
2018-03-30 14:46:36 +02:00
|
|
|
|
2019-04-17 11:22:32 +02:00
|
|
|
convo_id = Utils.context_to_conversation_id(object.data["context"])
|
2018-03-30 14:46:36 +02:00
|
|
|
|
2019-04-02 12:08:03 +02:00
|
|
|
activity = Activity.get_by_id(activity.id)
|
2018-03-30 14:46:36 +02:00
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: announce)
|
|
|
|
|
|
|
|
expected = %{
|
|
|
|
"activity_type" => "repeat",
|
|
|
|
"created_at" => announce.data["published"] |> Utils.date_to_asctime(),
|
|
|
|
"external_url" => announce.data["id"],
|
|
|
|
"id" => announce.id,
|
|
|
|
"is_local" => true,
|
|
|
|
"is_post_verb" => false,
|
2019-05-10 01:48:41 +02:00
|
|
|
"statusnet_html" => "shp repeated a status.",
|
|
|
|
"text" => "shp repeated a status.",
|
2018-03-30 14:46:36 +02:00
|
|
|
"uri" => "tag:#{announce.data["id"]}:objectType=note",
|
|
|
|
"user" => UserView.render("show.json", user: other_user),
|
|
|
|
"retweeted_status" => ActivityView.render("activity.json", activity: activity),
|
|
|
|
"statusnet_conversation_id" => convo_id
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result == expected
|
|
|
|
end
|
2018-03-30 15:17:13 +02:00
|
|
|
|
|
|
|
test "A follow activity" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user, %{nickname: "shp"})
|
|
|
|
|
|
|
|
{:ok, follower} = User.follow(user, other_user)
|
|
|
|
{:ok, follow} = ActivityPub.follow(follower, other_user)
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: follow)
|
|
|
|
|
|
|
|
expected = %{
|
|
|
|
"activity_type" => "follow",
|
|
|
|
"attentions" => [],
|
|
|
|
"created_at" => follow.data["published"] |> Utils.date_to_asctime(),
|
|
|
|
"external_url" => follow.data["id"],
|
|
|
|
"id" => follow.id,
|
|
|
|
"in_reply_to_status_id" => nil,
|
|
|
|
"is_local" => true,
|
|
|
|
"is_post_verb" => false,
|
|
|
|
"statusnet_html" => "#{user.nickname} started following shp",
|
|
|
|
"text" => "#{user.nickname} started following shp",
|
|
|
|
"user" => UserView.render("show.json", user: user)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result == expected
|
|
|
|
end
|
2018-03-30 15:32:43 +02:00
|
|
|
|
|
|
|
test "a delete activity" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
|
|
|
|
{:ok, delete} = CommonAPI.delete(activity.id, user)
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: delete)
|
|
|
|
|
|
|
|
expected = %{
|
|
|
|
"activity_type" => "delete",
|
|
|
|
"attentions" => [],
|
|
|
|
"created_at" => delete.data["published"] |> Utils.date_to_asctime(),
|
|
|
|
"external_url" => delete.data["id"],
|
|
|
|
"id" => delete.id,
|
|
|
|
"in_reply_to_status_id" => nil,
|
|
|
|
"is_local" => true,
|
|
|
|
"is_post_verb" => false,
|
|
|
|
"statusnet_html" => "deleted notice {{tag",
|
|
|
|
"text" => "deleted notice {{tag",
|
|
|
|
"uri" => delete.data["object"],
|
|
|
|
"user" => UserView.render("show.json", user: user)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result == expected
|
|
|
|
end
|
2018-12-23 14:59:06 +01:00
|
|
|
|
|
|
|
test "a peertube video" do
|
|
|
|
{:ok, object} =
|
2019-04-17 13:21:39 +02:00
|
|
|
Pleroma.Object.Fetcher.fetch_object_from_id(
|
2018-12-23 14:59:06 +01:00
|
|
|
"https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
|
|
|
|
)
|
|
|
|
|
2019-01-21 07:14:20 +01:00
|
|
|
%Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
|
2018-12-23 14:59:06 +01:00
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: activity)
|
|
|
|
|
|
|
|
assert length(result["attachments"]) == 1
|
|
|
|
assert result["summary"] == "Friday Night"
|
|
|
|
end
|
2019-04-24 03:21:17 +02:00
|
|
|
|
|
|
|
test "special characters are not escaped in text field for status created" do
|
|
|
|
text = "<3 is on the way"
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
|
|
|
|
|
|
|
|
result = ActivityView.render("activity.json", activity: activity)
|
|
|
|
|
|
|
|
assert result["text"] == text
|
|
|
|
end
|
2018-03-30 12:57:55 +02:00
|
|
|
end
|