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_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 people on remote instances" do
84 user = insert(:user, info: %{notification_settings: %{"remote" => false}})
85 other_user = insert(:user)
88 "@context" => "https://www.w3.org/ns/activitystreams",
90 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
91 "actor" => other_user.ap_id,
94 "content" => "Hi @#{user.nickname}",
95 "attributedTo" => other_user.ap_id
99 {:ok, %{local: false} = activity} = Transmogrifier.handle_incoming(create_activity)
100 assert nil == Notification.create_notification(activity, user)
103 test "it disables notifications from people on the local instance" do
104 user = insert(:user, info: %{notification_settings: %{"local" => false}})
105 other_user = insert(:user)
106 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
107 assert nil == Notification.create_notification(activity, user)
110 test "it disables notifications from followers" do
111 follower = insert(:user)
112 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
113 User.follow(follower, followed)
114 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
115 assert nil == Notification.create_notification(activity, followed)
118 test "it disables notifications from people the user follows" do
119 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
120 followed = insert(:user)
121 User.follow(follower, followed)
122 follower = Repo.get(User, follower.id)
123 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
124 assert nil == Notification.create_notification(activity, follower)
127 test "it doesn't create a notification for user if he is the activity author" do
128 activity = insert(:note_activity)
129 author = User.get_by_ap_id(activity.data["actor"])
131 assert nil == Notification.create_notification(activity, author)
134 test "it doesn't create a notification for follow-unfollow-follow chains" do
136 followed_user = insert(:user)
137 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
138 Notification.create_notification(activity, followed_user)
139 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
140 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
141 assert nil == Notification.create_notification(activity_dupe, followed_user)
144 test "it doesn't create a notification for like-unlike-like chains" do
146 liked_user = insert(:user)
147 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
148 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
149 Notification.create_notification(fav_status, liked_user)
150 TwitterAPI.unfav(user, status.id)
151 {:ok, dupe} = TwitterAPI.fav(user, status.id)
152 assert nil == Notification.create_notification(dupe, liked_user)
155 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
157 retweeted_user = insert(:user)
160 TwitterAPI.create_status(retweeted_user, %{
161 "status" => "Send dupe notifications to the shadow realm"
164 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
165 Notification.create_notification(retweeted_activity, retweeted_user)
166 TwitterAPI.unrepeat(user, status.id)
167 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
168 assert nil == Notification.create_notification(dupe, retweeted_user)
171 test "it doesn't create duplicate notifications for follow+subscribed users" do
173 subscriber = insert(:user)
175 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
176 User.subscribe(subscriber, user)
177 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
178 {:ok, [_notif]} = Notification.create_notifications(status)
181 test "it doesn't create subscription notifications if the recipient cannot see the status" do
183 subscriber = insert(:user)
185 User.subscribe(subscriber, user)
188 TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
190 assert {:ok, []} == Notification.create_notifications(status)
194 describe "get notification" do
195 test "it gets a notification that belongs 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 {:ok, notification} = Notification.get(other_user, notification.id)
205 assert notification.user_id == other_user.id
208 test "it returns error if the notification doesn't belong 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 {:error, _notification} = Notification.get(user, notification.id)
220 describe "dismiss notification" do
221 test "it dismisses a notification that belongs 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 {:ok, notification} = Notification.dismiss(other_user, notification.id)
231 assert notification.user_id == other_user.id
234 test "it returns error if the notification doesn't belong to the user" do
236 other_user = insert(:user)
239 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
241 {:ok, [notification]} = Notification.create_notifications(activity)
242 {:error, _notification} = Notification.dismiss(user, notification.id)
246 describe "clear notification" do
247 test "it clears all notifications belonging to the user" do
249 other_user = insert(:user)
250 third_user = insert(:user)
253 TwitterAPI.create_status(user, %{
254 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
257 {:ok, _notifs} = Notification.create_notifications(activity)
260 TwitterAPI.create_status(user, %{
261 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
264 {:ok, _notifs} = Notification.create_notifications(activity)
265 Notification.clear(other_user)
267 assert Notification.for_user(other_user) == []
268 assert Notification.for_user(third_user) != []
272 describe "set_read_up_to()" do
273 test "it sets all notifications as read up to a specified notification ID" do
275 other_user = insert(:user)
278 TwitterAPI.create_status(user, %{
279 "status" => "hey @#{other_user.nickname}!"
283 TwitterAPI.create_status(user, %{
284 "status" => "hey again @#{other_user.nickname}!"
287 [n2, n1] = notifs = Notification.for_user(other_user)
288 assert length(notifs) == 2
293 TwitterAPI.create_status(user, %{
294 "status" => "hey yet again @#{other_user.nickname}!"
297 Notification.set_read_up_to(other_user, n2.id)
299 [n3, n2, n1] = Notification.for_user(other_user)
301 assert n1.seen == true
302 assert n2.seen == true
303 assert n3.seen == false
307 describe "for_user_since/2" do
308 defp days_ago(days) do
310 NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
311 -days * 60 * 60 * 24,
316 test "Returns recent notifications" do
317 user1 = insert(:user)
318 user2 = insert(:user)
320 Enum.each(0..10, fn i ->
322 CommonAPI.post(user1, %{
323 "status" => "hey ##{i} @#{user2.nickname}!"
327 {old, new} = Enum.split(Notification.for_user(user2), 5)
329 Enum.each(old, fn notification ->
331 |> cast(%{updated_at: days_ago(10)}, [:updated_at])
332 |> Pleroma.Repo.update!()
335 recent_notifications_ids =
337 |> Notification.for_user_since(
338 NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
342 Enum.each(old, fn %{id: id} ->
343 refute id in recent_notifications_ids
346 Enum.each(new, fn %{id: id} ->
347 assert id in recent_notifications_ids
352 describe "notification target determination" do
353 test "it sends notifications to addressed users in new messages" do
355 other_user = insert(:user)
358 CommonAPI.post(user, %{
359 "status" => "hey @#{other_user.nickname}!"
362 assert other_user in Notification.get_notified_from_activity(activity)
365 test "it sends notifications to mentioned users in new messages" do
367 other_user = insert(:user)
370 "@context" => "https://www.w3.org/ns/activitystreams",
372 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
373 "actor" => user.ap_id,
376 "content" => "message with a Mention tag, but no explicit tagging",
380 "href" => other_user.ap_id,
381 "name" => other_user.nickname
384 "attributedTo" => user.ap_id
388 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
390 assert other_user in Notification.get_notified_from_activity(activity)
393 test "it does not send notifications to users who are only cc in new messages" do
395 other_user = insert(:user)
398 "@context" => "https://www.w3.org/ns/activitystreams",
400 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
401 "cc" => [other_user.ap_id],
402 "actor" => user.ap_id,
405 "content" => "hi everyone",
406 "attributedTo" => user.ap_id
410 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
412 assert other_user not in Notification.get_notified_from_activity(activity)
415 test "it does not send notification to mentioned users in likes" do
417 other_user = insert(:user)
418 third_user = insert(:user)
420 {:ok, activity_one} =
421 CommonAPI.post(user, %{
422 "status" => "hey @#{other_user.nickname}!"
425 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
427 assert other_user not in Notification.get_notified_from_activity(activity_two)
430 test "it does not send notification to mentioned users in announces" do
432 other_user = insert(:user)
433 third_user = insert(:user)
435 {:ok, activity_one} =
436 CommonAPI.post(user, %{
437 "status" => "hey @#{other_user.nickname}!"
440 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
442 assert other_user not in Notification.get_notified_from_activity(activity_two)
446 describe "notification lifecycle" do
447 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
449 other_user = insert(:user)
451 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
453 assert Enum.empty?(Notification.for_user(user))
455 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
457 assert length(Notification.for_user(user)) == 1
459 {:ok, _} = CommonAPI.delete(activity.id, user)
461 assert Enum.empty?(Notification.for_user(user))
464 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
466 other_user = insert(:user)
468 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
470 assert Enum.empty?(Notification.for_user(user))
472 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
474 assert length(Notification.for_user(user)) == 1
476 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
478 assert Enum.empty?(Notification.for_user(user))
481 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
483 other_user = insert(:user)
485 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
487 assert Enum.empty?(Notification.for_user(user))
489 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
491 assert length(Notification.for_user(user)) == 1
493 {:ok, _} = CommonAPI.delete(activity.id, user)
495 assert Enum.empty?(Notification.for_user(user))
498 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
500 other_user = insert(:user)
502 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
504 assert Enum.empty?(Notification.for_user(user))
506 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
508 assert length(Notification.for_user(user)) == 1
510 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
512 assert Enum.empty?(Notification.for_user(user))
515 test "liking an activity which is already deleted does not generate a notification" do
517 other_user = insert(:user)
519 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
521 assert Enum.empty?(Notification.for_user(user))
523 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
525 assert Enum.empty?(Notification.for_user(user))
527 {:error, _} = CommonAPI.favorite(activity.id, other_user)
529 assert Enum.empty?(Notification.for_user(user))
532 test "repeating an activity which is already deleted does not generate a notification" do
534 other_user = insert(:user)
536 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
538 assert Enum.empty?(Notification.for_user(user))
540 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
542 assert Enum.empty?(Notification.for_user(user))
544 {:error, _} = CommonAPI.repeat(activity.id, other_user)
546 assert Enum.empty?(Notification.for_user(user))
549 test "replying to a deleted post without tagging does not generate a notification" do
551 other_user = insert(:user)
553 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
554 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
556 {:ok, _reply_activity} =
557 CommonAPI.post(other_user, %{
558 "status" => "test reply",
559 "in_reply_to_status_id" => activity.id
562 assert Enum.empty?(Notification.for_user(user))