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
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Web.MastodonAPI.AccountView
14 alias Pleroma.Web.MastodonAPI.StatusView
15 alias Pleroma.Web.OStatus
16 import Pleroma.Factory
20 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 test "returns a temporary ap_id based user for activities missing db users" do
27 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
30 Cachex.clear(:user_cache)
32 %{account: ms_user} = StatusView.render("status.json", activity: activity)
34 assert ms_user.acct == "erroruser@example.com"
37 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
40 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
44 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
47 Cachex.clear(:user_cache)
49 result = StatusView.render("status.json", activity: activity)
51 assert result[:account][:id] == to_string(user.id)
54 test "a note with null content" do
55 note = insert(:note_activity)
59 |> put_in(["object", "content"], nil)
63 |> Map.put(:data, data)
65 User.get_cached_by_ap_id(note.data["actor"])
67 status = StatusView.render("status.json", %{activity: note})
69 assert status.content == ""
72 test "a note activity" do
73 note = insert(:note_activity)
74 user = User.get_cached_by_ap_id(note.data["actor"])
76 convo_id = Utils.context_to_conversation_id(note.data["object"]["context"])
78 status = StatusView.render("status.json", %{activity: note})
81 (note.data["object"]["published"] || "")
82 |> String.replace(~r/\.\d+Z/, ".000Z")
85 id: to_string(note.id),
86 uri: note.data["object"]["id"],
87 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
88 account: AccountView.render("account.json", %{user: user}),
90 in_reply_to_account_id: nil,
93 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
94 created_at: created_at,
104 spoiler_text: HtmlSanitizeEx.basic_html(note.data["object"]["summary"]),
105 visibility: "public",
106 media_attachments: [],
110 name: "#{note.data["object"]["tag"]}",
111 url: "/tag/#{note.data["object"]["tag"]}"
123 static_url: "corndog.png",
124 visible_in_picker: false
129 conversation_id: convo_id,
130 content: %{"text/plain" => HtmlSanitizeEx.strip_tags(note.data["object"]["content"])},
131 spoiler_text: %{"text/plain" => HtmlSanitizeEx.strip_tags(note.data["object"]["summary"])}
135 assert status == expected
138 test "tells if the message is muted for some reason" do
140 other_user = insert(:user)
142 {:ok, user} = User.mute(user, other_user)
144 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
145 status = StatusView.render("status.json", %{activity: activity})
147 assert status.muted == false
149 status = StatusView.render("status.json", %{activity: activity, for: user})
151 assert status.muted == true
155 note = insert(:note_activity)
159 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
161 status = StatusView.render("status.json", %{activity: activity})
163 assert status.in_reply_to_id == to_string(note.id)
165 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
167 assert status.in_reply_to_id == to_string(note.id)
170 test "contains mentions" do
171 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
172 # a user with this ap id might be in the cache.
173 recipient = "https://pleroma.soykaf.com/users/lain"
174 user = insert(:user, %{ap_id: recipient})
176 {:ok, [activity]} = OStatus.handle_incoming(incoming)
178 status = StatusView.render("status.json", %{activity: activity})
180 actor = User.get_by_ap_id(activity.actor)
182 assert status.mentions ==
183 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
186 test "attachments" do
191 "mediaType" => "image/png",
202 remote_url: "someurl",
203 preview_url: "someurl",
206 pleroma: %{mime_type: "image/png"}
209 assert expected == StatusView.render("attachment.json", %{attachment: object})
211 # If theres a "id", use that instead of the generated one
212 object = Map.put(object, "id", 2)
213 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
218 activity = insert(:note_activity)
220 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
222 represented = StatusView.render("status.json", %{for: user, activity: reblog})
224 assert represented[:id] == to_string(reblog.id)
225 assert represented[:reblog][:id] == to_string(activity.id)
226 assert represented[:emojis] == []
229 test "a peertube video" do
233 ActivityPub.fetch_object_from_id(
234 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
237 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
239 represented = StatusView.render("status.json", %{for: user, activity: activity})
241 assert represented[:id] == to_string(activity.id)
242 assert length(represented[:media_attachments]) == 1
245 describe "build_tags/1" do
246 test "it returns a a dictionary tags" do
252 "href" => "https://kawen.space/users/lain",
253 "name" => "@lain@kawen.space",
258 assert StatusView.build_tags(object_tags) == [
259 %{name: "fediverse", url: "/tag/fediverse"},
260 %{name: "mastodon", url: "/tag/mastodon"},
261 %{name: "nextcloud", url: "/tag/nextcloud"}
266 describe "rich media cards" do
267 test "a rich media card without a site name renders correctly" do
268 page_url = "http://example.com"
272 image: page_url <> "/example.jpg",
273 title: "Example website"
276 %{provider_name: "example.com"} =
277 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
280 test "a rich media card without a site name or image renders correctly" do
281 page_url = "http://example.com"
285 title: "Example website"
288 %{provider_name: "example.com"} =
289 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
292 test "a rich media card without an image renders correctly" do
293 page_url = "http://example.com"
297 site_name: "Example site name",
298 title: "Example website"
301 %{provider_name: "Example site name"} =
302 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
305 test "a rich media card with all relevant data renders correctly" do
306 page_url = "http://example.com"
310 site_name: "Example site name",
311 title: "Example website",
312 image: page_url <> "/example.jpg",
313 description: "Example description"
316 %{provider_name: "Example site name"} =
317 StatusView.render("card.json", %{page_url: page_url, rich_media: card})