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)
51 describe "get notification" do
52 test "it gets a notification that belongs to the user" do
54 other_user = insert(:user)
57 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
59 {:ok, [notification]} = Notification.create_notifications(activity)
60 {:ok, notification} = Notification.get(other_user, notification.id)
62 assert notification.user_id == other_user.id
65 test "it returns error if the notification doesn't belong to the user" do
67 other_user = insert(:user)
70 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
72 {:ok, [notification]} = Notification.create_notifications(activity)
73 {:error, _notification} = Notification.get(user, notification.id)
77 describe "dismiss notification" do
78 test "it dismisses a notification that belongs to the user" do
80 other_user = insert(:user)
83 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
85 {:ok, [notification]} = Notification.create_notifications(activity)
86 {:ok, notification} = Notification.dismiss(other_user, notification.id)
88 assert notification.user_id == other_user.id
91 test "it returns error if the notification doesn't belong to the user" do
93 other_user = insert(:user)
96 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
98 {:ok, [notification]} = Notification.create_notifications(activity)
99 {:error, _notification} = Notification.dismiss(user, notification.id)
103 describe "clear notification" do
104 test "it clears all notifications belonging to the user" do
106 other_user = insert(:user)
107 third_user = insert(:user)
110 TwitterAPI.create_status(user, %{
111 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
114 {:ok, _notifs} = Notification.create_notifications(activity)
117 TwitterAPI.create_status(user, %{
118 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
121 {:ok, _notifs} = Notification.create_notifications(activity)
122 Notification.clear(other_user)
124 assert Notification.for_user(other_user) == []
125 assert Notification.for_user(third_user) != []
129 describe "set_read_up_to()" do
130 test "it sets all notifications as read up to a specified notification ID" do
132 other_user = insert(:user)
135 TwitterAPI.create_status(user, %{
136 "status" => "hey @#{other_user.nickname}!"
140 TwitterAPI.create_status(user, %{
141 "status" => "hey again @#{other_user.nickname}!"
144 [n2, n1] = notifs = Notification.for_user(other_user)
145 assert length(notifs) == 2
150 TwitterAPI.create_status(user, %{
151 "status" => "hey yet again @#{other_user.nickname}!"
154 Notification.set_read_up_to(other_user, n2.id)
156 [n3, n2, n1] = Notification.for_user(other_user)
158 assert n1.seen == true
159 assert n2.seen == true
160 assert n3.seen == false
164 describe "notification target determination" do
165 test "it sends notifications to addressed users in new messages" do
167 other_user = insert(:user)
170 CommonAPI.post(user, %{
171 "status" => "hey @#{other_user.nickname}!"
174 assert other_user in Notification.get_notified_from_activity(activity)
177 test "it sends notifications to mentioned users in new messages" do
179 other_user = insert(:user)
182 "@context" => "https://www.w3.org/ns/activitystreams",
184 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
185 "actor" => user.ap_id,
188 "content" => "message with a Mention tag, but no explicit tagging",
192 "href" => other_user.ap_id,
193 "name" => other_user.nickname
196 "attributedTo" => user.ap_id
200 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
202 assert other_user in Notification.get_notified_from_activity(activity)
205 test "it does not send notifications to users who are only cc in new messages" do
207 other_user = insert(:user)
210 "@context" => "https://www.w3.org/ns/activitystreams",
212 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
213 "cc" => [other_user.ap_id],
214 "actor" => user.ap_id,
217 "content" => "hi everyone",
218 "attributedTo" => user.ap_id
222 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
224 assert other_user not in Notification.get_notified_from_activity(activity)
227 test "it does not send notification to mentioned users in likes" do
229 other_user = insert(:user)
230 third_user = insert(:user)
232 {:ok, activity_one} =
233 CommonAPI.post(user, %{
234 "status" => "hey @#{other_user.nickname}!"
237 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
239 assert other_user not in Notification.get_notified_from_activity(activity_two)
242 test "it does not send notification to mentioned users in announces" do
244 other_user = insert(:user)
245 third_user = insert(:user)
247 {:ok, activity_one} =
248 CommonAPI.post(user, %{
249 "status" => "hey @#{other_user.nickname}!"
252 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
254 assert other_user not in Notification.get_notified_from_activity(activity_two)
258 describe "notification lifecycle" do
259 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
261 other_user = insert(:user)
263 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
265 assert length(Notification.for_user(user)) == 0
267 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
269 assert length(Notification.for_user(user)) == 1
271 {:ok, _} = CommonAPI.delete(activity.id, user)
273 assert length(Notification.for_user(user)) == 0
276 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
278 other_user = insert(:user)
280 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
282 assert length(Notification.for_user(user)) == 0
284 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
286 assert length(Notification.for_user(user)) == 1
288 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
290 assert length(Notification.for_user(user)) == 0
293 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
295 other_user = insert(:user)
297 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
299 assert length(Notification.for_user(user)) == 0
301 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
303 assert length(Notification.for_user(user)) == 1
305 {:ok, _} = CommonAPI.delete(activity.id, user)
307 assert length(Notification.for_user(user)) == 0
310 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
312 other_user = insert(:user)
314 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
316 assert length(Notification.for_user(user)) == 0
318 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
320 assert length(Notification.for_user(user)) == 1
322 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
324 assert length(Notification.for_user(user)) == 0
327 test "liking an activity which is already deleted does not generate a notification" do
329 other_user = insert(:user)
331 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
333 assert length(Notification.for_user(user)) == 0
335 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
337 assert length(Notification.for_user(user)) == 0
339 {:error, _} = CommonAPI.favorite(activity.id, other_user)
341 assert length(Notification.for_user(user)) == 0
344 test "repeating an activity which is already deleted does not generate a notification" do
346 other_user = insert(:user)
348 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
350 assert length(Notification.for_user(user)) == 0
352 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
354 assert length(Notification.for_user(user)) == 0
356 {:error, _} = CommonAPI.repeat(activity.id, other_user)
358 assert length(Notification.for_user(user)) == 0
361 test "replying to a deleted post without tagging does not generate a notification" do
363 other_user = insert(:user)
365 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
366 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
368 {:ok, _reply_activity} =
369 CommonAPI.post(other_user, %{
370 "status" => "test reply",
371 "in_reply_to_status_id" => activity.id
374 assert length(Notification.for_user(user)) == 0