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.

1143 lines
37KB

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