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.ThreadMute
11 import Pleroma.Factory
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"])
17 assert activity == found_activity
20 test "returns activities by it's objects AP ids" do
21 activity = insert(:note_activity)
22 object_data = Object.normalize(activity).data
24 [found_activity] = Activity.get_all_create_by_object_ap_id(object_data["id"])
26 assert activity == found_activity
29 test "returns the activity that created an object" do
30 activity = insert(:note_activity)
31 object_data = Object.normalize(activity).data
33 found_activity = Activity.get_create_by_object_ap_id(object_data["id"])
35 assert activity == found_activity
38 test "preloading a bookmark" do
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)
48 Ecto.Query.from(Pleroma.Activity)
49 |> Activity.with_preloaded_bookmark(user3)
52 assert queried_activity.bookmark == bookmark3
55 test "setting thread_muted?" do
56 activity = insert(:note_activity)
58 annoyed_user = insert(:user)
59 {:ok, _} = ThreadMute.add_mute(annoyed_user.id, activity.data["context"])
61 activity_with_unset_thread_muted_field =
62 Ecto.Query.from(Activity)
66 Ecto.Query.from(Activity)
67 |> Activity.with_set_thread_muted_field(user)
70 activity_for_annoyed_user =
71 Ecto.Query.from(Activity)
72 |> Activity.with_set_thread_muted_field(annoyed_user)
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
80 describe "getting a bookmark" do
81 test "when association is loaded" do
83 activity = insert(:note_activity)
84 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
87 Ecto.Query.from(Pleroma.Activity)
88 |> Activity.with_preloaded_bookmark(user)
91 assert Activity.get_bookmark(queried_activity, user) == bookmark
94 test "when association is not loaded" do
96 activity = insert(:note_activity)
97 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
100 Ecto.Query.from(Pleroma.Activity)
103 assert Activity.get_bookmark(queried_activity, user) == bookmark
109 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
114 "@context" => "https://www.w3.org/ns/activitystreams",
115 "actor" => "http://mastodon.example.org/users/admin",
117 "id" => "http://mastodon.example.org/users/admin/activities/1",
120 "content" => "find me!",
121 "id" => "http://mastodon.example.org/users/admin/objects/1",
122 "attributedTo" => "http://mastodon.example.org/users/admin"
124 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
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}
132 test "find local and remote statuses for authenticated users", %{
133 local_activity: local_activity,
134 remote_activity: remote_activity,
137 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
139 assert [^local_activity, ^remote_activity] = activities
142 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
143 assert [^local_activity] = Activity.search(nil, "find me")
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)
153 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
155 local_activity: local_activity,
156 remote_activity: remote_activity
158 Pleroma.Config.put([:instance, :limit_to_local_content], false)
160 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
162 assert [^local_activity, ^remote_activity] = activities
164 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
168 test "add an activity with an expiration" do
169 activity = insert(:note_activity)
170 insert(:expiration_in_the_future, %{activity_id: activity.id})
172 Pleroma.ActivityExpiration
173 |> where([a], a.activity_id == ^activity.id)
177 test "all_by_ids_with_object/1" do
178 %{id: id1} = insert(:note_activity)
179 %{id: id2} = insert(:note_activity)
183 |> Activity.all_by_ids_with_object()
184 |> Enum.sort(&(&1.id < &2.id))
186 assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities
189 test "get_by_id_with_object/1" do
190 %{id: id} = insert(:note_activity)
192 assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id)
195 test "get_by_ap_id_with_object/1" do
196 %{data: %{"id" => ap_id}} = insert(:note_activity)
198 assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} =
199 Activity.get_by_ap_id_with_object(ap_id)
202 test "get_by_id/1" do
203 %{id: id} = insert(:note_activity)
205 assert %Activity{id: ^id} = Activity.get_by_id(id)
208 test "all_by_actor_and_id/2" do
211 {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofe"})
212 {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{"status" => "cofefe"})
214 assert [] == Activity.all_by_actor_and_id(user, [])
216 assert [%Activity{id: ^id2}, %Activity{id: ^id1}] =
217 Activity.all_by_actor_and_id(user.ap_id, [id1, id2])