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