[#1234] Merge remote-tracking branch 'remotes/upstream/develop' into 1234-mastodon...
[akkoma] / test / activity_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.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, job} = Pleroma.Web.Federator.incoming_ap_doc(params)
130 {:ok, remote_activity} = ObanHelpers.perform(job)
131 %{local_activity: local_activity, remote_activity: remote_activity, user: user}
132 end
133
134 test "find local and remote statuses for authenticated users", %{
135 local_activity: local_activity,
136 remote_activity: remote_activity,
137 user: user
138 } do
139 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
140
141 assert [^local_activity, ^remote_activity] = activities
142 end
143
144 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
145 assert [^local_activity] = Activity.search(nil, "find me")
146 end
147
148 test "find only local statuses for unauthenticated users when `limit_to_local_content` is `:all`",
149 %{local_activity: local_activity} do
150 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
151 assert [^local_activity] = Activity.search(nil, "find me")
152 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
153 end
154
155 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
156 %{
157 local_activity: local_activity,
158 remote_activity: remote_activity
159 } do
160 Pleroma.Config.put([:instance, :limit_to_local_content], false)
161
162 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
163
164 assert [^local_activity, ^remote_activity] = activities
165
166 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
167 end
168 end
169
170 test "add an activity with an expiration" do
171 activity = insert(:note_activity)
172 insert(:expiration_in_the_future, %{activity_id: activity.id})
173
174 Pleroma.ActivityExpiration
175 |> where([a], a.activity_id == ^activity.id)
176 |> Repo.one!()
177 end
178
179 test "all_by_ids_with_object/1" do
180 %{id: id1} = insert(:note_activity)
181 %{id: id2} = insert(:note_activity)
182
183 activities =
184 [id1, id2]
185 |> Activity.all_by_ids_with_object()
186 |> Enum.sort(&(&1.id < &2.id))
187
188 assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities
189 end
190
191 test "get_by_id_with_object/1" do
192 %{id: id} = insert(:note_activity)
193
194 assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id)
195 end
196
197 test "get_by_ap_id_with_object/1" do
198 %{data: %{"id" => ap_id}} = insert(:note_activity)
199
200 assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} =
201 Activity.get_by_ap_id_with_object(ap_id)
202 end
203
204 test "get_by_id/1" do
205 %{id: id} = insert(:note_activity)
206
207 assert %Activity{id: ^id} = Activity.get_by_id(id)
208 end
209
210 test "all_by_actor_and_id/2" do
211 user = insert(:user)
212
213 {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofe"})
214 {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofefe"})
215
216 assert [] == Activity.all_by_actor_and_id(user, [])
217
218 activities =
219 user.ap_id
220 |> Activity.all_by_actor_and_id([id1, id2])
221 |> Enum.sort(&(&1.id < &2.id))
222
223 assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities
224 end
225 end