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.MastodonAPI.AccountView
13 alias Pleroma.Web.MastodonAPI.StatusView
14 alias Pleroma.Web.OStatus
15 import Pleroma.Factory
19 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 test "returns a temporary ap_id based user for activities missing db users" do
26 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
29 Cachex.clear(:user_cache)
31 %{account: ms_user} = StatusView.render("status.json", activity: activity)
33 assert ms_user.acct == "erroruser@example.com"
36 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
39 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
43 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
46 Cachex.clear(:user_cache)
48 result = StatusView.render("status.json", activity: activity)
50 assert result[:account][:id] == to_string(user.id)
53 test "a note with null content" do
54 note = insert(:note_activity)
58 |> put_in(["object", "content"], nil)
62 |> Map.put(:data, data)
64 User.get_cached_by_ap_id(note.data["actor"])
66 status = StatusView.render("status.json", %{activity: note})
68 assert status.content == ""
71 test "a note activity" do
72 note = insert(:note_activity)
73 user = User.get_cached_by_ap_id(note.data["actor"])
75 status = StatusView.render("status.json", %{activity: note})
78 (note.data["object"]["published"] || "")
79 |> String.replace(~r/\.\d+Z/, ".000Z")
82 id: to_string(note.id),
83 uri: note.data["object"]["id"],
84 url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
85 account: AccountView.render("account.json", %{user: user}),
87 in_reply_to_account_id: nil,
90 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
91 created_at: created_at,
101 spoiler_text: note.data["object"]["summary"],
102 visibility: "public",
103 media_attachments: [],
107 name: "#{note.data["object"]["tag"]}",
108 url: "/tag/#{note.data["object"]["tag"]}"
120 static_url: "corndog.png",
121 visible_in_picker: false
129 assert status == expected
132 test "tells if the message is muted for some reason" do
134 other_user = insert(:user)
136 {:ok, user} = User.mute(user, other_user)
138 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
139 status = StatusView.render("status.json", %{activity: activity})
141 assert status.muted == false
143 status = StatusView.render("status.json", %{activity: activity, for: user})
145 assert status.muted == true
149 note = insert(:note_activity)
153 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
155 status = StatusView.render("status.json", %{activity: activity})
157 assert status.in_reply_to_id == to_string(note.id)
159 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
161 assert status.in_reply_to_id == to_string(note.id)
164 test "contains mentions" do
165 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
166 # a user with this ap id might be in the cache.
167 recipient = "https://pleroma.soykaf.com/users/lain"
168 user = insert(:user, %{ap_id: recipient})
170 {:ok, [activity]} = OStatus.handle_incoming(incoming)
172 status = StatusView.render("status.json", %{activity: activity})
174 actor = Repo.get_by(User, ap_id: activity.actor)
176 assert status.mentions ==
177 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
180 test "attachments" do
185 "mediaType" => "image/png",
196 remote_url: "someurl",
197 preview_url: "someurl",
200 pleroma: %{mime_type: "image/png"}
203 assert expected == StatusView.render("attachment.json", %{attachment: object})
205 # If theres a "id", use that instead of the generated one
206 object = Map.put(object, "id", 2)
207 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
212 activity = insert(:note_activity)
214 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
216 represented = StatusView.render("status.json", %{for: user, activity: reblog})
218 assert represented[:id] == to_string(reblog.id)
219 assert represented[:reblog][:id] == to_string(activity.id)
220 assert represented[:emojis] == []
223 test "a peertube video" do
227 ActivityPub.fetch_object_from_id(
228 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
231 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
233 represented = StatusView.render("status.json", %{for: user, activity: activity})
235 assert represented[:id] == to_string(activity.id)
236 assert length(represented[:media_attachments]) == 1
239 describe "build_tags/1" do
240 test "it returns a a dictionary tags" do
246 "href" => "https://kawen.space/users/lain",
247 "name" => "@lain@kawen.space",
252 assert StatusView.build_tags(object_tags) == [
253 %{name: "fediverse", url: "/tag/fediverse"},
254 %{name: "mastodon", url: "/tag/mastodon"},
255 %{name: "nextcloud", url: "/tag/nextcloud"}
260 describe "rich media cards" do
261 test "a rich media card without a site name renders correctly" do
262 page_url = "http://example.com"
266 image: page_url <> "/example.jpg",
267 title: "Example website"
270 %{provider_name: "example.com"} =
271 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
274 test "a rich media card without a site name or image renders correctly" do
275 page_url = "http://example.com"
279 title: "Example website"
282 %{provider_name: "example.com"} =
283 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
286 test "a rich media card without an image renders correctly" do
287 page_url = "http://example.com"
291 site_name: "Example site name",
292 title: "Example website"
295 %{provider_name: "Example site name"} =
296 StatusView.render("card.json", %{page_url: page_url, rich_media: card})
299 test "a rich media card with all relevant data renders correctly" do
300 page_url = "http://example.com"
304 site_name: "Example site name",
305 title: "Example website",
306 image: page_url <> "/example.jpg",
307 description: "Example description"
310 %{provider_name: "Example site name"} =
311 StatusView.render("card.json", %{page_url: page_url, rich_media: card})