1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 alias Pleroma.Web.OStatus
18 import Pleroma.Factory
22 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 test "returns the direct conversation id when given the `with_conversation_id` option" do
29 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
32 StatusView.render("status.json",
34 with_direct_conversation_id: true,
38 assert status[:pleroma][:direct_conversation_id]
41 test "returns a temporary ap_id based user for activities missing db users" do
44 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
47 Cachex.clear(:user_cache)
49 %{account: ms_user} = StatusView.render("status.json", activity: activity)
51 assert ms_user.acct == "erroruser@example.com"
54 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
57 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
61 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
64 Cachex.clear(:user_cache)
66 result = StatusView.render("status.json", activity: activity)
68 assert result[:account][:id] == to_string(user.id)
71 test "a note with null content" do
72 note = insert(:note_activity)
73 note_object = Object.normalize(note)
77 |> Map.put("content", nil)
79 Object.change(note_object, %{data: data})
80 |> Object.update_and_set_cache()
82 User.get_cached_by_ap_id(note.data["actor"])
84 status = StatusView.render("status.json", %{activity: note})
86 assert status.content == ""
89 test "a note activity" do
90 note = insert(:note_activity)
91 object_data = Object.normalize(note).data
92 user = User.get_cached_by_ap_id(note.data["actor"])
94 convo_id = Utils.context_to_conversation_id(object_data["context"])
96 status = StatusView.render("status.json", %{activity: note})
99 (object_data["published"] || "")
100 |> String.replace(~r/\.\d+Z/, ".000Z")
103 id: to_string(note.id),
104 uri: object_data["id"],
105 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
106 account: AccountView.render("account.json", %{user: user}),
108 in_reply_to_account_id: nil,
111 content: HtmlSanitizeEx.basic_html(object_data["content"]),
112 created_at: created_at,
123 spoiler_text: HtmlSanitizeEx.basic_html(object_data["summary"]),
124 visibility: "public",
125 media_attachments: [],
129 name: "#{object_data["tag"]}",
130 url: "/tag/#{object_data["tag"]}"
142 static_url: "corndog.png",
143 visible_in_picker: false
148 conversation_id: convo_id,
149 in_reply_to_account_acct: nil,
150 content: %{"text/plain" => HtmlSanitizeEx.strip_tags(object_data["content"])},
151 spoiler_text: %{"text/plain" => HtmlSanitizeEx.strip_tags(object_data["summary"])},
153 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("status.json", %{activity: activity})
169 assert status.muted == false
171 status = StatusView.render("status.json", %{activity: activity, for: user})
173 assert status.muted == true
176 test "tells if the status is bookmarked" do
179 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
180 status = StatusView.render("status.json", %{activity: activity})
182 assert status.bookmarked == false
184 status = StatusView.render("status.json", %{activity: activity, for: user})
186 assert status.bookmarked == false
188 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
190 activity = Activity.get_by_id_with_object(activity.id)
192 status = StatusView.render("status.json", %{activity: activity, for: user})
194 assert status.bookmarked == true
198 note = insert(:note_activity)
202 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
204 status = StatusView.render("status.json", %{activity: activity})
206 assert status.in_reply_to_id == to_string(note.id)
208 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
210 assert status.in_reply_to_id == to_string(note.id)
213 test "contains mentions" do
214 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
215 # a user with this ap id might be in the cache.
216 recipient = "https://pleroma.soykaf.com/users/lain"
217 user = insert(:user, %{ap_id: recipient})
219 {:ok, [activity]} = OStatus.handle_incoming(incoming)
221 status = StatusView.render("status.json", %{activity: activity})
223 assert status.mentions ==
224 Enum.map([user], fn u -> AccountView.render("mention.json", %{user: u}) end)
227 test "create mentions from the 'to' field" do
228 %User{ap_id: recipient_ap_id} = insert(:user)
229 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
234 "to" => [recipient_ap_id],
240 insert(:note_activity, %{
242 recipients: [recipient_ap_id | cc]
245 assert length(activity.recipients) == 3
247 %{mentions: [mention] = mentions} = StatusView.render("status.json", %{activity: activity})
249 assert length(mentions) == 1
250 assert mention.url == recipient_ap_id
253 test "create mentions from the 'tag' field" do
254 recipient = insert(:user)
255 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
263 "href" => recipient.ap_id,
264 "name" => recipient.nickname,
268 "href" => "https://example.com/search?tag=test",
277 insert(:note_activity, %{
279 recipients: [recipient.ap_id | cc]
282 assert length(activity.recipients) == 3
284 %{mentions: [mention] = mentions} = StatusView.render("status.json", %{activity: activity})
286 assert length(mentions) == 1
287 assert mention.url == recipient.ap_id
290 test "attachments" do
295 "mediaType" => "image/png",
306 remote_url: "someurl",
307 preview_url: "someurl",
310 pleroma: %{mime_type: "image/png"}
313 assert expected == StatusView.render("attachment.json", %{attachment: object})
315 # If theres a "id", use that instead of the generated one
316 object = Map.put(object, "id", 2)
317 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
320 test "put the url advertised in the Activity in to the url attribute" do
321 id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
322 [activity] = Activity.search(nil, id)
324 status = StatusView.render("status.json", %{activity: activity})
326 assert status.uri == id
327 assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
332 activity = insert(:note_activity)
334 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
336 represented = StatusView.render("status.json", %{for: user, activity: reblog})
338 assert represented[:id] == to_string(reblog.id)
339 assert represented[:reblog][:id] == to_string(activity.id)
340 assert represented[:emojis] == []
343 test "a peertube video" do
347 Pleroma.Object.Fetcher.fetch_object_from_id(
348 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
351 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
353 represented = StatusView.render("status.json", %{for: user, activity: activity})
355 assert represented[:id] == to_string(activity.id)
356 assert length(represented[:media_attachments]) == 1
359 describe "build_tags/1" do
360 test "it returns a a dictionary tags" do
366 "href" => "https://kawen.space/users/lain",
367 "name" => "@lain@kawen.space",
372 assert StatusView.build_tags(object_tags) == [
373 %{name: "fediverse", url: "/tag/fediverse"},
374 %{name: "mastodon", url: "/tag/mastodon"},
375 %{name: "nextcloud", url: "/tag/nextcloud"}
380 describe "rich media cards" do
381 test "a rich media card without a site name renders correctly" do
382 page_url = "http://example.com"
386 image: page_url <> "/example.jpg",
387 title: "Example website"
390 %{provider_name: "example.com"} =
391 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
394 test "a rich media card without a site name or image renders correctly" do
395 page_url = "http://example.com"
399 title: "Example website"
402 %{provider_name: "example.com"} =
403 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
406 test "a rich media card without an image renders correctly" do
407 page_url = "http://example.com"
411 site_name: "Example site name",
412 title: "Example website"
415 %{provider_name: "Example site name"} =
416 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
419 test "a rich media card with all relevant data renders correctly" do
420 page_url = "http://example.com"
424 site_name: "Example site name",
425 title: "Example website",
426 image: page_url <> "/example.jpg",
427 description: "Example description"
430 %{provider_name: "Example site name"} =
431 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
435 describe "poll view" do
436 test "renders a poll" do
440 CommonAPI.post(user, %{
441 "status" => "Is Tenshi eating a corndog cute?",
443 "options" => ["absolutely!", "sure", "yes", "why are you even asking?"],
448 object = Object.normalize(activity)
453 id: to_string(object.id),
456 %{title: "absolutely!", votes_count: 0},
457 %{title: "sure", votes_count: 0},
458 %{title: "yes", votes_count: 0},
459 %{title: "why are you even asking?", votes_count: 0}
465 result = StatusView.render("poll.json", %{object: object})
466 expires_at = result.expires_at
467 result = Map.delete(result, :expires_at)
469 assert result == expected
471 expires_at = NaiveDateTime.from_iso8601!(expires_at)
472 assert NaiveDateTime.diff(expires_at, NaiveDateTime.utc_now()) in 15..20
475 test "detects if it is multiple choice" do
479 CommonAPI.post(user, %{
480 "status" => "Which Mastodon developer is your favourite?",
482 "options" => ["Gargron", "Eugen"],
488 object = Object.normalize(activity)
490 assert %{multiple: true} = StatusView.render("poll.json", %{object: object})
493 test "detects emoji" do
497 CommonAPI.post(user, %{
498 "status" => "What's with the smug face?",
500 "options" => [":blank: sip", ":blank::blank: sip", ":blank::blank::blank: sip"],
505 object = Object.normalize(activity)
507 assert %{emojis: [%{shortcode: "blank"}]} =
508 StatusView.render("poll.json", %{object: object})
511 test "detects vote status" do
513 other_user = insert(:user)
516 CommonAPI.post(user, %{
517 "status" => "Which input devices do you use?",
519 "options" => ["mouse", "trackball", "trackpoint"],
525 object = Object.normalize(activity)
527 {:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
529 result = StatusView.render("poll.json", %{object: object, for: other_user})
531 assert result[:voted] == true
532 assert Enum.at(result[:options], 1)[:votes_count] == 1
533 assert Enum.at(result[:options], 2)[:votes_count] == 1
537 test "embeds a relationship in the account" do
539 other_user = insert(:user)
542 CommonAPI.post(user, %{
543 "status" => "drink more water"
546 result = StatusView.render("status.json", %{activity: activity, for: other_user})
548 assert result[:account][:pleroma][:relationship] ==
549 AccountView.render("relationship.json", %{user: other_user, target: user})
552 test "embeds a relationship in the account in reposts" do
554 other_user = insert(:user)
557 CommonAPI.post(user, %{
561 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
563 result = StatusView.render("status.json", %{activity: activity, for: user})
565 assert result[:account][:pleroma][:relationship] ==
566 AccountView.render("relationship.json", %{user: user, target: other_user})
568 assert result[:reblog][:account][:pleroma][:relationship] ==
569 AccountView.render("relationship.json", %{user: user, target: user})
572 test "visibility/list" do
575 {:ok, list} = Pleroma.List.create("foo", user)
578 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
580 status = StatusView.render("status.json", activity: activity)
582 assert status.visibility == "list"