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)
98 "https://localhost/.well-known/webfinger?resource=acct:#{user.nickname}@localhost"
100 Tesla.Mock.mock_global(fn
101 %{method: :get, url: "http://localhost/.well-known/host-meta"} ->
102 %Tesla.Env{status: 404, body: ""}
104 %{method: :get, url: "https://localhost/.well-known/host-meta"} ->
105 %Tesla.Env{status: 404, body: ""}
111 %Tesla.Env{status: 404, body: ""}
114 %{account: ms_user} = StatusView.render("show.json", activity: activity)
116 assert ms_user.acct == "erroruser@example.com"
119 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
122 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
126 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
129 Cachex.clear(:user_cache)
131 result = StatusView.render("show.json", activity: activity)
133 assert result[:account][:id] == to_string(user.id)
136 test "a note with null content" do
137 note = insert(:note_activity)
138 note_object = Object.normalize(note)
142 |> Map.put("content", nil)
144 Object.change(note_object, %{data: data})
145 |> Object.update_and_set_cache()
147 User.get_cached_by_ap_id(note.data["actor"])
149 status = StatusView.render("show.json", %{activity: note})
151 assert status.content == ""
154 test "a note activity" do
155 note = insert(:note_activity)
156 object_data = Object.normalize(note).data
157 user = User.get_cached_by_ap_id(note.data["actor"])
159 convo_id = Utils.context_to_conversation_id(object_data["context"])
161 status = StatusView.render("show.json", %{activity: note})
164 (object_data["published"] || "")
165 |> String.replace(~r/\.\d+Z/, ".000Z")
168 id: to_string(note.id),
169 uri: object_data["id"],
170 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
171 account: AccountView.render("show.json", %{user: user}),
173 in_reply_to_account_id: nil,
176 content: HTML.filter_tags(object_data["content"]),
177 created_at: created_at,
188 spoiler_text: HTML.filter_tags(object_data["summary"]),
189 visibility: "public",
190 media_attachments: [],
194 name: "#{object_data["tag"]}",
195 url: "/tag/#{object_data["tag"]}"
207 static_url: "corndog.png",
208 visible_in_picker: false
213 conversation_id: convo_id,
214 in_reply_to_account_acct: nil,
215 content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
216 spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
218 direct_conversation_id: nil,
224 assert status == expected
227 test "tells if the message is muted for some reason" do
229 other_user = insert(:user)
231 {:ok, _user_relationships} = User.mute(user, other_user)
233 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
235 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
237 opts = %{activity: activity}
238 status = StatusView.render("show.json", opts)
239 assert status.muted == false
241 status = StatusView.render("show.json", Map.put(opts, :relationships, relationships_opt))
242 assert status.muted == false
244 for_opts = %{activity: activity, for: user}
245 status = StatusView.render("show.json", for_opts)
246 assert status.muted == true
248 status = StatusView.render("show.json", Map.put(for_opts, :relationships, relationships_opt))
249 assert status.muted == true
252 test "tells if the message is thread muted" do
254 other_user = insert(:user)
256 {:ok, _user_relationships} = User.mute(user, other_user)
258 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
259 status = StatusView.render("show.json", %{activity: activity, for: user})
261 assert status.pleroma.thread_muted == false
263 {:ok, activity} = CommonAPI.add_mute(user, activity)
265 status = StatusView.render("show.json", %{activity: activity, for: user})
267 assert status.pleroma.thread_muted == true
270 test "tells if the status is bookmarked" do
273 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
274 status = StatusView.render("show.json", %{activity: activity})
276 assert status.bookmarked == false
278 status = StatusView.render("show.json", %{activity: activity, for: user})
280 assert status.bookmarked == false
282 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
284 activity = Activity.get_by_id_with_object(activity.id)
286 status = StatusView.render("show.json", %{activity: activity, for: user})
288 assert status.bookmarked == true
292 note = insert(:note_activity)
296 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
298 status = StatusView.render("show.json", %{activity: activity})
300 assert status.in_reply_to_id == to_string(note.id)
302 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
304 assert status.in_reply_to_id == to_string(note.id)
307 test "contains mentions" do
309 mentioned = insert(:user)
311 {:ok, activity} = CommonAPI.post(user, %{"status" => "hi @#{mentioned.nickname}"})
313 status = StatusView.render("show.json", %{activity: activity})
315 assert status.mentions ==
316 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
319 test "create mentions from the 'to' field" do
320 %User{ap_id: recipient_ap_id} = insert(:user)
321 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
326 "to" => [recipient_ap_id],
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 "create mentions from the 'tag' field" do
346 recipient = insert(:user)
347 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
355 "href" => recipient.ap_id,
356 "name" => recipient.nickname,
360 "href" => "https://example.com/search?tag=test",
369 insert(:note_activity, %{
371 recipients: [recipient.ap_id | cc]
374 assert length(activity.recipients) == 3
376 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
378 assert length(mentions) == 1
379 assert mention.url == recipient.ap_id
382 test "attachments" do
387 "mediaType" => "image/png",
398 remote_url: "someurl",
399 preview_url: "someurl",
402 pleroma: %{mime_type: "image/png"}
405 assert expected == StatusView.render("attachment.json", %{attachment: object})
407 # If theres a "id", use that instead of the generated one
408 object = Map.put(object, "id", 2)
409 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
412 test "put the url advertised in the Activity in to the url attribute" do
413 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
414 [activity] = Activity.search(nil, id)
416 status = StatusView.render("show.json", %{activity: activity})
418 assert status.uri == id
419 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
424 activity = insert(:note_activity)
426 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
428 represented = StatusView.render("show.json", %{for: user, activity: reblog})
430 assert represented[:id] == to_string(reblog.id)
431 assert represented[:reblog][:id] == to_string(activity.id)
432 assert represented[:emojis] == []
435 test "a peertube video" do
439 Pleroma.Object.Fetcher.fetch_object_from_id(
440 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
443 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
445 represented = StatusView.render("show.json", %{for: user, activity: activity})
447 assert represented[:id] == to_string(activity.id)
448 assert length(represented[:media_attachments]) == 1
451 test "funkwhale audio" do
455 Pleroma.Object.Fetcher.fetch_object_from_id(
456 "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871"
459 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
461 represented = StatusView.render("show.json", %{for: user, activity: activity})
463 assert represented[:id] == to_string(activity.id)
464 assert length(represented[:media_attachments]) == 1
467 test "a Mobilizon event" do
471 Pleroma.Object.Fetcher.fetch_object_from_id(
472 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
475 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
477 represented = StatusView.render("show.json", %{for: user, activity: activity})
479 assert represented[:id] == to_string(activity.id)
482 describe "build_tags/1" do
483 test "it returns a a dictionary tags" do
489 "href" => "https://kawen.space/users/lain",
490 "name" => "@lain@kawen.space",
495 assert StatusView.build_tags(object_tags) == [
496 %{name: "fediverse", url: "/tag/fediverse"},
497 %{name: "mastodon", url: "/tag/mastodon"},
498 %{name: "nextcloud", url: "/tag/nextcloud"}
503 describe "rich media cards" do
504 test "a rich media card without a site name renders correctly" do
505 page_url = "http://example.com"
509 image: page_url <> "/example.jpg",
510 title: "Example website"
513 %{provider_name: "example.com"} =
514 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
517 test "a rich media card without a site name or image renders correctly" do
518 page_url = "http://example.com"
522 title: "Example website"
525 %{provider_name: "example.com"} =
526 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
529 test "a rich media card without an image renders correctly" do
530 page_url = "http://example.com"
534 site_name: "Example site name",
535 title: "Example website"
538 %{provider_name: "example.com"} =
539 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
542 test "a rich media card with all relevant data renders correctly" do
543 page_url = "http://example.com"
547 site_name: "Example site name",
548 title: "Example website",
549 image: page_url <> "/example.jpg",
550 description: "Example description"
553 %{provider_name: "example.com"} =
554 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
558 test "embeds a relationship in the account" do
560 other_user = insert(:user)
563 CommonAPI.post(user, %{
564 "status" => "drink more water"
567 result = StatusView.render("show.json", %{activity: activity, for: other_user})
569 assert result[:account][:pleroma][:relationship] ==
570 AccountView.render("relationship.json", %{user: other_user, target: user})
573 test "embeds a relationship in the account in reposts" do
575 other_user = insert(:user)
578 CommonAPI.post(user, %{
582 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
584 result = StatusView.render("show.json", %{activity: activity, for: user})
586 assert result[:account][:pleroma][:relationship] ==
587 AccountView.render("relationship.json", %{user: user, target: other_user})
589 assert result[:reblog][:account][:pleroma][:relationship] ==
590 AccountView.render("relationship.json", %{user: user, target: user})
593 test "visibility/list" do
596 {:ok, list} = Pleroma.List.create("foo", user)
599 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
601 status = StatusView.render("show.json", activity: activity)
603 assert status.visibility == "list"
606 test "successfully renders a Listen activity (pleroma extension)" do
607 listen_activity = insert(:listen)
609 status = StatusView.render("listen.json", activity: listen_activity)
611 assert status.length == listen_activity.data["object"]["length"]
612 assert status.title == listen_activity.data["object"]["title"]