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, 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] == [["ā", 2], ["šµ", 1]]
42 test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
45 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
46 [participation] = Participation.for_user(user)
49 StatusView.render("show.json",
51 with_direct_conversation_id: true,
55 assert status[:pleroma][:direct_conversation_id] == participation.id
57 status = StatusView.render("show.json", activity: activity, for: user)
58 assert status[:pleroma][:direct_conversation_id] == nil
61 test "returns the direct conversation id when given the `direct_conversation_id` option" do
64 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
65 [participation] = Participation.for_user(user)
68 StatusView.render("show.json",
70 direct_conversation_id: participation.id,
74 assert status[:pleroma][:direct_conversation_id] == participation.id
77 test "returns a temporary ap_id based user for activities missing db users" do
80 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
83 Cachex.clear(:user_cache)
85 %{account: ms_user} = StatusView.render("show.json", activity: activity)
87 assert ms_user.acct == "erroruser@example.com"
90 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
93 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
97 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
100 Cachex.clear(:user_cache)
102 result = StatusView.render("show.json", activity: activity)
104 assert result[:account][:id] == to_string(user.id)
107 test "a note with null content" do
108 note = insert(:note_activity)
109 note_object = Object.normalize(note)
113 |> Map.put("content", nil)
115 Object.change(note_object, %{data: data})
116 |> Object.update_and_set_cache()
118 User.get_cached_by_ap_id(note.data["actor"])
120 status = StatusView.render("show.json", %{activity: note})
122 assert status.content == ""
125 test "a note activity" do
126 note = insert(:note_activity)
127 object_data = Object.normalize(note).data
128 user = User.get_cached_by_ap_id(note.data["actor"])
130 convo_id = Utils.context_to_conversation_id(object_data["context"])
132 status = StatusView.render("show.json", %{activity: note})
135 (object_data["published"] || "")
136 |> String.replace(~r/\.\d+Z/, ".000Z")
139 id: to_string(note.id),
140 uri: object_data["id"],
141 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
142 account: AccountView.render("show.json", %{user: user}),
144 in_reply_to_account_id: nil,
147 content: HTML.filter_tags(object_data["content"]),
148 created_at: created_at,
159 spoiler_text: HTML.filter_tags(object_data["summary"]),
160 visibility: "public",
161 media_attachments: [],
165 name: "#{object_data["tag"]}",
166 url: "/tag/#{object_data["tag"]}"
178 static_url: "corndog.png",
179 visible_in_picker: false
184 conversation_id: convo_id,
185 in_reply_to_account_acct: nil,
186 content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
187 spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
189 direct_conversation_id: nil,
195 assert status == expected
198 test "tells if the message is muted for some reason" do
200 other_user = insert(:user)
202 {:ok, _user_relationships} = User.mute(user, other_user)
204 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
205 status = StatusView.render("show.json", %{activity: activity})
207 assert status.muted == false
209 status = StatusView.render("show.json", %{activity: activity, for: user})
211 assert status.muted == true
214 test "tells if the message is thread muted" do
216 other_user = insert(:user)
218 {:ok, _user_relationships} = User.mute(user, other_user)
220 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
221 status = StatusView.render("show.json", %{activity: activity, for: user})
223 assert status.pleroma.thread_muted == false
225 {:ok, activity} = CommonAPI.add_mute(user, activity)
227 status = StatusView.render("show.json", %{activity: activity, for: user})
229 assert status.pleroma.thread_muted == true
232 test "tells if the status is bookmarked" do
235 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
236 status = StatusView.render("show.json", %{activity: activity})
238 assert status.bookmarked == false
240 status = StatusView.render("show.json", %{activity: activity, for: user})
242 assert status.bookmarked == false
244 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
246 activity = Activity.get_by_id_with_object(activity.id)
248 status = StatusView.render("show.json", %{activity: activity, for: user})
250 assert status.bookmarked == true
254 note = insert(:note_activity)
258 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
260 status = StatusView.render("show.json", %{activity: activity})
262 assert status.in_reply_to_id == to_string(note.id)
264 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
266 assert status.in_reply_to_id == to_string(note.id)
269 test "contains mentions" do
271 mentioned = insert(:user)
273 {:ok, activity} = CommonAPI.post(user, %{"status" => "hi @#{mentioned.nickname}"})
275 status = StatusView.render("show.json", %{activity: activity})
277 assert status.mentions ==
278 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
281 test "create mentions from the 'to' field" do
282 %User{ap_id: recipient_ap_id} = insert(:user)
283 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
288 "to" => [recipient_ap_id],
294 insert(:note_activity, %{
296 recipients: [recipient_ap_id | cc]
299 assert length(activity.recipients) == 3
301 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
303 assert length(mentions) == 1
304 assert mention.url == recipient_ap_id
307 test "create mentions from the 'tag' field" do
308 recipient = insert(:user)
309 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
317 "href" => recipient.ap_id,
318 "name" => recipient.nickname,
322 "href" => "https://example.com/search?tag=test",
331 insert(:note_activity, %{
333 recipients: [recipient.ap_id | cc]
336 assert length(activity.recipients) == 3
338 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
340 assert length(mentions) == 1
341 assert mention.url == recipient.ap_id
344 test "attachments" do
349 "mediaType" => "image/png",
360 remote_url: "someurl",
361 preview_url: "someurl",
364 pleroma: %{mime_type: "image/png"}
367 assert expected == StatusView.render("attachment.json", %{attachment: object})
369 # If theres a "id", use that instead of the generated one
370 object = Map.put(object, "id", 2)
371 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
374 test "put the url advertised in the Activity in to the url attribute" do
375 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
376 [activity] = Activity.search(nil, id)
378 status = StatusView.render("show.json", %{activity: activity})
380 assert status.uri == id
381 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
386 activity = insert(:note_activity)
388 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
390 represented = StatusView.render("show.json", %{for: user, activity: reblog})
392 assert represented[:id] == to_string(reblog.id)
393 assert represented[:reblog][:id] == to_string(activity.id)
394 assert represented[:emojis] == []
397 test "a peertube video" do
401 Pleroma.Object.Fetcher.fetch_object_from_id(
402 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
405 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
407 represented = StatusView.render("show.json", %{for: user, activity: activity})
409 assert represented[:id] == to_string(activity.id)
410 assert length(represented[:media_attachments]) == 1
413 test "a Mobilizon event" do
417 Pleroma.Object.Fetcher.fetch_object_from_id(
418 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
421 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
423 represented = StatusView.render("show.json", %{for: user, activity: activity})
425 assert represented[:id] == to_string(activity.id)
428 describe "build_tags/1" do
429 test "it returns a a dictionary tags" do
435 "href" => "https://kawen.space/users/lain",
436 "name" => "@lain@kawen.space",
441 assert StatusView.build_tags(object_tags) == [
442 %{name: "fediverse", url: "/tag/fediverse"},
443 %{name: "mastodon", url: "/tag/mastodon"},
444 %{name: "nextcloud", url: "/tag/nextcloud"}
449 describe "rich media cards" do
450 test "a rich media card without a site name renders correctly" do
451 page_url = "http://example.com"
455 image: page_url <> "/example.jpg",
456 title: "Example website"
459 %{provider_name: "example.com"} =
460 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
463 test "a rich media card without a site name or image renders correctly" do
464 page_url = "http://example.com"
468 title: "Example website"
471 %{provider_name: "example.com"} =
472 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
475 test "a rich media card without an image renders correctly" do
476 page_url = "http://example.com"
480 site_name: "Example site name",
481 title: "Example website"
484 %{provider_name: "Example site name"} =
485 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
488 test "a rich media card with all relevant data renders correctly" do
489 page_url = "http://example.com"
493 site_name: "Example site name",
494 title: "Example website",
495 image: page_url <> "/example.jpg",
496 description: "Example description"
499 %{provider_name: "Example site name"} =
500 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
504 test "embeds a relationship in the account" do
506 other_user = insert(:user)
509 CommonAPI.post(user, %{
510 "status" => "drink more water"
513 result = StatusView.render("show.json", %{activity: activity, for: other_user})
515 assert result[:account][:pleroma][:relationship] ==
516 AccountView.render("relationship.json", %{user: other_user, target: user})
519 test "embeds a relationship in the account in reposts" do
521 other_user = insert(:user)
524 CommonAPI.post(user, %{
525 "status" => "ĖĖÉŹns"
528 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
530 result = StatusView.render("show.json", %{activity: activity, for: user})
532 assert result[:account][:pleroma][:relationship] ==
533 AccountView.render("relationship.json", %{user: user, target: other_user})
535 assert result[:reblog][:account][:pleroma][:relationship] ==
536 AccountView.render("relationship.json", %{user: user, target: user})
539 test "visibility/list" do
542 {:ok, list} = Pleroma.List.create("foo", user)
545 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
547 status = StatusView.render("show.json", activity: activity)
549 assert status.visibility == "list"
552 test "successfully renders a Listen activity (pleroma extension)" do
553 listen_activity = insert(:listen)
555 status = StatusView.render("listen.json", activity: listen_activity)
557 assert status.length == listen_activity.data["object"]["length"]
558 assert status.title == listen_activity.data["object"]["title"]