Fix merge conflict
[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 reblog: nil,
88 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
89 created_at: created_at,
90 reblogs_count: 0,
91 replies_count: 0,
92 favourites_count: 0,
93 reblogged: false,
94 favourited: false,
95 muted: false,
96 pinned: false,
97 sensitive: false,
98 spoiler_text: note.data["object"]["summary"],
99 visibility: "public",
100 media_attachments: [],
101 mentions: [],
102 tags: [
103 %{
104 name: "#{note.data["object"]["tag"]}",
105 url: "/tag/#{note.data["object"]["tag"]}"
106 }
107 ],
108 application: %{
109 name: "Web",
110 website: nil
111 },
112 language: nil,
113 emojis: [
114 %{
115 shortcode: "2hu",
116 url: "corndog.png",
117 static_url: "corndog.png",
118 visible_in_picker: false
119 }
120 ]
121 }
122
123 assert status == expected
124 end
125
126 test "a reply" do
127 note = insert(:note_activity)
128 user = insert(:user)
129
130 {:ok, activity} =
131 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
132
133 status = StatusView.render("status.json", %{activity: activity})
134
135 assert status.in_reply_to_id == to_string(note.id)
136
137 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
138
139 assert status.in_reply_to_id == to_string(note.id)
140 end
141
142 test "contains mentions" do
143 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
144 # a user with this ap id might be in the cache.
145 recipient = "https://pleroma.soykaf.com/users/lain"
146 user = insert(:user, %{ap_id: recipient})
147
148 {:ok, [activity]} = OStatus.handle_incoming(incoming)
149
150 status = StatusView.render("status.json", %{activity: activity})
151
152 assert status.mentions == [AccountView.render("mention.json", %{user: user})]
153 end
154
155 test "attachments" do
156 object = %{
157 "type" => "Image",
158 "url" => [
159 %{
160 "mediaType" => "image/png",
161 "href" => "someurl"
162 }
163 ],
164 "uuid" => 6
165 }
166
167 expected = %{
168 id: "1638338801",
169 type: "image",
170 url: "someurl",
171 remote_url: "someurl",
172 preview_url: "someurl",
173 text_url: "someurl",
174 description: nil
175 }
176
177 assert expected == StatusView.render("attachment.json", %{attachment: object})
178
179 # If theres a "id", use that instead of the generated one
180 object = Map.put(object, "id", 2)
181 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
182 end
183
184 test "a reblog" do
185 user = insert(:user)
186 activity = insert(:note_activity)
187
188 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
189
190 represented = StatusView.render("status.json", %{for: user, activity: reblog})
191
192 assert represented[:id] == to_string(reblog.id)
193 assert represented[:reblog][:id] == to_string(activity.id)
194 assert represented[:emojis] == []
195 end
196
197 test "a peertube video" do
198 user = insert(:user)
199
200 {:ok, object} =
201 ActivityPub.fetch_object_from_id(
202 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
203 )
204
205 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
206
207 represented = StatusView.render("status.json", %{for: user, activity: activity})
208
209 assert represented[:id] == to_string(activity.id)
210 assert length(represented[:media_attachments]) == 1
211 end
212
213 describe "build_tags/1" do
214 test "it returns a a dictionary tags" do
215 object_tags = [
216 "fediverse",
217 "mastodon",
218 "nextcloud",
219 %{
220 "href" => "https://kawen.space/users/lain",
221 "name" => "@lain@kawen.space",
222 "type" => "Mention"
223 }
224 ]
225
226 assert StatusView.build_tags(object_tags) == [
227 %{name: "fediverse", url: "/tag/fediverse"},
228 %{name: "mastodon", url: "/tag/mastodon"},
229 %{name: "nextcloud", url: "/tag/nextcloud"}
230 ]
231 end
232 end
233 end