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.Web.TwitterAPI.TwitterAPI
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.{User, Notification}
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 import Pleroma.Factory
13 describe "create_notifications" do
14 test "notifies someone when they are directly addressed" do
16 other_user = insert(:user)
17 third_user = insert(:user)
20 TwitterAPI.create_status(user, %{
21 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
24 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
26 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
27 assert notified_ids == [other_user.id, third_user.id]
28 assert notification.activity_id == activity.id
29 assert other_notification.activity_id == activity.id
33 describe "create_notification" do
34 test "it doesn't create a notification for user if the user blocks the activity author" do
35 activity = insert(:note_activity)
36 author = User.get_by_ap_id(activity.data["actor"])
38 {:ok, user} = User.block(user, author)
40 assert nil == Notification.create_notification(activity, user)
43 test "it doesn't create a notification for user if he is the activity author" do
44 activity = insert(:note_activity)
45 author = User.get_by_ap_id(activity.data["actor"])
47 assert nil == Notification.create_notification(activity, author)
50 test "it doesn't create a notification for follow-unfollow-follow chains" do
52 followed_user = insert(:user)
53 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
54 Notification.create_notification(activity, followed_user)
55 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
56 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
57 assert nil == Notification.create_notification(activity_dupe, followed_user)
60 test "it doesn't create a notification for like-unlike-like chains" do
62 liked_user = insert(:user)
63 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
64 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
65 Notification.create_notification(fav_status, liked_user)
66 TwitterAPI.unfav(user, status.id)
67 {:ok, dupe} = TwitterAPI.fav(user, status.id)
68 assert nil == Notification.create_notification(dupe, liked_user)
71 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
73 retweeted_user = insert(:user)
76 TwitterAPI.create_status(retweeted_user, %{
77 "status" => "Send dupe notifications to the shadow realm"
80 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
81 Notification.create_notification(retweeted_activity, retweeted_user)
82 TwitterAPI.unrepeat(user, status.id)
83 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
84 assert nil == Notification.create_notification(dupe, retweeted_user)
88 describe "get notification" do
89 test "it gets a notification that belongs to the user" do
91 other_user = insert(:user)
94 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
96 {:ok, [notification]} = Notification.create_notifications(activity)
97 {:ok, notification} = Notification.get(other_user, notification.id)
99 assert notification.user_id == other_user.id
102 test "it returns error if the notification doesn't belong to the user" do
104 other_user = insert(:user)
107 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
109 {:ok, [notification]} = Notification.create_notifications(activity)
110 {:error, _notification} = Notification.get(user, notification.id)
114 describe "dismiss notification" do
115 test "it dismisses a notification that belongs to the user" do
117 other_user = insert(:user)
120 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
122 {:ok, [notification]} = Notification.create_notifications(activity)
123 {:ok, notification} = Notification.dismiss(other_user, notification.id)
125 assert notification.user_id == other_user.id
128 test "it returns error if the notification doesn't belong to the user" do
130 other_user = insert(:user)
133 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
135 {:ok, [notification]} = Notification.create_notifications(activity)
136 {:error, _notification} = Notification.dismiss(user, notification.id)
140 describe "clear notification" do
141 test "it clears all notifications belonging to the user" do
143 other_user = insert(:user)
144 third_user = insert(:user)
147 TwitterAPI.create_status(user, %{
148 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
151 {:ok, _notifs} = Notification.create_notifications(activity)
154 TwitterAPI.create_status(user, %{
155 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
158 {:ok, _notifs} = Notification.create_notifications(activity)
159 Notification.clear(other_user)
161 assert Notification.for_user(other_user) == []
162 assert Notification.for_user(third_user) != []
166 describe "set_read_up_to()" do
167 test "it sets all notifications as read up to a specified notification ID" do
169 other_user = insert(:user)
172 TwitterAPI.create_status(user, %{
173 "status" => "hey @#{other_user.nickname}!"
177 TwitterAPI.create_status(user, %{
178 "status" => "hey again @#{other_user.nickname}!"
181 [n2, n1] = notifs = Notification.for_user(other_user)
182 assert length(notifs) == 2
187 TwitterAPI.create_status(user, %{
188 "status" => "hey yet again @#{other_user.nickname}!"
191 Notification.set_read_up_to(other_user, n2.id)
193 [n3, n2, n1] = Notification.for_user(other_user)
195 assert n1.seen == true
196 assert n2.seen == true
197 assert n3.seen == false
201 describe "notification target determination" do
202 test "it sends notifications to addressed users in new messages" do
204 other_user = insert(:user)
207 CommonAPI.post(user, %{
208 "status" => "hey @#{other_user.nickname}!"
211 assert other_user in Notification.get_notified_from_activity(activity)
214 test "it sends notifications to mentioned users in new messages" do
216 other_user = insert(:user)
219 "@context" => "https://www.w3.org/ns/activitystreams",
221 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
222 "actor" => user.ap_id,
225 "content" => "message with a Mention tag, but no explicit tagging",
229 "href" => other_user.ap_id,
230 "name" => other_user.nickname
233 "attributedTo" => user.ap_id
237 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
239 assert other_user in Notification.get_notified_from_activity(activity)
242 test "it does not send notifications to users who are only cc in new messages" do
244 other_user = insert(:user)
247 "@context" => "https://www.w3.org/ns/activitystreams",
249 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
250 "cc" => [other_user.ap_id],
251 "actor" => user.ap_id,
254 "content" => "hi everyone",
255 "attributedTo" => user.ap_id
259 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
261 assert other_user not in Notification.get_notified_from_activity(activity)
264 test "it does not send notification to mentioned users in likes" do
266 other_user = insert(:user)
267 third_user = insert(:user)
269 {:ok, activity_one} =
270 CommonAPI.post(user, %{
271 "status" => "hey @#{other_user.nickname}!"
274 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
276 assert other_user not in Notification.get_notified_from_activity(activity_two)
279 test "it does not send notification to mentioned users in announces" do
281 other_user = insert(:user)
282 third_user = insert(:user)
284 {:ok, activity_one} =
285 CommonAPI.post(user, %{
286 "status" => "hey @#{other_user.nickname}!"
289 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
291 assert other_user not in Notification.get_notified_from_activity(activity_two)
295 describe "notification lifecycle" do
296 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
298 other_user = insert(:user)
300 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
302 assert length(Notification.for_user(user)) == 0
304 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
306 assert length(Notification.for_user(user)) == 1
308 {:ok, _} = CommonAPI.delete(activity.id, user)
310 assert length(Notification.for_user(user)) == 0
313 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
315 other_user = insert(:user)
317 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
319 assert length(Notification.for_user(user)) == 0
321 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
323 assert length(Notification.for_user(user)) == 1
325 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
327 assert length(Notification.for_user(user)) == 0
330 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
332 other_user = insert(:user)
334 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
336 assert length(Notification.for_user(user)) == 0
338 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
340 assert length(Notification.for_user(user)) == 1
342 {:ok, _} = CommonAPI.delete(activity.id, user)
344 assert length(Notification.for_user(user)) == 0
347 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
349 other_user = insert(:user)
351 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
353 assert length(Notification.for_user(user)) == 0
355 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
357 assert length(Notification.for_user(user)) == 1
359 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
361 assert length(Notification.for_user(user)) == 0
364 test "liking an activity which is already deleted does not generate a notification" do
366 other_user = insert(:user)
368 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
370 assert length(Notification.for_user(user)) == 0
372 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
374 assert length(Notification.for_user(user)) == 0
376 {:error, _} = CommonAPI.favorite(activity.id, other_user)
378 assert length(Notification.for_user(user)) == 0
381 test "repeating an activity which is already deleted does not generate a notification" do
383 other_user = insert(:user)
385 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
387 assert length(Notification.for_user(user)) == 0
389 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
391 assert length(Notification.for_user(user)) == 0
393 {:error, _} = CommonAPI.repeat(activity.id, other_user)
395 assert length(Notification.for_user(user)) == 0
398 test "replying to a deleted post without tagging does not generate a notification" do
400 other_user = insert(:user)
402 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
403 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
405 {:ok, _reply_activity} =
406 CommonAPI.post(other_user, %{
407 "status" => "test reply",
408 "in_reply_to_status_id" => activity.id
411 assert length(Notification.for_user(user)) == 0