logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

activity_test.exs (7361B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.ActivityTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Bookmark
  8. alias Pleroma.Object
  9. alias Pleroma.Tests.ObanHelpers
  10. alias Pleroma.ThreadMute
  11. import Pleroma.Factory
  12. setup_all do
  13. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  14. :ok
  15. end
  16. test "returns an activity by it's AP id" do
  17. activity = insert(:note_activity)
  18. found_activity = Activity.get_by_ap_id(activity.data["id"])
  19. assert activity == found_activity
  20. end
  21. test "returns activities by it's objects AP ids" do
  22. activity = insert(:note_activity)
  23. object_data = Object.normalize(activity).data
  24. [found_activity] = Activity.get_all_create_by_object_ap_id(object_data["id"])
  25. assert activity == found_activity
  26. end
  27. test "returns the activity that created an object" do
  28. activity = insert(:note_activity)
  29. object_data = Object.normalize(activity).data
  30. found_activity = Activity.get_create_by_object_ap_id(object_data["id"])
  31. assert activity == found_activity
  32. end
  33. test "preloading a bookmark" do
  34. user = insert(:user)
  35. user2 = insert(:user)
  36. user3 = insert(:user)
  37. activity = insert(:note_activity)
  38. {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
  39. {:ok, _bookmark2} = Bookmark.create(user2.id, activity.id)
  40. {:ok, bookmark3} = Bookmark.create(user3.id, activity.id)
  41. queried_activity =
  42. Ecto.Query.from(Pleroma.Activity)
  43. |> Activity.with_preloaded_bookmark(user3)
  44. |> Repo.one()
  45. assert queried_activity.bookmark == bookmark3
  46. end
  47. test "setting thread_muted?" do
  48. activity = insert(:note_activity)
  49. user = insert(:user)
  50. annoyed_user = insert(:user)
  51. {:ok, _} = ThreadMute.add_mute(annoyed_user.id, activity.data["context"])
  52. activity_with_unset_thread_muted_field =
  53. Ecto.Query.from(Activity)
  54. |> Repo.one()
  55. activity_for_user =
  56. Ecto.Query.from(Activity)
  57. |> Activity.with_set_thread_muted_field(user)
  58. |> Repo.one()
  59. activity_for_annoyed_user =
  60. Ecto.Query.from(Activity)
  61. |> Activity.with_set_thread_muted_field(annoyed_user)
  62. |> Repo.one()
  63. assert activity_with_unset_thread_muted_field.thread_muted? == nil
  64. assert activity_for_user.thread_muted? == false
  65. assert activity_for_annoyed_user.thread_muted? == true
  66. end
  67. describe "getting a bookmark" do
  68. test "when association is loaded" do
  69. user = insert(:user)
  70. activity = insert(:note_activity)
  71. {:ok, bookmark} = Bookmark.create(user.id, activity.id)
  72. queried_activity =
  73. Ecto.Query.from(Pleroma.Activity)
  74. |> Activity.with_preloaded_bookmark(user)
  75. |> Repo.one()
  76. assert Activity.get_bookmark(queried_activity, user) == bookmark
  77. end
  78. test "when association is not loaded" do
  79. user = insert(:user)
  80. activity = insert(:note_activity)
  81. {:ok, bookmark} = Bookmark.create(user.id, activity.id)
  82. queried_activity =
  83. Ecto.Query.from(Pleroma.Activity)
  84. |> Repo.one()
  85. assert Activity.get_bookmark(queried_activity, user) == bookmark
  86. end
  87. end
  88. describe "search" do
  89. setup do
  90. user = insert(:user)
  91. params = %{
  92. "@context" => "https://www.w3.org/ns/activitystreams",
  93. "actor" => "http://mastodon.example.org/users/admin",
  94. "type" => "Create",
  95. "id" => "http://mastodon.example.org/users/admin/activities/1",
  96. "object" => %{
  97. "type" => "Note",
  98. "content" => "find me!",
  99. "id" => "http://mastodon.example.org/users/admin/objects/1",
  100. "attributedTo" => "http://mastodon.example.org/users/admin",
  101. "to" => ["https://www.w3.org/ns/activitystreams#Public"]
  102. },
  103. "to" => ["https://www.w3.org/ns/activitystreams#Public"]
  104. }
  105. {:ok, local_activity} = Pleroma.Web.CommonAPI.post(user, %{status: "find me!"})
  106. {:ok, japanese_activity} = Pleroma.Web.CommonAPI.post(user, %{status: "更新情報"})
  107. {:ok, job} = Pleroma.Web.Federator.incoming_ap_doc(params)
  108. {:ok, remote_activity} = ObanHelpers.perform(job)
  109. remote_activity = Activity.get_by_id_with_object(remote_activity.id)
  110. %{
  111. japanese_activity: japanese_activity,
  112. local_activity: local_activity,
  113. remote_activity: remote_activity,
  114. user: user
  115. }
  116. end
  117. setup do: clear_config([:instance, :limit_to_local_content])
  118. test "finds utf8 text in statuses", %{
  119. japanese_activity: japanese_activity,
  120. user: user
  121. } do
  122. activities = Activity.search(user, "更新情報")
  123. assert [^japanese_activity] = activities
  124. end
  125. test "find local and remote statuses for authenticated users", %{
  126. local_activity: local_activity,
  127. remote_activity: remote_activity,
  128. user: user
  129. } do
  130. activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
  131. assert [^local_activity, ^remote_activity] = activities
  132. end
  133. test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
  134. assert [^local_activity] = Activity.search(nil, "find me")
  135. end
  136. test "find only local statuses for unauthenticated users when `limit_to_local_content` is `:all`",
  137. %{local_activity: local_activity} do
  138. Pleroma.Config.put([:instance, :limit_to_local_content], :all)
  139. assert [^local_activity] = Activity.search(nil, "find me")
  140. end
  141. test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
  142. %{
  143. local_activity: local_activity,
  144. remote_activity: remote_activity
  145. } do
  146. Pleroma.Config.put([:instance, :limit_to_local_content], false)
  147. activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
  148. assert [^local_activity, ^remote_activity] = activities
  149. end
  150. end
  151. test "all_by_ids_with_object/1" do
  152. %{id: id1} = insert(:note_activity)
  153. %{id: id2} = insert(:note_activity)
  154. activities =
  155. [id1, id2]
  156. |> Activity.all_by_ids_with_object()
  157. |> Enum.sort(&(&1.id < &2.id))
  158. assert [%{id: ^id1, object: %Object{}}, %{id: ^id2, object: %Object{}}] = activities
  159. end
  160. test "get_by_id_with_object/1" do
  161. %{id: id} = insert(:note_activity)
  162. assert %Activity{id: ^id, object: %Object{}} = Activity.get_by_id_with_object(id)
  163. end
  164. test "get_by_ap_id_with_object/1" do
  165. %{data: %{"id" => ap_id}} = insert(:note_activity)
  166. assert %Activity{data: %{"id" => ^ap_id}, object: %Object{}} =
  167. Activity.get_by_ap_id_with_object(ap_id)
  168. end
  169. test "get_by_id/1" do
  170. %{id: id} = insert(:note_activity)
  171. assert %Activity{id: ^id} = Activity.get_by_id(id)
  172. end
  173. test "all_by_actor_and_id/2" do
  174. user = insert(:user)
  175. {:ok, %{id: id1}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofe"})
  176. {:ok, %{id: id2}} = Pleroma.Web.CommonAPI.post(user, %{status: "cofefe"})
  177. assert [] == Activity.all_by_actor_and_id(user, [])
  178. activities =
  179. user.ap_id
  180. |> Activity.all_by_actor_and_id([id1, id2])
  181. |> Enum.sort(&(&1.id < &2.id))
  182. assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities
  183. end
  184. end