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 activity = Activity.get_by_id_with_object(activity.id)
173 status = StatusView.render("status.json", %{activity: activity, for: user})
175 assert status.bookmarked == true
179 note = insert(:note_activity)
183 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
185 status = StatusView.render("status.json", %{activity: activity})
187 assert status.in_reply_to_id == to_string(note.id)
189 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
191 assert status.in_reply_to_id == to_string(note.id)
194 test "contains mentions" do
195 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
196 # a user with this ap id might be in the cache.
197 recipient = "https://pleroma.soykaf.com/users/lain"
198 user = insert(:user, %{ap_id: recipient})
200 {:ok, [activity]} = OStatus.handle_incoming(incoming)
202 status = StatusView.render("status.json", %{activity: activity})
204 actor = User.get_cached_by_ap_id(activity.actor)
206 assert status.mentions ==
207 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
210 test "attachments" do
215 "mediaType" => "image/png",
226 remote_url: "someurl",
227 preview_url: "someurl",
230 pleroma: %{mime_type: "image/png"}
233 assert expected == StatusView.render("attachment.json", %{attachment: object})
235 # If theres a "id", use that instead of the generated one
236 object = Map.put(object, "id", 2)
237 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
242 activity = insert(:note_activity)
244 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
246 represented = StatusView.render("status.json", %{for: user, activity: reblog})
248 assert represented[:id] == to_string(reblog.id)
249 assert represented[:reblog][:id] == to_string(activity.id)
250 assert represented[:emojis] == []
253 test "a peertube video" do
257 Pleroma.Object.Fetcher.fetch_object_from_id(
258 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
261 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
263 represented = StatusView.render("status.json", %{for: user, activity: activity})
265 assert represented[:id] == to_string(activity.id)
266 assert length(represented[:media_attachments]) == 1
269 describe "build_tags/1" do
270 test "it returns a a dictionary tags" do
276 "href" => "https://kawen.space/users/lain",
277 "name" => "@lain@kawen.space",
282 assert StatusView.build_tags(object_tags) == [
283 %{name: "fediverse", url: "/tag/fediverse"},
284 %{name: "mastodon", url: "/tag/mastodon"},
285 %{name: "nextcloud", url: "/tag/nextcloud"}
290 describe "rich media cards" do
291 test "a rich media card without a site name renders correctly" do
292 page_url = "http://example.com"
296 image: page_url <> "/example.jpg",
297 title: "Example website"
300 %{provider_name: "example.com"} =
301 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
304 test "a rich media card without a site name or image renders correctly" do
305 page_url = "http://example.com"
309 title: "Example website"
312 %{provider_name: "example.com"} =
313 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
316 test "a rich media card without an image renders correctly" do
317 page_url = "http://example.com"
321 site_name: "Example site name",
322 title: "Example website"
325 %{provider_name: "Example site name"} =
326 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
329 test "a rich media card with all relevant data renders correctly" do
330 page_url = "http://example.com"
334 site_name: "Example site name",
335 title: "Example website",
336 image: page_url <> "/example.jpg",
337 description: "Example description"
340 %{provider_name: "Example site name"} =
341 StatusView.render("card.json", %{page_url: page_url, rich_media: card})