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