logo

pleroma

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

conversation_test.exs (5744B)


  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.ConversationTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Conversation
  8. alias Pleroma.Object
  9. alias Pleroma.Web.CommonAPI
  10. import Pleroma.Factory
  11. setup_all do: clear_config([:instance, :federating], true)
  12. test "it goes through old direct conversations" do
  13. user = insert(:user)
  14. other_user = insert(:user)
  15. {:ok, _activity} =
  16. CommonAPI.post(user, %{visibility: "direct", status: "hey @#{other_user.nickname}"})
  17. Pleroma.Tests.ObanHelpers.perform_all()
  18. Repo.delete_all(Conversation)
  19. Repo.delete_all(Conversation.Participation)
  20. refute Repo.one(Conversation)
  21. Conversation.bump_for_all_activities()
  22. assert Repo.one(Conversation)
  23. [participation, _p2] = Repo.all(Conversation.Participation)
  24. assert participation.read
  25. end
  26. test "it creates a conversation for given ap_id" do
  27. assert {:ok, %Conversation{} = conversation} =
  28. Conversation.create_for_ap_id("https://some_ap_id")
  29. # Inserting again returns the same
  30. assert {:ok, conversation_two} = Conversation.create_for_ap_id("https://some_ap_id")
  31. assert conversation_two.id == conversation.id
  32. end
  33. test "public posts don't create conversations" do
  34. user = insert(:user)
  35. {:ok, activity} = CommonAPI.post(user, %{status: "Hey"})
  36. object = Pleroma.Object.normalize(activity)
  37. context = object.data["context"]
  38. conversation = Conversation.get_for_ap_id(context)
  39. refute conversation
  40. end
  41. test "it creates or updates a conversation and participations for a given DM" do
  42. har = insert(:user)
  43. jafnhar = insert(:user, local: false)
  44. tridi = insert(:user)
  45. {:ok, activity} =
  46. CommonAPI.post(har, %{status: "Hey @#{jafnhar.nickname}", visibility: "direct"})
  47. object = Pleroma.Object.normalize(activity)
  48. context = object.data["context"]
  49. conversation =
  50. Conversation.get_for_ap_id(context)
  51. |> Repo.preload(:participations)
  52. assert conversation
  53. assert Enum.find(conversation.participations, fn %{user_id: user_id} -> har.id == user_id end)
  54. assert Enum.find(conversation.participations, fn %{user_id: user_id} ->
  55. jafnhar.id == user_id
  56. end)
  57. {:ok, activity} =
  58. CommonAPI.post(jafnhar, %{
  59. status: "Hey @#{har.nickname}",
  60. visibility: "direct",
  61. in_reply_to_status_id: activity.id
  62. })
  63. object = Pleroma.Object.normalize(activity)
  64. context = object.data["context"]
  65. conversation_two =
  66. Conversation.get_for_ap_id(context)
  67. |> Repo.preload(:participations)
  68. assert conversation_two.id == conversation.id
  69. assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
  70. har.id == user_id
  71. end)
  72. assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
  73. jafnhar.id == user_id
  74. end)
  75. {:ok, activity} =
  76. CommonAPI.post(tridi, %{
  77. status: "Hey @#{har.nickname}",
  78. visibility: "direct",
  79. in_reply_to_status_id: activity.id
  80. })
  81. object = Pleroma.Object.normalize(activity)
  82. context = object.data["context"]
  83. conversation_three =
  84. Conversation.get_for_ap_id(context)
  85. |> Repo.preload([:participations, :users])
  86. assert conversation_three.id == conversation.id
  87. assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
  88. har.id == user_id
  89. end)
  90. assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
  91. jafnhar.id == user_id
  92. end)
  93. assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
  94. tridi.id == user_id
  95. end)
  96. assert Enum.find(conversation_three.users, fn %{id: user_id} ->
  97. har.id == user_id
  98. end)
  99. assert Enum.find(conversation_three.users, fn %{id: user_id} ->
  100. jafnhar.id == user_id
  101. end)
  102. assert Enum.find(conversation_three.users, fn %{id: user_id} ->
  103. tridi.id == user_id
  104. end)
  105. end
  106. test "create_or_bump_for returns the conversation with participations" do
  107. har = insert(:user)
  108. jafnhar = insert(:user, local: false)
  109. {:ok, activity} =
  110. CommonAPI.post(har, %{status: "Hey @#{jafnhar.nickname}", visibility: "direct"})
  111. {:ok, conversation} = Conversation.create_or_bump_for(activity)
  112. assert length(conversation.participations) == 2
  113. {:ok, activity} =
  114. CommonAPI.post(har, %{status: "Hey @#{jafnhar.nickname}", visibility: "public"})
  115. assert {:error, _} = Conversation.create_or_bump_for(activity)
  116. end
  117. test "create_or_bump_for does not normalize objects before checking the activity type" do
  118. note = insert(:note)
  119. note_id = note.data["id"]
  120. Repo.delete(note)
  121. refute Object.get_by_ap_id(note_id)
  122. Tesla.Mock.mock(fn env ->
  123. case env.url do
  124. ^note_id ->
  125. # TODO: add attributedTo and tag to the note factory
  126. body =
  127. note.data
  128. |> Map.put("attributedTo", note.data["actor"])
  129. |> Map.put("tag", [])
  130. |> Jason.encode!()
  131. %Tesla.Env{status: 200, body: body}
  132. end
  133. end)
  134. undo = %Activity{
  135. id: "fake",
  136. data: %{
  137. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  138. "actor" => note.data["actor"],
  139. "to" => [note.data["actor"]],
  140. "object" => note_id,
  141. "type" => "Undo"
  142. }
  143. }
  144. Conversation.create_or_bump_for(undo)
  145. refute Object.get_by_ap_id(note_id)
  146. end
  147. end