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: 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
133 assert status == expected
136 test "tells if the message is muted for some reason" do
138 other_user = insert(:user)
140 {:ok, user} = User.mute(user, other_user)
142 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
143 status = StatusView.render("status.json", %{activity: activity})
145 assert status.muted == false
147 status = StatusView.render("status.json", %{activity: activity, for: user})
149 assert status.muted == true
153 note = insert(:note_activity)
157 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
159 status = StatusView.render("status.json", %{activity: activity})
161 assert status.in_reply_to_id == to_string(note.id)
163 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
165 assert status.in_reply_to_id == to_string(note.id)
168 test "contains mentions" do
169 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
170 # a user with this ap id might be in the cache.
171 recipient = "https://pleroma.soykaf.com/users/lain"
172 user = insert(:user, %{ap_id: recipient})
174 {:ok, [activity]} = OStatus.handle_incoming(incoming)
176 status = StatusView.render("status.json", %{activity: activity})
178 actor = User.get_by_ap_id(activity.actor)
180 assert status.mentions ==
181 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
184 test "attachments" do
189 "mediaType" => "image/png",
200 remote_url: "someurl",
201 preview_url: "someurl",
204 pleroma: %{mime_type: "image/png"}
207 assert expected == StatusView.render("attachment.json", %{attachment: object})
209 # If theres a "id", use that instead of the generated one
210 object = Map.put(object, "id", 2)
211 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
216 activity = insert(:note_activity)
218 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
220 represented = StatusView.render("status.json", %{for: user, activity: reblog})
222 assert represented[:id] == to_string(reblog.id)
223 assert represented[:reblog][:id] == to_string(activity.id)
224 assert represented[:emojis] == []
227 test "a peertube video" do
231 ActivityPub.fetch_object_from_id(
232 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
235 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
237 represented = StatusView.render("status.json", %{for: user, activity: activity})
239 assert represented[:id] == to_string(activity.id)
240 assert length(represented[:media_attachments]) == 1
243 describe "build_tags/1" do
244 test "it returns a a dictionary tags" do
250 "href" => "https://kawen.space/users/lain",
251 "name" => "@lain@kawen.space",
256 assert StatusView.build_tags(object_tags) == [
257 %{name: "fediverse", url: "/tag/fediverse"},
258 %{name: "mastodon", url: "/tag/mastodon"},
259 %{name: "nextcloud", url: "/tag/nextcloud"}
264 describe "rich media cards" do
265 test "a rich media card without a site name renders correctly" do
266 page_url = "http://example.com"
270 image: page_url <> "/example.jpg",
271 title: "Example website"
274 %{provider_name: "example.com"} =
275 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
278 test "a rich media card without a site name or image renders correctly" do
279 page_url = "http://example.com"
283 title: "Example website"
286 %{provider_name: "example.com"} =
287 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
290 test "a rich media card without an image renders correctly" do
291 page_url = "http://example.com"
295 site_name: "Example site name",
296 title: "Example website"
299 %{provider_name: "Example site name"} =
300 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
303 test "a rich media card with all relevant data renders correctly" do
304 page_url = "http://example.com"
308 site_name: "Example site name",
309 title: "Example website",
310 image: page_url <> "/example.jpg",
311 description: "Example description"
314 %{provider_name: "Example site name"} =
315 StatusView.render("card.json", %{page_url: page_url, rich_media: card})