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
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.CommonAPI.Utils
15 alias Pleroma.Web.MastodonAPI.AccountView
16 alias Pleroma.Web.MastodonAPI.StatusView
17 import Pleroma.Factory
21 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
25 test "returns the direct conversation id when given the `with_conversation_id` option" do
28 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
31 StatusView.render("show.json",
33 with_direct_conversation_id: true,
37 assert status[:pleroma][:direct_conversation_id]
40 test "returns a temporary ap_id based user for activities missing db users" do
43 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
46 Cachex.clear(:user_cache)
48 %{account: ms_user} = StatusView.render("show.json", activity: activity)
50 assert ms_user.acct == "erroruser@example.com"
53 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
56 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
60 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
63 Cachex.clear(:user_cache)
65 result = StatusView.render("show.json", activity: activity)
67 assert result[:account][:id] == to_string(user.id)
70 test "a note with null content" do
71 note = insert(:note_activity)
72 note_object = Object.normalize(note)
76 |> Map.put("content", nil)
78 Object.change(note_object, %{data: data})
79 |> Object.update_and_set_cache()
81 User.get_cached_by_ap_id(note.data["actor"])
83 status = StatusView.render("show.json", %{activity: note})
85 assert status.content == ""
88 test "a note activity" do
89 note = insert(:note_activity)
90 object_data = Object.normalize(note).data
91 user = User.get_cached_by_ap_id(note.data["actor"])
93 convo_id = Utils.context_to_conversation_id(object_data["context"])
95 status = StatusView.render("show.json", %{activity: note})
98 (object_data["published"] || "")
99 |> String.replace(~r/\.\d+Z/, ".000Z")
102 id: to_string(note.id),
103 uri: object_data["id"],
104 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
105 account: AccountView.render("show.json", %{user: user}),
107 in_reply_to_account_id: nil,
110 content: HtmlSanitizeEx.basic_html(object_data["content"]),
111 created_at: created_at,
122 spoiler_text: HtmlSanitizeEx.basic_html(object_data["summary"]),
123 visibility: "public",
124 media_attachments: [],
128 name: "#{object_data["tag"]}",
129 url: "/tag/#{object_data["tag"]}"
141 static_url: "corndog.png",
142 visible_in_picker: false
147 conversation_id: convo_id,
148 in_reply_to_account_acct: nil,
149 content: %{"text/plain" => HtmlSanitizeEx.strip_tags(object_data["content"])},
150 spoiler_text: %{"text/plain" => HtmlSanitizeEx.strip_tags(object_data["summary"])},
152 direct_conversation_id: nil,
157 assert status == expected
160 test "tells if the message is muted for some reason" do
162 other_user = insert(:user)
164 {:ok, user} = User.mute(user, other_user)
166 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
167 status = StatusView.render("show.json", %{activity: activity})
169 assert status.muted == false
171 status = StatusView.render("show.json", %{activity: activity, for: user})
173 assert status.muted == true
176 test "tells if the message is thread muted" do
178 other_user = insert(:user)
180 {:ok, user} = User.mute(user, other_user)
182 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
183 status = StatusView.render("show.json", %{activity: activity, for: user})
185 assert status.pleroma.thread_muted == false
187 {:ok, activity} = CommonAPI.add_mute(user, activity)
189 status = StatusView.render("show.json", %{activity: activity, for: user})
191 assert status.pleroma.thread_muted == true
194 test "tells if the status is bookmarked" do
197 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
198 status = StatusView.render("show.json", %{activity: activity})
200 assert status.bookmarked == false
202 status = StatusView.render("show.json", %{activity: activity, for: user})
204 assert status.bookmarked == false
206 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
208 activity = Activity.get_by_id_with_object(activity.id)
210 status = StatusView.render("show.json", %{activity: activity, for: user})
212 assert status.bookmarked == true
216 note = insert(:note_activity)
220 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
222 status = StatusView.render("show.json", %{activity: activity})
224 assert status.in_reply_to_id == to_string(note.id)
226 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
228 assert status.in_reply_to_id == to_string(note.id)
231 test "contains mentions" do
233 mentioned = insert(:user)
235 {:ok, activity} = CommonAPI.post(user, %{"status" => "hi @#{mentioned.nickname}"})
237 status = StatusView.render("show.json", %{activity: activity})
239 assert status.mentions ==
240 Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
243 test "create mentions from the 'to' field" do
244 %User{ap_id: recipient_ap_id} = insert(:user)
245 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
250 "to" => [recipient_ap_id],
256 insert(:note_activity, %{
258 recipients: [recipient_ap_id | cc]
261 assert length(activity.recipients) == 3
263 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
265 assert length(mentions) == 1
266 assert mention.url == recipient_ap_id
269 test "create mentions from the 'tag' field" do
270 recipient = insert(:user)
271 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
279 "href" => recipient.ap_id,
280 "name" => recipient.nickname,
284 "href" => "https://example.com/search?tag=test",
293 insert(:note_activity, %{
295 recipients: [recipient.ap_id | cc]
298 assert length(activity.recipients) == 3
300 %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
302 assert length(mentions) == 1
303 assert mention.url == recipient.ap_id
306 test "attachments" do
311 "mediaType" => "image/png",
322 remote_url: "someurl",
323 preview_url: "someurl",
326 pleroma: %{mime_type: "image/png"}
329 assert expected == StatusView.render("attachment.json", %{attachment: object})
331 # If theres a "id", use that instead of the generated one
332 object = Map.put(object, "id", 2)
333 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
336 test "put the url advertised in the Activity in to the url attribute" do
337 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
338 [activity] = Activity.search(nil, id)
340 status = StatusView.render("show.json", %{activity: activity})
342 assert status.uri == id
343 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
348 activity = insert(:note_activity)
350 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
352 represented = StatusView.render("show.json", %{for: user, activity: reblog})
354 assert represented[:id] == to_string(reblog.id)
355 assert represented[:reblog][:id] == to_string(activity.id)
356 assert represented[:emojis] == []
359 test "a peertube video" do
363 Pleroma.Object.Fetcher.fetch_object_from_id(
364 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
367 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
369 represented = StatusView.render("show.json", %{for: user, activity: activity})
371 assert represented[:id] == to_string(activity.id)
372 assert length(represented[:media_attachments]) == 1
375 describe "build_tags/1" do
376 test "it returns a a dictionary tags" do
382 "href" => "https://kawen.space/users/lain",
383 "name" => "@lain@kawen.space",
388 assert StatusView.build_tags(object_tags) == [
389 %{name: "fediverse", url: "/tag/fediverse"},
390 %{name: "mastodon", url: "/tag/mastodon"},
391 %{name: "nextcloud", url: "/tag/nextcloud"}
396 describe "rich media cards" do
397 test "a rich media card without a site name renders correctly" do
398 page_url = "http://example.com"
402 image: page_url <> "/example.jpg",
403 title: "Example website"
406 %{provider_name: "example.com"} =
407 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
410 test "a rich media card without a site name or image renders correctly" do
411 page_url = "http://example.com"
415 title: "Example website"
418 %{provider_name: "example.com"} =
419 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
422 test "a rich media card without an image renders correctly" do
423 page_url = "http://example.com"
427 site_name: "Example site name",
428 title: "Example website"
431 %{provider_name: "Example site name"} =
432 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
435 test "a rich media card with all relevant data renders correctly" do
436 page_url = "http://example.com"
440 site_name: "Example site name",
441 title: "Example website",
442 image: page_url <> "/example.jpg",
443 description: "Example description"
446 %{provider_name: "Example site name"} =
447 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
451 test "embeds a relationship in the account" do
453 other_user = insert(:user)
456 CommonAPI.post(user, %{
457 "status" => "drink more water"
460 result = StatusView.render("show.json", %{activity: activity, for: other_user})
462 assert result[:account][:pleroma][:relationship] ==
463 AccountView.render("relationship.json", %{user: other_user, target: user})
466 test "embeds a relationship in the account in reposts" do
468 other_user = insert(:user)
471 CommonAPI.post(user, %{
475 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
477 result = StatusView.render("show.json", %{activity: activity, for: user})
479 assert result[:account][:pleroma][:relationship] ==
480 AccountView.render("relationship.json", %{user: user, target: other_user})
482 assert result[:reblog][:account][:pleroma][:relationship] ==
483 AccountView.render("relationship.json", %{user: user, target: user})
486 test "visibility/list" do
489 {:ok, list} = Pleroma.List.create("foo", user)
492 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
494 status = StatusView.render("show.json", activity: activity)
496 assert status.visibility == "list"
499 test "successfully renders a Listen activity (pleroma extension)" do
500 listen_activity = insert(:listen)
502 status = StatusView.render("listen.json", activity: listen_activity)
504 assert status.length == listen_activity.data["object"]["length"]
505 assert status.title == listen_activity.data["object"]["title"]