First
[anni] / test / pleroma / activity_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ActivityTest do
6   use Pleroma.DataCase
7   alias Pleroma.Activity
8   alias Pleroma.Bookmark
9   alias Pleroma.Object
10   alias Pleroma.Tests.ObanHelpers
11   alias Pleroma.ThreadMute
12   import Pleroma.Factory
13
14   setup_all do
15     Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
16     :ok
17   end
18
19   test "returns an activity by it's AP id" do
20     activity = insert(:note_activity)
21     found_activity = Activity.get_by_ap_id(activity.data["id"])
22
23     assert activity == found_activity
24   end
25
26   test "returns activities by it's objects AP ids" do
27     activity = insert(:note_activity)
28     object_data = Object.normalize(activity, fetch: false).data
29
30     [found_activity] = Activity.get_all_create_by_object_ap_id(object_data["id"])
31
32     assert activity == found_activity
33   end
34
35   test "returns the activity that created an object" do
36     activity = insert(:note_activity)
37     object_data = Object.normalize(activity, fetch: false).data
38
39     found_activity = Activity.get_create_by_object_ap_id(object_data["id"])
40
41     assert activity == found_activity
42   end
43
44   test "preloading a bookmark" do
45     user = insert(:user)
46     user2 = insert(:user)
47     user3 = insert(:user)
48     activity = insert(:note_activity)
49     {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
50     {:ok, _bookmark2} = Bookmark.create(user2.id, activity.id)
51     {:ok, bookmark3} = Bookmark.create(user3.id, activity.id)
52
53     queried_activity =
54       Ecto.Query.from(Pleroma.Activity)
55       |> Activity.with_preloaded_bookmark(user3)
56       |> Repo.one()
57
58     assert queried_activity.bookmark == bookmark3
59   end
60
61   test "setting thread_muted?" do
62     activity = insert(:note_activity)
63     user = insert(:user)
64     annoyed_user = insert(:user)
65     {:ok, _} = ThreadMute.add_mute(annoyed_user.id, activity.data["context"])
66
67     activity_with_unset_thread_muted_field =
68       Ecto.Query.from(Activity)
69       |> Repo.one()
70
71     activity_for_user =
72       Ecto.Query.from(Activity)
73       |> Activity.with_set_thread_muted_field(user)
74       |> Repo.one()
75
76     activity_for_annoyed_user =
77       Ecto.Query.from(Activity)
78       |> Activity.with_set_thread_muted_field(annoyed_user)
79       |> Repo.one()
80
81     assert activity_with_unset_thread_muted_field.thread_muted? == nil
82     assert activity_for_user.thread_muted? == false
83     assert activity_for_annoyed_user.thread_muted? == true
84   end
85
86   describe "getting a bookmark" do
87     test "when association is loaded" do
88       user = insert(:user)
89       activity = insert(:note_activity)
90       {:ok, bookmark} = Bookmark.create(user.id, activity.id)
91
92       queried_activity =
93         Ecto.Query.from(Pleroma.Activity)
94         |> Activity.with_preloaded_bookmark(user)
95         |> Repo.one()
96
97       assert Activity.get_bookmark(queried_activity, user) == bookmark
98     end
99
100     test "when association is not loaded" do
101       user = insert(:user)
102       activity = insert(:note_activity)
103       {:ok, bookmark} = Bookmark.create(user.id, activity.id)
104
105       queried_activity =
106         Ecto.Query.from(Pleroma.Activity)
107         |> Repo.one()
108
109       assert Activity.get_bookmark(queried_activity, user) == bookmark
110     end
111   end
112
113   describe "search" do
114     setup do
115       user = insert(:user)
116
117       params = %{
118         "@context" => "https://www.w3.org/ns/activitystreams",
119         "actor" => "http://mastodon.example.org/users/admin",
120         "type" => "Create",
121         "id" => "http://mastodon.example.org/users/admin/activities/1",
122         "object" => %{
123           "type" => "Note",
124           "content" => "find me!",
125           "id" => "http://mastodon.example.org/users/admin/objects/1",
126           "attributedTo" => "http://mastodon.example.org/users/admin",
127           "to" => ["https://www.w3.org/ns/activitystreams#Public"]
128         },
129         "to" => ["https://www.w3.org/ns/activitystreams#Public"]
130       }
131
132       {:ok, local_activity} = Pleroma.Web.CommonAPI.post(user, %{status: "find me!"})
133       {:ok, japanese_activity} = Pleroma.Web.CommonAPI.post(user, %{status: "更新情報"})
134       {:ok, job} = Pleroma.Web.Federator.incoming_ap_doc(params)
135       {:ok, remote_activity} = ObanHelpers.perform(job)
136       remote_activity = Activity.get_by_id_with_object(remote_activity.id)
137
138       %{
139         japanese_activity: japanese_activity,
140         local_activity: local_activity,
141         remote_activity: remote_activity,
142         user: user
143       }
144     end
145
146     setup do: clear_config([:instance, :limit_to_local_content])
147
148     @tag :skip_on_mac
149     test "finds utf8 text in statuses", %{
150       japanese_activity: japanese_activity,
151       user: user
152     } do
153       activities = Activity.search(user, "更新情報")
154
155       assert [^japanese_activity] = activities
156     end
157
158     test "find local and remote statuses for authenticated users", %{
159       local_activity: local_activity,
160       remote_activity: remote_activity,
161       user: user
162     } do
163       activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
164
165       assert [^local_activity, ^remote_activity] = activities
166     end
167
168     test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
169       assert [^local_activity] = Activity.search(nil, "find me")
170     end
171
172     test "find only local statuses for unauthenticated users  when `limit_to_local_content` is `:all`",
173          %{local_activity: local_activity} do
174       clear_config([:instance, :limit_to_local_content], :all)
175       assert [^local_activity] = Activity.search(nil, "find me")
176     end
177
178     test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
179          %{
180            local_activity: local_activity,
181            remote_activity: remote_activity
182          } do
183       clear_config([:instance, :limit_to_local_content], false)
184
185       activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
186
187       assert [^local_activity, ^remote_activity] = activities
188     end
189   end
190
191   test "all_by_ids_with_object/1" do
192     %{id: id1} = insert(:note_activity)
193     %{id: id2} = insert(:note_activity)
194
195     activities =
196       [id1, id2]
197       |> Activity.all_by_ids_with_object()
198       |> Enum.sort(&(&1.id < &2.id))
199
200     assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities
201   end
202
203   test "get_by_id_with_user_actor/1" do
204     user = insert(:user)
205     activity = insert(:note_activity, note: insert(:note, user: user))
206
207     assert Activity.get_by_id_with_user_actor(activity.id).user_actor == user
208   end
209
210   test "get_by_id_with_object/1" do
211     %{id: id} = insert(:note_activity)
212
213     assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id)
214   end
215
216   test "get_by_ap_id_with_object/1" do
217     %{data: %{"id" => ap_id}} = insert(:note_activity)
218
219     assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} =
220              Activity.get_by_ap_id_with_object(ap_id)
221   end
222
223   test "get_by_id/1" do
224     %{id: id} = insert(:note_activity)
225
226     assert %Activity{id: ^id} = Activity.get_by_id(id)
227   end
228
229   test "all_by_actor_and_id/2" do
230     user = insert(:user)
231
232     {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofe"})
233     {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofefe"})
234
235     assert [] == Activity.all_by_actor_and_id(user, [])
236
237     activities =
238       user.ap_id
239       |> Activity.all_by_actor_and_id([id1, id2])
240       |> Enum.sort(&(&1.id < &2.id))
241
242     assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities
243   end
244
245   test "get_by_object_ap_id_with_object/1" do
246     user = insert(:user)
247     another = insert(:user)
248
249     {:ok, %{id: id, object: %{data: %{"id" => obj_id}}}} =
250       Pleroma.Web.CommonAPI.post(user, %{status: "cofe"})
251
252     Pleroma.Web.CommonAPI.favorite(another, id)
253
254     assert obj_id
255            |> Pleroma.Activity.Queries.by_object_id()
256            |> Repo.aggregate(:count, :id) == 2
257
258     assert %{id: ^id} = Activity.get_by_object_ap_id_with_object(obj_id)
259   end
260
261   test "add_by_params_query/3" do
262     user = insert(:user)
263
264     note = insert(:note_activity, user: user)
265
266     insert(:add_activity, user: user, note: note)
267     insert(:add_activity, user: user, note: note)
268     insert(:add_activity, user: user)
269
270     assert Repo.aggregate(Activity, :count, :id) == 4
271
272     add_query =
273       Activity.add_by_params_query(note.data["object"], user.ap_id, user.featured_address)
274
275     assert Repo.aggregate(add_query, :count, :id) == 2
276
277     Repo.delete_all(add_query)
278     assert Repo.aggregate(add_query, :count, :id) == 0
279
280     assert Repo.aggregate(Activity, :count, :id) == 2
281   end
282
283   describe "associated_object_id() sql function" do
284     test "with json object" do
285       %{rows: [[object_id]]} =
286         Ecto.Adapters.SQL.query!(
287           Pleroma.Repo,
288           """
289           select associated_object_id('{"object": {"id":"foobar"}}'::jsonb);
290           """
291         )
292
293       assert object_id == "foobar"
294     end
295
296     test "with string object" do
297       %{rows: [[object_id]]} =
298         Ecto.Adapters.SQL.query!(
299           Pleroma.Repo,
300           """
301           select associated_object_id('{"object": "foobar"}'::jsonb);
302           """
303         )
304
305       assert object_id == "foobar"
306     end
307
308     test "with array object" do
309       %{rows: [[object_id]]} =
310         Ecto.Adapters.SQL.query!(
311           Pleroma.Repo,
312           """
313           select associated_object_id('{"object": ["foobar", {}]}'::jsonb);
314           """
315         )
316
317       assert object_id == "foobar"
318     end
319
320     test "invalid" do
321       %{rows: [[object_id]]} =
322         Ecto.Adapters.SQL.query!(
323           Pleroma.Repo,
324           """
325           select associated_object_id('{"object": {}}'::jsonb);
326           """
327         )
328
329       assert is_nil(object_id)
330     end
331
332     test "invalid object id" do
333       %{rows: [[object_id]]} =
334         Ecto.Adapters.SQL.query!(
335           Pleroma.Repo,
336           """
337           select associated_object_id('{"object": {"id": 123}}'::jsonb);
338           """
339         )
340
341       assert is_nil(object_id)
342     end
343
344     test "no object field" do
345       %{rows: [[object_id]]} =
346         Ecto.Adapters.SQL.query!(
347           Pleroma.Repo,
348           """
349           select associated_object_id('{}'::jsonb);
350           """
351         )
352
353       assert is_nil(object_id)
354     end
355   end
356 end