logo

pleroma

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

participation_test.exs (12923B)


  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.Conversation.ParticipationTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.Conversation
  8. alias Pleroma.Conversation.Participation
  9. alias Pleroma.Repo
  10. alias Pleroma.User
  11. alias Pleroma.Web.CommonAPI
  12. test "getting a participation will also preload things" do
  13. user = insert(:user)
  14. other_user = insert(:user)
  15. {:ok, _activity} =
  16. CommonAPI.post(user, %{status: "Hey @#{other_user.nickname}.", visibility: "direct"})
  17. [participation] = Participation.for_user(user)
  18. participation = Participation.get(participation.id, preload: [:conversation])
  19. assert %Pleroma.Conversation{} = participation.conversation
  20. end
  21. test "for a new conversation or a reply, it doesn't mark the author's participation as unread" do
  22. user = insert(:user)
  23. other_user = insert(:user)
  24. {:ok, _} =
  25. CommonAPI.post(user, %{status: "Hey @#{other_user.nickname}.", visibility: "direct"})
  26. user = User.get_cached_by_id(user.id)
  27. other_user = User.get_cached_by_id(other_user.id)
  28. [%{read: true}] = Participation.for_user(user)
  29. [%{read: false} = participation] = Participation.for_user(other_user)
  30. assert User.get_cached_by_id(user.id).unread_conversation_count == 0
  31. assert User.get_cached_by_id(other_user.id).unread_conversation_count == 1
  32. {:ok, _} =
  33. CommonAPI.post(other_user, %{
  34. status: "Hey @#{user.nickname}.",
  35. visibility: "direct",
  36. in_reply_to_conversation_id: participation.id
  37. })
  38. user = User.get_cached_by_id(user.id)
  39. other_user = User.get_cached_by_id(other_user.id)
  40. [%{read: false}] = Participation.for_user(user)
  41. [%{read: true}] = Participation.for_user(other_user)
  42. assert User.get_cached_by_id(user.id).unread_conversation_count == 1
  43. assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
  44. end
  45. test "for a new conversation, it sets the recipents of the participation" do
  46. user = insert(:user)
  47. other_user = insert(:user)
  48. third_user = insert(:user)
  49. {:ok, activity} =
  50. CommonAPI.post(user, %{status: "Hey @#{other_user.nickname}.", visibility: "direct"})
  51. user = User.get_cached_by_id(user.id)
  52. other_user = User.get_cached_by_id(other_user.id)
  53. [participation] = Participation.for_user(user)
  54. participation = Pleroma.Repo.preload(participation, :recipients)
  55. assert length(participation.recipients) == 2
  56. assert user in participation.recipients
  57. assert other_user in participation.recipients
  58. # Mentioning another user in the same conversation will not add a new recipients.
  59. {:ok, _activity} =
  60. CommonAPI.post(user, %{
  61. in_reply_to_status_id: activity.id,
  62. status: "Hey @#{third_user.nickname}.",
  63. visibility: "direct"
  64. })
  65. [participation] = Participation.for_user(user)
  66. participation = Pleroma.Repo.preload(participation, :recipients)
  67. assert length(participation.recipients) == 2
  68. end
  69. test "it creates a participation for a conversation and a user" do
  70. user = insert(:user)
  71. conversation = insert(:conversation)
  72. {:ok, %Participation{} = participation} =
  73. Participation.create_for_user_and_conversation(user, conversation)
  74. assert participation.user_id == user.id
  75. assert participation.conversation_id == conversation.id
  76. # Needed because updated_at is accurate down to a second
  77. :timer.sleep(1000)
  78. # Creating again returns the same participation
  79. {:ok, %Participation{} = participation_two} =
  80. Participation.create_for_user_and_conversation(user, conversation)
  81. assert participation.id == participation_two.id
  82. refute participation.updated_at == participation_two.updated_at
  83. end
  84. test "recreating an existing participations sets it to unread" do
  85. participation = insert(:participation, %{read: true})
  86. {:ok, participation} =
  87. Participation.create_for_user_and_conversation(
  88. participation.user,
  89. participation.conversation
  90. )
  91. refute participation.read
  92. end
  93. test "it marks a participation as read" do
  94. participation = insert(:participation, %{read: false})
  95. {:ok, updated_participation} = Participation.mark_as_read(participation)
  96. assert updated_participation.read
  97. assert updated_participation.updated_at == participation.updated_at
  98. end
  99. test "it marks a participation as unread" do
  100. participation = insert(:participation, %{read: true})
  101. {:ok, participation} = Participation.mark_as_unread(participation)
  102. refute participation.read
  103. end
  104. test "it marks all the user's participations as read" do
  105. user = insert(:user)
  106. other_user = insert(:user)
  107. participation1 = insert(:participation, %{read: false, user: user})
  108. participation2 = insert(:participation, %{read: false, user: user})
  109. participation3 = insert(:participation, %{read: false, user: other_user})
  110. {:ok, _, [%{read: true}, %{read: true}]} = Participation.mark_all_as_read(user)
  111. assert Participation.get(participation1.id).read == true
  112. assert Participation.get(participation2.id).read == true
  113. assert Participation.get(participation3.id).read == false
  114. end
  115. test "gets all the participations for a user, ordered by updated at descending" do
  116. user = insert(:user)
  117. {:ok, activity_one} = CommonAPI.post(user, %{status: "x", visibility: "direct"})
  118. {:ok, activity_two} = CommonAPI.post(user, %{status: "x", visibility: "direct"})
  119. {:ok, activity_three} =
  120. CommonAPI.post(user, %{
  121. status: "x",
  122. visibility: "direct",
  123. in_reply_to_status_id: activity_one.id
  124. })
  125. # Offset participations because the accuracy of updated_at is down to a second
  126. for {activity, offset} <- [{activity_two, 1}, {activity_three, 2}] do
  127. conversation = Conversation.get_for_ap_id(activity.data["context"])
  128. participation = Participation.for_user_and_conversation(user, conversation)
  129. updated_at = NaiveDateTime.add(Map.get(participation, :updated_at), offset)
  130. Ecto.Changeset.change(participation, %{updated_at: updated_at})
  131. |> Repo.update!()
  132. end
  133. assert [participation_one, participation_two] = Participation.for_user(user)
  134. object2 = Pleroma.Object.normalize(activity_two)
  135. object3 = Pleroma.Object.normalize(activity_three)
  136. user = Repo.get(Pleroma.User, user.id)
  137. assert participation_one.conversation.ap_id == object3.data["context"]
  138. assert participation_two.conversation.ap_id == object2.data["context"]
  139. assert participation_one.conversation.users == [user]
  140. # Pagination
  141. assert [participation_one] = Participation.for_user(user, %{"limit" => 1})
  142. assert participation_one.conversation.ap_id == object3.data["context"]
  143. # With last_activity_id
  144. assert [participation_one] =
  145. Participation.for_user_with_last_activity_id(user, %{"limit" => 1})
  146. assert participation_one.last_activity_id == activity_three.id
  147. end
  148. test "Doesn't die when the conversation gets empty" do
  149. user = insert(:user)
  150. {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
  151. [participation] = Participation.for_user_with_last_activity_id(user)
  152. assert participation.last_activity_id == activity.id
  153. {:ok, _} = CommonAPI.delete(activity.id, user)
  154. [] = Participation.for_user_with_last_activity_id(user)
  155. end
  156. test "it sets recipients, always keeping the owner of the participation even when not explicitly set" do
  157. user = insert(:user)
  158. other_user = insert(:user)
  159. {:ok, _activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
  160. [participation] = Participation.for_user_with_last_activity_id(user)
  161. participation = Repo.preload(participation, :recipients)
  162. user = User.get_cached_by_id(user.id)
  163. assert participation.recipients |> length() == 1
  164. assert user in participation.recipients
  165. {:ok, participation} = Participation.set_recipients(participation, [other_user.id])
  166. assert participation.recipients |> length() == 2
  167. assert user in participation.recipients
  168. assert other_user in participation.recipients
  169. end
  170. describe "blocking" do
  171. test "when the user blocks a recipient, the existing conversations with them are marked as read" do
  172. blocker = insert(:user)
  173. blocked = insert(:user)
  174. third_user = insert(:user)
  175. {:ok, _direct1} =
  176. CommonAPI.post(third_user, %{
  177. status: "Hi @#{blocker.nickname}",
  178. visibility: "direct"
  179. })
  180. {:ok, _direct2} =
  181. CommonAPI.post(third_user, %{
  182. status: "Hi @#{blocker.nickname}, @#{blocked.nickname}",
  183. visibility: "direct"
  184. })
  185. {:ok, _direct3} =
  186. CommonAPI.post(blocked, %{
  187. status: "Hi @#{blocker.nickname}",
  188. visibility: "direct"
  189. })
  190. {:ok, _direct4} =
  191. CommonAPI.post(blocked, %{
  192. status: "Hi @#{blocker.nickname}, @#{third_user.nickname}",
  193. visibility: "direct"
  194. })
  195. assert [%{read: false}, %{read: false}, %{read: false}, %{read: false}] =
  196. Participation.for_user(blocker)
  197. assert User.get_cached_by_id(blocker.id).unread_conversation_count == 4
  198. {:ok, _user_relationship} = User.block(blocker, blocked)
  199. # The conversations with the blocked user are marked as read
  200. assert [%{read: true}, %{read: true}, %{read: true}, %{read: false}] =
  201. Participation.for_user(blocker)
  202. assert User.get_cached_by_id(blocker.id).unread_conversation_count == 1
  203. # The conversation is not marked as read for the blocked user
  204. assert [_, _, %{read: false}] = Participation.for_user(blocked)
  205. assert User.get_cached_by_id(blocked.id).unread_conversation_count == 1
  206. # The conversation is not marked as read for the third user
  207. assert [%{read: false}, _, _] = Participation.for_user(third_user)
  208. assert User.get_cached_by_id(third_user.id).unread_conversation_count == 1
  209. end
  210. test "the new conversation with the blocked user is not marked as unread " do
  211. blocker = insert(:user)
  212. blocked = insert(:user)
  213. third_user = insert(:user)
  214. {:ok, _user_relationship} = User.block(blocker, blocked)
  215. # When the blocked user is the author
  216. {:ok, _direct1} =
  217. CommonAPI.post(blocked, %{
  218. status: "Hi @#{blocker.nickname}",
  219. visibility: "direct"
  220. })
  221. assert [%{read: true}] = Participation.for_user(blocker)
  222. assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
  223. # When the blocked user is a recipient
  224. {:ok, _direct2} =
  225. CommonAPI.post(third_user, %{
  226. status: "Hi @#{blocker.nickname}, @#{blocked.nickname}",
  227. visibility: "direct"
  228. })
  229. assert [%{read: true}, %{read: true}] = Participation.for_user(blocker)
  230. assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
  231. assert [%{read: false}, _] = Participation.for_user(blocked)
  232. assert User.get_cached_by_id(blocked.id).unread_conversation_count == 1
  233. end
  234. test "the conversation with the blocked user is not marked as unread on a reply" do
  235. blocker = insert(:user)
  236. blocked = insert(:user)
  237. third_user = insert(:user)
  238. {:ok, _direct1} =
  239. CommonAPI.post(blocker, %{
  240. status: "Hi @#{third_user.nickname}, @#{blocked.nickname}",
  241. visibility: "direct"
  242. })
  243. {:ok, _user_relationship} = User.block(blocker, blocked)
  244. assert [%{read: true}] = Participation.for_user(blocker)
  245. assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
  246. assert [blocked_participation] = Participation.for_user(blocked)
  247. # When it's a reply from the blocked user
  248. {:ok, _direct2} =
  249. CommonAPI.post(blocked, %{
  250. status: "reply",
  251. visibility: "direct",
  252. in_reply_to_conversation_id: blocked_participation.id
  253. })
  254. assert [%{read: true}] = Participation.for_user(blocker)
  255. assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
  256. assert [third_user_participation] = Participation.for_user(third_user)
  257. # When it's a reply from the third user
  258. {:ok, _direct3} =
  259. CommonAPI.post(third_user, %{
  260. status: "reply",
  261. visibility: "direct",
  262. in_reply_to_conversation_id: third_user_participation.id
  263. })
  264. assert [%{read: true}] = Participation.for_user(blocker)
  265. assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
  266. # Marked as unread for the blocked user
  267. assert [%{read: false}] = Participation.for_user(blocked)
  268. assert User.get_cached_by_id(blocked.id).unread_conversation_count == 1
  269. end
  270. end
  271. end