1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
10 alias Pleroma.Conversation.Participation
15 alias Pleroma.UserRelationship
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.CommonAPI.Utils
18 alias Pleroma.Web.MastodonAPI.AccountView
19 alias Pleroma.Web.MastodonAPI.StatusView
21 import Pleroma.Factory
23 import OpenApiSpex.TestAssertions
26 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
30 test "has an emoji reaction list" do
32 other_user = insert(:user)
33 third_user = insert(:user)
34 {:ok, activity} = CommonAPI.post(user, %{status: "dae cofe??"})
36 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "☕")
37 {:ok, _} = CommonAPI.react_with_emoji(activity.id, third_user, "🍵")
38 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
39 activity = Repo.get(Activity, activity.id)
40 status = StatusView.render("show.json", activity: activity)
42 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
44 assert status[:pleroma][:emoji_reactions] == [
45 %{name: "☕", count: 2, me: false},
46 %{name: "🍵", count: 1, me: false}
49 status = StatusView.render("show.json", activity: activity, for: user)
51 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
53 assert status[:pleroma][:emoji_reactions] == [
54 %{name: "☕", count: 2, me: true},
55 %{name: "🍵", count: 1, me: false}
59 test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
62 {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
63 [participation] = Participation.for_user(user)
66 StatusView.render("show.json",
68 with_direct_conversation_id: true,
72 assert status[:pleroma][:direct_conversation_id] == participation.id
74 status = StatusView.render("show.json", activity: activity, for: user)
75 assert status[:pleroma][:direct_conversation_id] == nil
76 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
79 test "returns the direct conversation id when given the `direct_conversation_id` option" do
82 {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
83 [participation] = Participation.for_user(user)
86 StatusView.render("show.json",
88 direct_conversation_id: participation.id,
92 assert status[:pleroma][:direct_conversation_id] == participation.id
93 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
96 test "returns a temporary ap_id based user for activities missing db users" do
99 {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
102 Cachex.clear(:user_cache)
105 "https://localhost/.well-known/webfinger?resource=acct:#{user.nickname}@localhost"
107 Tesla.Mock.mock_global(fn
108 %{method: :get, url: "http://localhost/.well-known/host-meta"} ->
109 %Tesla.Env{status: 404, body: ""}
111 %{method: :get, url: "https://localhost/.well-known/host-meta"} ->
112 %Tesla.Env{status: 404, body: ""}
118 %Tesla.Env{status: 404, body: ""}
121 %{account: ms_user} = StatusView.render("show.json", activity: activity)
123 assert ms_user.acct == "erroruser@example.com"
126 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
129 {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
133 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
136 Cachex.clear(:user_cache)
138 result = StatusView.render("show.json", activity: activity)
140 assert result[:account][:id] == to_string(user.id)
141 assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
144 test "a note with null content" do
145 note = insert(:note_activity)
146 note_object = Object.normalize(note)
150 |> Map.put("content", nil)
152 Object.change(note_object, %{data: data})
153 |> Object.update_and_set_cache()
155 User.get_cached_by_ap_id(note.data["actor"])
157 status = StatusView.render("show.json", %{activity: note})
159 assert status.content == ""
160 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
163 test "a note activity" do
164 note = insert(:note_activity)
165 object_data = Object.normalize(note).data
166 user = User.get_cached_by_ap_id(note.data["actor"])
168 convo_id = Utils.context_to_conversation_id(object_data["context"])
170 status = StatusView.render("show.json", %{activity: note})
173 (object_data["published"] || "")
174 |> String.replace(~r/\.\d+Z/, ".000Z")
177 id: to_string(note.id),
178 uri: object_data["id"],
179 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
180 account: AccountView.render("show.json", %{user: user}),
182 in_reply_to_account_id: nil,
185 content: HTML.filter_tags(object_data["content"]),
187 created_at: created_at,
198 spoiler_text: HTML.filter_tags(object_data["summary"]),
199 visibility: "public",
200 media_attachments: [],
204 name: "#{object_data["tag"]}",
205 url: "/tag/#{object_data["tag"]}"
217 static_url: "corndog.png",
218 visible_in_picker: false
223 conversation_id: convo_id,
224 in_reply_to_account_acct: nil,
225 content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
226 spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
228 direct_conversation_id: nil,
231 parent_visible: false
235 assert status == expected
236 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
239 test "tells if the message is muted for some reason" do
241 other_user = insert(:user)
243 {:ok, _user_relationships} = User.mute(user, other_user)
245 {:ok, activity} = CommonAPI.post(other_user, %{status: "test"})
247 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
249 opts = %{activity: activity}
250 status = StatusView.render("show.json", opts)
251 assert status.muted == false
252 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
254 status = StatusView.render("show.json", Map.put(opts, :relationships, relationships_opt))
255 assert status.muted == false
257 for_opts = %{activity: activity, for: user}
258 status = StatusView.render("show.json", for_opts)
259 assert status.muted == true
261 status = StatusView.render("show.json", Map.put(for_opts, :relationships, relationships_opt))
262 assert status.muted == true
263 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
266 test "tells if the message is thread muted" do
268 other_user = insert(:user)
270 {:ok, _user_relationships} = User.mute(user, other_user)
272 {:ok, activity} = CommonAPI.post(other_user, %{status: "test"})
273 status = StatusView.render("show.json", %{activity: activity, for: user})
275 assert status.pleroma.thread_muted == false
277 {:ok, activity} = CommonAPI.add_mute(user, activity)
279 status = StatusView.render("show.json", %{activity: activity, for: user})
281 assert status.pleroma.thread_muted == true
284 test "tells if the status is bookmarked" do
287 {:ok, activity} = CommonAPI.post(user, %{status: "Cute girls doing cute things"})
288 status = StatusView.render("show.json", %{activity: activity})
290 assert status.bookmarked == false
292 status = StatusView.render("show.json", %{activity: activity, for: user})
294 assert status.bookmarked == false
296 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
298 activity = Activity.get_by_id_with_object(activity.id)
300 status = StatusView.render("show.json", %{activity: activity, for: user})
302 assert status.bookmarked == true
306 note = insert(:note_activity)
309 {:ok, activity} = CommonAPI.post(user, %{status: "he", in_reply_to_status_id: note.id})
311 status = StatusView.render("show.json", %{activity: activity})
313 assert status.in_reply_to_id == to_string(note.id)
315 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
317 assert status.in_reply_to_id == to_string(note.id)
320 test "contains mentions" do
322 mentioned = insert(:user)
324 {:ok, activity} = CommonAPI.post(user, %{status: "hi @#{mentioned.nickname}"})
326 status = StatusView.render("show.json", %{activity: activity})
328 assert status.mentions ==
329 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
331 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
334 test "create mentions from the 'to' field" do
335 %User{ap_id: recipient_ap_id} = insert(:user)
336 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
341 "to" => [recipient_ap_id],
347 insert(:note_activity, %{
349 recipients: [recipient_ap_id | cc]
352 assert length(activity.recipients) == 3
354 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
356 assert length(mentions) == 1
357 assert mention.url == recipient_ap_id
360 test "create mentions from the 'tag' field" do
361 recipient = insert(:user)
362 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
370 "href" => recipient.ap_id,
371 "name" => recipient.nickname,
375 "href" => "https://example.com/search?tag=test",
384 insert(:note_activity, %{
386 recipients: [recipient.ap_id | cc]
389 assert length(activity.recipients) == 3
391 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
393 assert length(mentions) == 1
394 assert mention.url == recipient.ap_id
397 test "attachments" do
402 "mediaType" => "image/png",
413 remote_url: "someurl",
414 preview_url: "someurl",
417 pleroma: %{mime_type: "image/png"}
420 api_spec = Pleroma.Web.ApiSpec.spec()
422 assert expected == StatusView.render("attachment.json", %{attachment: object})
423 assert_schema(expected, "Attachment", api_spec)
425 # If theres a "id", use that instead of the generated one
426 object = Map.put(object, "id", 2)
427 result = StatusView.render("attachment.json", %{attachment: object})
429 assert %{id: "2"} = result
430 assert_schema(result, "Attachment", api_spec)
433 test "put the url advertised in the Activity in to the url attribute" do
434 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
435 [activity] = Activity.search(nil, id)
437 status = StatusView.render("show.json", %{activity: activity})
439 assert status.uri == id
440 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
445 activity = insert(:note_activity)
447 {:ok, reblog} = CommonAPI.repeat(activity.id, user)
449 represented = StatusView.render("show.json", %{for: user, activity: reblog})
451 assert represented[:id] == to_string(reblog.id)
452 assert represented[:reblog][:id] == to_string(activity.id)
453 assert represented[:emojis] == []
454 assert_schema(represented, "Status", Pleroma.Web.ApiSpec.spec())
457 test "a peertube video" do
461 Pleroma.Object.Fetcher.fetch_object_from_id(
462 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
465 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
467 represented = StatusView.render("show.json", %{for: user, activity: activity})
469 assert represented[:id] == to_string(activity.id)
470 assert length(represented[:media_attachments]) == 1
471 assert_schema(represented, "Status", Pleroma.Web.ApiSpec.spec())
474 test "funkwhale audio" do
478 Pleroma.Object.Fetcher.fetch_object_from_id(
479 "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871"
482 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
484 represented = StatusView.render("show.json", %{for: user, activity: activity})
486 assert represented[:id] == to_string(activity.id)
487 assert length(represented[:media_attachments]) == 1
490 test "a Mobilizon event" do
494 Pleroma.Object.Fetcher.fetch_object_from_id(
495 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
498 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
500 represented = StatusView.render("show.json", %{for: user, activity: activity})
502 assert represented[:id] == to_string(activity.id)
505 describe "build_tags/1" do
506 test "it returns a a dictionary tags" do
512 "href" => "https://kawen.space/users/lain",
513 "name" => "@lain@kawen.space",
518 assert StatusView.build_tags(object_tags) == [
519 %{name: "fediverse", url: "/tag/fediverse"},
520 %{name: "mastodon", url: "/tag/mastodon"},
521 %{name: "nextcloud", url: "/tag/nextcloud"}
526 describe "rich media cards" do
527 test "a rich media card without a site name renders correctly" do
528 page_url = "http://example.com"
532 image: page_url <> "/example.jpg",
533 title: "Example website"
536 %{provider_name: "example.com"} =
537 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
540 test "a rich media card without a site name or image renders correctly" do
541 page_url = "http://example.com"
545 title: "Example website"
548 %{provider_name: "example.com"} =
549 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
552 test "a rich media card without an image renders correctly" do
553 page_url = "http://example.com"
557 site_name: "Example site name",
558 title: "Example website"
561 %{provider_name: "example.com"} =
562 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
565 test "a rich media card with all relevant data renders correctly" do
566 page_url = "http://example.com"
570 site_name: "Example site name",
571 title: "Example website",
572 image: page_url <> "/example.jpg",
573 description: "Example description"
576 %{provider_name: "example.com"} =
577 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
581 test "does not embed a relationship in the account" do
583 other_user = insert(:user)
586 CommonAPI.post(user, %{
587 status: "drink more water"
590 result = StatusView.render("show.json", %{activity: activity, for: other_user})
592 assert result[:account][:pleroma][:relationship] == %{}
593 assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
596 test "does not embed a relationship in the account in reposts" do
598 other_user = insert(:user)
601 CommonAPI.post(user, %{
605 {:ok, activity} = CommonAPI.repeat(activity.id, other_user)
607 result = StatusView.render("show.json", %{activity: activity, for: user})
609 assert result[:account][:pleroma][:relationship] == %{}
610 assert result[:reblog][:account][:pleroma][:relationship] == %{}
611 assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
614 test "visibility/list" do
617 {:ok, list} = Pleroma.List.create("foo", user)
619 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
621 status = StatusView.render("show.json", activity: activity)
623 assert status.visibility == "list"
626 test "has a field for parent visibility" do
628 poster = insert(:user)
630 {:ok, invisible} = CommonAPI.post(poster, %{status: "hey", visibility: "private"})
633 CommonAPI.post(poster, %{status: "hey", visibility: "private", in_reply_to_id: invisible.id})
635 status = StatusView.render("show.json", activity: visible, for: user)
636 refute status.pleroma.parent_visible
638 status = StatusView.render("show.json", activity: visible, for: poster)
639 assert status.pleroma.parent_visible