Move /litepub-1.0.jsonld to /schemas/litepub-0.1.jsonld
[akkoma] / test / notification_test.exs
1 defmodule Pleroma.NotificationTest do
2 use Pleroma.DataCase
3 alias Pleroma.Web.TwitterAPI.TwitterAPI
4 alias Pleroma.Web.CommonAPI
5 alias Pleroma.{User, Notification}
6 import Pleroma.Factory
7
8 describe "create_notifications" do
9 test "notifies someone when they are directly addressed" do
10 user = insert(:user)
11 other_user = insert(:user)
12 third_user = insert(:user)
13
14 {:ok, activity} =
15 TwitterAPI.create_status(user, %{
16 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
17 })
18
19 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
20
21 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
22 assert notified_ids == [other_user.id, third_user.id]
23 assert notification.activity_id == activity.id
24 assert other_notification.activity_id == activity.id
25 end
26 end
27
28 describe "create_notification" do
29 test "it doesn't create a notification for user if the user blocks the activity author" do
30 activity = insert(:note_activity)
31 author = User.get_by_ap_id(activity.data["actor"])
32 user = insert(:user)
33 {:ok, user} = User.block(user, author)
34
35 assert nil == Notification.create_notification(activity, user)
36 end
37
38 test "it doesn't create a notification for user if he is the activity author" do
39 activity = insert(:note_activity)
40 author = User.get_by_ap_id(activity.data["actor"])
41
42 assert nil == Notification.create_notification(activity, author)
43 end
44 end
45
46 describe "get notification" do
47 test "it gets a notification that belongs to the user" do
48 user = insert(:user)
49 other_user = insert(:user)
50
51 {:ok, activity} =
52 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
53
54 {:ok, [notification]} = Notification.create_notifications(activity)
55 {:ok, notification} = Notification.get(other_user, notification.id)
56
57 assert notification.user_id == other_user.id
58 end
59
60 test "it returns error if the notification doesn't belong to the user" do
61 user = insert(:user)
62 other_user = insert(:user)
63
64 {:ok, activity} =
65 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
66
67 {:ok, [notification]} = Notification.create_notifications(activity)
68 {:error, _notification} = Notification.get(user, notification.id)
69 end
70 end
71
72 describe "dismiss notification" do
73 test "it dismisses a notification that belongs to the user" do
74 user = insert(:user)
75 other_user = insert(:user)
76
77 {:ok, activity} =
78 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
79
80 {:ok, [notification]} = Notification.create_notifications(activity)
81 {:ok, notification} = Notification.dismiss(other_user, notification.id)
82
83 assert notification.user_id == other_user.id
84 end
85
86 test "it returns error if the notification doesn't belong to the user" do
87 user = insert(:user)
88 other_user = insert(:user)
89
90 {:ok, activity} =
91 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
92
93 {:ok, [notification]} = Notification.create_notifications(activity)
94 {:error, _notification} = Notification.dismiss(user, notification.id)
95 end
96 end
97
98 describe "clear notification" do
99 test "it clears all notifications belonging to the user" do
100 user = insert(:user)
101 other_user = insert(:user)
102 third_user = insert(:user)
103
104 {:ok, activity} =
105 TwitterAPI.create_status(user, %{
106 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
107 })
108
109 {:ok, _notifs} = Notification.create_notifications(activity)
110
111 {:ok, activity} =
112 TwitterAPI.create_status(user, %{
113 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
114 })
115
116 {:ok, _notifs} = Notification.create_notifications(activity)
117 Notification.clear(other_user)
118
119 assert Notification.for_user(other_user) == []
120 assert Notification.for_user(third_user) != []
121 end
122 end
123
124 describe "set_read_up_to()" do
125 test "it sets all notifications as read up to a specified notification ID" do
126 user = insert(:user)
127 other_user = insert(:user)
128
129 {:ok, activity} =
130 TwitterAPI.create_status(user, %{
131 "status" => "hey @#{other_user.nickname}!"
132 })
133
134 {:ok, activity} =
135 TwitterAPI.create_status(user, %{
136 "status" => "hey again @#{other_user.nickname}!"
137 })
138
139 [n2, n1] = notifs = Notification.for_user(other_user)
140 assert length(notifs) == 2
141
142 assert n2.id > n1.id
143
144 {:ok, activity} =
145 TwitterAPI.create_status(user, %{
146 "status" => "hey yet again @#{other_user.nickname}!"
147 })
148
149 Notification.set_read_up_to(other_user, n2.id)
150
151 [n3, n2, n1] = notifs = Notification.for_user(other_user)
152
153 assert n1.seen == true
154 assert n2.seen == true
155 assert n3.seen == false
156 end
157 end
158
159 describe "notification lifecycle" do
160 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
161 user = insert(:user)
162 other_user = insert(:user)
163
164 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
165
166 assert length(Notification.for_user(user)) == 0
167
168 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
169
170 assert length(Notification.for_user(user)) == 1
171
172 {:ok, _} = CommonAPI.delete(activity.id, user)
173
174 assert length(Notification.for_user(user)) == 0
175 end
176
177 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
178 user = insert(:user)
179 other_user = insert(:user)
180
181 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
182
183 assert length(Notification.for_user(user)) == 0
184
185 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
186
187 assert length(Notification.for_user(user)) == 1
188
189 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
190
191 assert length(Notification.for_user(user)) == 0
192 end
193
194 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
195 user = insert(:user)
196 other_user = insert(:user)
197
198 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
199
200 assert length(Notification.for_user(user)) == 0
201
202 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
203
204 assert length(Notification.for_user(user)) == 1
205
206 {:ok, _} = CommonAPI.delete(activity.id, user)
207
208 assert length(Notification.for_user(user)) == 0
209 end
210
211 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
212 user = insert(:user)
213 other_user = insert(:user)
214
215 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
216
217 assert length(Notification.for_user(user)) == 0
218
219 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
220
221 assert length(Notification.for_user(user)) == 1
222
223 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
224
225 assert length(Notification.for_user(user)) == 0
226 end
227
228 test "liking an activity which is already deleted does not generate a notification" do
229 user = insert(:user)
230 other_user = insert(:user)
231
232 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
233
234 assert length(Notification.for_user(user)) == 0
235
236 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
237
238 assert length(Notification.for_user(user)) == 0
239
240 {:error, _} = CommonAPI.favorite(activity.id, other_user)
241
242 assert length(Notification.for_user(user)) == 0
243 end
244
245 test "repeating an activity which is already deleted does not generate a notification" do
246 user = insert(:user)
247 other_user = insert(:user)
248
249 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
250
251 assert length(Notification.for_user(user)) == 0
252
253 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
254
255 assert length(Notification.for_user(user)) == 0
256
257 {:error, _} = CommonAPI.repeat(activity.id, other_user)
258
259 assert length(Notification.for_user(user)) == 0
260 end
261
262 test "replying to a deleted post without tagging does not generate a notification" do
263 user = insert(:user)
264 other_user = insert(:user)
265
266 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
267 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
268
269 {:ok, _reply_activity} =
270 CommonAPI.post(other_user, %{
271 "status" => "test reply",
272 "in_reply_to_status_id" => activity.id
273 })
274
275 assert length(Notification.for_user(user)) == 0
276 end
277 end
278 end