1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.NotificationTest do
10 alias Pleroma.Notification
12 alias Pleroma.Web.ActivityPub.Transmogrifier
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.TwitterAPI.TwitterAPI
16 describe "create_notifications" do
17 test "notifies someone when they are directly addressed" do
19 other_user = insert(:user)
20 third_user = insert(:user)
23 TwitterAPI.create_status(user, %{
24 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
27 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
29 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
30 assert notified_ids == [other_user.id, third_user.id]
31 assert notification.activity_id == activity.id
32 assert other_notification.activity_id == activity.id
35 test "it creates a notification for subscribed users" do
37 subscriber = insert(:user)
39 User.subscribe(subscriber, user)
41 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
42 {:ok, [notification]} = Notification.create_notifications(status)
44 assert notification.user_id == subscriber.id
48 describe "create_notification" do
49 test "it doesn't create a notification for user if the user blocks the activity author" do
50 activity = insert(:note_activity)
51 author = User.get_cached_by_ap_id(activity.data["actor"])
53 {:ok, user} = User.block(user, author)
55 assert nil == Notification.create_notification(activity, user)
58 test "it doesn't create a notificatin for the user if the user mutes the activity author" do
61 {:ok, _} = User.mute(muter, muted)
62 muter = Repo.get(User, muter.id)
63 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
65 assert nil == Notification.create_notification(activity, muter)
68 test "it doesn't create a notification for an activity from a muted thread" do
70 other_user = insert(:user)
71 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
72 CommonAPI.add_mute(muter, activity)
75 CommonAPI.post(other_user, %{
76 "status" => "Hi @#{muter.nickname}",
77 "in_reply_to_status_id" => activity.id
80 assert nil == Notification.create_notification(activity, muter)
83 test "it disables notifications from followers" do
84 follower = insert(:user)
85 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
86 User.follow(follower, followed)
87 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
88 assert nil == Notification.create_notification(activity, followed)
91 test "it disables notifications from non-followers" do
92 follower = insert(:user)
93 followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
94 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
95 assert nil == Notification.create_notification(activity, followed)
98 test "it disables notifications from people the user follows" do
99 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
100 followed = insert(:user)
101 User.follow(follower, followed)
102 follower = Repo.get(User, follower.id)
103 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
104 assert nil == Notification.create_notification(activity, follower)
107 test "it disables notifications from people the user does not follow" do
108 follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
109 followed = insert(:user)
110 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
111 assert nil == Notification.create_notification(activity, follower)
114 test "it doesn't create a notification for user if he is the activity author" do
115 activity = insert(:note_activity)
116 author = User.get_cached_by_ap_id(activity.data["actor"])
118 assert nil == Notification.create_notification(activity, author)
121 test "it doesn't create a notification for follow-unfollow-follow chains" do
123 followed_user = insert(:user)
124 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
125 Notification.create_notification(activity, followed_user)
126 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
127 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
128 assert nil == Notification.create_notification(activity_dupe, followed_user)
131 test "it doesn't create a notification for like-unlike-like chains" do
133 liked_user = insert(:user)
134 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
135 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
136 Notification.create_notification(fav_status, liked_user)
137 TwitterAPI.unfav(user, status.id)
138 {:ok, dupe} = TwitterAPI.fav(user, status.id)
139 assert nil == Notification.create_notification(dupe, liked_user)
142 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
144 retweeted_user = insert(:user)
147 TwitterAPI.create_status(retweeted_user, %{
148 "status" => "Send dupe notifications to the shadow realm"
151 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
152 Notification.create_notification(retweeted_activity, retweeted_user)
153 TwitterAPI.unrepeat(user, status.id)
154 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
155 assert nil == Notification.create_notification(dupe, retweeted_user)
158 test "it doesn't create duplicate notifications for follow+subscribed users" do
160 subscriber = insert(:user)
162 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
163 User.subscribe(subscriber, user)
164 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
165 {:ok, [_notif]} = Notification.create_notifications(status)
168 test "it doesn't create subscription notifications if the recipient cannot see the status" do
170 subscriber = insert(:user)
172 User.subscribe(subscriber, user)
175 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
177 assert {:ok, []} == Notification.create_notifications(status)
181 describe "get notification" do
182 test "it gets a notification that belongs to the user" do
184 other_user = insert(:user)
187 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
189 {:ok, [notification]} = Notification.create_notifications(activity)
190 {:ok, notification} = Notification.get(other_user, notification.id)
192 assert notification.user_id == other_user.id
195 test "it returns error if the notification doesn't belong to the user" do
197 other_user = insert(:user)
200 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
202 {:ok, [notification]} = Notification.create_notifications(activity)
203 {:error, _notification} = Notification.get(user, notification.id)
207 describe "dismiss notification" do
208 test "it dismisses a notification that belongs to the user" do
210 other_user = insert(:user)
213 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
215 {:ok, [notification]} = Notification.create_notifications(activity)
216 {:ok, notification} = Notification.dismiss(other_user, notification.id)
218 assert notification.user_id == other_user.id
221 test "it returns error if the notification doesn't belong to the user" do
223 other_user = insert(:user)
226 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
228 {:ok, [notification]} = Notification.create_notifications(activity)
229 {:error, _notification} = Notification.dismiss(user, notification.id)
233 describe "clear notification" do
234 test "it clears all notifications belonging to the user" do
236 other_user = insert(:user)
237 third_user = insert(:user)
240 TwitterAPI.create_status(user, %{
241 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
244 {:ok, _notifs} = Notification.create_notifications(activity)
247 TwitterAPI.create_status(user, %{
248 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
251 {:ok, _notifs} = Notification.create_notifications(activity)
252 Notification.clear(other_user)
254 assert Notification.for_user(other_user) == []
255 assert Notification.for_user(third_user) != []
259 describe "set_read_up_to()" do
260 test "it sets all notifications as read up to a specified notification ID" do
262 other_user = insert(:user)
265 TwitterAPI.create_status(user, %{
266 "status" => "hey @#{other_user.nickname}!"
270 TwitterAPI.create_status(user, %{
271 "status" => "hey again @#{other_user.nickname}!"
274 [n2, n1] = notifs = Notification.for_user(other_user)
275 assert length(notifs) == 2
280 TwitterAPI.create_status(user, %{
281 "status" => "hey yet again @#{other_user.nickname}!"
284 Notification.set_read_up_to(other_user, n2.id)
286 [n3, n2, n1] = Notification.for_user(other_user)
288 assert n1.seen == true
289 assert n2.seen == true
290 assert n3.seen == false
294 describe "for_user_since/2" do
295 defp days_ago(days) do
297 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
298 -days * 60 * 60 * 24,
303 test "Returns recent notifications" do
304 user1 = insert(:user)
305 user2 = insert(:user)
307 Enum.each(0..10, fn i ->
309 CommonAPI.post(user1, %{
310 "status" => "hey ##{i} @#{user2.nickname}!"
314 {old, new} = Enum.split(Notification.for_user(user2), 5)
316 Enum.each(old, fn notification ->
318 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
319 |> Pleroma.Repo.update!()
322 recent_notifications_ids =
324 |> Notification.for_user_since(
325 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
329 Enum.each(old, fn %{id: id} ->
330 refute id in recent_notifications_ids
333 Enum.each(new, fn %{id: id} ->
334 assert id in recent_notifications_ids
339 describe "notification target determination" do
340 test "it sends notifications to addressed users in new messages" do
342 other_user = insert(:user)
345 CommonAPI.post(user, %{
346 "status" => "hey @#{other_user.nickname}!"
349 assert other_user in Notification.get_notified_from_activity(activity)
352 test "it sends notifications to mentioned users in new messages" do
354 other_user = insert(:user)
357 "@context" => "https://www.w3.org/ns/activitystreams",
359 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
360 "actor" => user.ap_id,
363 "content" => "message with a Mention tag, but no explicit tagging",
367 "href" => other_user.ap_id,
368 "name" => other_user.nickname
371 "attributedTo" => user.ap_id
375 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
377 assert other_user in Notification.get_notified_from_activity(activity)
380 test "it does not send notifications to users who are only cc in new messages" do
382 other_user = insert(:user)
385 "@context" => "https://www.w3.org/ns/activitystreams",
387 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
388 "cc" => [other_user.ap_id],
389 "actor" => user.ap_id,
392 "content" => "hi everyone",
393 "attributedTo" => user.ap_id
397 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
399 assert other_user not in Notification.get_notified_from_activity(activity)
402 test "it does not send notification to mentioned users in likes" do
404 other_user = insert(:user)
405 third_user = insert(:user)
407 {:ok, activity_one} =
408 CommonAPI.post(user, %{
409 "status" => "hey @#{other_user.nickname}!"
412 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
414 assert other_user not in Notification.get_notified_from_activity(activity_two)
417 test "it does not send notification to mentioned users in announces" do
419 other_user = insert(:user)
420 third_user = insert(:user)
422 {:ok, activity_one} =
423 CommonAPI.post(user, %{
424 "status" => "hey @#{other_user.nickname}!"
427 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
429 assert other_user not in Notification.get_notified_from_activity(activity_two)
433 describe "notification lifecycle" do
434 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
436 other_user = insert(:user)
438 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
440 assert Enum.empty?(Notification.for_user(user))
442 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
444 assert length(Notification.for_user(user)) == 1
446 {:ok, _} = CommonAPI.delete(activity.id, user)
448 assert Enum.empty?(Notification.for_user(user))
451 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
453 other_user = insert(:user)
455 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
457 assert Enum.empty?(Notification.for_user(user))
459 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
461 assert length(Notification.for_user(user)) == 1
463 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
465 assert Enum.empty?(Notification.for_user(user))
468 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
470 other_user = insert(:user)
472 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
474 assert Enum.empty?(Notification.for_user(user))
476 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
478 assert length(Notification.for_user(user)) == 1
480 {:ok, _} = CommonAPI.delete(activity.id, user)
482 assert Enum.empty?(Notification.for_user(user))
485 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
487 other_user = insert(:user)
489 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
491 assert Enum.empty?(Notification.for_user(user))
493 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
495 assert length(Notification.for_user(user)) == 1
497 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
499 assert Enum.empty?(Notification.for_user(user))
502 test "liking an activity which is already deleted does not generate a notification" do
504 other_user = insert(:user)
506 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
508 assert Enum.empty?(Notification.for_user(user))
510 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
512 assert Enum.empty?(Notification.for_user(user))
514 {:error, _} = CommonAPI.favorite(activity.id, other_user)
516 assert Enum.empty?(Notification.for_user(user))
519 test "repeating an activity which is already deleted does not generate a notification" do
521 other_user = insert(:user)
523 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
525 assert Enum.empty?(Notification.for_user(user))
527 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
529 assert Enum.empty?(Notification.for_user(user))
531 {:error, _} = CommonAPI.repeat(activity.id, other_user)
533 assert Enum.empty?(Notification.for_user(user))
536 test "replying to a deleted post without tagging does not generate a notification" do
538 other_user = insert(:user)
540 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
541 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
543 {:ok, _reply_activity} =
544 CommonAPI.post(other_user, %{
545 "status" => "test reply",
546 "in_reply_to_status_id" => activity.id
549 assert Enum.empty?(Notification.for_user(user))