1 # Pleroma: A lightweight social networking server
2 # Copyright Ā© 2017-2019 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.Web.CommonAPI
16 alias Pleroma.Web.CommonAPI.Utils
17 alias Pleroma.Web.MastodonAPI.AccountView
18 alias Pleroma.Web.MastodonAPI.StatusView
19 import Pleroma.Factory
23 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 test "has an emoji reaction list" do
29 other_user = insert(:user)
30 third_user = insert(:user)
31 {:ok, activity} = CommonAPI.post(user, %{"status" => "dae cofe??"})
33 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, user, "ā")
34 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "ā")
35 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, third_user, "šµ")
36 activity = Repo.get(Activity, activity.id)
37 status = StatusView.render("show.json", activity: activity)
39 assert status[:pleroma][:emoji_reactions]["šµ"] == 1
40 assert status[:pleroma][:emoji_reactions]["ā"] == 2
43 test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
46 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
47 [participation] = Participation.for_user(user)
50 StatusView.render("show.json",
52 with_direct_conversation_id: true,
56 assert status[:pleroma][:direct_conversation_id] == participation.id
58 status = StatusView.render("show.json", activity: activity, for: user)
59 assert status[:pleroma][:direct_conversation_id] == nil
62 test "returns the direct conversation id when given the `direct_conversation_id` option" do
65 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
66 [participation] = Participation.for_user(user)
69 StatusView.render("show.json",
71 direct_conversation_id: participation.id,
75 assert status[:pleroma][:direct_conversation_id] == participation.id
78 test "returns a temporary ap_id based user for activities missing db users" do
81 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
84 Cachex.clear(:user_cache)
86 %{account: ms_user} = StatusView.render("show.json", activity: activity)
88 assert ms_user.acct == "erroruser@example.com"
91 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
94 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
98 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
101 Cachex.clear(:user_cache)
103 result = StatusView.render("show.json", activity: activity)
105 assert result[:account][:id] == to_string(user.id)
108 test "a note with null content" do
109 note = insert(:note_activity)
110 note_object = Object.normalize(note)
114 |> Map.put("content", nil)
116 Object.change(note_object, %{data: data})
117 |> Object.update_and_set_cache()
119 User.get_cached_by_ap_id(note.data["actor"])
121 status = StatusView.render("show.json", %{activity: note})
123 assert status.content == ""
126 test "a note activity" do
127 note = insert(:note_activity)
128 object_data = Object.normalize(note).data
129 user = User.get_cached_by_ap_id(note.data["actor"])
131 convo_id = Utils.context_to_conversation_id(object_data["context"])
133 status = StatusView.render("show.json", %{activity: note})
136 (object_data["published"] || "")
137 |> String.replace(~r/\.\d+Z/, ".000Z")
140 id: to_string(note.id),
141 uri: object_data["id"],
142 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
143 account: AccountView.render("show.json", %{user: user}),
145 in_reply_to_account_id: nil,
148 content: HTML.filter_tags(object_data["content"]),
149 created_at: created_at,
160 spoiler_text: HTML.filter_tags(object_data["summary"]),
161 visibility: "public",
162 media_attachments: [],
166 name: "#{object_data["tag"]}",
167 url: "/tag/#{object_data["tag"]}"
179 static_url: "corndog.png",
180 visible_in_picker: false
185 conversation_id: convo_id,
186 in_reply_to_account_acct: nil,
187 content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
188 spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
190 direct_conversation_id: nil,
196 assert status == expected
199 test "tells if the message is muted for some reason" do
201 other_user = insert(:user)
203 {:ok, _user_relationships} = User.mute(user, other_user)
205 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
206 status = StatusView.render("show.json", %{activity: activity})
208 assert status.muted == false
210 status = StatusView.render("show.json", %{activity: activity, for: user})
212 assert status.muted == true
215 test "tells if the message is thread muted" do
217 other_user = insert(:user)
219 {:ok, _user_relationships} = User.mute(user, other_user)
221 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
222 status = StatusView.render("show.json", %{activity: activity, for: user})
224 assert status.pleroma.thread_muted == false
226 {:ok, activity} = CommonAPI.add_mute(user, activity)
228 status = StatusView.render("show.json", %{activity: activity, for: user})
230 assert status.pleroma.thread_muted == true
233 test "tells if the status is bookmarked" do
236 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
237 status = StatusView.render("show.json", %{activity: activity})
239 assert status.bookmarked == false
241 status = StatusView.render("show.json", %{activity: activity, for: user})
243 assert status.bookmarked == false
245 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
247 activity = Activity.get_by_id_with_object(activity.id)
249 status = StatusView.render("show.json", %{activity: activity, for: user})
251 assert status.bookmarked == true
255 note = insert(:note_activity)
259 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
261 status = StatusView.render("show.json", %{activity: activity})
263 assert status.in_reply_to_id == to_string(note.id)
265 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
267 assert status.in_reply_to_id == to_string(note.id)
270 test "contains mentions" do
272 mentioned = insert(:user)
274 {:ok, activity} = CommonAPI.post(user, %{"status" => "hi @#{mentioned.nickname}"})
276 status = StatusView.render("show.json", %{activity: activity})
278 assert status.mentions ==
279 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
282 test "create mentions from the 'to' field" do
283 %User{ap_id: recipient_ap_id} = insert(:user)
284 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
289 "to" => [recipient_ap_id],
295 insert(:note_activity, %{
297 recipients: [recipient_ap_id | cc]
300 assert length(activity.recipients) == 3
302 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
304 assert length(mentions) == 1
305 assert mention.url == recipient_ap_id
308 test "create mentions from the 'tag' field" do
309 recipient = insert(:user)
310 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
318 "href" => recipient.ap_id,
319 "name" => recipient.nickname,
323 "href" => "https://example.com/search?tag=test",
332 insert(:note_activity, %{
334 recipients: [recipient.ap_id | cc]
337 assert length(activity.recipients) == 3
339 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
341 assert length(mentions) == 1
342 assert mention.url == recipient.ap_id
345 test "attachments" do
350 "mediaType" => "image/png",
361 remote_url: "someurl",
362 preview_url: "someurl",
365 pleroma: %{mime_type: "image/png"}
368 assert expected == StatusView.render("attachment.json", %{attachment: object})
370 # If theres a "id", use that instead of the generated one
371 object = Map.put(object, "id", 2)
372 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
375 test "put the url advertised in the Activity in to the url attribute" do
376 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
377 [activity] = Activity.search(nil, id)
379 status = StatusView.render("show.json", %{activity: activity})
381 assert status.uri == id
382 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
387 activity = insert(:note_activity)
389 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
391 represented = StatusView.render("show.json", %{for: user, activity: reblog})
393 assert represented[:id] == to_string(reblog.id)
394 assert represented[:reblog][:id] == to_string(activity.id)
395 assert represented[:emojis] == []
398 test "a peertube video" do
402 Pleroma.Object.Fetcher.fetch_object_from_id(
403 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
406 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
408 represented = StatusView.render("show.json", %{for: user, activity: activity})
410 assert represented[:id] == to_string(activity.id)
411 assert length(represented[:media_attachments]) == 1
414 test "a Mobilizon event" do
418 Pleroma.Object.Fetcher.fetch_object_from_id(
419 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
422 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
424 represented = StatusView.render("show.json", %{for: user, activity: activity})
426 assert represented[:id] == to_string(activity.id)
429 describe "build_tags/1" do
430 test "it returns a a dictionary tags" do
436 "href" => "https://kawen.space/users/lain",
437 "name" => "@lain@kawen.space",
442 assert StatusView.build_tags(object_tags) == [
443 %{name: "fediverse", url: "/tag/fediverse"},
444 %{name: "mastodon", url: "/tag/mastodon"},
445 %{name: "nextcloud", url: "/tag/nextcloud"}
450 describe "rich media cards" do
451 test "a rich media card without a site name renders correctly" do
452 page_url = "http://example.com"
456 image: page_url <> "/example.jpg",
457 title: "Example website"
460 %{provider_name: "example.com"} =
461 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
464 test "a rich media card without a site name or image renders correctly" do
465 page_url = "http://example.com"
469 title: "Example website"
472 %{provider_name: "example.com"} =
473 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
476 test "a rich media card without an image renders correctly" do
477 page_url = "http://example.com"
481 site_name: "Example site name",
482 title: "Example website"
485 %{provider_name: "Example site name"} =
486 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
489 test "a rich media card with all relevant data renders correctly" do
490 page_url = "http://example.com"
494 site_name: "Example site name",
495 title: "Example website",
496 image: page_url <> "/example.jpg",
497 description: "Example description"
500 %{provider_name: "Example site name"} =
501 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
505 test "embeds a relationship in the account" do
507 other_user = insert(:user)
510 CommonAPI.post(user, %{
511 "status" => "drink more water"
514 result = StatusView.render("show.json", %{activity: activity, for: other_user})
516 assert result[:account][:pleroma][:relationship] ==
517 AccountView.render("relationship.json", %{user: other_user, target: user})
520 test "embeds a relationship in the account in reposts" do
522 other_user = insert(:user)
525 CommonAPI.post(user, %{
526 "status" => "ĖĖÉŹns"
529 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
531 result = StatusView.render("show.json", %{activity: activity, for: user})
533 assert result[:account][:pleroma][:relationship] ==
534 AccountView.render("relationship.json", %{user: user, target: other_user})
536 assert result[:reblog][:account][:pleroma][:relationship] ==
537 AccountView.render("relationship.json", %{user: user, target: user})
540 test "visibility/list" do
543 {:ok, list} = Pleroma.List.create("foo", user)
546 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
548 status = StatusView.render("show.json", activity: activity)
550 assert status.visibility == "list"
553 test "successfully renders a Listen activity (pleroma extension)" do
554 listen_activity = insert(:listen)
556 status = StatusView.render("listen.json", activity: listen_activity)
558 assert status.length == listen_activity.data["object"]["length"]
559 assert status.title == listen_activity.data["object"]["title"]