1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ActivityTest do
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.ThreadMute
12 import Pleroma.Factory
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
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"])
23 assert activity == found_activity
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
30 [found_activity] = Activity.get_all_create_by_object_ap_id(object_data["id"])
32 assert activity == found_activity
35 test "returns the activity that created an object" do
36 activity = insert(:note_activity)
37 object_data = Object.normalize(activity, fetch: false).data
39 found_activity = Activity.get_create_by_object_ap_id(object_data["id"])
41 assert activity == found_activity
44 test "preloading a bookmark" do
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)
54 Ecto.Query.from(Pleroma.Activity)
55 |> Activity.with_preloaded_bookmark(user3)
58 assert queried_activity.bookmark == bookmark3
61 test "setting thread_muted?" do
62 activity = insert(:note_activity)
64 annoyed_user = insert(:user)
65 {:ok, _} = ThreadMute.add_mute(annoyed_user.id, activity.data["context"])
67 activity_with_unset_thread_muted_field =
68 Ecto.Query.from(Activity)
72 Ecto.Query.from(Activity)
73 |> Activity.with_set_thread_muted_field(user)
76 activity_for_annoyed_user =
77 Ecto.Query.from(Activity)
78 |> Activity.with_set_thread_muted_field(annoyed_user)
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
86 describe "getting a bookmark" do
87 test "when association is loaded" do
89 activity = insert(:note_activity)
90 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
93 Ecto.Query.from(Pleroma.Activity)
94 |> Activity.with_preloaded_bookmark(user)
97 assert Activity.get_bookmark(queried_activity, user) == bookmark
100 test "when association is not loaded" do
102 activity = insert(:note_activity)
103 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
106 Ecto.Query.from(Pleroma.Activity)
109 assert Activity.get_bookmark(queried_activity, user) == bookmark
118 "@context" => "https://www.w3.org/ns/activitystreams",
119 "actor" => "http://mastodon.example.org/users/admin",
121 "id" => "http://mastodon.example.org/users/admin/activities/1",
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"]
129 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
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)
139 japanese_activity: japanese_activity,
140 local_activity: local_activity,
141 remote_activity: remote_activity,
146 setup do: clear_config([:instance, :limit_to_local_content])
148 test "finds utf8 text in statuses", %{
149 japanese_activity: japanese_activity,
152 activities = Activity.search(user, "更新情報")
154 assert [^japanese_activity] = activities
157 test "find local and remote statuses for authenticated users", %{
158 local_activity: local_activity,
159 remote_activity: remote_activity,
162 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
164 assert [^local_activity, ^remote_activity] = activities
167 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
168 assert [^local_activity] = Activity.search(nil, "find me")
171 test "find only local statuses for unauthenticated users when `limit_to_local_content` is `:all`",
172 %{local_activity: local_activity} do
173 clear_config([:instance, :limit_to_local_content], :all)
174 assert [^local_activity] = Activity.search(nil, "find me")
177 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
179 local_activity: local_activity,
180 remote_activity: remote_activity
182 clear_config([:instance, :limit_to_local_content], false)
184 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
186 assert [^local_activity, ^remote_activity] = activities
190 test "all_by_ids_with_object/1" do
191 %{id: id1} = insert(:note_activity)
192 %{id: id2} = insert(:note_activity)
196 |> Activity.all_by_ids_with_object()
197 |> Enum.sort(&(&1.id < &2.id))
199 assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities
202 test "get_by_id_with_user_actor/1" do
204 activity = insert(:note_activity, note: insert(:note, user: user))
206 assert Activity.get_by_id_with_user_actor(activity.id).user_actor == user
209 test "get_by_id_with_object/1" do
210 %{id: id} = insert(:note_activity)
212 assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id)
215 test "get_by_ap_id_with_object/1" do
216 %{data: %{"id" => ap_id}} = insert(:note_activity)
218 assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} =
219 Activity.get_by_ap_id_with_object(ap_id)
222 test "get_by_id/1" do
223 %{id: id} = insert(:note_activity)
225 assert %Activity{id: ^id} = Activity.get_by_id(id)
228 test "all_by_actor_and_id/2" do
231 {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofe"})
232 {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofefe"})
234 assert [] == Activity.all_by_actor_and_id(user, [])
238 |> Activity.all_by_actor_and_id([id1, id2])
239 |> Enum.sort(&(&1.id < &2.id))
241 assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities
244 test "get_by_object_ap_id_with_object/1" do
246 another = insert(:user)
248 {:ok, %{id: id, object: %{data: %{"id" => obj_id}}}} =
249 Pleroma.Web.CommonAPI.post(user, %{status: "cofe"})
251 Pleroma.Web.CommonAPI.favorite(another, id)
254 |> Pleroma.Activity.Queries.by_object_id()
255 |> Repo.aggregate(:count, :id) == 2
257 assert %{id: ^id} = Activity.get_by_object_ap_id_with_object(obj_id)
260 test "add_by_params_query/3" do
263 note = insert(:note_activity, user: user)
265 insert(:add_activity, user: user, note: note)
266 insert(:add_activity, user: user, note: note)
267 insert(:add_activity, user: user)
269 assert Repo.aggregate(Activity, :count, :id) == 4
272 Activity.add_by_params_query(note.data["object"], user.ap_id, user.featured_address)
274 assert Repo.aggregate(add_query, :count, :id) == 2
276 Repo.delete_all(add_query)
277 assert Repo.aggregate(add_query, :count, :id) == 0
279 assert Repo.aggregate(Activity, :count, :id) == 2