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
33 test "it creates a notification for subscribed users" do
35 subscriber = insert(:user)
37 {:ok, _, _} = TwitterAPI.subscribe(subscriber, %{"user_id" => user.id})
38 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
39 {:ok, [notification]} = Notification.create_notifications(status)
41 assert notification.user_id == subscriber.id
45 describe "create_notification" do
46 test "it doesn't create a notification for user if the user blocks the activity author" do
47 activity = insert(:note_activity)
48 author = User.get_by_ap_id(activity.data["actor"])
50 {:ok, user} = User.block(user, author)
52 assert nil == Notification.create_notification(activity, user)
55 test "it doesn't create a notification for user if he is the activity author" do
56 activity = insert(:note_activity)
57 author = User.get_by_ap_id(activity.data["actor"])
59 assert nil == Notification.create_notification(activity, author)
62 test "it doesn't create a notification for follow-unfollow-follow chains" do
64 followed_user = insert(:user)
65 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
66 Notification.create_notification(activity, followed_user)
67 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
68 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
69 assert nil == Notification.create_notification(activity_dupe, followed_user)
72 test "it doesn't create a notification for like-unlike-like chains" do
74 liked_user = insert(:user)
75 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
76 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
77 Notification.create_notification(fav_status, liked_user)
78 TwitterAPI.unfav(user, status.id)
79 {:ok, dupe} = TwitterAPI.fav(user, status.id)
80 assert nil == Notification.create_notification(dupe, liked_user)
83 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
85 retweeted_user = insert(:user)
88 TwitterAPI.create_status(retweeted_user, %{
89 "status" => "Send dupe notifications to the shadow realm"
92 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
93 Notification.create_notification(retweeted_activity, retweeted_user)
94 TwitterAPI.unrepeat(user, status.id)
95 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
96 assert nil == Notification.create_notification(dupe, retweeted_user)
99 test "it doesn't create duplicate notifications for follow+subscribed users" do
101 subscriber = insert(:user)
103 {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
104 {:ok, _, _} = TwitterAPI.subscribe(subscriber, %{"user_id" => user.id})
105 {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
106 {:ok, [_notif]} = Notification.create_notifications(status)
110 describe "get notification" do
111 test "it gets a notification that belongs to the user" do
113 other_user = insert(:user)
116 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
118 {:ok, [notification]} = Notification.create_notifications(activity)
119 {:ok, notification} = Notification.get(other_user, notification.id)
121 assert notification.user_id == other_user.id
124 test "it returns error if the notification doesn't belong to the user" do
126 other_user = insert(:user)
129 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
131 {:ok, [notification]} = Notification.create_notifications(activity)
132 {:error, _notification} = Notification.get(user, notification.id)
136 describe "dismiss notification" do
137 test "it dismisses a notification that belongs to the user" do
139 other_user = insert(:user)
142 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
144 {:ok, [notification]} = Notification.create_notifications(activity)
145 {:ok, notification} = Notification.dismiss(other_user, notification.id)
147 assert notification.user_id == other_user.id
150 test "it returns error if the notification doesn't belong to the user" do
152 other_user = insert(:user)
155 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
157 {:ok, [notification]} = Notification.create_notifications(activity)
158 {:error, _notification} = Notification.dismiss(user, notification.id)
162 describe "clear notification" do
163 test "it clears all notifications belonging to the user" do
165 other_user = insert(:user)
166 third_user = insert(:user)
169 TwitterAPI.create_status(user, %{
170 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
173 {:ok, _notifs} = Notification.create_notifications(activity)
176 TwitterAPI.create_status(user, %{
177 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
180 {:ok, _notifs} = Notification.create_notifications(activity)
181 Notification.clear(other_user)
183 assert Notification.for_user(other_user) == []
184 assert Notification.for_user(third_user) != []
188 describe "set_read_up_to()" do
189 test "it sets all notifications as read up to a specified notification ID" do
191 other_user = insert(:user)
194 TwitterAPI.create_status(user, %{
195 "status" => "hey @#{other_user.nickname}!"
199 TwitterAPI.create_status(user, %{
200 "status" => "hey again @#{other_user.nickname}!"
203 [n2, n1] = notifs = Notification.for_user(other_user)
204 assert length(notifs) == 2
209 TwitterAPI.create_status(user, %{
210 "status" => "hey yet again @#{other_user.nickname}!"
213 Notification.set_read_up_to(other_user, n2.id)
215 [n3, n2, n1] = Notification.for_user(other_user)
217 assert n1.seen == true
218 assert n2.seen == true
219 assert n3.seen == false
223 describe "notification target determination" do
224 test "it sends notifications to addressed users in new messages" do
226 other_user = insert(:user)
229 CommonAPI.post(user, %{
230 "status" => "hey @#{other_user.nickname}!"
233 assert other_user in Notification.get_notified_from_activity(activity)
236 test "it sends notifications to mentioned users in new messages" do
238 other_user = insert(:user)
241 "@context" => "https://www.w3.org/ns/activitystreams",
243 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
244 "actor" => user.ap_id,
247 "content" => "message with a Mention tag, but no explicit tagging",
251 "href" => other_user.ap_id,
252 "name" => other_user.nickname
255 "attributedTo" => user.ap_id
259 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
261 assert other_user in Notification.get_notified_from_activity(activity)
264 test "it does not send notifications to users who are only cc in new messages" do
266 other_user = insert(:user)
269 "@context" => "https://www.w3.org/ns/activitystreams",
271 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
272 "cc" => [other_user.ap_id],
273 "actor" => user.ap_id,
276 "content" => "hi everyone",
277 "attributedTo" => user.ap_id
281 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
283 assert other_user not in Notification.get_notified_from_activity(activity)
286 test "it does not send notification to mentioned users in likes" do
288 other_user = insert(:user)
289 third_user = insert(:user)
291 {:ok, activity_one} =
292 CommonAPI.post(user, %{
293 "status" => "hey @#{other_user.nickname}!"
296 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
298 assert other_user not in Notification.get_notified_from_activity(activity_two)
301 test "it does not send notification to mentioned users in announces" do
303 other_user = insert(:user)
304 third_user = insert(:user)
306 {:ok, activity_one} =
307 CommonAPI.post(user, %{
308 "status" => "hey @#{other_user.nickname}!"
311 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
313 assert other_user not in Notification.get_notified_from_activity(activity_two)
317 describe "notification lifecycle" do
318 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
320 other_user = insert(:user)
322 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
324 assert Enum.empty?(Notification.for_user(user))
326 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
328 assert length(Notification.for_user(user)) == 1
330 {:ok, _} = CommonAPI.delete(activity.id, user)
332 assert Enum.empty?(Notification.for_user(user))
335 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
337 other_user = insert(:user)
339 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
341 assert Enum.empty?(Notification.for_user(user))
343 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
345 assert length(Notification.for_user(user)) == 1
347 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
349 assert Enum.empty?(Notification.for_user(user))
352 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
354 other_user = insert(:user)
356 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
358 assert Enum.empty?(Notification.for_user(user))
360 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
362 assert length(Notification.for_user(user)) == 1
364 {:ok, _} = CommonAPI.delete(activity.id, user)
366 assert Enum.empty?(Notification.for_user(user))
369 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
371 other_user = insert(:user)
373 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
375 assert Enum.empty?(Notification.for_user(user))
377 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
379 assert length(Notification.for_user(user)) == 1
381 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
383 assert Enum.empty?(Notification.for_user(user))
386 test "liking an activity which is already deleted does not generate a notification" do
388 other_user = insert(:user)
390 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
392 assert Enum.empty?(Notification.for_user(user))
394 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
396 assert Enum.empty?(Notification.for_user(user))
398 {:error, _} = CommonAPI.favorite(activity.id, other_user)
400 assert Enum.empty?(Notification.for_user(user))
403 test "repeating an activity which is already deleted does not generate a notification" do
405 other_user = insert(:user)
407 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
409 assert Enum.empty?(Notification.for_user(user))
411 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
413 assert Enum.empty?(Notification.for_user(user))
415 {:error, _} = CommonAPI.repeat(activity.id, other_user)
417 assert Enum.empty?(Notification.for_user(user))
420 test "replying to a deleted post without tagging does not generate a notification" do
422 other_user = insert(:user)
424 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
425 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
427 {:ok, _reply_activity} =
428 CommonAPI.post(other_user, %{
429 "status" => "test reply",
430 "in_reply_to_status_id" => activity.id
433 assert Enum.empty?(Notification.for_user(user))