1 defmodule Pleroma.NotificationTest do
3 alias Pleroma.Web.TwitterAPI.TwitterAPI
4 alias Pleroma.Web.CommonAPI
5 alias Pleroma.{User, Notification}
6 alias Pleroma.Web.ActivityPub.Transmogrifier
9 describe "create_notifications" do
10 test "notifies someone when they are directly addressed" do
12 other_user = insert(:user)
13 third_user = insert(:user)
16 TwitterAPI.create_status(user, %{
17 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
20 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
22 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
23 assert notified_ids == [other_user.id, third_user.id]
24 assert notification.activity_id == activity.id
25 assert other_notification.activity_id == activity.id
29 describe "create_notification" do
30 test "it doesn't create a notification for user if the user blocks the activity author" do
31 activity = insert(:note_activity)
32 author = User.get_by_ap_id(activity.data["actor"])
34 {:ok, user} = User.block(user, author)
36 assert nil == Notification.create_notification(activity, user)
39 test "it doesn't create a notification for user if he is the activity author" do
40 activity = insert(:note_activity)
41 author = User.get_by_ap_id(activity.data["actor"])
43 assert nil == Notification.create_notification(activity, author)
47 describe "get notification" do
48 test "it gets a notification that belongs to the user" do
50 other_user = insert(:user)
53 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
55 {:ok, [notification]} = Notification.create_notifications(activity)
56 {:ok, notification} = Notification.get(other_user, notification.id)
58 assert notification.user_id == other_user.id
61 test "it returns error if the notification doesn't belong to the user" do
63 other_user = insert(:user)
66 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
68 {:ok, [notification]} = Notification.create_notifications(activity)
69 {:error, _notification} = Notification.get(user, notification.id)
73 describe "dismiss notification" do
74 test "it dismisses a notification that belongs to the user" do
76 other_user = insert(:user)
79 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
81 {:ok, [notification]} = Notification.create_notifications(activity)
82 {:ok, notification} = Notification.dismiss(other_user, notification.id)
84 assert notification.user_id == other_user.id
87 test "it returns error if the notification doesn't belong to the user" do
89 other_user = insert(:user)
92 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
94 {:ok, [notification]} = Notification.create_notifications(activity)
95 {:error, _notification} = Notification.dismiss(user, notification.id)
99 describe "clear notification" do
100 test "it clears all notifications belonging to the user" do
102 other_user = insert(:user)
103 third_user = insert(:user)
106 TwitterAPI.create_status(user, %{
107 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
110 {:ok, _notifs} = Notification.create_notifications(activity)
113 TwitterAPI.create_status(user, %{
114 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
117 {:ok, _notifs} = Notification.create_notifications(activity)
118 Notification.clear(other_user)
120 assert Notification.for_user(other_user) == []
121 assert Notification.for_user(third_user) != []
125 describe "set_read_up_to()" do
126 test "it sets all notifications as read up to a specified notification ID" do
128 other_user = insert(:user)
131 TwitterAPI.create_status(user, %{
132 "status" => "hey @#{other_user.nickname}!"
136 TwitterAPI.create_status(user, %{
137 "status" => "hey again @#{other_user.nickname}!"
140 [n2, n1] = notifs = Notification.for_user(other_user)
141 assert length(notifs) == 2
146 TwitterAPI.create_status(user, %{
147 "status" => "hey yet again @#{other_user.nickname}!"
150 Notification.set_read_up_to(other_user, n2.id)
152 [n3, n2, n1] = notifs = Notification.for_user(other_user)
154 assert n1.seen == true
155 assert n2.seen == true
156 assert n3.seen == false
160 describe "notification target determination" do
161 test "it sends notifications to addressed users in new messages" do
163 other_user = insert(:user)
166 CommonAPI.post(user, %{
167 "status" => "hey @#{other_user.nickname}!"
170 assert other_user in Notification.get_notified_from_activity(activity)
173 test "it sends notifications to mentioned users in new messages" do
175 other_user = insert(:user)
178 "@context" => "https://www.w3.org/ns/activitystreams",
180 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
181 "actor" => user.ap_id,
184 "content" => "message with a Mention tag, but no explicit tagging",
188 "href" => other_user.ap_id,
189 "name" => other_user.nickname
192 "attributedTo" => user.ap_id
196 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
198 assert other_user in Notification.get_notified_from_activity(activity)
201 test "it does not send notifications to users who are only cc in new messages" do
203 other_user = insert(:user)
206 "@context" => "https://www.w3.org/ns/activitystreams",
208 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
209 "cc" => [other_user.ap_id],
210 "actor" => user.ap_id,
213 "content" => "hi everyone",
214 "attributedTo" => user.ap_id
218 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
220 assert other_user not in Notification.get_notified_from_activity(activity)
223 test "it does not send notification to mentioned users in likes" do
225 other_user = insert(:user)
226 third_user = insert(:user)
228 {:ok, activity_one} =
229 CommonAPI.post(user, %{
230 "status" => "hey @#{other_user.nickname}!"
233 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
235 assert other_user not in Notification.get_notified_from_activity(activity_two)
238 test "it does not send notification to mentioned users in announces" do
240 other_user = insert(:user)
241 third_user = insert(:user)
243 {:ok, activity_one} =
244 CommonAPI.post(user, %{
245 "status" => "hey @#{other_user.nickname}!"
248 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
250 assert other_user not in Notification.get_notified_from_activity(activity_two)
254 describe "notification lifecycle" do
255 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
257 other_user = insert(:user)
259 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
261 assert length(Notification.for_user(user)) == 0
263 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
265 assert length(Notification.for_user(user)) == 1
267 {:ok, _} = CommonAPI.delete(activity.id, user)
269 assert length(Notification.for_user(user)) == 0
272 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
274 other_user = insert(:user)
276 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
278 assert length(Notification.for_user(user)) == 0
280 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
282 assert length(Notification.for_user(user)) == 1
284 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
286 assert length(Notification.for_user(user)) == 0
289 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
291 other_user = insert(:user)
293 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
295 assert length(Notification.for_user(user)) == 0
297 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
299 assert length(Notification.for_user(user)) == 1
301 {:ok, _} = CommonAPI.delete(activity.id, user)
303 assert length(Notification.for_user(user)) == 0
306 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
308 other_user = insert(:user)
310 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
312 assert length(Notification.for_user(user)) == 0
314 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
316 assert length(Notification.for_user(user)) == 1
318 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
320 assert length(Notification.for_user(user)) == 0
323 test "liking an activity which is already deleted does not generate a notification" do
325 other_user = insert(:user)
327 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
329 assert length(Notification.for_user(user)) == 0
331 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
333 assert length(Notification.for_user(user)) == 0
335 {:error, _} = CommonAPI.favorite(activity.id, other_user)
337 assert length(Notification.for_user(user)) == 0
340 test "repeating an activity which is already deleted does not generate a notification" do
342 other_user = insert(:user)
344 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
346 assert length(Notification.for_user(user)) == 0
348 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
350 assert length(Notification.for_user(user)) == 0
352 {:error, _} = CommonAPI.repeat(activity.id, other_user)
354 assert length(Notification.for_user(user)) == 0
357 test "replying to a deleted post without tagging does not generate a notification" do
359 other_user = insert(:user)
361 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
362 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
364 {:ok, _reply_activity} =
365 CommonAPI.post(other_user, %{
366 "status" => "test reply",
367 "in_reply_to_status_id" => activity.id
370 assert length(Notification.for_user(user)) == 0