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.

374 lines
12KB

  1. defmodule Pleroma.NotificationTest do
  2. use Pleroma.DataCase
  3. alias Pleroma.Web.TwitterAPI.TwitterAPI
  4. alias Pleroma.Web.CommonAPI
  5. alias Pleroma.{User, Notification}
  6. alias Pleroma.Web.ActivityPub.Transmogrifier
  7. import Pleroma.Factory
  8. describe "create_notifications" do
  9. test "notifies someone when they are directly addressed" do
  10. user = insert(:user)
  11. other_user = insert(:user)
  12. third_user = insert(:user)
  13. {:ok, activity} =
  14. TwitterAPI.create_status(user, %{
  15. "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
  16. })
  17. {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
  18. notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
  19. assert notified_ids == [other_user.id, third_user.id]
  20. assert notification.activity_id == activity.id
  21. assert other_notification.activity_id == activity.id
  22. end
  23. end
  24. describe "create_notification" do
  25. test "it doesn't create a notification for user if the user blocks the activity author" do
  26. activity = insert(:note_activity)
  27. author = User.get_by_ap_id(activity.data["actor"])
  28. user = insert(:user)
  29. {:ok, user} = User.block(user, author)
  30. assert nil == Notification.create_notification(activity, user)
  31. end
  32. test "it doesn't create a notification for user if he is the activity author" do
  33. activity = insert(:note_activity)
  34. author = User.get_by_ap_id(activity.data["actor"])
  35. assert nil == Notification.create_notification(activity, author)
  36. end
  37. end
  38. describe "get notification" do
  39. test "it gets a notification that belongs to the user" do
  40. user = insert(:user)
  41. other_user = insert(:user)
  42. {:ok, activity} =
  43. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  44. {:ok, [notification]} = Notification.create_notifications(activity)
  45. {:ok, notification} = Notification.get(other_user, notification.id)
  46. assert notification.user_id == other_user.id
  47. end
  48. test "it returns error if the notification doesn't belong to the user" do
  49. user = insert(:user)
  50. other_user = insert(:user)
  51. {:ok, activity} =
  52. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  53. {:ok, [notification]} = Notification.create_notifications(activity)
  54. {:error, _notification} = Notification.get(user, notification.id)
  55. end
  56. end
  57. describe "dismiss notification" do
  58. test "it dismisses a notification that belongs to the user" do
  59. user = insert(:user)
  60. other_user = insert(:user)
  61. {:ok, activity} =
  62. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  63. {:ok, [notification]} = Notification.create_notifications(activity)
  64. {:ok, notification} = Notification.dismiss(other_user, notification.id)
  65. assert notification.user_id == other_user.id
  66. end
  67. test "it returns error if the notification doesn't belong to the user" do
  68. user = insert(:user)
  69. other_user = insert(:user)
  70. {:ok, activity} =
  71. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  72. {:ok, [notification]} = Notification.create_notifications(activity)
  73. {:error, _notification} = Notification.dismiss(user, notification.id)
  74. end
  75. end
  76. describe "clear notification" do
  77. test "it clears all notifications belonging to the user" do
  78. user = insert(:user)
  79. other_user = insert(:user)
  80. third_user = insert(:user)
  81. {:ok, activity} =
  82. TwitterAPI.create_status(user, %{
  83. "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
  84. })
  85. {:ok, _notifs} = Notification.create_notifications(activity)
  86. {:ok, activity} =
  87. TwitterAPI.create_status(user, %{
  88. "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
  89. })
  90. {:ok, _notifs} = Notification.create_notifications(activity)
  91. Notification.clear(other_user)
  92. assert Notification.for_user(other_user) == []
  93. assert Notification.for_user(third_user) != []
  94. end
  95. end
  96. describe "set_read_up_to()" do
  97. test "it sets all notifications as read up to a specified notification ID" do
  98. user = insert(:user)
  99. other_user = insert(:user)
  100. {:ok, activity} =
  101. TwitterAPI.create_status(user, %{
  102. "status" => "hey @#{other_user.nickname}!"
  103. })
  104. {:ok, activity} =
  105. TwitterAPI.create_status(user, %{
  106. "status" => "hey again @#{other_user.nickname}!"
  107. })
  108. [n2, n1] = notifs = Notification.for_user(other_user)
  109. assert length(notifs) == 2
  110. assert n2.id > n1.id
  111. {:ok, activity} =
  112. TwitterAPI.create_status(user, %{
  113. "status" => "hey yet again @#{other_user.nickname}!"
  114. })
  115. Notification.set_read_up_to(other_user, n2.id)
  116. [n3, n2, n1] = notifs = Notification.for_user(other_user)
  117. assert n1.seen == true
  118. assert n2.seen == true
  119. assert n3.seen == false
  120. end
  121. end
  122. describe "notification target determination" do
  123. test "it sends notifications to addressed users in new messages" do
  124. user = insert(:user)
  125. other_user = insert(:user)
  126. {:ok, activity} =
  127. CommonAPI.post(user, %{
  128. "status" => "hey @#{other_user.nickname}!"
  129. })
  130. assert other_user in Notification.get_notified_from_activity(activity)
  131. end
  132. test "it sends notifications to mentioned users in new messages" do
  133. user = insert(:user)
  134. other_user = insert(:user)
  135. create_activity = %{
  136. "@context" => "https://www.w3.org/ns/activitystreams",
  137. "type" => "Create",
  138. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  139. "actor" => user.ap_id,
  140. "object" => %{
  141. "type" => "Note",
  142. "content" => "message with a Mention tag, but no explicit tagging",
  143. "tag" => [
  144. %{
  145. "type" => "Mention",
  146. "href" => other_user.ap_id,
  147. "name" => other_user.nickname
  148. }
  149. ],
  150. "attributedTo" => user.ap_id
  151. }
  152. }
  153. {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
  154. assert other_user in Notification.get_notified_from_activity(activity)
  155. end
  156. test "it does not send notifications to users who are only cc in new messages" do
  157. user = insert(:user)
  158. other_user = insert(:user)
  159. create_activity = %{
  160. "@context" => "https://www.w3.org/ns/activitystreams",
  161. "type" => "Create",
  162. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  163. "cc" => [other_user.ap_id],
  164. "actor" => user.ap_id,
  165. "object" => %{
  166. "type" => "Note",
  167. "content" => "hi everyone",
  168. "attributedTo" => user.ap_id
  169. }
  170. }
  171. {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
  172. assert other_user not in Notification.get_notified_from_activity(activity)
  173. end
  174. test "it does not send notification to mentioned users in likes" do
  175. user = insert(:user)
  176. other_user = insert(:user)
  177. third_user = insert(:user)
  178. {:ok, activity_one} =
  179. CommonAPI.post(user, %{
  180. "status" => "hey @#{other_user.nickname}!"
  181. })
  182. {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
  183. assert other_user not in Notification.get_notified_from_activity(activity_two)
  184. end
  185. test "it does not send notification to mentioned users in announces" do
  186. user = insert(:user)
  187. other_user = insert(:user)
  188. third_user = insert(:user)
  189. {:ok, activity_one} =
  190. CommonAPI.post(user, %{
  191. "status" => "hey @#{other_user.nickname}!"
  192. })
  193. {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
  194. assert other_user not in Notification.get_notified_from_activity(activity_two)
  195. end
  196. end
  197. describe "notification lifecycle" do
  198. test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
  199. user = insert(:user)
  200. other_user = insert(:user)
  201. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  202. assert length(Notification.for_user(user)) == 0
  203. {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
  204. assert length(Notification.for_user(user)) == 1
  205. {:ok, _} = CommonAPI.delete(activity.id, user)
  206. assert length(Notification.for_user(user)) == 0
  207. end
  208. test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
  209. user = insert(:user)
  210. other_user = insert(:user)
  211. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  212. assert length(Notification.for_user(user)) == 0
  213. {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
  214. assert length(Notification.for_user(user)) == 1
  215. {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
  216. assert length(Notification.for_user(user)) == 0
  217. end
  218. test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
  219. user = insert(:user)
  220. other_user = insert(:user)
  221. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  222. assert length(Notification.for_user(user)) == 0
  223. {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
  224. assert length(Notification.for_user(user)) == 1
  225. {:ok, _} = CommonAPI.delete(activity.id, user)
  226. assert length(Notification.for_user(user)) == 0
  227. end
  228. test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
  229. user = insert(:user)
  230. other_user = insert(:user)
  231. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  232. assert length(Notification.for_user(user)) == 0
  233. {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
  234. assert length(Notification.for_user(user)) == 1
  235. {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
  236. assert length(Notification.for_user(user)) == 0
  237. end
  238. test "liking an activity which is already deleted does not generate a notification" do
  239. user = insert(:user)
  240. other_user = insert(:user)
  241. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  242. assert length(Notification.for_user(user)) == 0
  243. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  244. assert length(Notification.for_user(user)) == 0
  245. {:error, _} = CommonAPI.favorite(activity.id, other_user)
  246. assert length(Notification.for_user(user)) == 0
  247. end
  248. test "repeating an activity which is already deleted does not generate a notification" do
  249. user = insert(:user)
  250. other_user = insert(:user)
  251. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  252. assert length(Notification.for_user(user)) == 0
  253. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  254. assert length(Notification.for_user(user)) == 0
  255. {:error, _} = CommonAPI.repeat(activity.id, other_user)
  256. assert length(Notification.for_user(user)) == 0
  257. end
  258. test "replying to a deleted post without tagging does not generate a notification" do
  259. user = insert(:user)
  260. other_user = insert(:user)
  261. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  262. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  263. {:ok, _reply_activity} =
  264. CommonAPI.post(other_user, %{
  265. "status" => "test reply",
  266. "in_reply_to_status_id" => activity.id
  267. })
  268. assert length(Notification.for_user(user)) == 0
  269. end
  270. end
  271. end