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.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, third_user, "🍵")
35 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
36 activity = Repo.get(Activity, activity.id)
37 status = StatusView.render("show.json", activity: activity)
39 assert status[:pleroma][:emoji_reactions] == [
40 %{name: "☕", count: 2, me: false},
41 %{name: "🍵", count: 1, me: false}
44 status = StatusView.render("show.json", activity: activity, for: user)
46 assert status[:pleroma][:emoji_reactions] == [
47 %{name: "☕", count: 2, me: true},
48 %{name: "🍵", count: 1, me: false}
52 test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
55 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
56 [participation] = Participation.for_user(user)
59 StatusView.render("show.json",
61 with_direct_conversation_id: true,
65 assert status[:pleroma][:direct_conversation_id] == participation.id
67 status = StatusView.render("show.json", activity: activity, for: user)
68 assert status[:pleroma][:direct_conversation_id] == nil
71 test "returns the direct conversation id when given the `direct_conversation_id` option" do
74 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
75 [participation] = Participation.for_user(user)
78 StatusView.render("show.json",
80 direct_conversation_id: participation.id,
84 assert status[:pleroma][:direct_conversation_id] == participation.id
87 test "returns a temporary ap_id based user for activities missing db users" do
90 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
93 Cachex.clear(:user_cache)
95 %{account: ms_user} = StatusView.render("show.json", activity: activity)
97 assert ms_user.acct == "erroruser@example.com"
100 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
103 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
107 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
110 Cachex.clear(:user_cache)
112 result = StatusView.render("show.json", activity: activity)
114 assert result[:account][:id] == to_string(user.id)
117 test "a note with null content" do
118 note = insert(:note_activity)
119 note_object = Object.normalize(note)
123 |> Map.put("content", nil)
125 Object.change(note_object, %{data: data})
126 |> Object.update_and_set_cache()
128 User.get_cached_by_ap_id(note.data["actor"])
130 status = StatusView.render("show.json", %{activity: note})
132 assert status.content == ""
135 test "a note activity" do
136 note = insert(:note_activity)
137 object_data = Object.normalize(note).data
138 user = User.get_cached_by_ap_id(note.data["actor"])
140 convo_id = Utils.context_to_conversation_id(object_data["context"])
142 status = StatusView.render("show.json", %{activity: note})
145 (object_data["published"] || "")
146 |> String.replace(~r/\.\d+Z/, ".000Z")
149 id: to_string(note.id),
150 uri: object_data["id"],
151 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
152 account: AccountView.render("show.json", %{user: user}),
154 in_reply_to_account_id: nil,
157 content: HTML.filter_tags(object_data["content"]),
158 created_at: created_at,
169 spoiler_text: HTML.filter_tags(object_data["summary"]),
170 visibility: "public",
171 media_attachments: [],
175 name: "#{object_data["tag"]}",
176 url: "/tag/#{object_data["tag"]}"
188 static_url: "corndog.png",
189 visible_in_picker: false
194 conversation_id: convo_id,
195 in_reply_to_account_acct: nil,
196 content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
197 spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
199 direct_conversation_id: nil,
205 assert status == expected
208 test "tells if the message is muted for some reason" do
210 other_user = insert(:user)
212 {:ok, _user_relationships} = User.mute(user, other_user)
214 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
215 status = StatusView.render("show.json", %{activity: activity})
217 assert status.muted == false
219 status = StatusView.render("show.json", %{activity: activity, for: user})
221 assert status.muted == true
224 test "tells if the message is thread muted" do
226 other_user = insert(:user)
228 {:ok, _user_relationships} = User.mute(user, other_user)
230 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
231 status = StatusView.render("show.json", %{activity: activity, for: user})
233 assert status.pleroma.thread_muted == false
235 {:ok, activity} = CommonAPI.add_mute(user, activity)
237 status = StatusView.render("show.json", %{activity: activity, for: user})
239 assert status.pleroma.thread_muted == true
242 test "tells if the status is bookmarked" do
245 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
246 status = StatusView.render("show.json", %{activity: activity})
248 assert status.bookmarked == false
250 status = StatusView.render("show.json", %{activity: activity, for: user})
252 assert status.bookmarked == false
254 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
256 activity = Activity.get_by_id_with_object(activity.id)
258 status = StatusView.render("show.json", %{activity: activity, for: user})
260 assert status.bookmarked == true
264 note = insert(:note_activity)
268 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
270 status = StatusView.render("show.json", %{activity: activity})
272 assert status.in_reply_to_id == to_string(note.id)
274 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
276 assert status.in_reply_to_id == to_string(note.id)
279 test "contains mentions" do
281 mentioned = insert(:user)
283 {:ok, activity} = CommonAPI.post(user, %{"status" => "hi @#{mentioned.nickname}"})
285 status = StatusView.render("show.json", %{activity: activity})
287 assert status.mentions ==
288 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
291 test "create mentions from the 'to' field" do
292 %User{ap_id: recipient_ap_id} = insert(:user)
293 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
298 "to" => [recipient_ap_id],
304 insert(:note_activity, %{
306 recipients: [recipient_ap_id | cc]
309 assert length(activity.recipients) == 3
311 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
313 assert length(mentions) == 1
314 assert mention.url == recipient_ap_id
317 test "create mentions from the 'tag' field" do
318 recipient = insert(:user)
319 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
327 "href" => recipient.ap_id,
328 "name" => recipient.nickname,
332 "href" => "https://example.com/search?tag=test",
341 insert(:note_activity, %{
343 recipients: [recipient.ap_id | cc]
346 assert length(activity.recipients) == 3
348 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
350 assert length(mentions) == 1
351 assert mention.url == recipient.ap_id
354 test "attachments" do
359 "mediaType" => "image/png",
370 remote_url: "someurl",
371 preview_url: "someurl",
374 pleroma: %{mime_type: "image/png"}
377 assert expected == StatusView.render("attachment.json", %{attachment: object})
379 # If theres a "id", use that instead of the generated one
380 object = Map.put(object, "id", 2)
381 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
384 test "put the url advertised in the Activity in to the url attribute" do
385 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
386 [activity] = Activity.search(nil, id)
388 status = StatusView.render("show.json", %{activity: activity})
390 assert status.uri == id
391 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
396 activity = insert(:note_activity)
398 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
400 represented = StatusView.render("show.json", %{for: user, activity: reblog})
402 assert represented[:id] == to_string(reblog.id)
403 assert represented[:reblog][:id] == to_string(activity.id)
404 assert represented[:emojis] == []
407 test "a peertube video" do
411 Pleroma.Object.Fetcher.fetch_object_from_id(
412 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
415 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
417 represented = StatusView.render("show.json", %{for: user, activity: activity})
419 assert represented[:id] == to_string(activity.id)
420 assert length(represented[:media_attachments]) == 1
423 test "a Mobilizon event" do
427 Pleroma.Object.Fetcher.fetch_object_from_id(
428 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
431 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
433 represented = StatusView.render("show.json", %{for: user, activity: activity})
435 assert represented[:id] == to_string(activity.id)
438 describe "build_tags/1" do
439 test "it returns a a dictionary tags" do
445 "href" => "https://kawen.space/users/lain",
446 "name" => "@lain@kawen.space",
451 assert StatusView.build_tags(object_tags) == [
452 %{name: "fediverse", url: "/tag/fediverse"},
453 %{name: "mastodon", url: "/tag/mastodon"},
454 %{name: "nextcloud", url: "/tag/nextcloud"}
459 describe "rich media cards" do
460 test "a rich media card without a site name renders correctly" do
461 page_url = "http://example.com"
465 image: page_url <> "/example.jpg",
466 title: "Example website"
469 %{provider_name: "example.com"} =
470 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
473 test "a rich media card without a site name or image renders correctly" do
474 page_url = "http://example.com"
478 title: "Example website"
481 %{provider_name: "example.com"} =
482 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
485 test "a rich media card without an image renders correctly" do
486 page_url = "http://example.com"
490 site_name: "Example site name",
491 title: "Example website"
494 %{provider_name: "example.com"} =
495 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
498 test "a rich media card with all relevant data renders correctly" do
499 page_url = "http://example.com"
503 site_name: "Example site name",
504 title: "Example website",
505 image: page_url <> "/example.jpg",
506 description: "Example description"
509 %{provider_name: "example.com"} =
510 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
514 test "embeds a relationship in the account" do
516 other_user = insert(:user)
519 CommonAPI.post(user, %{
520 "status" => "drink more water"
523 result = StatusView.render("show.json", %{activity: activity, for: other_user})
525 assert result[:account][:pleroma][:relationship] ==
526 AccountView.render("relationship.json", %{user: other_user, target: user})
529 test "embeds a relationship in the account in reposts" do
531 other_user = insert(:user)
534 CommonAPI.post(user, %{
538 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
540 result = StatusView.render("show.json", %{activity: activity, for: user})
542 assert result[:account][:pleroma][:relationship] ==
543 AccountView.render("relationship.json", %{user: user, target: other_user})
545 assert result[:reblog][:account][:pleroma][:relationship] ==
546 AccountView.render("relationship.json", %{user: user, target: user})
549 test "visibility/list" do
552 {:ok, list} = Pleroma.List.create("foo", user)
555 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
557 status = StatusView.render("show.json", activity: activity)
559 assert status.visibility == "list"
562 test "successfully renders a Listen activity (pleroma extension)" do
563 listen_activity = insert(:listen)
565 status = StatusView.render("listen.json", activity: listen_activity)
567 assert status.length == listen_activity.data["object"]["length"]
568 assert status.title == listen_activity.data["object"]["title"]