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.data["object"])
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 user = User.get_cached_by_ap_id(note.data["actor"])
78 convo_id = Utils.context_to_conversation_id(note.data["object"]["context"])
80 status = StatusView.render("status.json", %{activity: note})
83 (note.data["object"]["published"] || "")
84 |> String.replace(~r/\.\d+Z/, ".000Z")
87 id: to_string(note.id),
88 uri: note.data["object"]["id"],
89 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
90 account: AccountView.render("account.json", %{user: user}),
92 in_reply_to_account_id: nil,
95 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
96 created_at: created_at,
107 spoiler_text: HtmlSanitizeEx.basic_html(note.data["object"]["summary"]),
108 visibility: "public",
109 media_attachments: [],
113 name: "#{note.data["object"]["tag"]}",
114 url: "/tag/#{note.data["object"]["tag"]}"
126 static_url: "corndog.png",
127 visible_in_picker: false
132 conversation_id: convo_id,
133 in_reply_to_account_acct: nil,
134 content: %{"text/plain" => HtmlSanitizeEx.strip_tags(note.data["object"]["content"])},
135 spoiler_text: %{"text/plain" => HtmlSanitizeEx.strip_tags(note.data["object"]["summary"])}
139 assert status == expected
142 test "tells if the message is muted for some reason" do
144 other_user = insert(:user)
146 {:ok, user} = User.mute(user, other_user)
148 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
149 status = StatusView.render("status.json", %{activity: activity})
151 assert status.muted == false
153 status = StatusView.render("status.json", %{activity: activity, for: user})
155 assert status.muted == true
158 test "tells if the status is bookmarked" do
161 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
162 status = StatusView.render("status.json", %{activity: activity})
164 assert status.bookmarked == false
166 status = StatusView.render("status.json", %{activity: activity, for: user})
168 assert status.bookmarked == false
170 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
172 activity = Activity.get_by_id_with_object(activity.id)
174 status = StatusView.render("status.json", %{activity: activity, for: user})
176 assert status.bookmarked == true
180 note = insert(:note_activity)
184 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
186 status = StatusView.render("status.json", %{activity: activity})
188 assert status.in_reply_to_id == to_string(note.id)
190 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
192 assert status.in_reply_to_id == to_string(note.id)
195 test "contains mentions" do
196 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
197 # a user with this ap id might be in the cache.
198 recipient = "https://pleroma.soykaf.com/users/lain"
199 user = insert(:user, %{ap_id: recipient})
201 {:ok, [activity]} = OStatus.handle_incoming(incoming)
203 status = StatusView.render("status.json", %{activity: activity})
205 actor = User.get_cached_by_ap_id(activity.actor)
207 assert status.mentions ==
208 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
211 test "attachments" do
216 "mediaType" => "image/png",
227 remote_url: "someurl",
228 preview_url: "someurl",
231 pleroma: %{mime_type: "image/png"}
234 assert expected == StatusView.render("attachment.json", %{attachment: object})
236 # If theres a "id", use that instead of the generated one
237 object = Map.put(object, "id", 2)
238 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
243 activity = insert(:note_activity)
245 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
247 represented = StatusView.render("status.json", %{for: user, activity: reblog})
249 assert represented[:id] == to_string(reblog.id)
250 assert represented[:reblog][:id] == to_string(activity.id)
251 assert represented[:emojis] == []
254 test "a peertube video" do
258 Pleroma.Object.Fetcher.fetch_object_from_id(
259 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
262 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
264 represented = StatusView.render("status.json", %{for: user, activity: activity})
266 assert represented[:id] == to_string(activity.id)
267 assert length(represented[:media_attachments]) == 1
270 describe "build_tags/1" do
271 test "it returns a a dictionary tags" do
277 "href" => "https://kawen.space/users/lain",
278 "name" => "@lain@kawen.space",
283 assert StatusView.build_tags(object_tags) == [
284 %{name: "fediverse", url: "/tag/fediverse"},
285 %{name: "mastodon", url: "/tag/mastodon"},
286 %{name: "nextcloud", url: "/tag/nextcloud"}
291 describe "rich media cards" do
292 test "a rich media card without a site name renders correctly" do
293 page_url = "http://example.com"
297 image: page_url <> "/example.jpg",
298 title: "Example website"
301 %{provider_name: "example.com"} =
302 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
305 test "a rich media card without a site name or image renders correctly" do
306 page_url = "http://example.com"
310 title: "Example website"
313 %{provider_name: "example.com"} =
314 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
317 test "a rich media card without an image renders correctly" do
318 page_url = "http://example.com"
322 site_name: "Example site name",
323 title: "Example website"
326 %{provider_name: "Example site name"} =
327 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
330 test "a rich media card with all relevant data renders correctly" do
331 page_url = "http://example.com"
335 site_name: "Example site name",
336 title: "Example website",
337 image: page_url <> "/example.jpg",
338 description: "Example description"
341 %{provider_name: "Example site name"} =
342 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
346 describe "poll view" do
347 test "renders a poll" do
351 CommonAPI.post(user, %{
352 "status" => "Is Tenshi eating a corndog cute?",
354 "options" => ["absolutely!", "sure", "yes", "why are you even asking?"],
359 object = Object.normalize(activity)
367 %{title: "absolutely!", votes_count: 0},
368 %{title: "sure", votes_count: 0},
369 %{title: "yes", votes_count: 0},
370 %{title: "why are you even asking?", votes_count: 0}
376 result = StatusView.render("poll.json", %{object: object})
377 expires_at = result.expires_at
378 result = Map.delete(result, :expires_at)
380 assert result == expected
382 expires_at = NaiveDateTime.from_iso8601!(expires_at)
383 assert NaiveDateTime.diff(expires_at, NaiveDateTime.utc_now()) in 15..20
386 test "detects if it is multiple choice" do
390 CommonAPI.post(user, %{
391 "status" => "Which Mastodon developer is your favourite?",
393 "options" => ["Gargron", "Eugen"],
399 object = Object.normalize(activity)
401 assert %{multiple: true} = StatusView.render("poll.json", %{object: object})
404 test "detects emoji" do
408 CommonAPI.post(user, %{
409 "status" => "What's with the smug face?",
411 "options" => [":blank: sip", ":blank::blank: sip", ":blank::blank::blank: sip"],
416 object = Object.normalize(activity)
418 assert %{emojis: [%{shortcode: "blank"}]} =
419 StatusView.render("poll.json", %{object: object})
422 test "detects vote status" do
424 other_user = insert(:user)
427 CommonAPI.post(user, %{
428 "status" => "Which input devices do you use?",
430 "options" => ["mouse", "trackball", "trackpoint"],
436 object = Object.normalize(activity)
438 {:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
440 result = StatusView.render("poll.json", %{object: object, for: other_user})
442 assert result[:voted] == true
443 assert Enum.at(result[:options], 1)[:votes_count] == 1
444 assert Enum.at(result[:options], 2)[:votes_count] == 1