Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1134 lines
36KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.NotificationTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. import Mock
  8. alias Pleroma.FollowingRelationship
  9. alias Pleroma.Notification
  10. alias Pleroma.Repo
  11. alias Pleroma.Tests.ObanHelpers
  12. alias Pleroma.User
  13. alias Pleroma.Web.ActivityPub.ActivityPub
  14. alias Pleroma.Web.ActivityPub.Builder
  15. alias Pleroma.Web.ActivityPub.Transmogrifier
  16. alias Pleroma.Web.CommonAPI
  17. alias Pleroma.Web.MastodonAPI.NotificationView
  18. alias Pleroma.Web.Push
  19. alias Pleroma.Web.Streamer
  20. describe "create_notifications" do
  21. test "never returns nil" do
  22. user = insert(:user)
  23. other_user = insert(:user, %{invisible: true})
  24. {:ok, activity} = CommonAPI.post(user, %{status: "yeah"})
  25. {:ok, activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  26. refute {:ok, [nil]} == Notification.create_notifications(activity)
  27. end
  28. test "creates a notification for an emoji reaction" do
  29. user = insert(:user)
  30. other_user = insert(:user)
  31. {:ok, activity} = CommonAPI.post(user, %{status: "yeah"})
  32. {:ok, activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  33. {:ok, [notification]} = Notification.create_notifications(activity)
  34. assert notification.user_id == user.id
  35. assert notification.type == "pleroma:emoji_reaction"
  36. end
  37. test "notifies someone when they are directly addressed" do
  38. user = insert(:user)
  39. other_user = insert(:user)
  40. third_user = insert(:user)
  41. {:ok, activity} =
  42. CommonAPI.post(user, %{
  43. status: "hey @#{other_user.nickname} and @#{third_user.nickname}"
  44. })
  45. {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
  46. notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
  47. assert notified_ids == [other_user.id, third_user.id]
  48. assert notification.activity_id == activity.id
  49. assert notification.type == "mention"
  50. assert other_notification.activity_id == activity.id
  51. assert [%Pleroma.Marker{unread_count: 2}] =
  52. Pleroma.Marker.get_markers(other_user, ["notifications"])
  53. end
  54. test "it creates a notification for subscribed users" do
  55. user = insert(:user)
  56. subscriber = insert(:user)
  57. User.subscribe(subscriber, user)
  58. {:ok, status} = CommonAPI.post(user, %{status: "Akariiiin"})
  59. {:ok, [notification]} = Notification.create_notifications(status)
  60. assert notification.user_id == subscriber.id
  61. end
  62. test "does not create a notification for subscribed users if status is a reply" do
  63. user = insert(:user)
  64. other_user = insert(:user)
  65. subscriber = insert(:user)
  66. User.subscribe(subscriber, other_user)
  67. {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
  68. {:ok, _reply_activity} =
  69. CommonAPI.post(other_user, %{
  70. status: "test reply",
  71. in_reply_to_status_id: activity.id
  72. })
  73. user_notifications = Notification.for_user(user)
  74. assert length(user_notifications) == 1
  75. subscriber_notifications = Notification.for_user(subscriber)
  76. assert Enum.empty?(subscriber_notifications)
  77. end
  78. end
  79. describe "CommonApi.post/2 notification-related functionality" do
  80. test_with_mock "creates but does NOT send notification to blocker user",
  81. Push,
  82. [:passthrough],
  83. [] do
  84. user = insert(:user)
  85. blocker = insert(:user)
  86. {:ok, _user_relationship} = User.block(blocker, user)
  87. {:ok, _activity} = CommonAPI.post(user, %{status: "hey @#{blocker.nickname}!"})
  88. blocker_id = blocker.id
  89. assert [%Notification{user_id: ^blocker_id}] = Repo.all(Notification)
  90. refute called(Push.send(:_))
  91. end
  92. test_with_mock "creates but does NOT send notification to notification-muter user",
  93. Push,
  94. [:passthrough],
  95. [] do
  96. user = insert(:user)
  97. muter = insert(:user)
  98. {:ok, _user_relationships} = User.mute(muter, user)
  99. {:ok, _activity} = CommonAPI.post(user, %{status: "hey @#{muter.nickname}!"})
  100. muter_id = muter.id
  101. assert [%Notification{user_id: ^muter_id}] = Repo.all(Notification)
  102. refute called(Push.send(:_))
  103. end
  104. test_with_mock "creates but does NOT send notification to thread-muter user",
  105. Push,
  106. [:passthrough],
  107. [] do
  108. user = insert(:user)
  109. thread_muter = insert(:user)
  110. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{thread_muter.nickname}!"})
  111. {:ok, _} = CommonAPI.add_mute(thread_muter, activity)
  112. {:ok, _same_context_activity} =
  113. CommonAPI.post(user, %{
  114. status: "hey-hey-hey @#{thread_muter.nickname}!",
  115. in_reply_to_status_id: activity.id
  116. })
  117. [pre_mute_notification, post_mute_notification] =
  118. Repo.all(from(n in Notification, where: n.user_id == ^thread_muter.id, order_by: n.id))
  119. pre_mute_notification_id = pre_mute_notification.id
  120. post_mute_notification_id = post_mute_notification.id
  121. assert called(
  122. Push.send(
  123. :meck.is(fn
  124. %Notification{id: ^pre_mute_notification_id} -> true
  125. _ -> false
  126. end)
  127. )
  128. )
  129. refute called(
  130. Push.send(
  131. :meck.is(fn
  132. %Notification{id: ^post_mute_notification_id} -> true
  133. _ -> false
  134. end)
  135. )
  136. )
  137. end
  138. end
  139. describe "create_notification" do
  140. @tag needs_streamer: true
  141. test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
  142. user = insert(:user)
  143. task =
  144. Task.async(fn ->
  145. Streamer.get_topic_and_add_socket("user", user)
  146. assert_receive {:render_with_user, _, _, _}, 4_000
  147. end)
  148. task_user_notification =
  149. Task.async(fn ->
  150. Streamer.get_topic_and_add_socket("user:notification", user)
  151. assert_receive {:render_with_user, _, _, _}, 4_000
  152. end)
  153. activity = insert(:note_activity)
  154. notify = Notification.create_notification(activity, user)
  155. assert notify.user_id == user.id
  156. Task.await(task)
  157. Task.await(task_user_notification)
  158. end
  159. test "it creates a notification for user if the user blocks the activity author" do
  160. activity = insert(:note_activity)
  161. author = User.get_cached_by_ap_id(activity.data["actor"])
  162. user = insert(:user)
  163. {:ok, _user_relationship} = User.block(user, author)
  164. assert Notification.create_notification(activity, user)
  165. end
  166. test "it creates a notification for the user if the user mutes the activity author" do
  167. muter = insert(:user)
  168. muted = insert(:user)
  169. {:ok, _} = User.mute(muter, muted)
  170. muter = Repo.get(User, muter.id)
  171. {:ok, activity} = CommonAPI.post(muted, %{status: "Hi @#{muter.nickname}"})
  172. assert Notification.create_notification(activity, muter)
  173. end
  174. test "notification created if user is muted without notifications" do
  175. muter = insert(:user)
  176. muted = insert(:user)
  177. {:ok, _user_relationships} = User.mute(muter, muted, false)
  178. {:ok, activity} = CommonAPI.post(muted, %{status: "Hi @#{muter.nickname}"})
  179. assert Notification.create_notification(activity, muter)
  180. end
  181. test "it creates a notification for an activity from a muted thread" do
  182. muter = insert(:user)
  183. other_user = insert(:user)
  184. {:ok, activity} = CommonAPI.post(muter, %{status: "hey"})
  185. CommonAPI.add_mute(muter, activity)
  186. {:ok, activity} =
  187. CommonAPI.post(other_user, %{
  188. status: "Hi @#{muter.nickname}",
  189. in_reply_to_status_id: activity.id
  190. })
  191. assert Notification.create_notification(activity, muter)
  192. end
  193. test "it disables notifications from strangers" do
  194. follower = insert(:user)
  195. followed =
  196. insert(:user,
  197. notification_settings: %Pleroma.User.NotificationSetting{block_from_strangers: true}
  198. )
  199. {:ok, activity} = CommonAPI.post(follower, %{status: "hey @#{followed.nickname}"})
  200. refute Notification.create_notification(activity, followed)
  201. end
  202. test "it doesn't create a notification for user if he is the activity author" do
  203. activity = insert(:note_activity)
  204. author = User.get_cached_by_ap_id(activity.data["actor"])
  205. refute Notification.create_notification(activity, author)
  206. end
  207. test "it doesn't create duplicate notifications for follow+subscribed users" do
  208. user = insert(:user)
  209. subscriber = insert(:user)
  210. {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
  211. User.subscribe(subscriber, user)
  212. {:ok, status} = CommonAPI.post(user, %{status: "Akariiiin"})
  213. {:ok, [_notif]} = Notification.create_notifications(status)
  214. end
  215. test "it doesn't create subscription notifications if the recipient cannot see the status" do
  216. user = insert(:user)
  217. subscriber = insert(:user)
  218. User.subscribe(subscriber, user)
  219. {:ok, status} = CommonAPI.post(user, %{status: "inwisible", visibility: "direct"})
  220. assert {:ok, []} == Notification.create_notifications(status)
  221. end
  222. test "it disables notifications from people who are invisible" do
  223. author = insert(:user, invisible: true)
  224. user = insert(:user)
  225. {:ok, status} = CommonAPI.post(author, %{status: "hey @#{user.nickname}"})
  226. refute Notification.create_notification(status, user)
  227. end
  228. test "it doesn't create notifications if content matches with an irreversible filter" do
  229. user = insert(:user)
  230. subscriber = insert(:user)
  231. User.subscribe(subscriber, user)
  232. insert(:filter, user: subscriber, phrase: "cofe", hide: true)
  233. {:ok, status} = CommonAPI.post(user, %{status: "got cofe?"})
  234. assert {:ok, []} == Notification.create_notifications(status)
  235. end
  236. test "it creates notifications if content matches with a not irreversible filter" do
  237. user = insert(:user)
  238. subscriber = insert(:user)
  239. User.subscribe(subscriber, user)
  240. insert(:filter, user: subscriber, phrase: "cofe", hide: false)
  241. {:ok, status} = CommonAPI.post(user, %{status: "got cofe?"})
  242. {:ok, [notification]} = Notification.create_notifications(status)
  243. assert notification
  244. end
  245. test "it creates notifications when someone likes user's status with a filtered word" do
  246. user = insert(:user)
  247. other_user = insert(:user)
  248. insert(:filter, user: user, phrase: "tesla", hide: true)
  249. {:ok, activity_one} = CommonAPI.post(user, %{status: "wow tesla"})
  250. {:ok, activity_two} = CommonAPI.favorite(other_user, activity_one.id)
  251. {:ok, [notification]} = Notification.create_notifications(activity_two)
  252. assert notification
  253. end
  254. end
  255. describe "follow / follow_request notifications" do
  256. test "it creates `follow` notification for approved Follow activity" do
  257. user = insert(:user)
  258. followed_user = insert(:user, locked: false)
  259. {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
  260. assert FollowingRelationship.following?(user, followed_user)
  261. assert [notification] = Notification.for_user(followed_user)
  262. assert %{type: "follow"} =
  263. NotificationView.render("show.json", %{
  264. notification: notification,
  265. for: followed_user
  266. })
  267. end
  268. test "it creates `follow_request` notification for pending Follow activity" do
  269. user = insert(:user)
  270. followed_user = insert(:user, locked: true)
  271. {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
  272. refute FollowingRelationship.following?(user, followed_user)
  273. assert [notification] = Notification.for_user(followed_user)
  274. render_opts = %{notification: notification, for: followed_user}
  275. assert %{type: "follow_request"} = NotificationView.render("show.json", render_opts)
  276. # After request is accepted, the same notification is rendered with type "follow":
  277. assert {:ok, _} = CommonAPI.accept_follow_request(user, followed_user)
  278. notification =
  279. Repo.get(Notification, notification.id)
  280. |> Repo.preload(:activity)
  281. assert %{type: "follow"} =
  282. NotificationView.render("show.json", notification: notification, for: followed_user)
  283. end
  284. test "it doesn't create a notification for follow-unfollow-follow chains" do
  285. user = insert(:user)
  286. followed_user = insert(:user, locked: false)
  287. {:ok, _, _, _activity} = CommonAPI.follow(user, followed_user)
  288. assert FollowingRelationship.following?(user, followed_user)
  289. assert [notification] = Notification.for_user(followed_user)
  290. CommonAPI.unfollow(user, followed_user)
  291. {:ok, _, _, _activity_dupe} = CommonAPI.follow(user, followed_user)
  292. notification_id = notification.id
  293. assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
  294. end
  295. test "dismisses the notification on follow request rejection" do
  296. user = insert(:user, locked: true)
  297. follower = insert(:user)
  298. {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, user)
  299. assert [notification] = Notification.for_user(user)
  300. {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
  301. assert [] = Notification.for_user(user)
  302. end
  303. end
  304. describe "get notification" do
  305. test "it gets a notification that belongs to the user" do
  306. user = insert(:user)
  307. other_user = insert(:user)
  308. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{other_user.nickname}"})
  309. {:ok, [notification]} = Notification.create_notifications(activity)
  310. {:ok, notification} = Notification.get(other_user, notification.id)
  311. assert notification.user_id == other_user.id
  312. end
  313. test "it returns error if the notification doesn't belong to the user" do
  314. user = insert(:user)
  315. other_user = insert(:user)
  316. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{other_user.nickname}"})
  317. {:ok, [notification]} = Notification.create_notifications(activity)
  318. {:error, _notification} = Notification.get(user, notification.id)
  319. end
  320. end
  321. describe "dismiss notification" do
  322. test "it dismisses a notification that belongs to the user" do
  323. user = insert(:user)
  324. other_user = insert(:user)
  325. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{other_user.nickname}"})
  326. {:ok, [notification]} = Notification.create_notifications(activity)
  327. {:ok, notification} = Notification.dismiss(other_user, notification.id)
  328. assert notification.user_id == other_user.id
  329. end
  330. test "it returns error if the notification doesn't belong to the user" do
  331. user = insert(:user)
  332. other_user = insert(:user)
  333. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{other_user.nickname}"})
  334. {:ok, [notification]} = Notification.create_notifications(activity)
  335. {:error, _notification} = Notification.dismiss(user, notification.id)
  336. end
  337. end
  338. describe "clear notification" do
  339. test "it clears all notifications belonging to the user" do
  340. user = insert(:user)
  341. other_user = insert(:user)
  342. third_user = insert(:user)
  343. {:ok, activity} =
  344. CommonAPI.post(user, %{
  345. status: "hey @#{other_user.nickname} and @#{third_user.nickname} !"
  346. })
  347. {:ok, _notifs} = Notification.create_notifications(activity)
  348. {:ok, activity} =
  349. CommonAPI.post(user, %{
  350. status: "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
  351. })
  352. {:ok, _notifs} = Notification.create_notifications(activity)
  353. Notification.clear(other_user)
  354. assert Notification.for_user(other_user) == []
  355. assert Notification.for_user(third_user) != []
  356. end
  357. end
  358. describe "set_read_up_to()" do
  359. test "it sets all notifications as read up to a specified notification ID" do
  360. user = insert(:user)
  361. other_user = insert(:user)
  362. {:ok, _activity} =
  363. CommonAPI.post(user, %{
  364. status: "hey @#{other_user.nickname}!"
  365. })
  366. {:ok, _activity} =
  367. CommonAPI.post(user, %{
  368. status: "hey again @#{other_user.nickname}!"
  369. })
  370. [n2, n1] = Notification.for_user(other_user)
  371. assert n2.id > n1.id
  372. {:ok, _activity} =
  373. CommonAPI.post(user, %{
  374. status: "hey yet again @#{other_user.nickname}!"
  375. })
  376. [_, read_notification] = Notification.set_read_up_to(other_user, n2.id)
  377. assert read_notification.activity.object
  378. [n3, n2, n1] = Notification.for_user(other_user)
  379. assert n1.seen == true
  380. assert n2.seen == true
  381. assert n3.seen == false
  382. assert %Pleroma.Marker{} =
  383. m =
  384. Pleroma.Repo.get_by(
  385. Pleroma.Marker,
  386. user_id: other_user.id,
  387. timeline: "notifications"
  388. )
  389. assert m.last_read_id == to_string(n2.id)
  390. end
  391. end
  392. describe "for_user_since/2" do
  393. defp days_ago(days) do
  394. NaiveDateTime.add(
  395. NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
  396. -days * 60 * 60 * 24,
  397. :second
  398. )
  399. end
  400. test "Returns recent notifications" do
  401. user1 = insert(:user)
  402. user2 = insert(:user)
  403. Enum.each(0..10, fn i ->
  404. {:ok, _activity} =
  405. CommonAPI.post(user1, %{
  406. status: "hey ##{i} @#{user2.nickname}!"
  407. })
  408. end)
  409. {old, new} = Enum.split(Notification.for_user(user2), 5)
  410. Enum.each(old, fn notification ->
  411. notification
  412. |> cast(%{updated_at: days_ago(10)}, [:updated_at])
  413. |> Pleroma.Repo.update!()
  414. end)
  415. recent_notifications_ids =
  416. user2
  417. |> Notification.for_user_since(
  418. NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
  419. )
  420. |> Enum.map(& &1.id)
  421. Enum.each(old, fn %{id: id} ->
  422. refute id in recent_notifications_ids
  423. end)
  424. Enum.each(new, fn %{id: id} ->
  425. assert id in recent_notifications_ids
  426. end)
  427. end
  428. end
  429. describe "notification target determination / get_notified_from_activity/2" do
  430. test "it sends notifications to addressed users in new messages" do
  431. user = insert(:user)
  432. other_user = insert(:user)
  433. {:ok, activity} =
  434. CommonAPI.post(user, %{
  435. status: "hey @#{other_user.nickname}!"
  436. })
  437. {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
  438. assert other_user in enabled_receivers
  439. end
  440. test "it sends notifications to mentioned users in new messages" do
  441. user = insert(:user)
  442. other_user = insert(:user)
  443. create_activity = %{
  444. "@context" => "https://www.w3.org/ns/activitystreams",
  445. "type" => "Create",
  446. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  447. "actor" => user.ap_id,
  448. "object" => %{
  449. "type" => "Note",
  450. "content" => "message with a Mention tag, but no explicit tagging",
  451. "tag" => [
  452. %{
  453. "type" => "Mention",
  454. "href" => other_user.ap_id,
  455. "name" => other_user.nickname
  456. }
  457. ],
  458. "attributedTo" => user.ap_id
  459. }
  460. }
  461. {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
  462. {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
  463. assert other_user in enabled_receivers
  464. end
  465. test "it does not send notifications to users who are only cc in new messages" do
  466. user = insert(:user)
  467. other_user = insert(:user)
  468. create_activity = %{
  469. "@context" => "https://www.w3.org/ns/activitystreams",
  470. "type" => "Create",
  471. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  472. "cc" => [other_user.ap_id],
  473. "actor" => user.ap_id,
  474. "object" => %{
  475. "type" => "Note",
  476. "content" => "hi everyone",
  477. "attributedTo" => user.ap_id
  478. }
  479. }
  480. {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
  481. {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(activity)
  482. assert other_user not in enabled_receivers
  483. end
  484. test "it does not send notification to mentioned users in likes" do
  485. user = insert(:user)
  486. other_user = insert(:user)
  487. third_user = insert(:user)
  488. {:ok, activity_one} =
  489. CommonAPI.post(user, %{
  490. status: "hey @#{other_user.nickname}!"
  491. })
  492. {:ok, activity_two} = CommonAPI.favorite(third_user, activity_one.id)
  493. {enabled_receivers, _disabled_receivers} =
  494. Notification.get_notified_from_activity(activity_two)
  495. assert other_user not in enabled_receivers
  496. end
  497. test "it only notifies the post's author in likes" do
  498. user = insert(:user)
  499. other_user = insert(:user)
  500. third_user = insert(:user)
  501. {:ok, activity_one} =
  502. CommonAPI.post(user, %{
  503. status: "hey @#{other_user.nickname}!"
  504. })
  505. {:ok, like_data, _} = Builder.like(third_user, activity_one.object)
  506. {:ok, like, _} =
  507. like_data
  508. |> Map.put("to", [other_user.ap_id | like_data["to"]])
  509. |> ActivityPub.persist(local: true)
  510. {enabled_receivers, _disabled_receivers} = Notification.get_notified_from_activity(like)
  511. assert other_user not in enabled_receivers
  512. end
  513. test "it does not send notification to mentioned users in announces" do
  514. user = insert(:user)
  515. other_user = insert(:user)
  516. third_user = insert(:user)
  517. {:ok, activity_one} =
  518. CommonAPI.post(user, %{
  519. status: "hey @#{other_user.nickname}!"
  520. })
  521. {:ok, activity_two} = CommonAPI.repeat(activity_one.id, third_user)
  522. {enabled_receivers, _disabled_receivers} =
  523. Notification.get_notified_from_activity(activity_two)
  524. assert other_user not in enabled_receivers
  525. end
  526. test "it returns blocking recipient in disabled recipients list" do
  527. user = insert(:user)
  528. other_user = insert(:user)
  529. {:ok, _user_relationship} = User.block(other_user, user)
  530. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{other_user.nickname}!"})
  531. {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
  532. assert [] == enabled_receivers
  533. assert [other_user] == disabled_receivers
  534. end
  535. test "it returns notification-muting recipient in disabled recipients list" do
  536. user = insert(:user)
  537. other_user = insert(:user)
  538. {:ok, _user_relationships} = User.mute(other_user, user)
  539. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{other_user.nickname}!"})
  540. {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
  541. assert [] == enabled_receivers
  542. assert [other_user] == disabled_receivers
  543. end
  544. test "it returns thread-muting recipient in disabled recipients list" do
  545. user = insert(:user)
  546. other_user = insert(:user)
  547. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{other_user.nickname}!"})
  548. {:ok, _} = CommonAPI.add_mute(other_user, activity)
  549. {:ok, same_context_activity} =
  550. CommonAPI.post(user, %{
  551. status: "hey-hey-hey @#{other_user.nickname}!",
  552. in_reply_to_status_id: activity.id
  553. })
  554. {enabled_receivers, disabled_receivers} =
  555. Notification.get_notified_from_activity(same_context_activity)
  556. assert [other_user] == disabled_receivers
  557. refute other_user in enabled_receivers
  558. end
  559. test "it returns non-following domain-blocking recipient in disabled recipients list" do
  560. blocked_domain = "blocked.domain"
  561. user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
  562. other_user = insert(:user)
  563. {:ok, other_user} = User.block_domain(other_user, blocked_domain)
  564. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{other_user.nickname}!"})
  565. {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
  566. assert [] == enabled_receivers
  567. assert [other_user] == disabled_receivers
  568. end
  569. test "it returns following domain-blocking recipient in enabled recipients list" do
  570. blocked_domain = "blocked.domain"
  571. user = insert(:user, %{ap_id: "https://#{blocked_domain}/@actor"})
  572. other_user = insert(:user)
  573. {:ok, other_user} = User.block_domain(other_user, blocked_domain)
  574. {:ok, other_user} = User.follow(other_user, user)
  575. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{other_user.nickname}!"})
  576. {enabled_receivers, disabled_receivers} = Notification.get_notified_from_activity(activity)
  577. assert [other_user] == enabled_receivers
  578. assert [] == disabled_receivers
  579. end
  580. end
  581. describe "notification lifecycle" do
  582. test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
  583. user = insert(:user)
  584. other_user = insert(:user)
  585. {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
  586. assert Enum.empty?(Notification.for_user(user))
  587. {:ok, _} = CommonAPI.favorite(other_user, activity.id)
  588. assert length(Notification.for_user(user)) == 1
  589. {:ok, _} = CommonAPI.delete(activity.id, user)
  590. assert Enum.empty?(Notification.for_user(user))
  591. end
  592. test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
  593. user = insert(:user)
  594. other_user = insert(:user)
  595. {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
  596. assert Enum.empty?(Notification.for_user(user))
  597. {:ok, _} = CommonAPI.favorite(other_user, activity.id)
  598. assert length(Notification.for_user(user)) == 1
  599. {:ok, _} = CommonAPI.unfavorite(activity.id, other_user)
  600. assert Enum.empty?(Notification.for_user(user))
  601. end
  602. test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
  603. user = insert(:user)
  604. other_user = insert(:user)
  605. {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
  606. assert Enum.empty?(Notification.for_user(user))
  607. {:ok, _} = CommonAPI.repeat(activity.id, other_user)
  608. assert length(Notification.for_user(user)) == 1
  609. {:ok, _} = CommonAPI.delete(activity.id, user)
  610. assert Enum.empty?(Notification.for_user(user))
  611. end
  612. test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
  613. user = insert(:user)
  614. other_user = insert(:user)
  615. {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
  616. assert Enum.empty?(Notification.for_user(user))
  617. {:ok, _} = CommonAPI.repeat(activity.id, other_user)
  618. assert length(Notification.for_user(user)) == 1
  619. {:ok, _} = CommonAPI.unrepeat(activity.id, other_user)
  620. assert Enum.empty?(Notification.for_user(user))
  621. end
  622. test "liking an activity which is already deleted does not generate a notification" do
  623. user = insert(:user)
  624. other_user = insert(:user)
  625. {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
  626. assert Enum.empty?(Notification.for_user(user))
  627. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  628. assert Enum.empty?(Notification.for_user(user))
  629. {:error, :not_found} = CommonAPI.favorite(other_user, activity.id)
  630. assert Enum.empty?(Notification.for_user(user))
  631. end
  632. test "repeating an activity which is already deleted does not generate a notification" do
  633. user = insert(:user)
  634. other_user = insert(:user)
  635. {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
  636. assert Enum.empty?(Notification.for_user(user))
  637. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  638. assert Enum.empty?(Notification.for_user(user))
  639. {:error, _} = CommonAPI.repeat(activity.id, other_user)
  640. assert Enum.empty?(Notification.for_user(user))
  641. end
  642. test "replying to a deleted post without tagging does not generate a notification" do
  643. user = insert(:user)
  644. other_user = insert(:user)
  645. {:ok, activity} = CommonAPI.post(user, %{status: "test post"})
  646. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  647. {:ok, _reply_activity} =
  648. CommonAPI.post(other_user, %{
  649. status: "test reply",
  650. in_reply_to_status_id: activity.id
  651. })
  652. assert Enum.empty?(Notification.for_user(user))
  653. end
  654. test "notifications are deleted if a local user is deleted" do
  655. user = insert(:user)
  656. other_user = insert(:user)
  657. {:ok, _activity} =
  658. CommonAPI.post(user, %{status: "hi @#{other_user.nickname}", visibility: "direct"})
  659. refute Enum.empty?(Notification.for_user(other_user))
  660. {:ok, job} = User.delete(user)
  661. ObanHelpers.perform(job)
  662. assert Enum.empty?(Notification.for_user(other_user))
  663. end
  664. test "notifications are deleted if a remote user is deleted" do
  665. remote_user = insert(:user)
  666. local_user = insert(:user)
  667. dm_message = %{
  668. "@context" => "https://www.w3.org/ns/activitystreams",
  669. "type" => "Create",
  670. "actor" => remote_user.ap_id,
  671. "id" => remote_user.ap_id <> "/activities/test",
  672. "to" => [local_user.ap_id],
  673. "cc" => [],
  674. "object" => %{
  675. "type" => "Note",
  676. "content" => "Hello!",
  677. "tag" => [
  678. %{
  679. "type" => "Mention",
  680. "href" => local_user.ap_id,
  681. "name" => "@#{local_user.nickname}"
  682. }
  683. ],
  684. "to" => [local_user.ap_id],
  685. "cc" => [],
  686. "attributedTo" => remote_user.ap_id
  687. }
  688. }
  689. {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
  690. refute Enum.empty?(Notification.for_user(local_user))
  691. delete_user_message = %{
  692. "@context" => "https://www.w3.org/ns/activitystreams",
  693. "id" => remote_user.ap_id <> "/activities/delete",
  694. "actor" => remote_user.ap_id,
  695. "type" => "Delete",
  696. "object" => remote_user.ap_id
  697. }
  698. remote_user_url = remote_user.ap_id
  699. Tesla.Mock.mock(fn
  700. %{method: :get, url: ^remote_user_url} ->
  701. %Tesla.Env{status: 404, body: ""}
  702. end)
  703. {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
  704. ObanHelpers.perform_all()
  705. assert Enum.empty?(Notification.for_user(local_user))
  706. end
  707. @tag capture_log: true
  708. test "move activity generates a notification" do
  709. %{ap_id: old_ap_id} = old_user = insert(:user)
  710. %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
  711. follower = insert(:user)
  712. other_follower = insert(:user, %{allow_following_move: false})
  713. User.follow(follower, old_user)
  714. User.follow(other_follower, old_user)
  715. old_user_url = old_user.ap_id
  716. body =
  717. File.read!("test/fixtures/users_mock/localhost.json")
  718. |> String.replace("{{nickname}}", old_user.nickname)
  719. |> Jason.encode!()
  720. Tesla.Mock.mock(fn
  721. %{method: :get, url: ^old_user_url} ->
  722. %Tesla.Env{status: 200, body: body}
  723. end)
  724. Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
  725. ObanHelpers.perform_all()
  726. assert [
  727. %{
  728. activity: %{
  729. data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
  730. }
  731. }
  732. ] = Notification.for_user(follower)
  733. assert [
  734. %{
  735. activity: %{
  736. data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
  737. }
  738. }
  739. ] = Notification.for_user(other_follower)
  740. end
  741. end
  742. describe "for_user" do
  743. setup do
  744. user = insert(:user)
  745. {:ok, %{user: user}}
  746. end
  747. test "it returns notifications for muted user without notifications", %{user: user} do
  748. muted = insert(:user)
  749. {:ok, _user_relationships} = User.mute(user, muted, false)
  750. {:ok, _activity} = CommonAPI.post(muted, %{status: "hey @#{user.nickname}"})
  751. [notification] = Notification.for_user(user)
  752. assert notification.activity.object
  753. end
  754. test "it doesn't return notifications for muted user with notifications", %{user: user} do
  755. muted = insert(:user)
  756. {:ok, _user_relationships} = User.mute(user, muted)
  757. {:ok, _activity} = CommonAPI.post(muted, %{status: "hey @#{user.nickname}"})
  758. assert Notification.for_user(user) == []
  759. end
  760. test "it doesn't return notifications for blocked user", %{user: user} do
  761. blocked = insert(:user)
  762. {:ok, _user_relationship} = User.block(user, blocked)
  763. {:ok, _activity} = CommonAPI.post(blocked, %{status: "hey @#{user.nickname}"})
  764. assert Notification.for_user(user) == []
  765. end
  766. test "it doesn't return notifications for domain-blocked non-followed user", %{user: user} do
  767. blocked = insert(:user, ap_id: "http://some-domain.com")
  768. {:ok, user} = User.block_domain(user, "some-domain.com")
  769. {:ok, _activity} = CommonAPI.post(blocked, %{status: "hey @#{user.nickname}"})
  770. assert Notification.for_user(user) == []
  771. end
  772. test "it returns notifications for domain-blocked but followed user" do
  773. user = insert(:user)
  774. blocked = insert(:user, ap_id: "http://some-domain.com")
  775. {:ok, user} = User.block_domain(user, "some-domain.com")
  776. {:ok, _} = User.follow(user, blocked)
  777. {:ok, _activity} = CommonAPI.post(blocked, %{status: "hey @#{user.nickname}"})
  778. assert length(Notification.for_user(user)) == 1
  779. end
  780. test "it doesn't return notifications for muted thread", %{user: user} do
  781. another_user = insert(:user)
  782. {:ok, activity} = CommonAPI.post(another_user, %{status: "hey @#{user.nickname}"})
  783. {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
  784. assert Notification.for_user(user) == []
  785. end
  786. test "it returns notifications from a muted user when with_muted is set", %{user: user} do
  787. muted = insert(:user)
  788. {:ok, _user_relationships} = User.mute(user, muted)
  789. {:ok, _activity} = CommonAPI.post(muted, %{status: "hey @#{user.nickname}"})
  790. assert length(Notification.for_user(user, %{with_muted: true})) == 1
  791. end
  792. test "it doesn't return notifications from a blocked user when with_muted is set", %{
  793. user: user
  794. } do
  795. blocked = insert(:user)
  796. {:ok, _user_relationship} = User.block(user, blocked)
  797. {:ok, _activity} = CommonAPI.post(blocked, %{status: "hey @#{user.nickname}"})
  798. assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
  799. end
  800. test "when with_muted is set, " <>
  801. "it doesn't return notifications from a domain-blocked non-followed user",
  802. %{user: user} do
  803. blocked = insert(:user, ap_id: "http://some-domain.com")
  804. {:ok, user} = User.block_domain(user, "some-domain.com")
  805. {:ok, _activity} = CommonAPI.post(blocked, %{status: "hey @#{user.nickname}"})
  806. assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
  807. end
  808. test "it returns notifications from muted threads when with_muted is set", %{user: user} do
  809. another_user = insert(:user)
  810. {:ok, activity} = CommonAPI.post(another_user, %{status: "hey @#{user.nickname}"})
  811. {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
  812. assert length(Notification.for_user(user, %{with_muted: true})) == 1
  813. end
  814. test "it doesn't return notifications about mentions with filtered word", %{user: user} do
  815. insert(:filter, user: user, phrase: "cofe", hide: true)
  816. another_user = insert(:user)
  817. {:ok, _activity} = CommonAPI.post(another_user, %{status: "@#{user.nickname} got cofe?"})
  818. assert Enum.empty?(Notification.for_user(user))
  819. end
  820. test "it returns notifications about mentions with not hidden filtered word", %{user: user} do
  821. insert(:filter, user: user, phrase: "test", hide: false)
  822. another_user = insert(:user)
  823. {:ok, _} = CommonAPI.post(another_user, %{status: "@#{user.nickname} test"})
  824. assert length(Notification.for_user(user)) == 1
  825. end
  826. test "it returns notifications about favorites with filtered word", %{user: user} do
  827. insert(:filter, user: user, phrase: "cofe", hide: true)
  828. another_user = insert(:user)
  829. {:ok, activity} = CommonAPI.post(user, %{status: "Give me my cofe!"})
  830. {:ok, _} = CommonAPI.favorite(another_user, activity.id)
  831. assert length(Notification.for_user(user)) == 1
  832. end
  833. end
  834. end