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 "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
30 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
31 [participation] = Participation.for_user(user)
34 StatusView.render("show.json",
36 with_direct_conversation_id: true,
40 assert status[:pleroma][:direct_conversation_id] == participation.id
42 status = StatusView.render("show.json", activity: activity, for: user)
43 assert status[:pleroma][:direct_conversation_id] == nil
46 test "returns the direct conversation id when given the `direct_conversation_id` option" do
49 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
50 [participation] = Participation.for_user(user)
53 StatusView.render("show.json",
55 direct_conversation_id: participation.id,
59 assert status[:pleroma][:direct_conversation_id] == participation.id
62 test "returns a temporary ap_id based user for activities missing db users" do
65 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
68 Cachex.clear(:user_cache)
70 %{account: ms_user} = StatusView.render("show.json", activity: activity)
72 assert ms_user.acct == "erroruser@example.com"
75 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
78 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
82 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
85 Cachex.clear(:user_cache)
87 result = StatusView.render("show.json", activity: activity)
89 assert result[:account][:id] == to_string(user.id)
92 test "a note with null content" do
93 note = insert(:note_activity)
94 note_object = Object.normalize(note)
98 |> Map.put("content", nil)
100 Object.change(note_object, %{data: data})
101 |> Object.update_and_set_cache()
103 User.get_cached_by_ap_id(note.data["actor"])
105 status = StatusView.render("show.json", %{activity: note})
107 assert status.content == ""
110 test "a note activity" do
111 note = insert(:note_activity)
112 object_data = Object.normalize(note).data
113 user = User.get_cached_by_ap_id(note.data["actor"])
115 convo_id = Utils.context_to_conversation_id(object_data["context"])
117 status = StatusView.render("show.json", %{activity: note})
120 (object_data["published"] || "")
121 |> String.replace(~r/\.\d+Z/, ".000Z")
124 id: to_string(note.id),
125 uri: object_data["id"],
126 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
127 account: AccountView.render("show.json", %{user: user}),
129 in_reply_to_account_id: nil,
132 content: HTML.filter_tags(object_data["content"]),
133 created_at: created_at,
144 spoiler_text: HTML.filter_tags(object_data["summary"]),
145 visibility: "public",
146 media_attachments: [],
150 name: "#{object_data["tag"]}",
151 url: "/tag/#{object_data["tag"]}"
163 static_url: "corndog.png",
164 visible_in_picker: false
169 conversation_id: convo_id,
170 in_reply_to_account_acct: nil,
171 content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
172 spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
174 direct_conversation_id: nil,
179 assert status == expected
182 test "tells if the message is muted for some reason" do
184 other_user = insert(:user)
186 {:ok, user} = User.mute(user, other_user)
188 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
189 status = StatusView.render("show.json", %{activity: activity})
191 assert status.muted == false
193 status = StatusView.render("show.json", %{activity: activity, for: user})
195 assert status.muted == true
198 test "tells if the message is thread muted" do
200 other_user = insert(:user)
202 {:ok, user} = User.mute(user, other_user)
204 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
205 status = StatusView.render("show.json", %{activity: activity, for: user})
207 assert status.pleroma.thread_muted == false
209 {:ok, activity} = CommonAPI.add_mute(user, activity)
211 status = StatusView.render("show.json", %{activity: activity, for: user})
213 assert status.pleroma.thread_muted == true
216 test "tells if the status is bookmarked" do
219 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
220 status = StatusView.render("show.json", %{activity: activity})
222 assert status.bookmarked == false
224 status = StatusView.render("show.json", %{activity: activity, for: user})
226 assert status.bookmarked == false
228 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
230 activity = Activity.get_by_id_with_object(activity.id)
232 status = StatusView.render("show.json", %{activity: activity, for: user})
234 assert status.bookmarked == true
238 note = insert(:note_activity)
242 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
244 status = StatusView.render("show.json", %{activity: activity})
246 assert status.in_reply_to_id == to_string(note.id)
248 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
250 assert status.in_reply_to_id == to_string(note.id)
253 test "contains mentions" do
255 mentioned = insert(:user)
257 {:ok, activity} = CommonAPI.post(user, %{"status" => "hi @#{mentioned.nickname}"})
259 status = StatusView.render("show.json", %{activity: activity})
261 assert status.mentions ==
262 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
265 test "create mentions from the 'to' field" do
266 %User{ap_id: recipient_ap_id} = insert(:user)
267 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
272 "to" => [recipient_ap_id],
278 insert(:note_activity, %{
280 recipients: [recipient_ap_id | cc]
283 assert length(activity.recipients) == 3
285 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
287 assert length(mentions) == 1
288 assert mention.url == recipient_ap_id
291 test "create mentions from the 'tag' field" do
292 recipient = insert(:user)
293 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
301 "href" => recipient.ap_id,
302 "name" => recipient.nickname,
306 "href" => "https://example.com/search?tag=test",
315 insert(:note_activity, %{
317 recipients: [recipient.ap_id | cc]
320 assert length(activity.recipients) == 3
322 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
324 assert length(mentions) == 1
325 assert mention.url == recipient.ap_id
328 test "attachments" do
333 "mediaType" => "image/png",
344 remote_url: "someurl",
345 preview_url: "someurl",
348 pleroma: %{mime_type: "image/png"}
351 assert expected == StatusView.render("attachment.json", %{attachment: object})
353 # If theres a "id", use that instead of the generated one
354 object = Map.put(object, "id", 2)
355 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
358 test "put the url advertised in the Activity in to the url attribute" do
359 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
360 [activity] = Activity.search(nil, id)
362 status = StatusView.render("show.json", %{activity: activity})
364 assert status.uri == id
365 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
370 activity = insert(:note_activity)
372 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
374 represented = StatusView.render("show.json", %{for: user, activity: reblog})
376 assert represented[:id] == to_string(reblog.id)
377 assert represented[:reblog][:id] == to_string(activity.id)
378 assert represented[:emojis] == []
381 test "a peertube video" do
385 Pleroma.Object.Fetcher.fetch_object_from_id(
386 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
389 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
391 represented = StatusView.render("show.json", %{for: user, activity: activity})
393 assert represented[:id] == to_string(activity.id)
394 assert length(represented[:media_attachments]) == 1
397 describe "build_tags/1" do
398 test "it returns a a dictionary tags" do
404 "href" => "https://kawen.space/users/lain",
405 "name" => "@lain@kawen.space",
410 assert StatusView.build_tags(object_tags) == [
411 %{name: "fediverse", url: "/tag/fediverse"},
412 %{name: "mastodon", url: "/tag/mastodon"},
413 %{name: "nextcloud", url: "/tag/nextcloud"}
418 describe "rich media cards" do
419 test "a rich media card without a site name renders correctly" do
420 page_url = "http://example.com"
424 image: page_url <> "/example.jpg",
425 title: "Example website"
428 %{provider_name: "example.com"} =
429 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
432 test "a rich media card without a site name or image renders correctly" do
433 page_url = "http://example.com"
437 title: "Example website"
440 %{provider_name: "example.com"} =
441 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
444 test "a rich media card without an image renders correctly" do
445 page_url = "http://example.com"
449 site_name: "Example site name",
450 title: "Example website"
453 %{provider_name: "Example site name"} =
454 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
457 test "a rich media card with all relevant data renders correctly" do
458 page_url = "http://example.com"
462 site_name: "Example site name",
463 title: "Example website",
464 image: page_url <> "/example.jpg",
465 description: "Example description"
468 %{provider_name: "Example site name"} =
469 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
473 test "embeds a relationship in the account" do
475 other_user = insert(:user)
478 CommonAPI.post(user, %{
479 "status" => "drink more water"
482 result = StatusView.render("show.json", %{activity: activity, for: other_user})
484 assert result[:account][:pleroma][:relationship] ==
485 AccountView.render("relationship.json", %{user: other_user, target: user})
488 test "embeds a relationship in the account in reposts" do
490 other_user = insert(:user)
493 CommonAPI.post(user, %{
497 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
499 result = StatusView.render("show.json", %{activity: activity, for: user})
501 assert result[:account][:pleroma][:relationship] ==
502 AccountView.render("relationship.json", %{user: user, target: other_user})
504 assert result[:reblog][:account][:pleroma][:relationship] ==
505 AccountView.render("relationship.json", %{user: user, target: user})
508 test "visibility/list" do
511 {:ok, list} = Pleroma.List.create("foo", user)
514 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
516 status = StatusView.render("show.json", activity: activity)
518 assert status.visibility == "list"
521 test "successfully renders a Listen activity (pleroma extension)" do
522 listen_activity = insert(:listen)
524 status = StatusView.render("listen.json", activity: listen_activity)
526 assert status.length == listen_activity.data["object"]["length"]
527 assert status.title == listen_activity.data["object"]["title"]