Merge branch 'develop' into global-status-expiration
[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 test "returns an activity by it's AP id" do
15 activity = insert(:note_activity)
16 found_activity = Activity.get_by_ap_id(activity.data["id"])
17
18 assert activity == found_activity
19 end
20
21 test "returns activities by it's objects AP ids" do
22 activity = insert(:note_activity)
23 object_data = Object.normalize(activity).data
24
25 [found_activity] = Activity.get_all_create_by_object_ap_id(object_data["id"])
26
27 assert activity == found_activity
28 end
29
30 test "returns the activity that created an object" do
31 activity = insert(:note_activity)
32 object_data = Object.normalize(activity).data
33
34 found_activity = Activity.get_create_by_object_ap_id(object_data["id"])
35
36 assert activity == found_activity
37 end
38
39 test "preloading a bookmark" do
40 user = insert(:user)
41 user2 = insert(:user)
42 user3 = insert(:user)
43 activity = insert(:note_activity)
44 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
45 {:ok, _bookmark2} = Bookmark.create(user2.id, activity.id)
46 {:ok, bookmark3} = Bookmark.create(user3.id, activity.id)
47
48 queried_activity =
49 Ecto.Query.from(Pleroma.Activity)
50 |> Activity.with_preloaded_bookmark(user3)
51 |> Repo.one()
52
53 assert queried_activity.bookmark == bookmark3
54 end
55
56 test "setting thread_muted?" do
57 activity = insert(:note_activity)
58 user = insert(:user)
59 annoyed_user = insert(:user)
60 {:ok, _} = ThreadMute.add_mute(annoyed_user.id, activity.data["context"])
61
62 activity_with_unset_thread_muted_field =
63 Ecto.Query.from(Activity)
64 |> Repo.one()
65
66 activity_for_user =
67 Ecto.Query.from(Activity)
68 |> Activity.with_set_thread_muted_field(user)
69 |> Repo.one()
70
71 activity_for_annoyed_user =
72 Ecto.Query.from(Activity)
73 |> Activity.with_set_thread_muted_field(annoyed_user)
74 |> Repo.one()
75
76 assert activity_with_unset_thread_muted_field.thread_muted? == nil
77 assert activity_for_user.thread_muted? == false
78 assert activity_for_annoyed_user.thread_muted? == true
79 end
80
81 describe "getting a bookmark" do
82 test "when association is loaded" do
83 user = insert(:user)
84 activity = insert(:note_activity)
85 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
86
87 queried_activity =
88 Ecto.Query.from(Pleroma.Activity)
89 |> Activity.with_preloaded_bookmark(user)
90 |> Repo.one()
91
92 assert Activity.get_bookmark(queried_activity, user) == bookmark
93 end
94
95 test "when association is not loaded" do
96 user = insert(:user)
97 activity = insert(:note_activity)
98 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
99
100 queried_activity =
101 Ecto.Query.from(Pleroma.Activity)
102 |> Repo.one()
103
104 assert Activity.get_bookmark(queried_activity, user) == bookmark
105 end
106 end
107
108 describe "search" do
109 setup do
110 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
111
112 user = insert(:user)
113
114 params = %{
115 "@context" => "https://www.w3.org/ns/activitystreams",
116 "actor" => "http://mastodon.example.org/users/admin",
117 "type" => "Create",
118 "id" => "http://mastodon.example.org/users/admin/activities/1",
119 "object" => %{
120 "type" => "Note",
121 "content" => "find me!",
122 "id" => "http://mastodon.example.org/users/admin/objects/1",
123 "attributedTo" => "http://mastodon.example.org/users/admin"
124 },
125 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
126 }
127
128 {:ok, local_activity} = Pleroma.Web.CommonAPI.post(user, %{"status" => "find me!"})
129 {:ok, japanese_activity} = Pleroma.Web.CommonAPI.post(user, %{"status" => "更新情報"})
130 {:ok, job} = Pleroma.Web.Federator.incoming_ap_doc(params)
131 {:ok, remote_activity} = ObanHelpers.perform(job)
132
133 %{
134 japanese_activity: japanese_activity,
135 local_activity: local_activity,
136 remote_activity: remote_activity,
137 user: user
138 }
139 end
140
141 setup do: clear_config([:instance, :limit_to_local_content])
142
143 test "finds utf8 text in statuses", %{
144 japanese_activity: japanese_activity,
145 user: user
146 } do
147 activities = Activity.search(user, "更新情報")
148
149 assert [^japanese_activity] = activities
150 end
151
152 test "find local and remote statuses for authenticated users", %{
153 local_activity: local_activity,
154 remote_activity: remote_activity,
155 user: user
156 } do
157 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
158
159 assert [^local_activity, ^remote_activity] = activities
160 end
161
162 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
163 assert [^local_activity] = Activity.search(nil, "find me")
164 end
165
166 test "find only local statuses for unauthenticated users when `limit_to_local_content` is `:all`",
167 %{local_activity: local_activity} do
168 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
169 assert [^local_activity] = Activity.search(nil, "find me")
170 end
171
172 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
173 %{
174 local_activity: local_activity,
175 remote_activity: remote_activity
176 } do
177 Pleroma.Config.put([:instance, :limit_to_local_content], false)
178
179 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
180
181 assert [^local_activity, ^remote_activity] = activities
182 end
183 end
184
185 test "add an activity with an expiration" do
186 activity = insert(:note_activity)
187 insert(:expiration_in_the_future, %{activity_id: activity.id})
188
189 Pleroma.ActivityExpiration
190 |> where([a], a.activity_id == ^activity.id)
191 |> Repo.one!()
192 end
193
194 test "all_by_ids_with_object/1" do
195 %{id: id1} = insert(:note_activity)
196 %{id: id2} = insert(:note_activity)
197
198 activities =
199 [id1, id2]
200 |> Activity.all_by_ids_with_object()
201 |> Enum.sort(&(&1.id < &2.id))
202
203 assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities
204 end
205
206 test "get_by_id_with_object/1" do
207 %{id: id} = insert(:note_activity)
208
209 assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id)
210 end
211
212 test "get_by_ap_id_with_object/1" do
213 %{data: %{"id" => ap_id}} = insert(:note_activity)
214
215 assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} =
216 Activity.get_by_ap_id_with_object(ap_id)
217 end
218
219 test "get_by_id/1" do
220 %{id: id} = insert(:note_activity)
221
222 assert %Activity{id: ^id} = Activity.get_by_id(id)
223 end
224
225 test "all_by_actor_and_id/2" do
226 user = insert(:user)
227
228 {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofe"})
229 {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofefe"})
230
231 assert [] == Activity.all_by_actor_and_id(user, [])
232
233 activities =
234 user.ap_id
235 |> Activity.all_by_actor_and_id([id1, id2])
236 |> Enum.sort(&(&1.id < &2.id))
237
238 assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities
239 end
240 end