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