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"])}
140 assert status == expected
143 test "tells if the message is muted for some reason" do
145 other_user = insert(:user)
147 {:ok, user} = User.mute(user, other_user)
149 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
150 status = StatusView.render("status.json", %{activity: activity})
152 assert status.muted == false
154 status = StatusView.render("status.json", %{activity: activity, for: user})
156 assert status.muted == true
159 test "tells if the status is bookmarked" do
162 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
163 status = StatusView.render("status.json", %{activity: activity})
165 assert status.bookmarked == false
167 status = StatusView.render("status.json", %{activity: activity, for: user})
169 assert status.bookmarked == false
171 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
173 activity = Activity.get_by_id_with_object(activity.id)
175 status = StatusView.render("status.json", %{activity: activity, for: user})
177 assert status.bookmarked == true
181 note = insert(:note_activity)
185 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
187 status = StatusView.render("status.json", %{activity: activity})
189 assert status.in_reply_to_id == to_string(note.id)
191 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
193 assert status.in_reply_to_id == to_string(note.id)
196 test "contains mentions" do
197 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
198 # a user with this ap id might be in the cache.
199 recipient = "https://pleroma.soykaf.com/users/lain"
200 user = insert(:user, %{ap_id: recipient})
202 {:ok, [activity]} = OStatus.handle_incoming(incoming)
204 status = StatusView.render("status.json", %{activity: activity})
206 actor = User.get_cached_by_ap_id(activity.actor)
208 assert status.mentions ==
209 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
212 test "attachments" do
217 "mediaType" => "image/png",
228 remote_url: "someurl",
229 preview_url: "someurl",
232 pleroma: %{mime_type: "image/png"}
235 assert expected == StatusView.render("attachment.json", %{attachment: object})
237 # If theres a "id", use that instead of the generated one
238 object = Map.put(object, "id", 2)
239 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
244 activity = insert(:note_activity)
246 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
248 represented = StatusView.render("status.json", %{for: user, activity: reblog})
250 assert represented[:id] == to_string(reblog.id)
251 assert represented[:reblog][:id] == to_string(activity.id)
252 assert represented[:emojis] == []
255 test "a peertube video" do
259 Pleroma.Object.Fetcher.fetch_object_from_id(
260 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
263 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
265 represented = StatusView.render("status.json", %{for: user, activity: activity})
267 assert represented[:id] == to_string(activity.id)
268 assert length(represented[:media_attachments]) == 1
271 describe "build_tags/1" do
272 test "it returns a a dictionary tags" do
278 "href" => "https://kawen.space/users/lain",
279 "name" => "@lain@kawen.space",
284 assert StatusView.build_tags(object_tags) == [
285 %{name: "fediverse", url: "/tag/fediverse"},
286 %{name: "mastodon", url: "/tag/mastodon"},
287 %{name: "nextcloud", url: "/tag/nextcloud"}
292 describe "rich media cards" do
293 test "a rich media card without a site name renders correctly" do
294 page_url = "http://example.com"
298 image: page_url <> "/example.jpg",
299 title: "Example website"
302 %{provider_name: "example.com"} =
303 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
306 test "a rich media card without a site name or image renders correctly" do
307 page_url = "http://example.com"
311 title: "Example website"
314 %{provider_name: "example.com"} =
315 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
318 test "a rich media card without an image renders correctly" do
319 page_url = "http://example.com"
323 site_name: "Example site name",
324 title: "Example website"
327 %{provider_name: "Example site name"} =
328 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
331 test "a rich media card with all relevant data renders correctly" do
332 page_url = "http://example.com"
336 site_name: "Example site name",
337 title: "Example website",
338 image: page_url <> "/example.jpg",
339 description: "Example description"
342 %{provider_name: "Example site name"} =
343 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
347 describe "poll view" do
348 test "renders a poll" do
352 CommonAPI.post(user, %{
353 "status" => "Is Tenshi eating a corndog cute?",
355 "options" => ["absolutely!", "sure", "yes", "why are you even asking?"],
360 object = Object.normalize(activity)
368 %{title: "absolutely!", votes_count: 0},
369 %{title: "sure", votes_count: 0},
370 %{title: "yes", votes_count: 0},
371 %{title: "why are you even asking?", votes_count: 0}
377 result = StatusView.render("poll.json", %{object: object})
378 expires_at = result.expires_at
379 result = Map.delete(result, :expires_at)
381 assert result == expected
383 expires_at = NaiveDateTime.from_iso8601!(expires_at)
384 assert NaiveDateTime.diff(expires_at, NaiveDateTime.utc_now()) in 15..20
387 test "detects if it is multiple choice" do
391 CommonAPI.post(user, %{
392 "status" => "Which Mastodon developer is your favourite?",
394 "options" => ["Gargron", "Eugen"],
400 object = Object.normalize(activity)
402 assert %{multiple: true} = StatusView.render("poll.json", %{object: object})
405 test "detects emoji" do
409 CommonAPI.post(user, %{
410 "status" => "What's with the smug face?",
412 "options" => [":blank: sip", ":blank::blank: sip", ":blank::blank::blank: sip"],
417 object = Object.normalize(activity)
419 assert %{emojis: [%{shortcode: "blank"}]} =
420 StatusView.render("poll.json", %{object: object})
423 test "detects vote status" do
425 other_user = insert(:user)
428 CommonAPI.post(user, %{
429 "status" => "Which input devices do you use?",
431 "options" => ["mouse", "trackball", "trackpoint"],
437 object = Object.normalize(activity)
439 {:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
441 result = StatusView.render("poll.json", %{object: object, for: other_user})
443 assert result[:voted] == true
444 assert Enum.at(result[:options], 1)[:votes_count] == 1
445 assert Enum.at(result[:options], 2)[:votes_count] == 1
449 test "embeds a relationship in the account" do
451 other_user = insert(:user)
454 CommonAPI.post(user, %{
455 "status" => "drink more water"
458 result = StatusView.render("status.json", %{activity: activity, for: other_user})
460 assert result[:account][:pleroma][:relationship] ==
461 AccountView.render("relationship.json", %{user: other_user, target: user})
464 test "embeds a relationship in the account in reposts" do
466 other_user = insert(:user)
469 CommonAPI.post(user, %{
473 {:ok, activity, _object} = CommonAPI.repeat(activity.id, other_user)
475 result = StatusView.render("status.json", %{activity: activity, for: user})
477 assert result[:account][:pleroma][:relationship] ==
478 AccountView.render("relationship.json", %{user: user, target: other_user})
480 assert result[:reblog][:account][:pleroma][:relationship] ==
481 AccountView.render("relationship.json", %{user: user, target: user})