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