Merge branch 'develop' into feature/activitypub
[akkoma] / test / web / twitter_api / representers / activity_representer_test.exs
1 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
2 use Pleroma.DataCase
3 alias Pleroma.{User, Activity, Object}
4 alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, ObjectRepresenter}
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Builders.UserBuilder
7 alias Pleroma.Web.TwitterAPI.UserView
8 import Pleroma.Factory
9
10 test "an announce activity" do
11 user = insert(:user)
12 note_activity = insert(:note_activity)
13 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
14 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
15
16 {:ok, announce_activity, _object} = ActivityPub.announce(user, object)
17 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
18
19 status = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user})
20
21 assert status["id"] == announce_activity.id
22 assert status["user"] == UserView.render("show.json", %{user: user, for: user})
23
24 retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
25 assert retweeted_status["repeated"] == true
26 assert retweeted_status["id"] == note_activity.id
27 assert status["statusnet_conversation_id"] == retweeted_status["statusnet_conversation_id"]
28
29 assert status["retweeted_status"] == retweeted_status
30 assert status["activity_type"] == "repeat"
31 end
32
33 test "a like activity" do
34 user = insert(:user)
35 note_activity = insert(:note_activity)
36 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
37
38 {:ok, like_activity, _object} = ActivityPub.like(user, object)
39 status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
40
41 assert status["id"] == like_activity.id
42 assert status["in_reply_to_status_id"] == note_activity.id
43
44 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
45 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
46 liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
47 assert liked_status["favorited"] == true
48 assert status["activity_type"] == "like"
49 end
50
51 test "an activity" do
52 {:ok, user} = UserBuilder.insert
53 # {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
54 mentioned_user = insert(:user, %{nickname: "shp"})
55
56 # {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
57 follower = insert(:user, %{following: [User.ap_followers(user)]})
58
59 object = %Object{
60 data: %{
61 "type" => "Image",
62 "url" => [
63 %{
64 "type" => "Link",
65 "mediaType" => "image/jpg",
66 "href" => "http://example.org/image.jpg"
67 }
68 ],
69 "uuid" => 1
70 }
71 }
72
73 content_html = "<script>alert('YAY')</script>Some :2hu: content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"
74 content = HtmlSanitizeEx.strip_tags(content_html)
75 date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601
76
77 {:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert
78
79 activity = %Activity{
80 id: 1,
81 data: %{
82 "type" => "Create",
83 "id" => "id",
84 "to" => [
85 User.ap_followers(user),
86 "https://www.w3.org/ns/activitystreams#Public",
87 mentioned_user.ap_id
88 ],
89 "actor" => User.ap_id(user),
90 "object" => %{
91 "published" => date,
92 "type" => "Note",
93 "content" => content_html,
94 "summary" => "2hu",
95 "inReplyToStatusId" => 213123,
96 "attachment" => [
97 object
98 ],
99 "external_url" => "some url",
100 "like_count" => 5,
101 "announcement_count" => 3,
102 "context" => "2hu",
103 "tag" => ["content", "mentioning", "nsfw"],
104 "emoji" => %{
105 "2hu" => "corndog.png"
106 }
107 },
108 "published" => date,
109 "context" => "2hu"
110 },
111 local: false
112 }
113
114 expected_html = "<span>2hu</span><br />alert('YAY')Some <img height='32px' width='32px' alt='2hu' title='2hu' src='corndog.png' /> content mentioning <a href=\"#{mentioned_user.ap_id}\">@shp</a>"
115
116 expected_status = %{
117 "id" => activity.id,
118 "user" => UserView.render("show.json", %{user: user, for: follower}),
119 "is_local" => false,
120 "statusnet_html" => expected_html,
121 "text" => "2hu" <> content,
122 "is_post_verb" => true,
123 "created_at" => "Tue May 24 13:26:08 +0000 2016",
124 "in_reply_to_status_id" => 213123,
125 "statusnet_conversation_id" => convo_object.id,
126 "attachments" => [
127 ObjectRepresenter.to_map(object)
128 ],
129 "attentions" => [
130 UserView.render("show.json", %{user: mentioned_user, for: follower})
131 ],
132 "fave_num" => 5,
133 "repeat_num" => 3,
134 "favorited" => false,
135 "repeated" => false,
136 "external_url" => "some url",
137 "tags" => ["content", "mentioning", "nsfw"],
138 "activity_type" => "post",
139 "possibly_sensitive" => true,
140 "uri" => activity.data["object"]["id"]
141 }
142
143 assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
144 end
145
146 test "an undo for a follow" do
147 follower = insert(:user)
148 followed = insert(:user)
149
150 {:ok, _follow} = ActivityPub.follow(follower, followed)
151 {:ok, unfollow} = ActivityPub.unfollow(follower, followed)
152
153 map = ActivityRepresenter.to_map(unfollow, %{user: follower})
154 assert map["is_post_verb"] == false
155 assert map["activity_type"] == "undo"
156 end
157
158 test "a delete activity" do
159 object = insert(:note)
160 user = User.get_by_ap_id(object.data["actor"])
161
162 {:ok, delete} = ActivityPub.delete(object)
163
164 map = ActivityRepresenter.to_map(delete, %{user: user})
165
166 assert map["is_post_verb"] == false
167 assert map["activity_type"] == "delete"
168 assert map["uri"] == object.data["id"]
169 end
170 end