Local copy of Pleroma, an ActivityPub server software. Contains modifications running live on fedi.underscore.world
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.

notification_test.exs 25KB

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