get-packs for local generated pack
[akkoma] / test / activity_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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).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).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 },
128 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
129 }
130
131 {:ok, local_activity} = Pleroma.Web.CommonAPI.post(user, %{status: "find me!"})
132 {:ok, japanese_activity} = Pleroma.Web.CommonAPI.post(user, %{status: "更新情報"})
133 {:ok, job} = Pleroma.Web.Federator.incoming_ap_doc(params)
134 {:ok, remote_activity} = ObanHelpers.perform(job)
135
136 %{
137 japanese_activity: japanese_activity,
138 local_activity: local_activity,
139 remote_activity: remote_activity,
140 user: user
141 }
142 end
143
144 setup do: clear_config([:instance, :limit_to_local_content])
145
146 test "finds utf8 text in statuses", %{
147 japanese_activity: japanese_activity,
148 user: user
149 } do
150 activities = Activity.search(user, "更新情報")
151
152 assert [^japanese_activity] = activities
153 end
154
155 test "find local and remote statuses for authenticated users", %{
156 local_activity: local_activity,
157 remote_activity: remote_activity,
158 user: user
159 } do
160 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
161
162 assert [^local_activity, ^remote_activity] = activities
163 end
164
165 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
166 assert [^local_activity] = Activity.search(nil, "find me")
167 end
168
169 test "find only local statuses for unauthenticated users when `limit_to_local_content` is `:all`",
170 %{local_activity: local_activity} do
171 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
172 assert [^local_activity] = Activity.search(nil, "find me")
173 end
174
175 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
176 %{
177 local_activity: local_activity,
178 remote_activity: remote_activity
179 } do
180 Pleroma.Config.put([:instance, :limit_to_local_content], false)
181
182 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
183
184 assert [^local_activity, ^remote_activity] = activities
185 end
186 end
187
188 test "add an activity with an expiration" do
189 activity = insert(:note_activity)
190 insert(:expiration_in_the_future, %{activity_id: activity.id})
191
192 Pleroma.ActivityExpiration
193 |> where([a], a.activity_id == ^activity.id)
194 |> Repo.one!()
195 end
196
197 test "all_by_ids_with_object/1" do
198 %{id: id1} = insert(:note_activity)
199 %{id: id2} = insert(:note_activity)
200
201 activities =
202 [id1, id2]
203 |> Activity.all_by_ids_with_object()
204 |> Enum.sort(&(&1.id < &2.id))
205
206 assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities
207 end
208
209 test "get_by_id_with_object/1" do
210 %{id: id} = insert(:note_activity)
211
212 assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id)
213 end
214
215 test "get_by_ap_id_with_object/1" do
216 %{data: %{"id" => ap_id}} = insert(:note_activity)
217
218 assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} =
219 Activity.get_by_ap_id_with_object(ap_id)
220 end
221
222 test "get_by_id/1" do
223 %{id: id} = insert(:note_activity)
224
225 assert %Activity{id: ^id} = Activity.get_by_id(id)
226 end
227
228 test "all_by_actor_and_id/2" do
229 user = insert(:user)
230
231 {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofe"})
232 {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofefe"})
233
234 assert [] == Activity.all_by_actor_and_id(user, [])
235
236 activities =
237 user.ap_id
238 |> Activity.all_by_actor_and_id([id1, id2])
239 |> Enum.sort(&(&1.id < &2.id))
240
241 assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities
242 end
243 end