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
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.CommonAPI.Utils
14 alias Pleroma.Web.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.StatusView
16 alias Pleroma.Web.OStatus
17 import Pleroma.Factory
21 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
25 test "returns a temporary ap_id based user for activities missing db users" do
28 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
31 Cachex.clear(:user_cache)
33 %{account: ms_user} = StatusView.render("status.json", activity: activity)
35 assert ms_user.acct == "erroruser@example.com"
38 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
41 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
45 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
48 Cachex.clear(:user_cache)
50 result = StatusView.render("status.json", activity: activity)
52 assert result[:account][:id] == to_string(user.id)
55 test "a note with null content" do
56 note = insert(:note_activity)
57 note_object = Object.normalize(note.data["object"])
61 |> Map.put("content", nil)
63 Object.change(note_object, %{data: data})
64 |> Object.update_and_set_cache()
66 User.get_cached_by_ap_id(note.data["actor"])
68 status = StatusView.render("status.json", %{activity: note})
70 assert status.content == ""
73 test "a note activity" do
74 note = insert(:note_activity)
75 user = User.get_cached_by_ap_id(note.data["actor"])
77 convo_id = Utils.context_to_conversation_id(note.data["object"]["context"])
79 status = StatusView.render("status.json", %{activity: note})
82 (note.data["object"]["published"] || "")
83 |> String.replace(~r/\.\d+Z/, ".000Z")
86 id: to_string(note.id),
87 uri: note.data["object"]["id"],
88 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
89 account: AccountView.render("account.json", %{user: user}),
91 in_reply_to_account_id: nil,
94 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
95 created_at: created_at,
105 spoiler_text: HtmlSanitizeEx.basic_html(note.data["object"]["summary"]),
106 visibility: "public",
107 media_attachments: [],
111 name: "#{note.data["object"]["tag"]}",
112 url: "/tag/#{note.data["object"]["tag"]}"
124 static_url: "corndog.png",
125 visible_in_picker: false
130 conversation_id: convo_id,
131 in_reply_to_account_acct: nil,
132 content: %{"text/plain" => HtmlSanitizeEx.strip_tags(note.data["object"]["content"])},
133 spoiler_text: %{"text/plain" => HtmlSanitizeEx.strip_tags(note.data["object"]["summary"])}
137 assert status == expected
140 test "tells if the message is muted for some reason" do
142 other_user = insert(:user)
144 {:ok, user} = User.mute(user, other_user)
146 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
147 status = StatusView.render("status.json", %{activity: activity})
149 assert status.muted == false
151 status = StatusView.render("status.json", %{activity: activity, for: user})
153 assert status.muted == true
157 note = insert(:note_activity)
161 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
163 status = StatusView.render("status.json", %{activity: activity})
165 assert status.in_reply_to_id == to_string(note.id)
167 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
169 assert status.in_reply_to_id == to_string(note.id)
172 test "contains mentions" do
173 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
174 # a user with this ap id might be in the cache.
175 recipient = "https://pleroma.soykaf.com/users/lain"
176 user = insert(:user, %{ap_id: recipient})
178 {:ok, [activity]} = OStatus.handle_incoming(incoming)
180 status = StatusView.render("status.json", %{activity: activity})
182 actor = User.get_cached_by_ap_id(activity.actor)
184 assert status.mentions ==
185 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
188 test "attachments" do
193 "mediaType" => "image/png",
204 remote_url: "someurl",
205 preview_url: "someurl",
208 pleroma: %{mime_type: "image/png"}
211 assert expected == StatusView.render("attachment.json", %{attachment: object})
213 # If theres a "id", use that instead of the generated one
214 object = Map.put(object, "id", 2)
215 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
220 activity = insert(:note_activity)
222 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
224 represented = StatusView.render("status.json", %{for: user, activity: reblog})
226 assert represented[:id] == to_string(reblog.id)
227 assert represented[:reblog][:id] == to_string(activity.id)
228 assert represented[:emojis] == []
231 test "a peertube video" do
235 Pleroma.Object.Fetcher.fetch_object_from_id(
236 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
239 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
241 represented = StatusView.render("status.json", %{for: user, activity: activity})
243 assert represented[:id] == to_string(activity.id)
244 assert length(represented[:media_attachments]) == 1
247 describe "build_tags/1" do
248 test "it returns a a dictionary tags" do
254 "href" => "https://kawen.space/users/lain",
255 "name" => "@lain@kawen.space",
260 assert StatusView.build_tags(object_tags) == [
261 %{name: "fediverse", url: "/tag/fediverse"},
262 %{name: "mastodon", url: "/tag/mastodon"},
263 %{name: "nextcloud", url: "/tag/nextcloud"}
268 describe "rich media cards" do
269 test "a rich media card without a site name renders correctly" do
270 page_url = "http://example.com"
274 image: page_url <> "/example.jpg",
275 title: "Example website"
278 %{provider_name: "example.com"} =
279 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
282 test "a rich media card without a site name or image renders correctly" do
283 page_url = "http://example.com"
287 title: "Example website"
290 %{provider_name: "example.com"} =
291 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
294 test "a rich media card without an image renders correctly" do
295 page_url = "http://example.com"
299 site_name: "Example site name",
300 title: "Example website"
303 %{provider_name: "Example site name"} =
304 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
307 test "a rich media card with all relevant data renders correctly" do
308 page_url = "http://example.com"
312 site_name: "Example site name",
313 title: "Example website",
314 image: page_url <> "/example.jpg",
315 description: "Example description"
318 %{provider_name: "Example site name"} =
319 StatusView.render("card.json", %{page_url: page_url, rich_media: card})