Merge branch 'fix/dont-show-dms-in-mentions-timeline' into 'develop'
[akkoma] / test / web / mastodon_api / status_view_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.MastodonAPI.StatusViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Web.MastodonAPI.AccountView
9 alias Pleroma.Web.MastodonAPI.StatusView
10 alias Pleroma.User
11 alias Pleroma.Web.OStatus
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Activity
15 import Pleroma.Factory
16 import Tesla.Mock
17
18 setup do
19 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 :ok
21 end
22
23 test "returns a temporary ap_id based user for activities missing db users" do
24 user = insert(:user)
25
26 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
27
28 Repo.delete(user)
29 Cachex.clear(:user_cache)
30
31 %{account: ms_user} = StatusView.render("status.json", activity: activity)
32
33 assert ms_user.acct == "erroruser@example.com"
34 end
35
36 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
37 user = insert(:user)
38
39 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
40
41 {:ok, user} =
42 user
43 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
44 |> Repo.update()
45
46 Cachex.clear(:user_cache)
47
48 result = StatusView.render("status.json", activity: activity)
49
50 assert result[:account][:id] == to_string(user.id)
51 end
52
53 test "a note with null content" do
54 note = insert(:note_activity)
55
56 data =
57 note.data
58 |> put_in(["object", "content"], nil)
59
60 note =
61 note
62 |> Map.put(:data, data)
63
64 User.get_cached_by_ap_id(note.data["actor"])
65
66 status = StatusView.render("status.json", %{activity: note})
67
68 assert status.content == ""
69 end
70
71 test "a note activity" do
72 note = insert(:note_activity)
73 user = User.get_cached_by_ap_id(note.data["actor"])
74
75 status = StatusView.render("status.json", %{activity: note})
76
77 created_at =
78 (note.data["object"]["published"] || "")
79 |> String.replace(~r/\.\d+Z/, ".000Z")
80
81 expected = %{
82 id: to_string(note.id),
83 uri: note.data["object"]["id"],
84 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
85 account: AccountView.render("account.json", %{user: user}),
86 in_reply_to_id: nil,
87 in_reply_to_account_id: nil,
88 card: nil,
89 reblog: nil,
90 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
91 created_at: created_at,
92 reblogs_count: 0,
93 replies_count: 0,
94 favourites_count: 0,
95 reblogged: false,
96 bookmarked: false,
97 favourited: false,
98 muted: false,
99 pinned: false,
100 sensitive: false,
101 spoiler_text: note.data["object"]["summary"],
102 visibility: "public",
103 media_attachments: [],
104 mentions: [],
105 tags: [
106 %{
107 name: "#{note.data["object"]["tag"]}",
108 url: "/tag/#{note.data["object"]["tag"]}"
109 }
110 ],
111 application: %{
112 name: "Web",
113 website: nil
114 },
115 language: nil,
116 emojis: [
117 %{
118 shortcode: "2hu",
119 url: "corndog.png",
120 static_url: "corndog.png",
121 visible_in_picker: false
122 }
123 ]
124 }
125
126 assert status == expected
127 end
128
129 test "tells if the message is muted for some reason" do
130 user = insert(:user)
131 other_user = insert(:user)
132
133 {:ok, user} = User.mute(user, other_user)
134
135 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
136 status = StatusView.render("status.json", %{activity: activity})
137
138 assert status.muted == false
139
140 status = StatusView.render("status.json", %{activity: activity, for: user})
141
142 assert status.muted == true
143 end
144
145 test "a reply" do
146 note = insert(:note_activity)
147 user = insert(:user)
148
149 {:ok, activity} =
150 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
151
152 status = StatusView.render("status.json", %{activity: activity})
153
154 assert status.in_reply_to_id == to_string(note.id)
155
156 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
157
158 assert status.in_reply_to_id == to_string(note.id)
159 end
160
161 test "contains mentions" do
162 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
163 # a user with this ap id might be in the cache.
164 recipient = "https://pleroma.soykaf.com/users/lain"
165 user = insert(:user, %{ap_id: recipient})
166
167 {:ok, [activity]} = OStatus.handle_incoming(incoming)
168
169 status = StatusView.render("status.json", %{activity: activity})
170
171 actor = Repo.get_by(User, ap_id: activity.actor)
172
173 assert status.mentions ==
174 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
175 end
176
177 test "attachments" do
178 object = %{
179 "type" => "Image",
180 "url" => [
181 %{
182 "mediaType" => "image/png",
183 "href" => "someurl"
184 }
185 ],
186 "uuid" => 6
187 }
188
189 expected = %{
190 id: "1638338801",
191 type: "image",
192 url: "someurl",
193 remote_url: "someurl",
194 preview_url: "someurl",
195 text_url: "someurl",
196 description: nil
197 }
198
199 assert expected == StatusView.render("attachment.json", %{attachment: object})
200
201 # If theres a "id", use that instead of the generated one
202 object = Map.put(object, "id", 2)
203 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
204 end
205
206 test "a reblog" do
207 user = insert(:user)
208 activity = insert(:note_activity)
209
210 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
211
212 represented = StatusView.render("status.json", %{for: user, activity: reblog})
213
214 assert represented[:id] == to_string(reblog.id)
215 assert represented[:reblog][:id] == to_string(activity.id)
216 assert represented[:emojis] == []
217 end
218
219 test "a peertube video" do
220 user = insert(:user)
221
222 {:ok, object} =
223 ActivityPub.fetch_object_from_id(
224 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
225 )
226
227 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
228
229 represented = StatusView.render("status.json", %{for: user, activity: activity})
230
231 assert represented[:id] == to_string(activity.id)
232 assert length(represented[:media_attachments]) == 1
233 end
234
235 describe "build_tags/1" do
236 test "it returns a a dictionary tags" do
237 object_tags = [
238 "fediverse",
239 "mastodon",
240 "nextcloud",
241 %{
242 "href" => "https://kawen.space/users/lain",
243 "name" => "@lain@kawen.space",
244 "type" => "Mention"
245 }
246 ]
247
248 assert StatusView.build_tags(object_tags) == [
249 %{name: "fediverse", url: "/tag/fediverse"},
250 %{name: "mastodon", url: "/tag/mastodon"},
251 %{name: "nextcloud", url: "/tag/nextcloud"}
252 ]
253 end
254 end
255
256 describe "rich media cards" do
257 test "a rich media card without a site name renders correctly" do
258 page_url = "http://example.com"
259
260 card = %{
261 url: page_url,
262 image: page_url <> "/example.jpg",
263 title: "Example website"
264 }
265
266 %{provider_name: "example.com"} =
267 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
268 end
269
270 test "a rich media card without a site name or image renders correctly" do
271 page_url = "http://example.com"
272
273 card = %{
274 url: page_url,
275 title: "Example website"
276 }
277
278 %{provider_name: "example.com"} =
279 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
280 end
281
282 test "a rich media card without an image renders correctly" do
283 page_url = "http://example.com"
284
285 card = %{
286 url: page_url,
287 site_name: "Example site name",
288 title: "Example website"
289 }
290
291 %{provider_name: "Example site name"} =
292 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
293 end
294
295 test "a rich media card with all relevant data renders correctly" do
296 page_url = "http://example.com"
297
298 card = %{
299 url: page_url,
300 site_name: "Example site name",
301 title: "Example website",
302 image: page_url <> "/example.jpg",
303 description: "Example description"
304 }
305
306 %{provider_name: "Example site name"} =
307 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
308 end
309 end
310 end