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,
106 spoiler_text: HtmlSanitizeEx.basic_html(note.data["object"]["summary"]),
107 visibility: "public",
108 media_attachments: [],
112 name: "#{note.data["object"]["tag"]}",
113 url: "/tag/#{note.data["object"]["tag"]}"
125 static_url: "corndog.png",
126 visible_in_picker: false
131 conversation_id: convo_id,
132 in_reply_to_account_acct: nil,
133 content: %{"text/plain" => HtmlSanitizeEx.strip_tags(note.data["object"]["content"])},
134 spoiler_text: %{"text/plain" => HtmlSanitizeEx.strip_tags(note.data["object"]["summary"])}
138 assert status == expected
141 test "tells if the message is muted for some reason" do
143 other_user = insert(:user)
145 {:ok, user} = User.mute(user, other_user)
147 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
148 status = StatusView.render("status.json", %{activity: activity})
150 assert status.muted == false
152 status = StatusView.render("status.json", %{activity: activity, for: user})
154 assert status.muted == true
157 test "tells if the status is bookmarked" do
160 {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
161 status = StatusView.render("status.json", %{activity: activity})
163 assert status.bookmarked == false
165 status = StatusView.render("status.json", %{activity: activity, for: user})
167 assert status.bookmarked == false
169 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
171 status = StatusView.render("status.json", %{activity: activity, for: user})
173 assert status.bookmarked == true
177 note = insert(:note_activity)
181 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
183 status = StatusView.render("status.json", %{activity: activity})
185 assert status.in_reply_to_id == to_string(note.id)
187 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
189 assert status.in_reply_to_id == to_string(note.id)
192 test "contains mentions" do
193 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
194 # a user with this ap id might be in the cache.
195 recipient = "https://pleroma.soykaf.com/users/lain"
196 user = insert(:user, %{ap_id: recipient})
198 {:ok, [activity]} = OStatus.handle_incoming(incoming)
200 status = StatusView.render("status.json", %{activity: activity})
202 actor = User.get_cached_by_ap_id(activity.actor)
204 assert status.mentions ==
205 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
208 test "attachments" do
213 "mediaType" => "image/png",
224 remote_url: "someurl",
225 preview_url: "someurl",
228 pleroma: %{mime_type: "image/png"}
231 assert expected == StatusView.render("attachment.json", %{attachment: object})
233 # If theres a "id", use that instead of the generated one
234 object = Map.put(object, "id", 2)
235 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
240 activity = insert(:note_activity)
242 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
244 represented = StatusView.render("status.json", %{for: user, activity: reblog})
246 assert represented[:id] == to_string(reblog.id)
247 assert represented[:reblog][:id] == to_string(activity.id)
248 assert represented[:emojis] == []
251 test "a peertube video" do
255 Pleroma.Object.Fetcher.fetch_object_from_id(
256 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
259 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
261 represented = StatusView.render("status.json", %{for: user, activity: activity})
263 assert represented[:id] == to_string(activity.id)
264 assert length(represented[:media_attachments]) == 1
267 describe "build_tags/1" do
268 test "it returns a a dictionary tags" do
274 "href" => "https://kawen.space/users/lain",
275 "name" => "@lain@kawen.space",
280 assert StatusView.build_tags(object_tags) == [
281 %{name: "fediverse", url: "/tag/fediverse"},
282 %{name: "mastodon", url: "/tag/mastodon"},
283 %{name: "nextcloud", url: "/tag/nextcloud"}
288 describe "rich media cards" do
289 test "a rich media card without a site name renders correctly" do
290 page_url = "http://example.com"
294 image: page_url <> "/example.jpg",
295 title: "Example website"
298 %{provider_name: "example.com"} =
299 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
302 test "a rich media card without a site name or image renders correctly" do
303 page_url = "http://example.com"
307 title: "Example website"
310 %{provider_name: "example.com"} =
311 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
314 test "a rich media card without an image renders correctly" do
315 page_url = "http://example.com"
319 site_name: "Example site name",
320 title: "Example website"
323 %{provider_name: "Example site name"} =
324 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
327 test "a rich media card with all relevant data renders correctly" do
328 page_url = "http://example.com"
332 site_name: "Example site name",
333 title: "Example website",
334 image: page_url <> "/example.jpg",
335 description: "Example description"
338 %{provider_name: "Example site name"} =
339 StatusView.render("card.json", %{page_url: page_url, rich_media: card})