Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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