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
7 alias Pleroma.Notification
9 alias Pleroma.Web.ActivityPub.Transmogrifier
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.TwitterAPI.TwitterAPI
12 import Pleroma.Factory
14 describe "create_notifications" do
15 test "notifies someone when they are directly addressed" do
17 other_user = insert(:user)
18 third_user = insert(:user)
21 TwitterAPI.create_status(user, %{
22 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
25 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
27 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
28 assert notified_ids == [other_user.id, third_user.id]
29 assert notification.activity_id == activity.id
30 assert other_notification.activity_id == activity.id
34 describe "create_notification" do
35 test "it doesn't create a notification for user if the user blocks the activity author" do
36 activity = insert(:note_activity)
37 author = User.get_by_ap_id(activity.data["actor"])
39 {:ok, user} = User.block(user, author)
41 assert nil == Notification.create_notification(activity, user)
44 test "it doesn't create a notificatin for the user if the user mutes the activity author" do
47 {:ok, _} = User.mute(muter, muted)
48 muter = Repo.get(User, muter.id)
49 {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
51 assert nil == Notification.create_notification(activity, muter)
54 test "it doesn't create a notification for an activity from a muted thread" do
56 other_user = insert(:user)
57 {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
58 CommonAPI.add_mute(muter, activity)
61 CommonAPI.post(other_user, %{
62 "status" => "Hi @#{muter.nickname}",
63 "in_reply_to_status_id" => activity.id
66 assert nil == Notification.create_notification(activity, muter)
69 test "it disables notifications from people on remote instances" do
70 user = insert(:user, info: %{notification_settings: %{"remote" => false}})
71 other_user = insert(:user)
74 "@context" => "https://www.w3.org/ns/activitystreams",
76 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
77 "actor" => other_user.ap_id,
80 "content" => "Hi @#{user.nickname}",
81 "attributedTo" => other_user.ap_id
85 {:ok, %{local: false} = activity} = Transmogrifier.handle_incoming(create_activity)
86 assert nil == Notification.create_notification(activity, user)
89 test "it disables notifications from people on the local instance" do
90 user = insert(:user, info: %{notification_settings: %{"local" => false}})
91 other_user = insert(:user)
92 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
93 assert nil == Notification.create_notification(activity, user)
96 test "it disables notifications from followers" do
97 follower = insert(:user)
98 followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
99 User.follow(follower, followed)
100 {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
101 assert nil == Notification.create_notification(activity, followed)
104 test "it disables notifications from people the user follows" do
105 follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
106 followed = insert(:user)
107 User.follow(follower, followed)
108 follower = Repo.get(User, follower.id)
109 {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
110 assert nil == Notification.create_notification(activity, follower)
113 test "it doesn't create a notification for user if he is the activity author" do
114 activity = insert(:note_activity)
115 author = User.get_by_ap_id(activity.data["actor"])
117 assert nil == Notification.create_notification(activity, author)
120 test "it doesn't create a notification for follow-unfollow-follow chains" do
122 followed_user = insert(:user)
123 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
124 Notification.create_notification(activity, followed_user)
125 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
126 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
127 assert nil == Notification.create_notification(activity_dupe, followed_user)
130 test "it doesn't create a notification for like-unlike-like chains" do
132 liked_user = insert(:user)
133 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
134 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
135 Notification.create_notification(fav_status, liked_user)
136 TwitterAPI.unfav(user, status.id)
137 {:ok, dupe} = TwitterAPI.fav(user, status.id)
138 assert nil == Notification.create_notification(dupe, liked_user)
141 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
143 retweeted_user = insert(:user)
146 TwitterAPI.create_status(retweeted_user, %{
147 "status" => "Send dupe notifications to the shadow realm"
150 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
151 Notification.create_notification(retweeted_activity, retweeted_user)
152 TwitterAPI.unrepeat(user, status.id)
153 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
154 assert nil == Notification.create_notification(dupe, retweeted_user)
158 describe "get notification" do
159 test "it gets a notification that belongs to the user" do
161 other_user = insert(:user)
164 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
166 {:ok, [notification]} = Notification.create_notifications(activity)
167 {:ok, notification} = Notification.get(other_user, notification.id)
169 assert notification.user_id == other_user.id
172 test "it returns error if the notification doesn't belong to the user" do
174 other_user = insert(:user)
177 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
179 {:ok, [notification]} = Notification.create_notifications(activity)
180 {:error, _notification} = Notification.get(user, notification.id)
184 describe "dismiss notification" do
185 test "it dismisses a notification that belongs to the user" do
187 other_user = insert(:user)
190 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
192 {:ok, [notification]} = Notification.create_notifications(activity)
193 {:ok, notification} = Notification.dismiss(other_user, notification.id)
195 assert notification.user_id == other_user.id
198 test "it returns error if the notification doesn't belong to the user" do
200 other_user = insert(:user)
203 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
205 {:ok, [notification]} = Notification.create_notifications(activity)
206 {:error, _notification} = Notification.dismiss(user, notification.id)
210 describe "clear notification" do
211 test "it clears all notifications belonging to the user" do
213 other_user = insert(:user)
214 third_user = insert(:user)
217 TwitterAPI.create_status(user, %{
218 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
221 {:ok, _notifs} = Notification.create_notifications(activity)
224 TwitterAPI.create_status(user, %{
225 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
228 {:ok, _notifs} = Notification.create_notifications(activity)
229 Notification.clear(other_user)
231 assert Notification.for_user(other_user) == []
232 assert Notification.for_user(third_user) != []
236 describe "set_read_up_to()" do
237 test "it sets all notifications as read up to a specified notification ID" do
239 other_user = insert(:user)
242 TwitterAPI.create_status(user, %{
243 "status" => "hey @#{other_user.nickname}!"
247 TwitterAPI.create_status(user, %{
248 "status" => "hey again @#{other_user.nickname}!"
251 [n2, n1] = notifs = Notification.for_user(other_user)
252 assert length(notifs) == 2
257 TwitterAPI.create_status(user, %{
258 "status" => "hey yet again @#{other_user.nickname}!"
261 Notification.set_read_up_to(other_user, n2.id)
263 [n3, n2, n1] = Notification.for_user(other_user)
265 assert n1.seen == true
266 assert n2.seen == true
267 assert n3.seen == false
271 describe "notification target determination" do
272 test "it sends notifications to addressed users in new messages" do
274 other_user = insert(:user)
277 CommonAPI.post(user, %{
278 "status" => "hey @#{other_user.nickname}!"
281 assert other_user in Notification.get_notified_from_activity(activity)
284 test "it sends notifications to mentioned users in new messages" do
286 other_user = insert(:user)
289 "@context" => "https://www.w3.org/ns/activitystreams",
291 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
292 "actor" => user.ap_id,
295 "content" => "message with a Mention tag, but no explicit tagging",
299 "href" => other_user.ap_id,
300 "name" => other_user.nickname
303 "attributedTo" => user.ap_id
307 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
309 assert other_user in Notification.get_notified_from_activity(activity)
312 test "it does not send notifications to users who are only cc in new messages" do
314 other_user = insert(:user)
317 "@context" => "https://www.w3.org/ns/activitystreams",
319 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
320 "cc" => [other_user.ap_id],
321 "actor" => user.ap_id,
324 "content" => "hi everyone",
325 "attributedTo" => user.ap_id
329 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
331 assert other_user not in Notification.get_notified_from_activity(activity)
334 test "it does not send notification to mentioned users in likes" do
336 other_user = insert(:user)
337 third_user = insert(:user)
339 {:ok, activity_one} =
340 CommonAPI.post(user, %{
341 "status" => "hey @#{other_user.nickname}!"
344 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
346 assert other_user not in Notification.get_notified_from_activity(activity_two)
349 test "it does not send notification to mentioned users in announces" do
351 other_user = insert(:user)
352 third_user = insert(:user)
354 {:ok, activity_one} =
355 CommonAPI.post(user, %{
356 "status" => "hey @#{other_user.nickname}!"
359 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
361 assert other_user not in Notification.get_notified_from_activity(activity_two)
365 describe "notification lifecycle" do
366 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
368 other_user = insert(:user)
370 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
372 assert Enum.empty?(Notification.for_user(user))
374 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
376 assert length(Notification.for_user(user)) == 1
378 {:ok, _} = CommonAPI.delete(activity.id, user)
380 assert Enum.empty?(Notification.for_user(user))
383 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
385 other_user = insert(:user)
387 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
389 assert Enum.empty?(Notification.for_user(user))
391 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
393 assert length(Notification.for_user(user)) == 1
395 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
397 assert Enum.empty?(Notification.for_user(user))
400 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
402 other_user = insert(:user)
404 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
406 assert Enum.empty?(Notification.for_user(user))
408 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
410 assert length(Notification.for_user(user)) == 1
412 {:ok, _} = CommonAPI.delete(activity.id, user)
414 assert Enum.empty?(Notification.for_user(user))
417 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
419 other_user = insert(:user)
421 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
423 assert Enum.empty?(Notification.for_user(user))
425 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
427 assert length(Notification.for_user(user)) == 1
429 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
431 assert Enum.empty?(Notification.for_user(user))
434 test "liking an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
444 assert Enum.empty?(Notification.for_user(user))
446 {:error, _} = CommonAPI.favorite(activity.id, other_user)
448 assert Enum.empty?(Notification.for_user(user))
451 test "repeating an activity which is already deleted does not generate a notification" 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, _deletion_activity} = CommonAPI.delete(activity.id, user)
461 assert Enum.empty?(Notification.for_user(user))
463 {:error, _} = CommonAPI.repeat(activity.id, other_user)
465 assert Enum.empty?(Notification.for_user(user))
468 test "replying to a deleted post without tagging does not generate a notification" do
470 other_user = insert(:user)
472 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
473 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
475 {:ok, _reply_activity} =
476 CommonAPI.post(other_user, %{
477 "status" => "test reply",
478 "in_reply_to_status_id" => activity.id
481 assert Enum.empty?(Notification.for_user(user))