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 notification for user if he is the activity author" do
45 activity = insert(:note_activity)
46 author = User.get_by_ap_id(activity.data["actor"])
48 assert nil == Notification.create_notification(activity, author)
51 test "it doesn't create a notification for follow-unfollow-follow chains" do
53 followed_user = insert(:user)
54 {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
55 Notification.create_notification(activity, followed_user)
56 TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
57 {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
58 assert nil == Notification.create_notification(activity_dupe, followed_user)
61 test "it doesn't create a notification for like-unlike-like chains" do
63 liked_user = insert(:user)
64 {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
65 {:ok, fav_status} = TwitterAPI.fav(user, status.id)
66 Notification.create_notification(fav_status, liked_user)
67 TwitterAPI.unfav(user, status.id)
68 {:ok, dupe} = TwitterAPI.fav(user, status.id)
69 assert nil == Notification.create_notification(dupe, liked_user)
72 test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
74 retweeted_user = insert(:user)
77 TwitterAPI.create_status(retweeted_user, %{
78 "status" => "Send dupe notifications to the shadow realm"
81 {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
82 Notification.create_notification(retweeted_activity, retweeted_user)
83 TwitterAPI.unrepeat(user, status.id)
84 {:ok, dupe} = TwitterAPI.repeat(user, status.id)
85 assert nil == Notification.create_notification(dupe, retweeted_user)
89 describe "get notification" do
90 test "it gets a notification that belongs to the user" do
92 other_user = insert(:user)
95 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
97 {:ok, [notification]} = Notification.create_notifications(activity)
98 {:ok, notification} = Notification.get(other_user, notification.id)
100 assert notification.user_id == other_user.id
103 test "it returns error if the notification doesn't belong to the user" do
105 other_user = insert(:user)
108 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
110 {:ok, [notification]} = Notification.create_notifications(activity)
111 {:error, _notification} = Notification.get(user, notification.id)
115 describe "dismiss notification" do
116 test "it dismisses a notification that belongs to the user" do
118 other_user = insert(:user)
121 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
123 {:ok, [notification]} = Notification.create_notifications(activity)
124 {:ok, notification} = Notification.dismiss(other_user, notification.id)
126 assert notification.user_id == other_user.id
129 test "it returns error if the notification doesn't belong to the user" do
131 other_user = insert(:user)
134 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
136 {:ok, [notification]} = Notification.create_notifications(activity)
137 {:error, _notification} = Notification.dismiss(user, notification.id)
141 describe "clear notification" do
142 test "it clears all notifications belonging to the user" do
144 other_user = insert(:user)
145 third_user = insert(:user)
148 TwitterAPI.create_status(user, %{
149 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
152 {:ok, _notifs} = Notification.create_notifications(activity)
155 TwitterAPI.create_status(user, %{
156 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
159 {:ok, _notifs} = Notification.create_notifications(activity)
160 Notification.clear(other_user)
162 assert Notification.for_user(other_user) == []
163 assert Notification.for_user(third_user) != []
167 describe "set_read_up_to()" do
168 test "it sets all notifications as read up to a specified notification ID" do
170 other_user = insert(:user)
173 TwitterAPI.create_status(user, %{
174 "status" => "hey @#{other_user.nickname}!"
178 TwitterAPI.create_status(user, %{
179 "status" => "hey again @#{other_user.nickname}!"
182 [n2, n1] = notifs = Notification.for_user(other_user)
183 assert length(notifs) == 2
188 TwitterAPI.create_status(user, %{
189 "status" => "hey yet again @#{other_user.nickname}!"
192 Notification.set_read_up_to(other_user, n2.id)
194 [n3, n2, n1] = Notification.for_user(other_user)
196 assert n1.seen == true
197 assert n2.seen == true
198 assert n3.seen == false
202 describe "notification target determination" do
203 test "it sends notifications to addressed users in new messages" do
205 other_user = insert(:user)
208 CommonAPI.post(user, %{
209 "status" => "hey @#{other_user.nickname}!"
212 assert other_user in Notification.get_notified_from_activity(activity)
215 test "it sends notifications to mentioned users in new messages" do
217 other_user = insert(:user)
220 "@context" => "https://www.w3.org/ns/activitystreams",
222 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
223 "actor" => user.ap_id,
226 "content" => "message with a Mention tag, but no explicit tagging",
230 "href" => other_user.ap_id,
231 "name" => other_user.nickname
234 "attributedTo" => user.ap_id
238 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
240 assert other_user in Notification.get_notified_from_activity(activity)
243 test "it does not send notifications to users who are only cc in new messages" do
245 other_user = insert(:user)
248 "@context" => "https://www.w3.org/ns/activitystreams",
250 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
251 "cc" => [other_user.ap_id],
252 "actor" => user.ap_id,
255 "content" => "hi everyone",
256 "attributedTo" => user.ap_id
260 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
262 assert other_user not in Notification.get_notified_from_activity(activity)
265 test "it does not send notification to mentioned users in likes" do
267 other_user = insert(:user)
268 third_user = insert(:user)
270 {:ok, activity_one} =
271 CommonAPI.post(user, %{
272 "status" => "hey @#{other_user.nickname}!"
275 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
277 assert other_user not in Notification.get_notified_from_activity(activity_two)
280 test "it does not send notification to mentioned users in announces" do
282 other_user = insert(:user)
283 third_user = insert(:user)
285 {:ok, activity_one} =
286 CommonAPI.post(user, %{
287 "status" => "hey @#{other_user.nickname}!"
290 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
292 assert other_user not in Notification.get_notified_from_activity(activity_two)
296 describe "notification lifecycle" do
297 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
299 other_user = insert(:user)
301 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
303 assert Enum.empty?(Notification.for_user(user))
305 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
307 assert length(Notification.for_user(user)) == 1
309 {:ok, _} = CommonAPI.delete(activity.id, user)
311 assert Enum.empty?(Notification.for_user(user))
314 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
316 other_user = insert(:user)
318 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
320 assert Enum.empty?(Notification.for_user(user))
322 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
324 assert length(Notification.for_user(user)) == 1
326 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
328 assert Enum.empty?(Notification.for_user(user))
331 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
333 other_user = insert(:user)
335 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
337 assert Enum.empty?(Notification.for_user(user))
339 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
341 assert length(Notification.for_user(user)) == 1
343 {:ok, _} = CommonAPI.delete(activity.id, user)
345 assert Enum.empty?(Notification.for_user(user))
348 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
350 other_user = insert(:user)
352 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
354 assert Enum.empty?(Notification.for_user(user))
356 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
358 assert length(Notification.for_user(user)) == 1
360 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
362 assert Enum.empty?(Notification.for_user(user))
365 test "liking an activity which is already deleted does not generate a notification" do
367 other_user = insert(:user)
369 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
371 assert Enum.empty?(Notification.for_user(user))
373 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
375 assert Enum.empty?(Notification.for_user(user))
377 {:error, _} = CommonAPI.favorite(activity.id, other_user)
379 assert Enum.empty?(Notification.for_user(user))
382 test "repeating an activity which is already deleted does not generate a notification" do
384 other_user = insert(:user)
386 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
388 assert Enum.empty?(Notification.for_user(user))
390 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
392 assert Enum.empty?(Notification.for_user(user))
394 {:error, _} = CommonAPI.repeat(activity.id, other_user)
396 assert Enum.empty?(Notification.for_user(user))
399 test "replying to a deleted post without tagging does not generate a notification" do
401 other_user = insert(:user)
403 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
404 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
406 {:ok, _reply_activity} =
407 CommonAPI.post(other_user, %{
408 "status" => "test reply",
409 "in_reply_to_status_id" => activity.id
412 assert Enum.empty?(Notification.for_user(user))