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"]),
186 created_at: created_at,
197 spoiler_text: HTML.filter_tags(object_data["summary"]),
198 visibility: "public",
199 media_attachments: [],
203 name: "#{object_data["tag"]}",
204 url: "/tag/#{object_data["tag"]}"
216 static_url: "corndog.png",
217 visible_in_picker: false
222 conversation_id: convo_id,
223 in_reply_to_account_acct: nil,
224 content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
225 spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
227 direct_conversation_id: nil,
233 assert status == expected
234 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
237 test "tells if the message is muted for some reason" do
239 other_user = insert(:user)
241 {:ok, _user_relationships} = User.mute(user, other_user)
243 {:ok, activity} = CommonAPI.post(other_user, %{status: "test"})
245 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
247 opts = %{activity: activity}
248 status = StatusView.render("show.json", opts)
249 assert status.muted == false
250 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
252 status = StatusView.render("show.json", Map.put(opts, :relationships, relationships_opt))
253 assert status.muted == false
255 for_opts = %{activity: activity, for: user}
256 status = StatusView.render("show.json", for_opts)
257 assert status.muted == true
259 status = StatusView.render("show.json", Map.put(for_opts, :relationships, relationships_opt))
260 assert status.muted == true
261 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
264 test "tells if the message is thread muted" do
266 other_user = insert(:user)
268 {:ok, _user_relationships} = User.mute(user, other_user)
270 {:ok, activity} = CommonAPI.post(other_user, %{status: "test"})
271 status = StatusView.render("show.json", %{activity: activity, for: user})
273 assert status.pleroma.thread_muted == false
275 {:ok, activity} = CommonAPI.add_mute(user, activity)
277 status = StatusView.render("show.json", %{activity: activity, for: user})
279 assert status.pleroma.thread_muted == true
282 test "tells if the status is bookmarked" do
285 {:ok, activity} = CommonAPI.post(user, %{status: "Cute girls doing cute things"})
286 status = StatusView.render("show.json", %{activity: activity})
288 assert status.bookmarked == false
290 status = StatusView.render("show.json", %{activity: activity, for: user})
292 assert status.bookmarked == false
294 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
296 activity = Activity.get_by_id_with_object(activity.id)
298 status = StatusView.render("show.json", %{activity: activity, for: user})
300 assert status.bookmarked == true
304 note = insert(:note_activity)
307 {:ok, activity} = CommonAPI.post(user, %{status: "he", in_reply_to_status_id: note.id})
309 status = StatusView.render("show.json", %{activity: activity})
311 assert status.in_reply_to_id == to_string(note.id)
313 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
315 assert status.in_reply_to_id == to_string(note.id)
318 test "contains mentions" do
320 mentioned = insert(:user)
322 {:ok, activity} = CommonAPI.post(user, %{status: "hi @#{mentioned.nickname}"})
324 status = StatusView.render("show.json", %{activity: activity})
326 assert status.mentions ==
327 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
329 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
332 test "create mentions from the 'to' field" do
333 %User{ap_id: recipient_ap_id} = insert(:user)
334 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
339 "to" => [recipient_ap_id],
345 insert(:note_activity, %{
347 recipients: [recipient_ap_id | cc]
350 assert length(activity.recipients) == 3
352 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
354 assert length(mentions) == 1
355 assert mention.url == recipient_ap_id
358 test "create mentions from the 'tag' field" do
359 recipient = insert(:user)
360 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
368 "href" => recipient.ap_id,
369 "name" => recipient.nickname,
373 "href" => "https://example.com/search?tag=test",
382 insert(:note_activity, %{
384 recipients: [recipient.ap_id | cc]
387 assert length(activity.recipients) == 3
389 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
391 assert length(mentions) == 1
392 assert mention.url == recipient.ap_id
395 test "attachments" do
400 "mediaType" => "image/png",
411 remote_url: "someurl",
412 preview_url: "someurl",
415 pleroma: %{mime_type: "image/png"}
418 api_spec = Pleroma.Web.ApiSpec.spec()
420 assert expected == StatusView.render("attachment.json", %{attachment: object})
421 assert_schema(expected, "Attachment", api_spec)
423 # If theres a "id", use that instead of the generated one
424 object = Map.put(object, "id", 2)
425 result = StatusView.render("attachment.json", %{attachment: object})
427 assert %{id: "2"} = result
428 assert_schema(result, "Attachment", api_spec)
431 test "put the url advertised in the Activity in to the url attribute" do
432 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
433 [activity] = Activity.search(nil, id)
435 status = StatusView.render("show.json", %{activity: activity})
437 assert status.uri == id
438 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
443 activity = insert(:note_activity)
445 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
447 represented = StatusView.render("show.json", %{for: user, activity: reblog})
449 assert represented[:id] == to_string(reblog.id)
450 assert represented[:reblog][:id] == to_string(activity.id)
451 assert represented[:emojis] == []
452 assert_schema(represented, "Status", Pleroma.Web.ApiSpec.spec())
455 test "a peertube video" do
459 Pleroma.Object.Fetcher.fetch_object_from_id(
460 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
463 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
465 represented = StatusView.render("show.json", %{for: user, activity: activity})
467 assert represented[:id] == to_string(activity.id)
468 assert length(represented[:media_attachments]) == 1
469 assert_schema(represented, "Status", Pleroma.Web.ApiSpec.spec())
472 test "funkwhale audio" do
476 Pleroma.Object.Fetcher.fetch_object_from_id(
477 "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871"
480 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
482 represented = StatusView.render("show.json", %{for: user, activity: activity})
484 assert represented[:id] == to_string(activity.id)
485 assert length(represented[:media_attachments]) == 1
488 test "a Mobilizon event" do
492 Pleroma.Object.Fetcher.fetch_object_from_id(
493 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
496 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
498 represented = StatusView.render("show.json", %{for: user, activity: activity})
500 assert represented[:id] == to_string(activity.id)
503 describe "build_tags/1" do
504 test "it returns a a dictionary tags" do
510 "href" => "https://kawen.space/users/lain",
511 "name" => "@lain@kawen.space",
516 assert StatusView.build_tags(object_tags) == [
517 %{name: "fediverse", url: "/tag/fediverse"},
518 %{name: "mastodon", url: "/tag/mastodon"},
519 %{name: "nextcloud", url: "/tag/nextcloud"}
524 describe "rich media cards" do
525 test "a rich media card without a site name renders correctly" do
526 page_url = "http://example.com"
530 image: page_url <> "/example.jpg",
531 title: "Example website"
534 %{provider_name: "example.com"} =
535 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
538 test "a rich media card without a site name or image renders correctly" do
539 page_url = "http://example.com"
543 title: "Example website"
546 %{provider_name: "example.com"} =
547 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
550 test "a rich media card without an image renders correctly" do
551 page_url = "http://example.com"
555 site_name: "Example site name",
556 title: "Example website"
559 %{provider_name: "example.com"} =
560 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
563 test "a rich media card with all relevant data renders correctly" do
564 page_url = "http://example.com"
568 site_name: "Example site name",
569 title: "Example website",
570 image: page_url <> "/example.jpg",
571 description: "Example description"
574 %{provider_name: "example.com"} =
575 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
579 test "embeds a relationship in the account" do
581 other_user = insert(:user)
584 CommonAPI.post(user, %{
585 status: "drink more water"
588 result = StatusView.render("show.json", %{activity: activity, for: other_user})
590 assert result[:account][:pleroma][:relationship] ==
591 AccountView.render("relationship.json", %{user: other_user, target: user})
593 assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
596 test "embeds a relationship in the account in reposts" do
598 other_user = insert(:user)
601 CommonAPI.post(user, %{
605 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
607 result = StatusView.render("show.json", %{activity: activity, for: user})
609 assert result[:account][:pleroma][:relationship] ==
610 AccountView.render("relationship.json", %{user: user, target: other_user})
612 assert result[:reblog][:account][:pleroma][:relationship] ==
613 AccountView.render("relationship.json", %{user: user, target: user})
615 assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
618 test "visibility/list" do
621 {:ok, list} = Pleroma.List.create("foo", user)
623 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
625 status = StatusView.render("show.json", activity: activity)
627 assert status.visibility == "list"
630 test "successfully renders a Listen activity (pleroma extension)" do
631 listen_activity = insert(:listen)
633 status = StatusView.render("listen.json", activity: listen_activity)
635 assert status.length == listen_activity.data["object"]["length"]
636 assert status.title == listen_activity.data["object"]["title"]
637 assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())