1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Conversation.ParticipationTest do
8 alias Pleroma.Conversation.Participation
10 alias Pleroma.Web.CommonAPI
12 test "getting a participation will also preload things" do
14 other_user = insert(:user)
17 CommonAPI.post(user, %{"status" => "Hey @#{other_user.nickname}.", "visibility" => "direct"})
19 [participation] = Participation.for_user(user)
21 participation = Participation.get(participation.id, preload: [:conversation])
23 assert %Pleroma.Conversation{} = participation.conversation
26 test "for a new conversation or a reply, it doesn't mark the author's participation as unread" do
28 other_user = insert(:user)
31 CommonAPI.post(user, %{"status" => "Hey @#{other_user.nickname}.", "visibility" => "direct"})
33 user = User.get_cached_by_id(user.id)
34 other_user = User.get_cached_by_id(other_user.id)
36 [%{read: true}] = Participation.for_user(user)
37 [%{read: false} = participation] = Participation.for_user(other_user)
39 assert User.get_cached_by_id(user.id).unread_conversation_count == 0
40 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 1
43 CommonAPI.post(other_user, %{
44 "status" => "Hey @#{user.nickname}.",
45 "visibility" => "direct",
46 "in_reply_to_conversation_id" => participation.id
49 user = User.get_cached_by_id(user.id)
50 other_user = User.get_cached_by_id(other_user.id)
52 [%{read: false}] = Participation.for_user(user)
53 [%{read: true}] = Participation.for_user(other_user)
55 assert User.get_cached_by_id(user.id).unread_conversation_count == 1
56 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
59 test "for a new conversation, it sets the recipents of the participation" do
61 other_user = insert(:user)
62 third_user = insert(:user)
65 CommonAPI.post(user, %{"status" => "Hey @#{other_user.nickname}.", "visibility" => "direct"})
67 user = User.get_cached_by_id(user.id)
68 other_user = User.get_cached_by_id(other_user.id)
69 [participation] = Participation.for_user(user)
70 participation = Pleroma.Repo.preload(participation, :recipients)
72 assert length(participation.recipients) == 2
73 assert user in participation.recipients
74 assert other_user in participation.recipients
76 # Mentioning another user in the same conversation will not add a new recipients.
79 CommonAPI.post(user, %{
80 "in_reply_to_status_id" => activity.id,
81 "status" => "Hey @#{third_user.nickname}.",
82 "visibility" => "direct"
85 [participation] = Participation.for_user(user)
86 participation = Pleroma.Repo.preload(participation, :recipients)
88 assert length(participation.recipients) == 2
91 test "it creates a participation for a conversation and a user" do
93 conversation = insert(:conversation)
95 {:ok, %Participation{} = participation} =
96 Participation.create_for_user_and_conversation(user, conversation)
98 assert participation.user_id == user.id
99 assert participation.conversation_id == conversation.id
102 # Creating again returns the same participation
103 {:ok, %Participation{} = participation_two} =
104 Participation.create_for_user_and_conversation(user, conversation)
106 assert participation.id == participation_two.id
107 refute participation.updated_at == participation_two.updated_at
110 test "recreating an existing participations sets it to unread" do
111 participation = insert(:participation, %{read: true})
113 {:ok, participation} =
114 Participation.create_for_user_and_conversation(
116 participation.conversation
119 refute participation.read
122 test "it marks a participation as read" do
123 participation = insert(:participation, %{read: false})
124 {:ok, participation} = Participation.mark_as_read(participation)
126 assert participation.read
129 test "it marks a participation as unread" do
130 participation = insert(:participation, %{read: true})
131 {:ok, participation} = Participation.mark_as_unread(participation)
133 refute participation.read
136 test "it marks all the user's participations as read" do
138 other_user = insert(:user)
139 participation1 = insert(:participation, %{read: false, user: user})
140 participation2 = insert(:participation, %{read: false, user: user})
141 participation3 = insert(:participation, %{read: false, user: other_user})
143 {:ok, _, [%{read: true}, %{read: true}]} = Participation.mark_all_as_read(user)
145 assert Participation.get(participation1.id).read == true
146 assert Participation.get(participation2.id).read == true
147 assert Participation.get(participation3.id).read == false
150 test "gets all the participations for a user, ordered by updated at descending" do
152 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
154 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
157 {:ok, activity_three} =
158 CommonAPI.post(user, %{
160 "visibility" => "direct",
161 "in_reply_to_status_id" => activity_one.id
164 assert [participation_one, participation_two] = Participation.for_user(user)
166 object2 = Pleroma.Object.normalize(activity_two)
167 object3 = Pleroma.Object.normalize(activity_three)
169 user = Repo.get(Pleroma.User, user.id)
171 assert participation_one.conversation.ap_id == object3.data["context"]
172 assert participation_two.conversation.ap_id == object2.data["context"]
173 assert participation_one.conversation.users == [user]
176 assert [participation_one] = Participation.for_user(user, %{"limit" => 1})
178 assert participation_one.conversation.ap_id == object3.data["context"]
180 # With last_activity_id
181 assert [participation_one] =
182 Participation.for_user_with_last_activity_id(user, %{"limit" => 1})
184 assert participation_one.last_activity_id == activity_three.id
187 test "Doesn't die when the conversation gets empty" do
190 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
191 [participation] = Participation.for_user_with_last_activity_id(user)
193 assert participation.last_activity_id == activity.id
195 {:ok, _} = CommonAPI.delete(activity.id, user)
197 [] = Participation.for_user_with_last_activity_id(user)
200 test "it sets recipients, always keeping the owner of the participation even when not explicitly set" do
202 other_user = insert(:user)
204 {:ok, _activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
205 [participation] = Participation.for_user_with_last_activity_id(user)
207 participation = Repo.preload(participation, :recipients)
208 user = User.get_cached_by_id(user.id)
210 assert participation.recipients |> length() == 1
211 assert user in participation.recipients
213 {:ok, participation} = Participation.set_recipients(participation, [other_user.id])
215 assert participation.recipients |> length() == 2
216 assert user in participation.recipients
217 assert other_user in participation.recipients
220 describe "blocking" do
221 test "when the user blocks a recipient, the existing conversations with them are marked as read" do
222 blocker = insert(:user)
223 blocked = insert(:user)
224 third_user = insert(:user)
227 CommonAPI.post(third_user, %{
228 "status" => "Hi @#{blocker.nickname}",
229 "visibility" => "direct"
233 CommonAPI.post(third_user, %{
234 "status" => "Hi @#{blocker.nickname}, @#{blocked.nickname}",
235 "visibility" => "direct"
239 CommonAPI.post(blocked, %{
240 "status" => "Hi @#{blocker.nickname}",
241 "visibility" => "direct"
245 CommonAPI.post(blocked, %{
246 "status" => "Hi @#{blocker.nickname}, @#{third_user.nickname}",
247 "visibility" => "direct"
250 assert [%{read: false}, %{read: false}, %{read: false}, %{read: false}] =
251 Participation.for_user(blocker)
253 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 4
255 {:ok, blocker} = User.block(blocker, blocked)
257 # The conversations with the blocked user are marked as read
258 assert [%{read: true}, %{read: true}, %{read: true}, %{read: false}] =
259 Participation.for_user(blocker)
261 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 1
263 # The conversation is not marked as read for the blocked user
264 assert [_, _, %{read: false}] = Participation.for_user(blocked)
265 assert User.get_cached_by_id(blocked.id).unread_conversation_count == 1
267 # The conversation is not marked as read for the third user
268 assert [%{read: false}, _, _] = Participation.for_user(third_user)
269 assert User.get_cached_by_id(third_user.id).unread_conversation_count == 1
272 test "the new conversation with the blocked user is not marked as unread " do
273 blocker = insert(:user)
274 blocked = insert(:user)
275 third_user = insert(:user)
277 {:ok, blocker} = User.block(blocker, blocked)
279 # When the blocked user is the author
281 CommonAPI.post(blocked, %{
282 "status" => "Hi @#{blocker.nickname}",
283 "visibility" => "direct"
286 assert [%{read: true}] = Participation.for_user(blocker)
287 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
289 # When the blocked user is a recipient
291 CommonAPI.post(third_user, %{
292 "status" => "Hi @#{blocker.nickname}, @#{blocked.nickname}",
293 "visibility" => "direct"
296 assert [%{read: true}, %{read: true}] = Participation.for_user(blocker)
297 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
299 assert [%{read: false}, _] = Participation.for_user(blocked)
300 assert User.get_cached_by_id(blocked.id).unread_conversation_count == 1
303 test "the conversation with the blocked user is not marked as unread on a reply" do
304 blocker = insert(:user)
305 blocked = insert(:user)
306 third_user = insert(:user)
309 CommonAPI.post(blocker, %{
310 "status" => "Hi @#{third_user.nickname}, @#{blocked.nickname}",
311 "visibility" => "direct"
314 {:ok, blocker} = User.block(blocker, blocked)
315 assert [%{read: true}] = Participation.for_user(blocker)
316 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
318 assert [blocked_participation] = Participation.for_user(blocked)
320 # When it's a reply from the blocked user
322 CommonAPI.post(blocked, %{
324 "visibility" => "direct",
325 "in_reply_to_conversation_id" => blocked_participation.id
328 assert [%{read: true}] = Participation.for_user(blocker)
329 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
331 assert [third_user_participation] = Participation.for_user(third_user)
333 # When it's a reply from the third user
335 CommonAPI.post(third_user, %{
337 "visibility" => "direct",
338 "in_reply_to_conversation_id" => third_user_participation.id
341 assert [%{read: true}] = Participation.for_user(blocker)
342 assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0
344 # Marked as unread for the blocked user
345 assert [%{read: false}] = Participation.for_user(blocked)
346 assert User.get_cached_by_id(blocked.id).unread_conversation_count == 1