1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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"])
18 assert activity == found_activity
21 test "returns activities by it's objects AP ids" do
22 activity = insert(:note_activity)
23 object_data = Object.normalize(activity).data
25 [found_activity] = Activity.get_all_create_by_object_ap_id(object_data["id"])
27 assert activity == found_activity
30 test "returns the activity that created an object" do
31 activity = insert(:note_activity)
32 object_data = Object.normalize(activity).data
34 found_activity = Activity.get_create_by_object_ap_id(object_data["id"])
36 assert activity == found_activity
39 test "preloading a bookmark" do
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)
49 Ecto.Query.from(Pleroma.Activity)
50 |> Activity.with_preloaded_bookmark(user3)
53 assert queried_activity.bookmark == bookmark3
56 test "setting thread_muted?" do
57 activity = insert(:note_activity)
59 annoyed_user = insert(:user)
60 {:ok, _} = ThreadMute.add_mute(annoyed_user.id, activity.data["context"])
62 activity_with_unset_thread_muted_field =
63 Ecto.Query.from(Activity)
67 Ecto.Query.from(Activity)
68 |> Activity.with_set_thread_muted_field(user)
71 activity_for_annoyed_user =
72 Ecto.Query.from(Activity)
73 |> Activity.with_set_thread_muted_field(annoyed_user)
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
81 describe "getting a bookmark" do
82 test "when association is loaded" do
84 activity = insert(:note_activity)
85 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
88 Ecto.Query.from(Pleroma.Activity)
89 |> Activity.with_preloaded_bookmark(user)
92 assert Activity.get_bookmark(queried_activity, user) == bookmark
95 test "when association is not loaded" do
97 activity = insert(:note_activity)
98 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
101 Ecto.Query.from(Pleroma.Activity)
104 assert Activity.get_bookmark(queried_activity, user) == bookmark
110 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
115 "@context" => "https://www.w3.org/ns/activitystreams",
116 "actor" => "http://mastodon.example.org/users/admin",
118 "id" => "http://mastodon.example.org/users/admin/activities/1",
121 "content" => "find me!",
122 "id" => "http://mastodon.example.org/users/admin/objects/1",
123 "attributedTo" => "http://mastodon.example.org/users/admin"
125 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
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)
134 japanese_activity: japanese_activity,
135 local_activity: local_activity,
136 remote_activity: remote_activity,
141 setup do: clear_config([:instance, :limit_to_local_content])
143 test "finds utf8 text in statuses", %{
144 japanese_activity: japanese_activity,
147 activities = Activity.search(user, "更新情報")
149 assert [^japanese_activity] = activities
152 test "find local and remote statuses for authenticated users", %{
153 local_activity: local_activity,
154 remote_activity: remote_activity,
157 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
159 assert [^local_activity, ^remote_activity] = activities
162 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
163 assert [^local_activity] = Activity.search(nil, "find me")
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")
172 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
174 local_activity: local_activity,
175 remote_activity: remote_activity
177 Pleroma.Config.put([:instance, :limit_to_local_content], false)
179 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
181 assert [^local_activity, ^remote_activity] = activities
185 test "add an activity with an expiration" do
186 activity = insert(:note_activity)
187 insert(:expiration_in_the_future, %{activity_id: activity.id})
189 Pleroma.ActivityExpiration
190 |> where([a], a.activity_id == ^activity.id)
194 test "all_by_ids_with_object/1" do
195 %{id: id1} = insert(:note_activity)
196 %{id: id2} = insert(:note_activity)
200 |> Activity.all_by_ids_with_object()
201 |> Enum.sort(&(&1.id < &2.id))
203 assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities
206 test "get_by_id_with_object/1" do
207 %{id: id} = insert(:note_activity)
209 assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id)
212 test "get_by_ap_id_with_object/1" do
213 %{data: %{"id" => ap_id}} = insert(:note_activity)
215 assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} =
216 Activity.get_by_ap_id_with_object(ap_id)
219 test "get_by_id/1" do
220 %{id: id} = insert(:note_activity)
222 assert %Activity{id: ^id} = Activity.get_by_id(id)
225 test "all_by_actor_and_id/2" do
228 {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofe"})
229 {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofefe"})
231 assert [] == Activity.all_by_actor_and_id(user, [])
235 |> Activity.all_by_actor_and_id([id1, id2])
236 |> Enum.sort(&(&1.id < &2.id))
238 assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities