Merge branch 'features/glitch-soc-frontend' into 'develop'
[akkoma] / test / web / mastodon_api / status_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Web.MastodonAPI.{StatusView, AccountView}
9 alias Pleroma.User
10 alias Pleroma.Web.OStatus
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Activity
14 import Pleroma.Factory
15 import Tesla.Mock
16
17 setup do
18 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 test "returns a temporary ap_id based user for activities missing db users" do
23 user = insert(:user)
24
25 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
26
27 Repo.delete(user)
28 Cachex.clear(:user_cache)
29
30 %{account: ms_user} = StatusView.render("status.json", activity: activity)
31
32 assert ms_user.acct == "erroruser@example.com"
33 end
34
35 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
36 user = insert(:user)
37
38 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
39
40 {:ok, user} =
41 user
42 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
43 |> Repo.update()
44
45 Cachex.clear(:user_cache)
46
47 result = StatusView.render("status.json", activity: activity)
48
49 assert result[:account][:id] == to_string(user.id)
50 end
51
52 test "a note with null content" do
53 note = insert(:note_activity)
54
55 data =
56 note.data
57 |> put_in(["object", "content"], nil)
58
59 note =
60 note
61 |> Map.put(:data, data)
62
63 User.get_cached_by_ap_id(note.data["actor"])
64
65 status = StatusView.render("status.json", %{activity: note})
66
67 assert status.content == ""
68 end
69
70 test "a note activity" do
71 note = insert(:note_activity)
72 user = User.get_cached_by_ap_id(note.data["actor"])
73
74 status = StatusView.render("status.json", %{activity: note})
75
76 created_at =
77 (note.data["object"]["published"] || "")
78 |> String.replace(~r/\.\d+Z/, ".000Z")
79
80 expected = %{
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}),
85 in_reply_to_id: nil,
86 in_reply_to_account_id: nil,
87 card: nil,
88 reblog: nil,
89 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
90 created_at: created_at,
91 reblogs_count: 0,
92 replies_count: 0,
93 favourites_count: 0,
94 reblogged: false,
95 bookmarked: false,
96 favourited: false,
97 muted: false,
98 pinned: false,
99 sensitive: false,
100 spoiler_text: note.data["object"]["summary"],
101 visibility: "public",
102 media_attachments: [],
103 mentions: [],
104 tags: [
105 %{
106 name: "#{note.data["object"]["tag"]}",
107 url: "/tag/#{note.data["object"]["tag"]}"
108 }
109 ],
110 application: %{
111 name: "Web",
112 website: nil
113 },
114 language: nil,
115 emojis: [
116 %{
117 shortcode: "2hu",
118 url: "corndog.png",
119 static_url: "corndog.png",
120 visible_in_picker: false
121 }
122 ]
123 }
124
125 assert status == expected
126 end
127
128 test "a reply" do
129 note = insert(:note_activity)
130 user = insert(:user)
131
132 {:ok, activity} =
133 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
134
135 status = StatusView.render("status.json", %{activity: activity})
136
137 assert status.in_reply_to_id == to_string(note.id)
138
139 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
140
141 assert status.in_reply_to_id == to_string(note.id)
142 end
143
144 test "contains mentions" do
145 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
146 # a user with this ap id might be in the cache.
147 recipient = "https://pleroma.soykaf.com/users/lain"
148 user = insert(:user, %{ap_id: recipient})
149
150 {:ok, [activity]} = OStatus.handle_incoming(incoming)
151
152 status = StatusView.render("status.json", %{activity: activity})
153
154 actor = Repo.get_by(User, ap_id: activity.actor)
155
156 assert status.mentions ==
157 Enum.map([user, actor], fn u -> AccountView.render("mention.json", %{user: u}) end)
158 end
159
160 test "attachments" do
161 object = %{
162 "type" => "Image",
163 "url" => [
164 %{
165 "mediaType" => "image/png",
166 "href" => "someurl"
167 }
168 ],
169 "uuid" => 6
170 }
171
172 expected = %{
173 id: "1638338801",
174 type: "image",
175 url: "someurl",
176 remote_url: "someurl",
177 preview_url: "someurl",
178 text_url: "someurl",
179 description: nil
180 }
181
182 assert expected == StatusView.render("attachment.json", %{attachment: object})
183
184 # If theres a "id", use that instead of the generated one
185 object = Map.put(object, "id", 2)
186 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
187 end
188
189 test "a reblog" do
190 user = insert(:user)
191 activity = insert(:note_activity)
192
193 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
194
195 represented = StatusView.render("status.json", %{for: user, activity: reblog})
196
197 assert represented[:id] == to_string(reblog.id)
198 assert represented[:reblog][:id] == to_string(activity.id)
199 assert represented[:emojis] == []
200 end
201
202 test "a peertube video" do
203 user = insert(:user)
204
205 {:ok, object} =
206 ActivityPub.fetch_object_from_id(
207 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
208 )
209
210 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
211
212 represented = StatusView.render("status.json", %{for: user, activity: activity})
213
214 assert represented[:id] == to_string(activity.id)
215 assert length(represented[:media_attachments]) == 1
216 end
217
218 describe "build_tags/1" do
219 test "it returns a a dictionary tags" do
220 object_tags = [
221 "fediverse",
222 "mastodon",
223 "nextcloud",
224 %{
225 "href" => "https://kawen.space/users/lain",
226 "name" => "@lain@kawen.space",
227 "type" => "Mention"
228 }
229 ]
230
231 assert StatusView.build_tags(object_tags) == [
232 %{name: "fediverse", url: "/tag/fediverse"},
233 %{name: "mastodon", url: "/tag/mastodon"},
234 %{name: "nextcloud", url: "/tag/nextcloud"}
235 ]
236 end
237 end
238 end