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
8 alias Pleroma.Web.MastodonAPI.{StatusView, AccountView}
10 alias Pleroma.Web.OStatus
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Activity
14 import Pleroma.Factory
18 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 test "returns a temporary ap_id based user for activities missing db users" do
25 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
28 Cachex.clear(:user_cache)
30 %{account: ms_user} = StatusView.render("status.json", activity: activity)
32 assert ms_user.acct == "erroruser@example.com"
35 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
38 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
42 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
45 Cachex.clear(:user_cache)
47 result = StatusView.render("status.json", activity: activity)
49 assert result[:account][:id] == to_string(user.id)
52 test "a note with null content" do
53 note = insert(:note_activity)
57 |> put_in(["object", "content"], nil)
61 |> Map.put(:data, data)
63 User.get_cached_by_ap_id(note.data["actor"])
65 status = StatusView.render("status.json", %{activity: note})
67 assert status.content == ""
70 test "a note activity" do
71 note = insert(:note_activity)
72 user = User.get_cached_by_ap_id(note.data["actor"])
74 status = StatusView.render("status.json", %{activity: note})
77 (note.data["object"]["published"] || "")
78 |> String.replace(~r/\.\d+Z/, ".000Z")
81 id: to_string(note.id),
82 uri: note.data["object"]["id"],
83 url: note.data["object"]["id"],
84 account: AccountView.render("account.json", %{user: user}),
86 in_reply_to_account_id: nil,
88 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
89 created_at: created_at,
99 spoiler_text: note.data["object"]["summary"],
100 visibility: "public",
101 media_attachments: [],
105 name: "#{note.data["object"]["tag"]}",
106 url: "/tag/#{note.data["object"]["tag"]}"
118 static_url: "corndog.png",
119 visible_in_picker: false
124 assert status == expected
128 note = insert(:note_activity)
132 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
134 status = StatusView.render("status.json", %{activity: activity})
136 assert status.in_reply_to_id == to_string(note.id)
138 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
140 assert status.in_reply_to_id == to_string(note.id)
143 test "contains mentions" do
144 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
145 # a user with this ap id might be in the cache.
146 recipient = "https://pleroma.soykaf.com/users/lain"
147 user = insert(:user, %{ap_id: recipient})
149 {:ok, [activity]} = OStatus.handle_incoming(incoming)
151 status = StatusView.render("status.json", %{activity: activity})
153 actor = Repo.get_by(User, ap_id: activity.actor)
155 assert status.mentions ==
156 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
159 test "attachments" do
164 "mediaType" => "image/png",
175 remote_url: "someurl",
176 preview_url: "someurl",
181 assert expected == StatusView.render("attachment.json", %{attachment: object})
183 # If theres a "id", use that instead of the generated one
184 object = Map.put(object, "id", 2)
185 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
190 activity = insert(:note_activity)
192 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
194 represented = StatusView.render("status.json", %{for: user, activity: reblog})
196 assert represented[:id] == to_string(reblog.id)
197 assert represented[:reblog][:id] == to_string(activity.id)
198 assert represented[:emojis] == []
201 test "a peertube video" do
205 ActivityPub.fetch_object_from_id(
206 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
209 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
211 represented = StatusView.render("status.json", %{for: user, activity: activity})
213 assert represented[:id] == to_string(activity.id)
214 assert length(represented[:media_attachments]) == 1
217 describe "build_tags/1" do
218 test "it returns a a dictionary tags" do
224 "href" => "https://kawen.space/users/lain",
225 "name" => "@lain@kawen.space",
230 assert StatusView.build_tags(object_tags) == [
231 %{name: "fediverse", url: "/tag/fediverse"},
232 %{name: "mastodon", url: "/tag/mastodon"},
233 %{name: "nextcloud", url: "/tag/nextcloud"}