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
25 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
29 test "has an emoji reaction list" do
31 other_user = insert(:user)
32 third_user = insert(:user)
33 {:ok, activity} = CommonAPI.post(user, %{"status" => "dae cofe??"})
35 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "☕")
36 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, third_user, "🍵")
37 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
38 activity = Repo.get(Activity, activity.id)
39 status = StatusView.render("show.json", activity: activity)
41 assert status[:pleroma][:emoji_reactions] == [
42 %{name: "☕", count: 2, me: false},
43 %{name: "🍵", count: 1, me: false}
46 status = StatusView.render("show.json", activity: activity, for: user)
48 assert status[:pleroma][:emoji_reactions] == [
49 %{name: "☕", count: 2, me: true},
50 %{name: "🍵", count: 1, me: false}
54 test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
57 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
58 [participation] = Participation.for_user(user)
61 StatusView.render("show.json",
63 with_direct_conversation_id: true,
67 assert status[:pleroma][:direct_conversation_id] == participation.id
69 status = StatusView.render("show.json", activity: activity, for: user)
70 assert status[:pleroma][:direct_conversation_id] == nil
73 test "returns the direct conversation id when given the `direct_conversation_id` option" do
76 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
77 [participation] = Participation.for_user(user)
80 StatusView.render("show.json",
82 direct_conversation_id: participation.id,
86 assert status[:pleroma][:direct_conversation_id] == participation.id
89 test "returns a temporary ap_id based user for activities missing db users" do
92 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
95 Cachex.clear(:user_cache)
97 %{account: ms_user} = StatusView.render("show.json", activity: activity)
99 assert ms_user.acct == "erroruser@example.com"
102 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
105 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
109 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
112 Cachex.clear(:user_cache)
114 result = StatusView.render("show.json", activity: activity)
116 assert result[:account][:id] == to_string(user.id)
119 test "a note with null content" do
120 note = insert(:note_activity)
121 note_object = Object.normalize(note)
125 |> Map.put("content", nil)
127 Object.change(note_object, %{data: data})
128 |> Object.update_and_set_cache()
130 User.get_cached_by_ap_id(note.data["actor"])
132 status = StatusView.render("show.json", %{activity: note})
134 assert status.content == ""
137 test "a note activity" do
138 note = insert(:note_activity)
139 object_data = Object.normalize(note).data
140 user = User.get_cached_by_ap_id(note.data["actor"])
142 convo_id = Utils.context_to_conversation_id(object_data["context"])
144 status = StatusView.render("show.json", %{activity: note})
147 (object_data["published"] || "")
148 |> String.replace(~r/\.\d+Z/, ".000Z")
151 id: to_string(note.id),
152 uri: object_data["id"],
153 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
154 account: AccountView.render("show.json", %{user: user}),
156 in_reply_to_account_id: nil,
159 content: HTML.filter_tags(object_data["content"]),
160 created_at: created_at,
171 spoiler_text: HTML.filter_tags(object_data["summary"]),
172 visibility: "public",
173 media_attachments: [],
177 name: "#{object_data["tag"]}",
178 url: "/tag/#{object_data["tag"]}"
190 static_url: "corndog.png",
191 visible_in_picker: false
196 conversation_id: convo_id,
197 in_reply_to_account_acct: nil,
198 content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
199 spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
201 direct_conversation_id: nil,
207 assert status == expected
210 test "tells if the message is muted for some reason" do
212 other_user = insert(:user)
214 {:ok, _user_relationships} = User.mute(user, other_user)
216 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
218 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
220 opts = %{activity: activity}
221 status = StatusView.render("show.json", opts)
222 assert status.muted == false
224 status = StatusView.render("show.json", Map.put(opts, :relationships, relationships_opt))
225 assert status.muted == false
227 for_opts = %{activity: activity, for: user}
228 status = StatusView.render("show.json", for_opts)
229 assert status.muted == true
231 status = StatusView.render("show.json", Map.put(for_opts, :relationships, relationships_opt))
232 assert status.muted == true
235 test "tells if the message is thread muted" do
237 other_user = insert(:user)
239 {:ok, _user_relationships} = User.mute(user, other_user)
241 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
242 status = StatusView.render("show.json", %{activity: activity, for: user})
244 assert status.pleroma.thread_muted == false
246 {:ok, activity} = CommonAPI.add_mute(user, activity)
248 status = StatusView.render("show.json", %{activity: activity, for: user})
250 assert status.pleroma.thread_muted == true
253 test "tells if the status is bookmarked" do
256 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
257 status = StatusView.render("show.json", %{activity: activity})
259 assert status.bookmarked == false
261 status = StatusView.render("show.json", %{activity: activity, for: user})
263 assert status.bookmarked == false
265 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
267 activity = Activity.get_by_id_with_object(activity.id)
269 status = StatusView.render("show.json", %{activity: activity, for: user})
271 assert status.bookmarked == true
275 note = insert(:note_activity)
279 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
281 status = StatusView.render("show.json", %{activity: activity})
283 assert status.in_reply_to_id == to_string(note.id)
285 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
287 assert status.in_reply_to_id == to_string(note.id)
290 test "contains mentions" do
292 mentioned = insert(:user)
294 {:ok, activity} = CommonAPI.post(user, %{"status" => "hi @#{mentioned.nickname}"})
296 status = StatusView.render("show.json", %{activity: activity})
298 assert status.mentions ==
299 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
302 test "create mentions from the 'to' field" do
303 %User{ap_id: recipient_ap_id} = insert(:user)
304 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
309 "to" => [recipient_ap_id],
315 insert(:note_activity, %{
317 recipients: [recipient_ap_id | cc]
320 assert length(activity.recipients) == 3
322 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
324 assert length(mentions) == 1
325 assert mention.url == recipient_ap_id
328 test "create mentions from the 'tag' field" do
329 recipient = insert(:user)
330 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
338 "href" => recipient.ap_id,
339 "name" => recipient.nickname,
343 "href" => "https://example.com/search?tag=test",
352 insert(:note_activity, %{
354 recipients: [recipient.ap_id | cc]
357 assert length(activity.recipients) == 3
359 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
361 assert length(mentions) == 1
362 assert mention.url == recipient.ap_id
365 test "attachments" do
370 "mediaType" => "image/png",
381 remote_url: "someurl",
382 preview_url: "someurl",
385 pleroma: %{mime_type: "image/png"}
388 assert expected == StatusView.render("attachment.json", %{attachment: object})
390 # If theres a "id", use that instead of the generated one
391 object = Map.put(object, "id", 2)
392 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
395 test "put the url advertised in the Activity in to the url attribute" do
396 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
397 [activity] = Activity.search(nil, id)
399 status = StatusView.render("show.json", %{activity: activity})
401 assert status.uri == id
402 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
407 activity = insert(:note_activity)
409 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
411 represented = StatusView.render("show.json", %{for: user, activity: reblog})
413 assert represented[:id] == to_string(reblog.id)
414 assert represented[:reblog][:id] == to_string(activity.id)
415 assert represented[:emojis] == []
418 test "a peertube video" do
422 Pleroma.Object.Fetcher.fetch_object_from_id(
423 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
426 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
428 represented = StatusView.render("show.json", %{for: user, activity: activity})
430 assert represented[:id] == to_string(activity.id)
431 assert length(represented[:media_attachments]) == 1
434 test "funkwhale audio" do
438 Pleroma.Object.Fetcher.fetch_object_from_id(
439 "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871"
442 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
444 represented = StatusView.render("show.json", %{for: user, activity: activity})
446 assert represented[:id] == to_string(activity.id)
447 assert length(represented[:media_attachments]) == 1
450 test "a Mobilizon event" do
454 Pleroma.Object.Fetcher.fetch_object_from_id(
455 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
458 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
460 represented = StatusView.render("show.json", %{for: user, activity: activity})
462 assert represented[:id] == to_string(activity.id)
465 describe "build_tags/1" do
466 test "it returns a a dictionary tags" do
472 "href" => "https://kawen.space/users/lain",
473 "name" => "@lain@kawen.space",
478 assert StatusView.build_tags(object_tags) == [
479 %{name: "fediverse", url: "/tag/fediverse"},
480 %{name: "mastodon", url: "/tag/mastodon"},
481 %{name: "nextcloud", url: "/tag/nextcloud"}
486 describe "rich media cards" do
487 test "a rich media card without a site name renders correctly" do
488 page_url = "http://example.com"
492 image: page_url <> "/example.jpg",
493 title: "Example website"
496 %{provider_name: "example.com"} =
497 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
500 test "a rich media card without a site name or image renders correctly" do
501 page_url = "http://example.com"
505 title: "Example website"
508 %{provider_name: "example.com"} =
509 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
512 test "a rich media card without an image renders correctly" do
513 page_url = "http://example.com"
517 site_name: "Example site name",
518 title: "Example website"
521 %{provider_name: "example.com"} =
522 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
525 test "a rich media card with all relevant data renders correctly" do
526 page_url = "http://example.com"
530 site_name: "Example site name",
531 title: "Example website",
532 image: page_url <> "/example.jpg",
533 description: "Example description"
536 %{provider_name: "example.com"} =
537 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
541 test "embeds a relationship in the account" do
543 other_user = insert(:user)
546 CommonAPI.post(user, %{
547 "status" => "drink more water"
550 result = StatusView.render("show.json", %{activity: activity, for: other_user})
552 assert result[:account][:pleroma][:relationship] ==
553 AccountView.render("relationship.json", %{user: other_user, target: user})
556 test "embeds a relationship in the account in reposts" do
558 other_user = insert(:user)
561 CommonAPI.post(user, %{
565 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
567 result = StatusView.render("show.json", %{activity: activity, for: user})
569 assert result[:account][:pleroma][:relationship] ==
570 AccountView.render("relationship.json", %{user: user, target: other_user})
572 assert result[:reblog][:account][:pleroma][:relationship] ==
573 AccountView.render("relationship.json", %{user: user, target: user})
576 test "visibility/list" do
579 {:ok, list} = Pleroma.List.create("foo", user)
582 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
584 status = StatusView.render("show.json", activity: activity)
586 assert status.visibility == "list"
589 test "successfully renders a Listen activity (pleroma extension)" do
590 listen_activity = insert(:listen)
592 status = StatusView.render("listen.json", activity: listen_activity)
594 assert status.length == listen_activity.data["object"]["length"]
595 assert status.title == listen_activity.data["object"]["title"]