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)
96 "https://localhost/.well-known/webfinger?resource=acct:#{user.nickname}@localhost"
98 Tesla.Mock.mock_global(fn
99 %{method: :get, url: "http://localhost/.well-known/host-meta"} ->
100 %Tesla.Env{status: 404, body: ""}
102 %{method: :get, url: "https://localhost/.well-known/host-meta"} ->
103 %Tesla.Env{status: 404, body: ""}
109 %Tesla.Env{status: 404, body: ""}
112 %{account: ms_user} = StatusView.render("show.json", activity: activity)
114 assert ms_user.acct == "erroruser@example.com"
117 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
120 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
124 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
127 Cachex.clear(:user_cache)
129 result = StatusView.render("show.json", activity: activity)
131 assert result[:account][:id] == to_string(user.id)
134 test "a note with null content" do
135 note = insert(:note_activity)
136 note_object = Object.normalize(note)
140 |> Map.put("content", nil)
142 Object.change(note_object, %{data: data})
143 |> Object.update_and_set_cache()
145 User.get_cached_by_ap_id(note.data["actor"])
147 status = StatusView.render("show.json", %{activity: note})
149 assert status.content == ""
152 test "a note activity" do
153 note = insert(:note_activity)
154 object_data = Object.normalize(note).data
155 user = User.get_cached_by_ap_id(note.data["actor"])
157 convo_id = Utils.context_to_conversation_id(object_data["context"])
159 status = StatusView.render("show.json", %{activity: note})
162 (object_data["published"] || "")
163 |> String.replace(~r/\.\d+Z/, ".000Z")
166 id: to_string(note.id),
167 uri: object_data["id"],
168 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
169 account: AccountView.render("show.json", %{user: user}),
171 in_reply_to_account_id: nil,
174 content: HTML.filter_tags(object_data["content"]),
175 created_at: created_at,
186 spoiler_text: HTML.filter_tags(object_data["summary"]),
187 visibility: "public",
188 media_attachments: [],
192 name: "#{object_data["tag"]}",
193 url: "/tag/#{object_data["tag"]}"
205 static_url: "corndog.png",
206 visible_in_picker: false
211 conversation_id: convo_id,
212 in_reply_to_account_acct: nil,
213 content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
214 spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
216 direct_conversation_id: nil,
222 assert status == expected
225 test "tells if the message is muted for some reason" do
227 other_user = insert(:user)
229 {:ok, _user_relationships} = User.mute(user, other_user)
231 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
232 status = StatusView.render("show.json", %{activity: activity})
234 assert status.muted == false
236 status = StatusView.render("show.json", %{activity: activity, for: user})
238 assert status.muted == true
241 test "tells if the message is thread muted" do
243 other_user = insert(:user)
245 {:ok, _user_relationships} = User.mute(user, other_user)
247 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
248 status = StatusView.render("show.json", %{activity: activity, for: user})
250 assert status.pleroma.thread_muted == false
252 {:ok, activity} = CommonAPI.add_mute(user, activity)
254 status = StatusView.render("show.json", %{activity: activity, for: user})
256 assert status.pleroma.thread_muted == true
259 test "tells if the status is bookmarked" do
262 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
263 status = StatusView.render("show.json", %{activity: activity})
265 assert status.bookmarked == false
267 status = StatusView.render("show.json", %{activity: activity, for: user})
269 assert status.bookmarked == false
271 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
273 activity = Activity.get_by_id_with_object(activity.id)
275 status = StatusView.render("show.json", %{activity: activity, for: user})
277 assert status.bookmarked == true
281 note = insert(:note_activity)
285 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
287 status = StatusView.render("show.json", %{activity: activity})
289 assert status.in_reply_to_id == to_string(note.id)
291 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
293 assert status.in_reply_to_id == to_string(note.id)
296 test "contains mentions" do
298 mentioned = insert(:user)
300 {:ok, activity} = CommonAPI.post(user, %{"status" => "hi @#{mentioned.nickname}"})
302 status = StatusView.render("show.json", %{activity: activity})
304 assert status.mentions ==
305 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
308 test "create mentions from the 'to' field" do
309 %User{ap_id: recipient_ap_id} = insert(:user)
310 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
315 "to" => [recipient_ap_id],
321 insert(:note_activity, %{
323 recipients: [recipient_ap_id | cc]
326 assert length(activity.recipients) == 3
328 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
330 assert length(mentions) == 1
331 assert mention.url == recipient_ap_id
334 test "create mentions from the 'tag' field" do
335 recipient = insert(:user)
336 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
344 "href" => recipient.ap_id,
345 "name" => recipient.nickname,
349 "href" => "https://example.com/search?tag=test",
358 insert(:note_activity, %{
360 recipients: [recipient.ap_id | cc]
363 assert length(activity.recipients) == 3
365 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
367 assert length(mentions) == 1
368 assert mention.url == recipient.ap_id
371 test "attachments" do
376 "mediaType" => "image/png",
387 remote_url: "someurl",
388 preview_url: "someurl",
391 pleroma: %{mime_type: "image/png"}
394 assert expected == StatusView.render("attachment.json", %{attachment: object})
396 # If theres a "id", use that instead of the generated one
397 object = Map.put(object, "id", 2)
398 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
401 test "put the url advertised in the Activity in to the url attribute" do
402 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
403 [activity] = Activity.search(nil, id)
405 status = StatusView.render("show.json", %{activity: activity})
407 assert status.uri == id
408 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
413 activity = insert(:note_activity)
415 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
417 represented = StatusView.render("show.json", %{for: user, activity: reblog})
419 assert represented[:id] == to_string(reblog.id)
420 assert represented[:reblog][:id] == to_string(activity.id)
421 assert represented[:emojis] == []
424 test "a peertube video" do
428 Pleroma.Object.Fetcher.fetch_object_from_id(
429 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
432 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
434 represented = StatusView.render("show.json", %{for: user, activity: activity})
436 assert represented[:id] == to_string(activity.id)
437 assert length(represented[:media_attachments]) == 1
440 test "a Mobilizon event" do
444 Pleroma.Object.Fetcher.fetch_object_from_id(
445 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
448 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
450 represented = StatusView.render("show.json", %{for: user, activity: activity})
452 assert represented[:id] == to_string(activity.id)
455 describe "build_tags/1" do
456 test "it returns a a dictionary tags" do
462 "href" => "https://kawen.space/users/lain",
463 "name" => "@lain@kawen.space",
468 assert StatusView.build_tags(object_tags) == [
469 %{name: "fediverse", url: "/tag/fediverse"},
470 %{name: "mastodon", url: "/tag/mastodon"},
471 %{name: "nextcloud", url: "/tag/nextcloud"}
476 describe "rich media cards" do
477 test "a rich media card without a site name renders correctly" do
478 page_url = "http://example.com"
482 image: page_url <> "/example.jpg",
483 title: "Example website"
486 %{provider_name: "example.com"} =
487 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
490 test "a rich media card without a site name or image renders correctly" do
491 page_url = "http://example.com"
495 title: "Example website"
498 %{provider_name: "example.com"} =
499 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
502 test "a rich media card without an image renders correctly" do
503 page_url = "http://example.com"
507 site_name: "Example site name",
508 title: "Example website"
511 %{provider_name: "example.com"} =
512 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
515 test "a rich media card with all relevant data renders correctly" do
516 page_url = "http://example.com"
520 site_name: "Example site name",
521 title: "Example website",
522 image: page_url <> "/example.jpg",
523 description: "Example description"
526 %{provider_name: "example.com"} =
527 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
531 test "embeds a relationship in the account" do
533 other_user = insert(:user)
536 CommonAPI.post(user, %{
537 "status" => "drink more water"
540 result = StatusView.render("show.json", %{activity: activity, for: other_user})
542 assert result[:account][:pleroma][:relationship] ==
543 AccountView.render("relationship.json", %{user: other_user, target: user})
546 test "embeds a relationship in the account in reposts" do
548 other_user = insert(:user)
551 CommonAPI.post(user, %{
555 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
557 result = StatusView.render("show.json", %{activity: activity, for: user})
559 assert result[:account][:pleroma][:relationship] ==
560 AccountView.render("relationship.json", %{user: user, target: other_user})
562 assert result[:reblog][:account][:pleroma][:relationship] ==
563 AccountView.render("relationship.json", %{user: user, target: user})
566 test "visibility/list" do
569 {:ok, list} = Pleroma.List.create("foo", user)
572 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
574 status = StatusView.render("show.json", activity: activity)
576 assert status.visibility == "list"
579 test "successfully renders a Listen activity (pleroma extension)" do
580 listen_activity = insert(:listen)
582 status = StatusView.render("listen.json", activity: listen_activity)
584 assert status.length == listen_activity.data["object"]["length"]
585 assert status.title == listen_activity.data["object"]["title"]