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 a temporary ap_id based user for activities missing db users" do
29 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
32 Cachex.clear(:user_cache)
34 %{account: ms_user} = StatusView.render("status.json", activity: activity)
36 assert ms_user.acct == "erroruser@example.com"
39 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
42 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
46 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
49 Cachex.clear(:user_cache)
51 result = StatusView.render("status.json", activity: activity)
53 assert result[:account][:id] == to_string(user.id)
56 test "a note with null content" do
57 note = insert(:note_activity)
58 note_object = Object.normalize(note)
62 |> Map.put("content", nil)
64 Object.change(note_object, %{data: data})
65 |> Object.update_and_set_cache()
67 User.get_cached_by_ap_id(note.data["actor"])
69 status = StatusView.render("status.json", %{activity: note})
71 assert status.content == ""
74 test "a note activity" do
75 note = insert(:note_activity)
76 object_data = Object.normalize(note).data
77 user = User.get_cached_by_ap_id(note.data["actor"])
79 convo_id = Utils.context_to_conversation_id(object_data["context"])
81 status = StatusView.render("status.json", %{activity: note})
84 (object_data["published"] || "")
85 |> String.replace(~r/\.\d+Z/, ".000Z")
88 id: to_string(note.id),
89 uri: object_data["id"],
90 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
91 account: AccountView.render("account.json", %{user: user}),
93 in_reply_to_account_id: nil,
96 content: HtmlSanitizeEx.basic_html(object_data["content"]),
97 created_at: created_at,
108 spoiler_text: HtmlSanitizeEx.basic_html(object_data["summary"]),
109 visibility: "public",
110 media_attachments: [],
114 name: "#{object_data["tag"]}",
115 url: "/tag/#{object_data["tag"]}"
127 static_url: "corndog.png",
128 visible_in_picker: false
133 conversation_id: convo_id,
134 in_reply_to_account_acct: nil,
135 content: %{"text/plain" => HtmlSanitizeEx.strip_tags(object_data["content"])},
136 spoiler_text: %{"text/plain" => HtmlSanitizeEx.strip_tags(object_data["summary"])},
141 assert status == expected
144 test "tells if the message is muted for some reason" do
146 other_user = insert(:user)
148 {:ok, user} = User.mute(user, other_user)
150 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
151 status = StatusView.render("status.json", %{activity: activity})
153 assert status.muted == false
155 status = StatusView.render("status.json", %{activity: activity, for: user})
157 assert status.muted == true
160 test "tells if the status is bookmarked" do
163 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
164 status = StatusView.render("status.json", %{activity: activity})
166 assert status.bookmarked == false
168 status = StatusView.render("status.json", %{activity: activity, for: user})
170 assert status.bookmarked == false
172 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
174 activity = Activity.get_by_id_with_object(activity.id)
176 status = StatusView.render("status.json", %{activity: activity, for: user})
178 assert status.bookmarked == true
182 note = insert(:note_activity)
186 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
188 status = StatusView.render("status.json", %{activity: activity})
190 assert status.in_reply_to_id == to_string(note.id)
192 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
194 assert status.in_reply_to_id == to_string(note.id)
197 test "contains mentions" do
198 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
199 # a user with this ap id might be in the cache.
200 recipient = "https://pleroma.soykaf.com/users/lain"
201 user = insert(:user, %{ap_id: recipient})
203 {:ok, [activity]} = OStatus.handle_incoming(incoming)
205 status = StatusView.render("status.json", %{activity: activity})
207 assert status.mentions ==
208 Enum.map([user], fn u -> AccountView.render("mention.json", %{user: u}) end)
211 test "create mentions from the 'to' field" do
212 %User{ap_id: recipient_ap_id} = insert(:user)
213 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
218 "to" => [recipient_ap_id],
224 insert(:note_activity, %{
226 recipients: [recipient_ap_id | cc]
229 assert length(activity.recipients) == 3
231 %{mentions: [mention] = mentions} = StatusView.render("status.json", %{activity: activity})
233 assert length(mentions) == 1
234 assert mention.url == recipient_ap_id
237 test "create mentions from the 'tag' field" do
238 recipient = insert(:user)
239 cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
247 "href" => recipient.ap_id,
248 "name" => recipient.nickname,
252 "href" => "https://example.com/search?tag=test",
261 insert(:note_activity, %{
263 recipients: [recipient.ap_id | cc]
266 assert length(activity.recipients) == 3
268 %{mentions: [mention] = mentions} = StatusView.render("status.json", %{activity: activity})
270 assert length(mentions) == 1
271 assert mention.url == recipient.ap_id
274 test "attachments" do
279 "mediaType" => "image/png",
290 remote_url: "someurl",
291 preview_url: "someurl",
294 pleroma: %{mime_type: "image/png"}
297 assert expected == StatusView.render("attachment.json", %{attachment: object})
299 # If theres a "id", use that instead of the generated one
300 object = Map.put(object, "id", 2)
301 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
306 activity = insert(:note_activity)
308 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
310 represented = StatusView.render("status.json", %{for: user, activity: reblog})
312 assert represented[:id] == to_string(reblog.id)
313 assert represented[:reblog][:id] == to_string(activity.id)
314 assert represented[:emojis] == []
317 test "a peertube video" do
321 Pleroma.Object.Fetcher.fetch_object_from_id(
322 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
325 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
327 represented = StatusView.render("status.json", %{for: user, activity: activity})
329 assert represented[:id] == to_string(activity.id)
330 assert length(represented[:media_attachments]) == 1
333 describe "build_tags/1" do
334 test "it returns a a dictionary tags" do
340 "href" => "https://kawen.space/users/lain",
341 "name" => "@lain@kawen.space",
346 assert StatusView.build_tags(object_tags) == [
347 %{name: "fediverse", url: "/tag/fediverse"},
348 %{name: "mastodon", url: "/tag/mastodon"},
349 %{name: "nextcloud", url: "/tag/nextcloud"}
354 describe "rich media cards" do
355 test "a rich media card without a site name renders correctly" do
356 page_url = "http://example.com"
360 image: page_url <> "/example.jpg",
361 title: "Example website"
364 %{provider_name: "example.com"} =
365 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
368 test "a rich media card without a site name or image renders correctly" do
369 page_url = "http://example.com"
373 title: "Example website"
376 %{provider_name: "example.com"} =
377 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
380 test "a rich media card without an image renders correctly" do
381 page_url = "http://example.com"
385 site_name: "Example site name",
386 title: "Example website"
389 %{provider_name: "Example site name"} =
390 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
393 test "a rich media card with all relevant data renders correctly" do
394 page_url = "http://example.com"
398 site_name: "Example site name",
399 title: "Example website",
400 image: page_url <> "/example.jpg",
401 description: "Example description"
404 %{provider_name: "Example site name"} =
405 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
409 describe "poll view" do
410 test "renders a poll" do
414 CommonAPI.post(user, %{
415 "status" => "Is Tenshi eating a corndog cute?",
417 "options" => ["absolutely!", "sure", "yes", "why are you even asking?"],
422 object = Object.normalize(activity)
427 id: to_string(object.id),
430 %{title: "absolutely!", votes_count: 0},
431 %{title: "sure", votes_count: 0},
432 %{title: "yes", votes_count: 0},
433 %{title: "why are you even asking?", votes_count: 0}
439 result = StatusView.render("poll.json", %{object: object})
440 expires_at = result.expires_at
441 result = Map.delete(result, :expires_at)
443 assert result == expected
445 expires_at = NaiveDateTime.from_iso8601!(expires_at)
446 assert NaiveDateTime.diff(expires_at, NaiveDateTime.utc_now()) in 15..20
449 test "detects if it is multiple choice" do
453 CommonAPI.post(user, %{
454 "status" => "Which Mastodon developer is your favourite?",
456 "options" => ["Gargron", "Eugen"],
462 object = Object.normalize(activity)
464 assert %{multiple: true} = StatusView.render("poll.json", %{object: object})
467 test "detects emoji" do
471 CommonAPI.post(user, %{
472 "status" => "What's with the smug face?",
474 "options" => [":blank: sip", ":blank::blank: sip", ":blank::blank::blank: sip"],
479 object = Object.normalize(activity)
481 assert %{emojis: [%{shortcode: "blank"}]} =
482 StatusView.render("poll.json", %{object: object})
485 test "detects vote status" do
487 other_user = insert(:user)
490 CommonAPI.post(user, %{
491 "status" => "Which input devices do you use?",
493 "options" => ["mouse", "trackball", "trackpoint"],
499 object = Object.normalize(activity)
501 {:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
503 result = StatusView.render("poll.json", %{object: object, for: other_user})
505 assert result[:voted] == true
506 assert Enum.at(result[:options], 1)[:votes_count] == 1
507 assert Enum.at(result[:options], 2)[:votes_count] == 1
511 test "embeds a relationship in the account" do
513 other_user = insert(:user)
516 CommonAPI.post(user, %{
517 "status" => "drink more water"
520 result = StatusView.render("status.json", %{activity: activity, for: other_user})
522 assert result[:account][:pleroma][:relationship] ==
523 AccountView.render("relationship.json", %{user: other_user, target: user})
526 test "embeds a relationship in the account in reposts" do
528 other_user = insert(:user)
531 CommonAPI.post(user, %{
535 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
537 result = StatusView.render("status.json", %{activity: activity, for: user})
539 assert result[:account][:pleroma][:relationship] ==
540 AccountView.render("relationship.json", %{user: user, target: other_user})
542 assert result[:reblog][:account][:pleroma][:relationship] ==
543 AccountView.render("relationship.json", %{user: user, target: user})
546 test "visibility/list" do
549 {:ok, list} = Pleroma.List.create("foo", user)
552 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
554 status = StatusView.render("status.json", activity: activity)
556 assert status.visibility == "list"