MastoAPI StatusView: Add locality indicator.
[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 pleroma: %{
125 local: true
126 }
127 }
128
129 assert status == expected
130 end
131
132 test "tells if the message is muted for some reason" do
133 user = insert(:user)
134 other_user = insert(:user)
135
136 {:ok, user} = User.mute(user, other_user)
137
138 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
139 status = StatusView.render("status.json", %{activity: activity})
140
141 assert status.muted == false
142
143 status = StatusView.render("status.json", %{activity: activity, for: user})
144
145 assert status.muted == true
146 end
147
148 test "a reply" do
149 note = insert(:note_activity)
150 user = insert(:user)
151
152 {:ok, activity} =
153 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
154
155 status = StatusView.render("status.json", %{activity: activity})
156
157 assert status.in_reply_to_id == to_string(note.id)
158
159 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
160
161 assert status.in_reply_to_id == to_string(note.id)
162 end
163
164 test "contains mentions" do
165 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
166 # a user with this ap id might be in the cache.
167 recipient = "https://pleroma.soykaf.com/users/lain"
168 user = insert(:user, %{ap_id: recipient})
169
170 {:ok, [activity]} = OStatus.handle_incoming(incoming)
171
172 status = StatusView.render("status.json", %{activity: activity})
173
174 actor = Repo.get_by(User, ap_id: activity.actor)
175
176 assert status.mentions ==
177 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
178 end
179
180 test "attachments" do
181 object = %{
182 "type" => "Image",
183 "url" => [
184 %{
185 "mediaType" => "image/png",
186 "href" => "someurl"
187 }
188 ],
189 "uuid" => 6
190 }
191
192 expected = %{
193 id: "1638338801",
194 type: "image",
195 url: "someurl",
196 remote_url: "someurl",
197 preview_url: "someurl",
198 text_url: "someurl",
199 description: nil
200 }
201
202 assert expected == StatusView.render("attachment.json", %{attachment: object})
203
204 # If theres a "id", use that instead of the generated one
205 object = Map.put(object, "id", 2)
206 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
207 end
208
209 test "a reblog" do
210 user = insert(:user)
211 activity = insert(:note_activity)
212
213 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
214
215 represented = StatusView.render("status.json", %{for: user, activity: reblog})
216
217 assert represented[:id] == to_string(reblog.id)
218 assert represented[:reblog][:id] == to_string(activity.id)
219 assert represented[:emojis] == []
220 end
221
222 test "a peertube video" do
223 user = insert(:user)
224
225 {:ok, object} =
226 ActivityPub.fetch_object_from_id(
227 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
228 )
229
230 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
231
232 represented = StatusView.render("status.json", %{for: user, activity: activity})
233
234 assert represented[:id] == to_string(activity.id)
235 assert length(represented[:media_attachments]) == 1
236 end
237
238 describe "build_tags/1" do
239 test "it returns a a dictionary tags" do
240 object_tags = [
241 "fediverse",
242 "mastodon",
243 "nextcloud",
244 %{
245 "href" => "https://kawen.space/users/lain",
246 "name" => "@lain@kawen.space",
247 "type" => "Mention"
248 }
249 ]
250
251 assert StatusView.build_tags(object_tags) == [
252 %{name: "fediverse", url: "/tag/fediverse"},
253 %{name: "mastodon", url: "/tag/mastodon"},
254 %{name: "nextcloud", url: "/tag/nextcloud"}
255 ]
256 end
257 end
258
259 describe "rich media cards" do
260 test "a rich media card without a site name renders correctly" do
261 page_url = "http://example.com"
262
263 card = %{
264 url: page_url,
265 image: page_url <> "/example.jpg",
266 title: "Example website"
267 }
268
269 %{provider_name: "example.com"} =
270 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
271 end
272
273 test "a rich media card without a site name or image renders correctly" do
274 page_url = "http://example.com"
275
276 card = %{
277 url: page_url,
278 title: "Example website"
279 }
280
281 %{provider_name: "example.com"} =
282 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
283 end
284
285 test "a rich media card without an image renders correctly" do
286 page_url = "http://example.com"
287
288 card = %{
289 url: page_url,
290 site_name: "Example site name",
291 title: "Example website"
292 }
293
294 %{provider_name: "Example site name"} =
295 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
296 end
297
298 test "a rich media card with all relevant data renders correctly" do
299 page_url = "http://example.com"
300
301 card = %{
302 url: page_url,
303 site_name: "Example site name",
304 title: "Example website",
305 image: page_url <> "/example.jpg",
306 description: "Example description"
307 }
308
309 %{provider_name: "Example site name"} =
310 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
311 end
312 end
313 end