91867bf7047341a066b7a2f810de305edc4e3021
[akkoma] / test / conversation / participation_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Conversation.ParticipationTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8 alias Pleroma.Conversation.Participation
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI
11
12 test "getting a participation will also preload things" do
13 user = insert(:user)
14 other_user = insert(:user)
15
16 {:ok, _activity} =
17 CommonAPI.post(user, %{"status" => "Hey @#{other_user.nickname}.", "visibility" => "direct"})
18
19 [participation] = Participation.for_user(user)
20
21 participation = Participation.get(participation.id, preload: [:conversation])
22
23 assert %Pleroma.Conversation{} = participation.conversation
24 end
25
26 test "for a new conversation or a reply, it doesn't mark the author's participation as unread" do
27 user = insert(:user)
28 other_user = insert(:user)
29
30 {:ok, _} =
31 CommonAPI.post(user, %{"status" => "Hey @#{other_user.nickname}.", "visibility" => "direct"})
32
33 user = User.get_cached_by_id(user.id)
34 other_user = User.get_cached_by_id(other_user.id)
35
36 [%{read: true}] = Participation.for_user(user)
37 [%{read: false} = participation] = Participation.for_user(other_user)
38
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
41
42 {:ok, _} =
43 CommonAPI.post(other_user, %{
44 "status" => "Hey @#{user.nickname}.",
45 "visibility" => "direct",
46 "in_reply_to_conversation_id" => participation.id
47 })
48
49 user = User.get_cached_by_id(user.id)
50 other_user = User.get_cached_by_id(other_user.id)
51
52 [%{read: false}] = Participation.for_user(user)
53 [%{read: true}] = Participation.for_user(other_user)
54
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
57 end
58
59 test "for a new conversation, it sets the recipents of the participation" do
60 user = insert(:user)
61 other_user = insert(:user)
62 third_user = insert(:user)
63
64 {:ok, activity} =
65 CommonAPI.post(user, %{"status" => "Hey @#{other_user.nickname}.", "visibility" => "direct"})
66
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)
71
72 assert length(participation.recipients) == 2
73 assert user in participation.recipients
74 assert other_user in participation.recipients
75
76 # Mentioning another user in the same conversation will not add a new recipients.
77
78 {:ok, _activity} =
79 CommonAPI.post(user, %{
80 "in_reply_to_status_id" => activity.id,
81 "status" => "Hey @#{third_user.nickname}.",
82 "visibility" => "direct"
83 })
84
85 [participation] = Participation.for_user(user)
86 participation = Pleroma.Repo.preload(participation, :recipients)
87
88 assert length(participation.recipients) == 2
89 end
90
91 test "it creates a participation for a conversation and a user" do
92 user = insert(:user)
93 conversation = insert(:conversation)
94
95 {:ok, %Participation{} = participation} =
96 Participation.create_for_user_and_conversation(user, conversation)
97
98 assert participation.user_id == user.id
99 assert participation.conversation_id == conversation.id
100
101 :timer.sleep(1000)
102 # Creating again returns the same participation
103 {:ok, %Participation{} = participation_two} =
104 Participation.create_for_user_and_conversation(user, conversation)
105
106 assert participation.id == participation_two.id
107 refute participation.updated_at == participation_two.updated_at
108 end
109
110 test "recreating an existing participations sets it to unread" do
111 participation = insert(:participation, %{read: true})
112
113 {:ok, participation} =
114 Participation.create_for_user_and_conversation(
115 participation.user,
116 participation.conversation
117 )
118
119 refute participation.read
120 end
121
122 test "it marks a participation as read" do
123 participation = insert(:participation, %{read: false})
124 {:ok, participation} = Participation.mark_as_read(participation)
125
126 assert participation.read
127 end
128
129 test "it marks a participation as unread" do
130 participation = insert(:participation, %{read: true})
131 {:ok, participation} = Participation.mark_as_unread(participation)
132
133 refute participation.read
134 end
135
136 test "it marks all the user's participations as read" do
137 user = insert(:user)
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})
142
143 {:ok, [%{read: true}, %{read: true}]} = Participation.mark_all_as_read(user)
144
145 assert Participation.get(participation1.id).read == true
146 assert Participation.get(participation2.id).read == true
147 assert Participation.get(participation3.id).read == false
148 end
149
150 test "gets all the participations for a user, ordered by updated at descending" do
151 user = insert(:user)
152 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
153 :timer.sleep(1000)
154 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
155 :timer.sleep(1000)
156
157 {:ok, activity_three} =
158 CommonAPI.post(user, %{
159 "status" => "x",
160 "visibility" => "direct",
161 "in_reply_to_status_id" => activity_one.id
162 })
163
164 assert [participation_one, participation_two] = Participation.for_user(user)
165
166 object2 = Pleroma.Object.normalize(activity_two)
167 object3 = Pleroma.Object.normalize(activity_three)
168
169 user = Repo.get(Pleroma.User, user.id)
170
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]
174
175 # Pagination
176 assert [participation_one] = Participation.for_user(user, %{"limit" => 1})
177
178 assert participation_one.conversation.ap_id == object3.data["context"]
179
180 # With last_activity_id
181 assert [participation_one] =
182 Participation.for_user_with_last_activity_id(user, %{"limit" => 1})
183
184 assert participation_one.last_activity_id == activity_three.id
185 end
186
187 test "Doesn't die when the conversation gets empty" do
188 user = insert(:user)
189
190 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
191 [participation] = Participation.for_user_with_last_activity_id(user)
192
193 assert participation.last_activity_id == activity.id
194
195 {:ok, _} = CommonAPI.delete(activity.id, user)
196
197 [] = Participation.for_user_with_last_activity_id(user)
198 end
199
200 test "it sets recipients, always keeping the owner of the participation even when not explicitly set" do
201 user = insert(:user)
202 other_user = insert(:user)
203
204 {:ok, _activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
205 [participation] = Participation.for_user_with_last_activity_id(user)
206
207 participation = Repo.preload(participation, :recipients)
208 user = User.get_cached_by_id(user.id)
209
210 assert participation.recipients |> length() == 1
211 assert user in participation.recipients
212
213 {:ok, participation} = Participation.set_recipients(participation, [other_user.id])
214
215 assert participation.recipients |> length() == 2
216 assert user in participation.recipients
217 assert other_user in participation.recipients
218 end
219 end