logo

pleroma

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

notification_view_test.exs (11584B)


  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.Web.MastodonAPI.NotificationViewTest do
  5. use Pleroma.DataCase, async: false
  6. alias Pleroma.Activity
  7. alias Pleroma.Chat
  8. alias Pleroma.Chat.MessageReference
  9. alias Pleroma.Notification
  10. alias Pleroma.Object
  11. alias Pleroma.Repo
  12. alias Pleroma.User
  13. alias Pleroma.Web.AdminAPI.Report
  14. alias Pleroma.Web.AdminAPI.ReportView
  15. alias Pleroma.Web.CommonAPI
  16. alias Pleroma.Web.CommonAPI.Utils
  17. alias Pleroma.Web.MastodonAPI.AccountView
  18. alias Pleroma.Web.MastodonAPI.NotificationView
  19. alias Pleroma.Web.MastodonAPI.StatusView
  20. alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
  21. import Pleroma.Factory
  22. setup do
  23. Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config)
  24. :ok
  25. end
  26. defp test_notifications_rendering(notifications, user, expected_result) do
  27. result = NotificationView.render("index.json", %{notifications: notifications, for: user})
  28. assert expected_result == result
  29. result =
  30. NotificationView.render("index.json", %{
  31. notifications: notifications,
  32. for: user,
  33. relationships: nil
  34. })
  35. assert expected_result == result
  36. end
  37. test "ChatMessage notification" do
  38. user = insert(:user)
  39. recipient = insert(:user)
  40. {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
  41. {:ok, [notification]} = Notification.create_notifications(activity)
  42. object = Object.normalize(activity, fetch: false)
  43. chat = Chat.get(recipient.id, user.ap_id)
  44. cm_ref = MessageReference.for_chat_and_object(chat, object)
  45. expected = %{
  46. id: to_string(notification.id),
  47. pleroma: %{is_seen: false, is_muted: false},
  48. type: "pleroma:chat_mention",
  49. account: AccountView.render("show.json", %{user: user, for: recipient}),
  50. chat_message: MessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
  51. created_at: Utils.to_masto_date(notification.inserted_at)
  52. }
  53. test_notifications_rendering([notification], recipient, [expected])
  54. end
  55. test "Mention notification" do
  56. user = insert(:user)
  57. mentioned_user = insert(:user)
  58. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
  59. {:ok, [notification]} = Notification.create_notifications(activity)
  60. user = User.get_cached_by_id(user.id)
  61. expected = %{
  62. id: to_string(notification.id),
  63. pleroma: %{is_seen: false, is_muted: false},
  64. type: "mention",
  65. account:
  66. AccountView.render("show.json", %{
  67. user: user,
  68. for: mentioned_user
  69. }),
  70. status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
  71. created_at: Utils.to_masto_date(notification.inserted_at)
  72. }
  73. test_notifications_rendering([notification], mentioned_user, [expected])
  74. end
  75. test "Favourite notification" do
  76. user = insert(:user)
  77. another_user = insert(:user)
  78. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  79. {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
  80. {:ok, [notification]} = Notification.create_notifications(favorite_activity)
  81. create_activity = Activity.get_by_id(create_activity.id)
  82. expected = %{
  83. id: to_string(notification.id),
  84. pleroma: %{is_seen: false, is_muted: false},
  85. type: "favourite",
  86. account: AccountView.render("show.json", %{user: another_user, for: user}),
  87. status: StatusView.render("show.json", %{activity: create_activity, for: user}),
  88. created_at: Utils.to_masto_date(notification.inserted_at)
  89. }
  90. test_notifications_rendering([notification], user, [expected])
  91. end
  92. test "Reblog notification" do
  93. user = insert(:user)
  94. another_user = insert(:user)
  95. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  96. {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
  97. {:ok, [notification]} = Notification.create_notifications(reblog_activity)
  98. reblog_activity = Activity.get_by_id(create_activity.id)
  99. expected = %{
  100. id: to_string(notification.id),
  101. pleroma: %{is_seen: false, is_muted: false},
  102. type: "reblog",
  103. account: AccountView.render("show.json", %{user: another_user, for: user}),
  104. status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
  105. created_at: Utils.to_masto_date(notification.inserted_at)
  106. }
  107. test_notifications_rendering([notification], user, [expected])
  108. end
  109. test "Follow notification" do
  110. follower = insert(:user)
  111. followed = insert(:user)
  112. {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
  113. notification = Notification |> Repo.one() |> Repo.preload(:activity)
  114. expected = %{
  115. id: to_string(notification.id),
  116. pleroma: %{is_seen: false, is_muted: false},
  117. type: "follow",
  118. account: AccountView.render("show.json", %{user: follower, for: followed}),
  119. created_at: Utils.to_masto_date(notification.inserted_at)
  120. }
  121. test_notifications_rendering([notification], followed, [expected])
  122. User.perform(:delete, follower)
  123. refute Repo.one(Notification)
  124. end
  125. test "Move notification" do
  126. old_user = insert(:user)
  127. new_user = insert(:user, also_known_as: [old_user.ap_id])
  128. follower = insert(:user)
  129. User.follow(follower, old_user)
  130. Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
  131. Pleroma.Tests.ObanHelpers.perform_all()
  132. old_user = refresh_record(old_user)
  133. new_user = refresh_record(new_user)
  134. [notification] = Notification.for_user(follower)
  135. expected = %{
  136. id: to_string(notification.id),
  137. pleroma: %{is_seen: false, is_muted: false},
  138. type: "move",
  139. account: AccountView.render("show.json", %{user: old_user, for: follower}),
  140. target: AccountView.render("show.json", %{user: new_user, for: follower}),
  141. created_at: Utils.to_masto_date(notification.inserted_at)
  142. }
  143. test_notifications_rendering([notification], follower, [expected])
  144. end
  145. test "EmojiReact notification" do
  146. user = insert(:user)
  147. other_user = insert(:user)
  148. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  149. {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  150. activity = Repo.get(Activity, activity.id)
  151. [notification] = Notification.for_user(user)
  152. assert notification
  153. expected = %{
  154. id: to_string(notification.id),
  155. pleroma: %{is_seen: false, is_muted: false},
  156. type: "pleroma:emoji_reaction",
  157. emoji: "☕",
  158. account: AccountView.render("show.json", %{user: other_user, for: user}),
  159. status: StatusView.render("show.json", %{activity: activity, for: user}),
  160. created_at: Utils.to_masto_date(notification.inserted_at),
  161. emoji_url: nil
  162. }
  163. test_notifications_rendering([notification], user, [expected])
  164. end
  165. test "EmojiReact custom emoji notification" do
  166. user = insert(:user)
  167. other_user = insert(:user)
  168. note =
  169. insert(:note,
  170. user: user,
  171. data: %{
  172. "reactions" => [
  173. ["👍", [user.ap_id], nil],
  174. ["dinosaur", [user.ap_id], "http://localhost:4001/emoji/dino walking.gif"]
  175. ]
  176. }
  177. )
  178. activity = insert(:note_activity, note: note, user: user)
  179. {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "dinosaur")
  180. activity = Repo.get(Activity, activity.id)
  181. [notification] = Notification.for_user(user)
  182. assert notification
  183. expected = %{
  184. id: to_string(notification.id),
  185. pleroma: %{is_seen: false, is_muted: false},
  186. type: "pleroma:emoji_reaction",
  187. emoji: ":dinosaur:",
  188. account: AccountView.render("show.json", %{user: other_user, for: user}),
  189. status: StatusView.render("show.json", %{activity: activity, for: user}),
  190. created_at: Utils.to_masto_date(notification.inserted_at),
  191. emoji_url: "http://localhost:4001/emoji/dino walking.gif"
  192. }
  193. test_notifications_rendering([notification], user, [expected])
  194. end
  195. test "Poll notification" do
  196. user = insert(:user)
  197. activity = insert(:question_activity, user: user)
  198. {:ok, [notification]} = Notification.create_poll_notifications(activity)
  199. expected = %{
  200. id: to_string(notification.id),
  201. pleroma: %{is_seen: false, is_muted: false},
  202. type: "poll",
  203. account:
  204. AccountView.render("show.json", %{
  205. user: user,
  206. for: user
  207. }),
  208. status: StatusView.render("show.json", %{activity: activity, for: user}),
  209. created_at: Utils.to_masto_date(notification.inserted_at)
  210. }
  211. test_notifications_rendering([notification], user, [expected])
  212. end
  213. test "Report notification" do
  214. clear_config([:instance, :moderator_privileges], [:reports_manage_reports])
  215. reporting_user = insert(:user)
  216. reported_user = insert(:user)
  217. moderator_user = insert(:user, is_moderator: true)
  218. {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
  219. {:ok, [notification]} = Notification.create_notifications(activity)
  220. expected = %{
  221. id: to_string(notification.id),
  222. pleroma: %{is_seen: false, is_muted: false},
  223. type: "pleroma:report",
  224. account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
  225. created_at: Utils.to_masto_date(notification.inserted_at),
  226. report: ReportView.render("show.json", Report.extract_report_info(activity))
  227. }
  228. test_notifications_rendering([notification], moderator_user, [expected])
  229. end
  230. test "Edit notification" do
  231. user = insert(:user)
  232. repeat_user = insert(:user)
  233. {:ok, activity} = CommonAPI.post(user, %{status: "mew"})
  234. {:ok, _} = CommonAPI.repeat(activity.id, repeat_user)
  235. {:ok, update} = CommonAPI.update(user, activity, %{status: "mew mew"})
  236. user = Pleroma.User.get_by_ap_id(user.ap_id)
  237. activity = Pleroma.Activity.normalize(activity)
  238. update = Pleroma.Activity.normalize(update)
  239. {:ok, [notification]} = Notification.create_notifications(update)
  240. expected = %{
  241. id: to_string(notification.id),
  242. pleroma: %{is_seen: false, is_muted: false},
  243. type: "update",
  244. account: AccountView.render("show.json", %{user: user, for: repeat_user}),
  245. created_at: Utils.to_masto_date(notification.inserted_at),
  246. status: StatusView.render("show.json", %{activity: activity, for: repeat_user})
  247. }
  248. test_notifications_rendering([notification], repeat_user, [expected])
  249. end
  250. test "muted notification" do
  251. user = insert(:user)
  252. another_user = insert(:user)
  253. {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
  254. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  255. {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
  256. {:ok, [notification]} = Notification.create_notifications(favorite_activity)
  257. create_activity = Activity.get_by_id(create_activity.id)
  258. expected = %{
  259. id: to_string(notification.id),
  260. pleroma: %{is_seen: true, is_muted: true},
  261. type: "favourite",
  262. account: AccountView.render("show.json", %{user: another_user, for: user}),
  263. status: StatusView.render("show.json", %{activity: create_activity, for: user}),
  264. created_at: Utils.to_masto_date(notification.inserted_at)
  265. }
  266. test_notifications_rendering([notification], user, [expected])
  267. end
  268. end