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.

780 lines
25KB

  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. alias Pleroma.Notification
  8. alias Pleroma.Tests.ObanHelpers
  9. alias Pleroma.User
  10. alias Pleroma.Web.ActivityPub.Transmogrifier
  11. alias Pleroma.Web.CommonAPI
  12. alias Pleroma.Web.Streamer
  13. describe "create_notifications" do
  14. test "creates a notification for an emoji reaction" do
  15. user = insert(:user)
  16. other_user = insert(:user)
  17. {:ok, activity} = CommonAPI.post(user, %{"status" => "yeah"})
  18. {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  19. {:ok, [notification]} = Notification.create_notifications(activity)
  20. assert notification.user_id == user.id
  21. end
  22. test "notifies someone when they are directly addressed" do
  23. user = insert(:user)
  24. other_user = insert(:user)
  25. third_user = insert(:user)
  26. {:ok, activity} =
  27. CommonAPI.post(user, %{
  28. "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
  29. })
  30. {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
  31. notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
  32. assert notified_ids == [other_user.id, third_user.id]
  33. assert notification.activity_id == activity.id
  34. assert other_notification.activity_id == activity.id
  35. end
  36. test "it creates a notification for subscribed users" do
  37. user = insert(:user)
  38. subscriber = insert(:user)
  39. User.subscribe(subscriber, user)
  40. {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
  41. {:ok, [notification]} = Notification.create_notifications(status)
  42. assert notification.user_id == subscriber.id
  43. end
  44. test "does not create a notification for subscribed users if status is a reply" do
  45. user = insert(:user)
  46. other_user = insert(:user)
  47. subscriber = insert(:user)
  48. User.subscribe(subscriber, other_user)
  49. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  50. {:ok, _reply_activity} =
  51. CommonAPI.post(other_user, %{
  52. "status" => "test reply",
  53. "in_reply_to_status_id" => activity.id
  54. })
  55. user_notifications = Notification.for_user(user)
  56. assert length(user_notifications) == 1
  57. subscriber_notifications = Notification.for_user(subscriber)
  58. assert Enum.empty?(subscriber_notifications)
  59. end
  60. end
  61. describe "create_notification" do
  62. @tag needs_streamer: true
  63. test "it creates a notification for user and send to the 'user' and the 'user:notification' stream" do
  64. user = insert(:user)
  65. task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
  66. task_user_notification = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
  67. Streamer.add_socket("user", %{transport_pid: task.pid, assigns: %{user: user}})
  68. Streamer.add_socket(
  69. "user:notification",
  70. %{transport_pid: task_user_notification.pid, assigns: %{user: user}}
  71. )
  72. activity = insert(:note_activity)
  73. notify = Notification.create_notification(activity, user)
  74. assert notify.user_id == user.id
  75. Task.await(task)
  76. Task.await(task_user_notification)
  77. end
  78. test "it creates a notification for user if the user blocks the activity author" do
  79. activity = insert(:note_activity)
  80. author = User.get_cached_by_ap_id(activity.data["actor"])
  81. user = insert(:user)
  82. {:ok, _user_relationship} = User.block(user, author)
  83. assert Notification.create_notification(activity, user)
  84. end
  85. test "it creates a notification for the user if the user mutes the activity author" do
  86. muter = insert(:user)
  87. muted = insert(:user)
  88. {:ok, _} = User.mute(muter, muted)
  89. muter = Repo.get(User, muter.id)
  90. {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
  91. assert Notification.create_notification(activity, muter)
  92. end
  93. test "notification created if user is muted without notifications" do
  94. muter = insert(:user)
  95. muted = insert(:user)
  96. {:ok, _user_relationships} = User.mute(muter, muted, false)
  97. {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
  98. assert Notification.create_notification(activity, muter)
  99. end
  100. test "it creates a notification for an activity from a muted thread" do
  101. muter = insert(:user)
  102. other_user = insert(:user)
  103. {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
  104. CommonAPI.add_mute(muter, activity)
  105. {:ok, activity} =
  106. CommonAPI.post(other_user, %{
  107. "status" => "Hi @#{muter.nickname}",
  108. "in_reply_to_status_id" => activity.id
  109. })
  110. assert Notification.create_notification(activity, muter)
  111. end
  112. test "it disables notifications from followers" do
  113. follower = insert(:user)
  114. followed =
  115. insert(:user, notification_settings: %Pleroma.User.NotificationSetting{followers: false})
  116. User.follow(follower, followed)
  117. {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
  118. refute Notification.create_notification(activity, followed)
  119. end
  120. test "it disables notifications from non-followers" do
  121. follower = insert(:user)
  122. followed =
  123. insert(:user,
  124. notification_settings: %Pleroma.User.NotificationSetting{non_followers: false}
  125. )
  126. {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
  127. refute Notification.create_notification(activity, followed)
  128. end
  129. test "it disables notifications from people the user follows" do
  130. follower =
  131. insert(:user, notification_settings: %Pleroma.User.NotificationSetting{follows: false})
  132. followed = insert(:user)
  133. User.follow(follower, followed)
  134. follower = Repo.get(User, follower.id)
  135. {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
  136. refute Notification.create_notification(activity, follower)
  137. end
  138. test "it disables notifications from people the user does not follow" do
  139. follower =
  140. insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
  141. followed = insert(:user)
  142. {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
  143. refute Notification.create_notification(activity, follower)
  144. end
  145. test "it doesn't create a notification for user if he is the activity author" do
  146. activity = insert(:note_activity)
  147. author = User.get_cached_by_ap_id(activity.data["actor"])
  148. refute Notification.create_notification(activity, author)
  149. end
  150. test "it doesn't create a notification for follow-unfollow-follow chains" do
  151. user = insert(:user)
  152. followed_user = insert(:user)
  153. {:ok, _, _, activity} = CommonAPI.follow(user, followed_user)
  154. Notification.create_notification(activity, followed_user)
  155. CommonAPI.unfollow(user, followed_user)
  156. {:ok, _, _, activity_dupe} = CommonAPI.follow(user, followed_user)
  157. refute Notification.create_notification(activity_dupe, followed_user)
  158. end
  159. test "it doesn't create duplicate notifications for follow+subscribed users" do
  160. user = insert(:user)
  161. subscriber = insert(:user)
  162. {:ok, _, _, _} = CommonAPI.follow(subscriber, user)
  163. User.subscribe(subscriber, user)
  164. {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"})
  165. {:ok, [_notif]} = Notification.create_notifications(status)
  166. end
  167. test "it doesn't create subscription notifications if the recipient cannot see the status" do
  168. user = insert(:user)
  169. subscriber = insert(:user)
  170. User.subscribe(subscriber, user)
  171. {:ok, status} = CommonAPI.post(user, %{"status" => "inwisible", "visibility" => "direct"})
  172. assert {:ok, []} == Notification.create_notifications(status)
  173. end
  174. end
  175. describe "get notification" do
  176. test "it gets a notification that belongs to the user" do
  177. user = insert(:user)
  178. other_user = insert(:user)
  179. {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
  180. {:ok, [notification]} = Notification.create_notifications(activity)
  181. {:ok, notification} = Notification.get(other_user, notification.id)
  182. assert notification.user_id == other_user.id
  183. end
  184. test "it returns error if the notification doesn't belong to the user" do
  185. user = insert(:user)
  186. other_user = insert(:user)
  187. {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
  188. {:ok, [notification]} = Notification.create_notifications(activity)
  189. {:error, _notification} = Notification.get(user, notification.id)
  190. end
  191. end
  192. describe "dismiss notification" do
  193. test "it dismisses a notification that belongs to the user" do
  194. user = insert(:user)
  195. other_user = insert(:user)
  196. {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
  197. {:ok, [notification]} = Notification.create_notifications(activity)
  198. {:ok, notification} = Notification.dismiss(other_user, notification.id)
  199. assert notification.user_id == other_user.id
  200. end
  201. test "it returns error if the notification doesn't belong to the user" do
  202. user = insert(:user)
  203. other_user = insert(:user)
  204. {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}"})
  205. {:ok, [notification]} = Notification.create_notifications(activity)
  206. {:error, _notification} = Notification.dismiss(user, notification.id)
  207. end
  208. end
  209. describe "clear notification" do
  210. test "it clears all notifications belonging to the user" do
  211. user = insert(:user)
  212. other_user = insert(:user)
  213. third_user = insert(:user)
  214. {:ok, activity} =
  215. CommonAPI.post(user, %{
  216. "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
  217. })
  218. {:ok, _notifs} = Notification.create_notifications(activity)
  219. {:ok, activity} =
  220. CommonAPI.post(user, %{
  221. "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
  222. })
  223. {:ok, _notifs} = Notification.create_notifications(activity)
  224. Notification.clear(other_user)
  225. assert Notification.for_user(other_user) == []
  226. assert Notification.for_user(third_user) != []
  227. end
  228. end
  229. describe "set_read_up_to()" do
  230. test "it sets all notifications as read up to a specified notification ID" do
  231. user = insert(:user)
  232. other_user = insert(:user)
  233. {:ok, _activity} =
  234. CommonAPI.post(user, %{
  235. "status" => "hey @#{other_user.nickname}!"
  236. })
  237. {:ok, _activity} =
  238. CommonAPI.post(user, %{
  239. "status" => "hey again @#{other_user.nickname}!"
  240. })
  241. [n2, n1] = notifs = Notification.for_user(other_user)
  242. assert length(notifs) == 2
  243. assert n2.id > n1.id
  244. {:ok, _activity} =
  245. CommonAPI.post(user, %{
  246. "status" => "hey yet again @#{other_user.nickname}!"
  247. })
  248. Notification.set_read_up_to(other_user, n2.id)
  249. [n3, n2, n1] = Notification.for_user(other_user)
  250. assert n1.seen == true
  251. assert n2.seen == true
  252. assert n3.seen == false
  253. end
  254. end
  255. describe "for_user_since/2" do
  256. defp days_ago(days) do
  257. NaiveDateTime.add(
  258. NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
  259. -days * 60 * 60 * 24,
  260. :second
  261. )
  262. end
  263. test "Returns recent notifications" do
  264. user1 = insert(:user)
  265. user2 = insert(:user)
  266. Enum.each(0..10, fn i ->
  267. {:ok, _activity} =
  268. CommonAPI.post(user1, %{
  269. "status" => "hey ##{i} @#{user2.nickname}!"
  270. })
  271. end)
  272. {old, new} = Enum.split(Notification.for_user(user2), 5)
  273. Enum.each(old, fn notification ->
  274. notification
  275. |> cast(%{updated_at: days_ago(10)}, [:updated_at])
  276. |> Pleroma.Repo.update!()
  277. end)
  278. recent_notifications_ids =
  279. user2
  280. |> Notification.for_user_since(
  281. NaiveDateTime.add(NaiveDateTime.utc_now(), -5 * 86_400, :second)
  282. )
  283. |> Enum.map(& &1.id)
  284. Enum.each(old, fn %{id: id} ->
  285. refute id in recent_notifications_ids
  286. end)
  287. Enum.each(new, fn %{id: id} ->
  288. assert id in recent_notifications_ids
  289. end)
  290. end
  291. end
  292. describe "notification target determination" do
  293. test "it sends notifications to addressed users in new messages" do
  294. user = insert(:user)
  295. other_user = insert(:user)
  296. {:ok, activity} =
  297. CommonAPI.post(user, %{
  298. "status" => "hey @#{other_user.nickname}!"
  299. })
  300. assert other_user in Notification.get_notified_from_activity(activity)
  301. end
  302. test "it sends notifications to mentioned users in new messages" do
  303. user = insert(:user)
  304. other_user = insert(:user)
  305. create_activity = %{
  306. "@context" => "https://www.w3.org/ns/activitystreams",
  307. "type" => "Create",
  308. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  309. "actor" => user.ap_id,
  310. "object" => %{
  311. "type" => "Note",
  312. "content" => "message with a Mention tag, but no explicit tagging",
  313. "tag" => [
  314. %{
  315. "type" => "Mention",
  316. "href" => other_user.ap_id,
  317. "name" => other_user.nickname
  318. }
  319. ],
  320. "attributedTo" => user.ap_id
  321. }
  322. }
  323. {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
  324. assert other_user in Notification.get_notified_from_activity(activity)
  325. end
  326. test "it does not send notifications to users who are only cc in new messages" do
  327. user = insert(:user)
  328. other_user = insert(:user)
  329. create_activity = %{
  330. "@context" => "https://www.w3.org/ns/activitystreams",
  331. "type" => "Create",
  332. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  333. "cc" => [other_user.ap_id],
  334. "actor" => user.ap_id,
  335. "object" => %{
  336. "type" => "Note",
  337. "content" => "hi everyone",
  338. "attributedTo" => user.ap_id
  339. }
  340. }
  341. {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
  342. assert other_user not in Notification.get_notified_from_activity(activity)
  343. end
  344. test "it does not send notification to mentioned users in likes" do
  345. user = insert(:user)
  346. other_user = insert(:user)
  347. third_user = insert(:user)
  348. {:ok, activity_one} =
  349. CommonAPI.post(user, %{
  350. "status" => "hey @#{other_user.nickname}!"
  351. })
  352. {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
  353. assert other_user not in Notification.get_notified_from_activity(activity_two)
  354. end
  355. test "it does not send notification to mentioned users in announces" do
  356. user = insert(:user)
  357. other_user = insert(:user)
  358. third_user = insert(:user)
  359. {:ok, activity_one} =
  360. CommonAPI.post(user, %{
  361. "status" => "hey @#{other_user.nickname}!"
  362. })
  363. {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
  364. assert other_user not in Notification.get_notified_from_activity(activity_two)
  365. end
  366. end
  367. describe "notification lifecycle" do
  368. test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
  369. user = insert(:user)
  370. other_user = insert(:user)
  371. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  372. assert Enum.empty?(Notification.for_user(user))
  373. {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
  374. assert length(Notification.for_user(user)) == 1
  375. {:ok, _} = CommonAPI.delete(activity.id, user)
  376. assert Enum.empty?(Notification.for_user(user))
  377. end
  378. test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
  379. user = insert(:user)
  380. other_user = insert(:user)
  381. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  382. assert Enum.empty?(Notification.for_user(user))
  383. {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
  384. assert length(Notification.for_user(user)) == 1
  385. {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
  386. assert Enum.empty?(Notification.for_user(user))
  387. end
  388. test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
  389. user = insert(:user)
  390. other_user = insert(:user)
  391. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  392. assert Enum.empty?(Notification.for_user(user))
  393. {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
  394. assert length(Notification.for_user(user)) == 1
  395. {:ok, _} = CommonAPI.delete(activity.id, user)
  396. assert Enum.empty?(Notification.for_user(user))
  397. end
  398. test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
  399. user = insert(:user)
  400. other_user = insert(:user)
  401. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  402. assert Enum.empty?(Notification.for_user(user))
  403. {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
  404. assert length(Notification.for_user(user)) == 1
  405. {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
  406. assert Enum.empty?(Notification.for_user(user))
  407. end
  408. test "liking an activity which is already deleted does not generate a notification" do
  409. user = insert(:user)
  410. other_user = insert(:user)
  411. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  412. assert Enum.empty?(Notification.for_user(user))
  413. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  414. assert Enum.empty?(Notification.for_user(user))
  415. {:error, _} = CommonAPI.favorite(activity.id, other_user)
  416. assert Enum.empty?(Notification.for_user(user))
  417. end
  418. test "repeating an activity which is already deleted does not generate a notification" do
  419. user = insert(:user)
  420. other_user = insert(:user)
  421. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  422. assert Enum.empty?(Notification.for_user(user))
  423. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  424. assert Enum.empty?(Notification.for_user(user))
  425. {:error, _} = CommonAPI.repeat(activity.id, other_user)
  426. assert Enum.empty?(Notification.for_user(user))
  427. end
  428. test "replying to a deleted post without tagging does not generate a notification" do
  429. user = insert(:user)
  430. other_user = insert(:user)
  431. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  432. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  433. {:ok, _reply_activity} =
  434. CommonAPI.post(other_user, %{
  435. "status" => "test reply",
  436. "in_reply_to_status_id" => activity.id
  437. })
  438. assert Enum.empty?(Notification.for_user(user))
  439. end
  440. test "notifications are deleted if a local user is deleted" do
  441. user = insert(:user)
  442. other_user = insert(:user)
  443. {:ok, _activity} =
  444. CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}", "visibility" => "direct"})
  445. refute Enum.empty?(Notification.for_user(other_user))
  446. {:ok, job} = User.delete(user)
  447. ObanHelpers.perform(job)
  448. assert Enum.empty?(Notification.for_user(other_user))
  449. end
  450. test "notifications are deleted if a remote user is deleted" do
  451. remote_user = insert(:user)
  452. local_user = insert(:user)
  453. dm_message = %{
  454. "@context" => "https://www.w3.org/ns/activitystreams",
  455. "type" => "Create",
  456. "actor" => remote_user.ap_id,
  457. "id" => remote_user.ap_id <> "/activities/test",
  458. "to" => [local_user.ap_id],
  459. "cc" => [],
  460. "object" => %{
  461. "type" => "Note",
  462. "content" => "Hello!",
  463. "tag" => [
  464. %{
  465. "type" => "Mention",
  466. "href" => local_user.ap_id,
  467. "name" => "@#{local_user.nickname}"
  468. }
  469. ],
  470. "to" => [local_user.ap_id],
  471. "cc" => [],
  472. "attributedTo" => remote_user.ap_id
  473. }
  474. }
  475. {:ok, _dm_activity} = Transmogrifier.handle_incoming(dm_message)
  476. refute Enum.empty?(Notification.for_user(local_user))
  477. delete_user_message = %{
  478. "@context" => "https://www.w3.org/ns/activitystreams",
  479. "id" => remote_user.ap_id <> "/activities/delete",
  480. "actor" => remote_user.ap_id,
  481. "type" => "Delete",
  482. "object" => remote_user.ap_id
  483. }
  484. {:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
  485. ObanHelpers.perform_all()
  486. assert Enum.empty?(Notification.for_user(local_user))
  487. end
  488. test "move activity generates a notification" do
  489. %{ap_id: old_ap_id} = old_user = insert(:user)
  490. %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
  491. follower = insert(:user)
  492. other_follower = insert(:user, %{allow_following_move: false})
  493. User.follow(follower, old_user)
  494. User.follow(other_follower, old_user)
  495. Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
  496. ObanHelpers.perform_all()
  497. assert [
  498. %{
  499. activity: %{
  500. data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
  501. }
  502. }
  503. ] = Notification.for_user(follower)
  504. assert [
  505. %{
  506. activity: %{
  507. data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
  508. }
  509. }
  510. ] = Notification.for_user(other_follower)
  511. end
  512. end
  513. describe "for_user" do
  514. test "it returns notifications for muted user without notifications" do
  515. user = insert(:user)
  516. muted = insert(:user)
  517. {:ok, _user_relationships} = User.mute(user, muted, false)
  518. {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
  519. assert length(Notification.for_user(user)) == 1
  520. end
  521. test "it doesn't return notifications for muted user with notifications" do
  522. user = insert(:user)
  523. muted = insert(:user)
  524. {:ok, _user_relationships} = User.mute(user, muted)
  525. {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
  526. assert Notification.for_user(user) == []
  527. end
  528. test "it doesn't return notifications for blocked user" do
  529. user = insert(:user)
  530. blocked = insert(:user)
  531. {:ok, _user_relationship} = User.block(user, blocked)
  532. {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
  533. assert Notification.for_user(user) == []
  534. end
  535. test "it doesn't return notificatitons for blocked domain" do
  536. user = insert(:user)
  537. blocked = insert(:user, ap_id: "http://some-domain.com")
  538. {:ok, user} = User.block_domain(user, "some-domain.com")
  539. {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
  540. assert Notification.for_user(user) == []
  541. end
  542. test "it doesn't return notifications for muted thread" do
  543. user = insert(:user)
  544. another_user = insert(:user)
  545. {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
  546. {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
  547. assert Notification.for_user(user) == []
  548. end
  549. test "it returns notifications from a muted user when with_muted is set" do
  550. user = insert(:user)
  551. muted = insert(:user)
  552. {:ok, _user_relationships} = User.mute(user, muted)
  553. {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"})
  554. assert length(Notification.for_user(user, %{with_muted: true})) == 1
  555. end
  556. test "it doesn't return notifications from a blocked user when with_muted is set" do
  557. user = insert(:user)
  558. blocked = insert(:user)
  559. {:ok, _user_relationship} = User.block(user, blocked)
  560. {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
  561. assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
  562. end
  563. test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
  564. user = insert(:user)
  565. blocked = insert(:user, ap_id: "http://some-domain.com")
  566. {:ok, user} = User.block_domain(user, "some-domain.com")
  567. {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
  568. assert Enum.empty?(Notification.for_user(user, %{with_muted: true}))
  569. end
  570. test "it returns notifications from muted threads when with_muted is set" do
  571. user = insert(:user)
  572. another_user = insert(:user)
  573. {:ok, activity} = CommonAPI.post(another_user, %{"status" => "hey @#{user.nickname}"})
  574. {:ok, _} = Pleroma.ThreadMute.add_mute(user.id, activity.data["context"])
  575. assert length(Notification.for_user(user, %{with_muted: true})) == 1
  576. end
  577. end
  578. end