1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Conversation.ParticipationTest do
8 alias Pleroma.Conversation
9 alias Pleroma.Conversation.Participation
12 alias Pleroma.Web.CommonAPI
14 test "getting a participation will also preload things" do
16 other_user = insert(:user)
19 CommonAPI.post(user, %{status: "Hey @#{other_user.nickname}.", visibility: "direct"})
21 [participation] = Participation.for_user(user)
23 participation = Participation.get(participation.id, preload: [:conversation])
25 assert %Pleroma.Conversation{} = participation.conversation
28 test "for a new conversation or a reply, it doesn't mark the author's participation as unread" do
30 other_user = insert(:user)
33 CommonAPI.post(user, %{status: "Hey @#{other_user.nickname}.", visibility: "direct"})
35 user = User.get_cached_by_id(user.id)
36 other_user = User.get_cached_by_id(other_user.id)
38 [%{read: true}] = Participation.for_user(user)
39 [%{read: false} = participation] = Participation.for_user(other_user)
41 assert User.get_cached_by_id(user.id).unread_conversation_count == 0
42 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 1
45 CommonAPI.post(other_user, %{
46 status: "Hey @#{user.nickname}.",
48 in_reply_to_conversation_id: participation.id
51 user = User.get_cached_by_id(user.id)
52 other_user = User.get_cached_by_id(other_user.id)
54 [%{read: false}] = Participation.for_user(user)
55 [%{read: true}] = Participation.for_user(other_user)
57 assert User.get_cached_by_id(user.id).unread_conversation_count == 1
58 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
61 test "for a new conversation, it sets the recipents of the participation" do
63 other_user = insert(:user)
64 third_user = insert(:user)
67 CommonAPI.post(user, %{status: "Hey @#{other_user.nickname}.", visibility: "direct"})
69 user = User.get_cached_by_id(user.id)
70 other_user = User.get_cached_by_id(other_user.id)
71 [participation] = Participation.for_user(user)
72 participation = Pleroma.Repo.preload(participation, :recipients)
74 assert length(participation.recipients) == 2
75 assert user in participation.recipients
76 assert other_user in participation.recipients
78 # Mentioning another user in the same conversation will not add a new recipients.
81 CommonAPI.post(user, %{
82 in_reply_to_status_id: activity.id,
83 status: "Hey @#{third_user.nickname}.",
87 [participation] = Participation.for_user(user)
88 participation = Pleroma.Repo.preload(participation, :recipients)
90 assert length(participation.recipients) == 2
93 test "it creates a participation for a conversation and a user" do
95 conversation = insert(:conversation)
97 {:ok, %Participation{} = participation} =
98 Participation.create_for_user_and_conversation(user, conversation)
100 assert participation.user_id == user.id
101 assert participation.conversation_id == conversation.id
103 # Needed because updated_at is accurate down to a second
106 # Creating again returns the same participation
107 {:ok, %Participation{} = participation_two} =
108 Participation.create_for_user_and_conversation(user, conversation)
110 assert participation.id == participation_two.id
111 refute participation.updated_at == participation_two.updated_at
114 test "recreating an existing participations sets it to unread" do
115 participation = insert(:participation, %{read: true})
117 {:ok, participation} =
118 Participation.create_for_user_and_conversation(
120 participation.conversation
123 refute participation.read
126 test "it marks a participation as read" do
127 participation = insert(:participation, %{read: false})
128 {:ok, updated_participation} = Participation.mark_as_read(participation)
130 assert updated_participation.read
131 assert updated_participation.updated_at == participation.updated_at
134 test "it marks a participation as unread" do
135 participation = insert(:participation, %{read: true})
136 {:ok, participation} = Participation.mark_as_unread(participation)
138 refute participation.read
141 test "it marks all the user's participations as read" do
143 other_user = insert(:user)
144 participation1 = insert(:participation, %{read: false, user: user})
145 participation2 = insert(:participation, %{read: false, user: user})
146 participation3 = insert(:participation, %{read: false, user: other_user})
148 {:ok, _, [%{read: true}, %{read: true}]} = Participation.mark_all_as_read(user)
150 assert Participation.get(participation1.id).read == true
151 assert Participation.get(participation2.id).read == true
152 assert Participation.get(participation3.id).read == false
155 test "gets all the participations for a user, ordered by updated at descending" do
157 {:ok, activity_one} = CommonAPI.post(user, %{status: "x", visibility: "direct"})
158 {:ok, activity_two} = CommonAPI.post(user, %{status: "x", visibility: "direct"})
160 {:ok, activity_three} =
161 CommonAPI.post(user, %{
163 visibility: "direct",
164 in_reply_to_status_id: activity_one.id
167 # Offset participations because the accuracy of updated_at is down to a second
169 for {activity, offset} <- [{activity_two, 1}, {activity_three, 2}] do
170 conversation = Conversation.get_for_ap_id(activity.data["context"])
171 participation = Participation.for_user_and_conversation(user, conversation)
172 updated_at = NaiveDateTime.add(Map.get(participation, :updated_at), offset)
174 Ecto.Changeset.change(participation, %{updated_at: updated_at})
178 assert [participation_one, participation_two] = Participation.for_user(user)
180 object2 = Pleroma.Object.normalize(activity_two)
181 object3 = Pleroma.Object.normalize(activity_three)
183 user = Repo.get(Pleroma.User, user.id)
185 assert participation_one.conversation.ap_id == object3.data["context"]
186 assert participation_two.conversation.ap_id == object2.data["context"]
187 assert participation_one.conversation.users == [user]
190 assert [participation_one] = Participation.for_user(user, %{"limit" => 1})
192 assert participation_one.conversation.ap_id == object3.data["context"]
194 # With last_activity_id
195 assert [participation_one] =
196 Participation.for_user_with_last_activity_id(user, %{"limit" => 1})
198 assert participation_one.last_activity_id == activity_three.id
201 test "Doesn't die when the conversation gets empty" do
204 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
205 [participation] = Participation.for_user_with_last_activity_id(user)
207 assert participation.last_activity_id == activity.id
209 {:ok, _} = CommonAPI.delete(activity.id, user)
211 [] = Participation.for_user_with_last_activity_id(user)
214 test "it sets recipients, always keeping the owner of the participation even when not explicitly set" do
216 other_user = insert(:user)
218 {:ok, _activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
219 [participation] = Participation.for_user_with_last_activity_id(user)
221 participation = Repo.preload(participation, :recipients)
222 user = User.get_cached_by_id(user.id)
224 assert participation.recipients |> length() == 1
225 assert user in participation.recipients
227 {:ok, participation} = Participation.set_recipients(participation, [other_user.id])
229 assert participation.recipients |> length() == 2
230 assert user in participation.recipients
231 assert other_user in participation.recipients
234 describe "blocking" do
235 test "when the user blocks a recipient, the existing conversations with them are marked as read" do
236 blocker = insert(:user)
237 blocked = insert(:user)
238 third_user = insert(:user)
241 CommonAPI.post(third_user, %{
242 status: "Hi @#{blocker.nickname}",
247 CommonAPI.post(third_user, %{
248 status: "Hi @#{blocker.nickname}, @#{blocked.nickname}",
253 CommonAPI.post(blocked, %{
254 status: "Hi @#{blocker.nickname}",
259 CommonAPI.post(blocked, %{
260 status: "Hi @#{blocker.nickname}, @#{third_user.nickname}",
264 assert [%{read: false}, %{read: false}, %{read: false}, %{read: false}] =
265 Participation.for_user(blocker)
267 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 4
269 {:ok, _user_relationship} = User.block(blocker, blocked)
271 # The conversations with the blocked user are marked as read
272 assert [%{read: true}, %{read: true}, %{read: true}, %{read: false}] =
273 Participation.for_user(blocker)
275 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 1
277 # The conversation is not marked as read for the blocked user
278 assert [_, _, %{read: false}] = Participation.for_user(blocked)
279 assert User.get_cached_by_id(blocked.id).unread_conversation_count == 1
281 # The conversation is not marked as read for the third user
282 assert [%{read: false}, _, _] = Participation.for_user(third_user)
283 assert User.get_cached_by_id(third_user.id).unread_conversation_count == 1
286 test "the new conversation with the blocked user is not marked as unread " do
287 blocker = insert(:user)
288 blocked = insert(:user)
289 third_user = insert(:user)
291 {:ok, _user_relationship} = User.block(blocker, blocked)
293 # When the blocked user is the author
295 CommonAPI.post(blocked, %{
296 status: "Hi @#{blocker.nickname}",
300 assert [%{read: true}] = Participation.for_user(blocker)
301 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
303 # When the blocked user is a recipient
305 CommonAPI.post(third_user, %{
306 status: "Hi @#{blocker.nickname}, @#{blocked.nickname}",
310 assert [%{read: true}, %{read: true}] = Participation.for_user(blocker)
311 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
313 assert [%{read: false}, _] = Participation.for_user(blocked)
314 assert User.get_cached_by_id(blocked.id).unread_conversation_count == 1
317 test "the conversation with the blocked user is not marked as unread on a reply" do
318 blocker = insert(:user)
319 blocked = insert(:user)
320 third_user = insert(:user)
323 CommonAPI.post(blocker, %{
324 status: "Hi @#{third_user.nickname}, @#{blocked.nickname}",
328 {:ok, _user_relationship} = User.block(blocker, blocked)
329 assert [%{read: true}] = Participation.for_user(blocker)
330 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
332 assert [blocked_participation] = Participation.for_user(blocked)
334 # When it's a reply from the blocked user
336 CommonAPI.post(blocked, %{
338 visibility: "direct",
339 in_reply_to_conversation_id: blocked_participation.id
342 assert [%{read: true}] = Participation.for_user(blocker)
343 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
345 assert [third_user_participation] = Participation.for_user(third_user)
347 # When it's a reply from the third user
349 CommonAPI.post(third_user, %{
351 visibility: "direct",
352 in_reply_to_conversation_id: third_user_participation.id
355 assert [%{read: true}] = Participation.for_user(blocker)
356 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
358 # Marked as unread for the blocked user
359 assert [%{read: false}] = Participation.for_user(blocked)
360 assert User.get_cached_by_id(blocked.id).unread_conversation_count == 1