Preload bookmarks wherever the object is preloaded
[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 import Pleroma.Factory
11
12 test "returns an activity by it's AP id" do
13 activity = insert(:note_activity)
14 found_activity = Activity.get_by_ap_id(activity.data["id"])
15
16 assert activity == found_activity
17 end
18
19 test "returns activities by it's objects AP ids" do
20 activity = insert(:note_activity)
21 [found_activity] = Activity.get_all_create_by_object_ap_id(activity.data["object"]["id"])
22
23 assert activity == found_activity
24 end
25
26 test "returns the activity that created an object" do
27 activity = insert(:note_activity)
28
29 found_activity = Activity.get_create_by_object_ap_id(activity.data["object"]["id"])
30
31 assert activity == found_activity
32 end
33
34 test "preloading object preloads bookmarks" do
35 user1 = insert(:user)
36 user2 = insert(:user)
37 activity = insert(:note_activity)
38 {:ok, bookmark1} = Bookmark.create(user1.id, activity.id)
39 {:ok, bookmark2} = Bookmark.create(user2.id, activity.id)
40 bookmarks = Enum.sort([bookmark1, bookmark2])
41
42 queried_activity =
43 Ecto.Query.from(a in Activity, where: a.id == ^activity.id)
44 |> Activity.with_preloaded_object()
45 |> Repo.one()
46
47 assert Enum.sort(queried_activity.bookmarks) == bookmarks
48
49 queried_activity = Activity.get_by_ap_id_with_object(activity.data["id"])
50 assert Enum.sort(queried_activity.bookmarks) == bookmarks
51
52 queried_activity = Activity.get_by_id_with_object(activity.id)
53 assert Enum.sort(queried_activity.bookmarks) == bookmarks
54
55 queried_activity =
56 Activity.get_create_by_object_ap_id_with_object(Object.normalize(activity).data["id"])
57
58 assert Enum.sort(queried_activity.bookmarks) == bookmarks
59 end
60 end