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