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 api_spec = Pleroma.Web.ApiSpec.spec()
407 assert expected == StatusView.render("attachment.json", %{attachment: object})
408 OpenApiSpex.TestAssertions.assert_schema(expected, "Attachment", api_spec)
410 # If theres a "id", use that instead of the generated one
411 object = Map.put(object, "id", 2)
412 result = StatusView.render("attachment.json", %{attachment: object})
414 assert %{id: "2"} = result
415 OpenApiSpex.TestAssertions.assert_schema(result, "Attachment", api_spec)
418 test "put the url advertised in the Activity in to the url attribute" do
419 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
420 [activity] = Activity.search(nil, id)
422 status = StatusView.render("show.json", %{activity: activity})
424 assert status.uri == id
425 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
430 activity = insert(:note_activity)
432 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
434 represented = StatusView.render("show.json", %{for: user, activity: reblog})
436 assert represented[:id] == to_string(reblog.id)
437 assert represented[:reblog][:id] == to_string(activity.id)
438 assert represented[:emojis] == []
441 test "a peertube video" do
445 Pleroma.Object.Fetcher.fetch_object_from_id(
446 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
449 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
451 represented = StatusView.render("show.json", %{for: user, activity: activity})
453 assert represented[:id] == to_string(activity.id)
454 assert length(represented[:media_attachments]) == 1
457 test "funkwhale audio" do
461 Pleroma.Object.Fetcher.fetch_object_from_id(
462 "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871"
465 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
467 represented = StatusView.render("show.json", %{for: user, activity: activity})
469 assert represented[:id] == to_string(activity.id)
470 assert length(represented[:media_attachments]) == 1
473 test "a Mobilizon event" do
477 Pleroma.Object.Fetcher.fetch_object_from_id(
478 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
481 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
483 represented = StatusView.render("show.json", %{for: user, activity: activity})
485 assert represented[:id] == to_string(activity.id)
488 describe "build_tags/1" do
489 test "it returns a a dictionary tags" do
495 "href" => "https://kawen.space/users/lain",
496 "name" => "@lain@kawen.space",
501 assert StatusView.build_tags(object_tags) == [
502 %{name: "fediverse", url: "/tag/fediverse"},
503 %{name: "mastodon", url: "/tag/mastodon"},
504 %{name: "nextcloud", url: "/tag/nextcloud"}
509 describe "rich media cards" do
510 test "a rich media card without a site name renders correctly" do
511 page_url = "http://example.com"
515 image: page_url <> "/example.jpg",
516 title: "Example website"
519 %{provider_name: "example.com"} =
520 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
523 test "a rich media card without a site name or image renders correctly" do
524 page_url = "http://example.com"
528 title: "Example website"
531 %{provider_name: "example.com"} =
532 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
535 test "a rich media card without an image renders correctly" do
536 page_url = "http://example.com"
540 site_name: "Example site name",
541 title: "Example website"
544 %{provider_name: "example.com"} =
545 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
548 test "a rich media card with all relevant data renders correctly" do
549 page_url = "http://example.com"
553 site_name: "Example site name",
554 title: "Example website",
555 image: page_url <> "/example.jpg",
556 description: "Example description"
559 %{provider_name: "example.com"} =
560 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
564 test "embeds a relationship in the account" do
566 other_user = insert(:user)
569 CommonAPI.post(user, %{
570 "status" => "drink more water"
573 result = StatusView.render("show.json", %{activity: activity, for: other_user})
575 assert result[:account][:pleroma][:relationship] ==
576 AccountView.render("relationship.json", %{user: other_user, target: user})
579 test "embeds a relationship in the account in reposts" do
581 other_user = insert(:user)
584 CommonAPI.post(user, %{
588 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
590 result = StatusView.render("show.json", %{activity: activity, for: user})
592 assert result[:account][:pleroma][:relationship] ==
593 AccountView.render("relationship.json", %{user: user, target: other_user})
595 assert result[:reblog][:account][:pleroma][:relationship] ==
596 AccountView.render("relationship.json", %{user: user, target: user})
599 test "visibility/list" do
602 {:ok, list} = Pleroma.List.create("foo", user)
605 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
607 status = StatusView.render("show.json", activity: activity)
609 assert status.visibility == "list"
612 test "successfully renders a Listen activity (pleroma extension)" do
613 listen_activity = insert(:listen)
615 status = StatusView.render("listen.json", activity: listen_activity)
617 assert status.length == listen_activity.data["object"]["length"]
618 assert status.title == listen_activity.data["object"]["title"]