logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

notification_test.exs (37442B)


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