1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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, 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}
134 test "find local and remote statuses for authenticated users", %{
135 local_activity: local_activity,
136 remote_activity: remote_activity,
139 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
141 assert [^local_activity, ^remote_activity] = activities
144 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
145 assert [^local_activity] = Activity.search(nil, "find me")
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)
155 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
157 local_activity: local_activity,
158 remote_activity: remote_activity
160 Pleroma.Config.put([:instance, :limit_to_local_content], false)
162 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
164 assert [^local_activity, ^remote_activity] = activities
166 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
170 test "add an activity with an expiration" do
171 activity = insert(:note_activity)
172 insert(:expiration_in_the_future, %{activity_id: activity.id})
174 Pleroma.ActivityExpiration
175 |> where([a], a.activity_id == ^activity.id)
179 test "all_by_ids_with_object/1" do
180 %{id: id1} = insert(:note_activity)
181 %{id: id2} = insert(:note_activity)
185 |> Activity.all_by_ids_with_object()
186 |> Enum.sort(&(&1.id < &2.id))
188 assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities
191 test "get_by_id_with_object/1" do
192 %{id: id} = insert(:note_activity)
194 assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id)
197 test "get_by_ap_id_with_object/1" do
198 %{data: %{"id" => ap_id}} = insert(:note_activity)
200 assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} =
201 Activity.get_by_ap_id_with_object(ap_id)
204 test "get_by_id/1" do
205 %{id: id} = insert(:note_activity)
207 assert %Activity{id: ^id} = Activity.get_by_id(id)
210 test "all_by_actor_and_id/2" do
213 {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofe"})
214 {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofefe"})
216 assert [] == Activity.all_by_actor_and_id(user, [])
220 |> Activity.all_by_actor_and_id([id1, id2])
221 |> Enum.sort(&(&1.id < &2.id))
223 assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities