logo

pleroma

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

notification_view_test.exs (13173B)


  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.Test.StaticConfig)
  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. group_key: "ungrouped-#{to_string(notification.id)}",
  48. pleroma: %{is_seen: false, is_muted: false},
  49. type: "pleroma:chat_mention",
  50. account: AccountView.render("show.json", %{user: user, for: recipient}),
  51. chat_message: MessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
  52. created_at: Utils.to_masto_date(notification.inserted_at)
  53. }
  54. test_notifications_rendering([notification], recipient, [expected])
  55. end
  56. test "Mention notification" do
  57. user = insert(:user)
  58. mentioned_user = insert(:user)
  59. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
  60. {:ok, [notification]} = Notification.create_notifications(activity)
  61. user = User.get_cached_by_id(user.id)
  62. expected = %{
  63. id: to_string(notification.id),
  64. group_key: "ungrouped-#{to_string(notification.id)}",
  65. pleroma: %{is_seen: false, is_muted: false},
  66. type: "mention",
  67. account:
  68. AccountView.render("show.json", %{
  69. user: user,
  70. for: mentioned_user
  71. }),
  72. status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
  73. created_at: Utils.to_masto_date(notification.inserted_at)
  74. }
  75. test_notifications_rendering([notification], mentioned_user, [expected])
  76. end
  77. test "Favourite notification" do
  78. user = insert(:user)
  79. another_user = insert(:user)
  80. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  81. {:ok, favorite_activity} = CommonAPI.favorite(create_activity.id, another_user)
  82. {:ok, [notification]} = Notification.create_notifications(favorite_activity)
  83. create_activity = Activity.get_by_id(create_activity.id)
  84. expected = %{
  85. id: to_string(notification.id),
  86. group_key: "ungrouped-#{to_string(notification.id)}",
  87. pleroma: %{is_seen: false, is_muted: false},
  88. type: "favourite",
  89. account: AccountView.render("show.json", %{user: another_user, for: user}),
  90. status: StatusView.render("show.json", %{activity: create_activity, for: user}),
  91. created_at: Utils.to_masto_date(notification.inserted_at)
  92. }
  93. test_notifications_rendering([notification], user, [expected])
  94. end
  95. test "Reblog notification" do
  96. user = insert(:user)
  97. another_user = insert(:user)
  98. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  99. {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
  100. {:ok, [notification]} = Notification.create_notifications(reblog_activity)
  101. reblog_activity = Activity.get_by_id(create_activity.id)
  102. expected = %{
  103. id: to_string(notification.id),
  104. group_key: "ungrouped-#{to_string(notification.id)}",
  105. pleroma: %{is_seen: false, is_muted: false},
  106. type: "reblog",
  107. account: AccountView.render("show.json", %{user: another_user, for: user}),
  108. status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
  109. created_at: Utils.to_masto_date(notification.inserted_at)
  110. }
  111. test_notifications_rendering([notification], user, [expected])
  112. end
  113. test "Follow notification" do
  114. follower = insert(:user)
  115. followed = insert(:user)
  116. {:ok, followed, follower, _activity} = CommonAPI.follow(followed, follower)
  117. notification = Notification |> Repo.one() |> Repo.preload(:activity)
  118. expected = %{
  119. id: to_string(notification.id),
  120. group_key: "ungrouped-#{to_string(notification.id)}",
  121. pleroma: %{is_seen: false, is_muted: false},
  122. type: "follow",
  123. account: AccountView.render("show.json", %{user: follower, for: followed}),
  124. created_at: Utils.to_masto_date(notification.inserted_at)
  125. }
  126. test_notifications_rendering([notification], followed, [expected])
  127. User.perform(:delete, follower)
  128. refute Repo.one(Notification)
  129. end
  130. test "Move notification" do
  131. old_user = insert(:user)
  132. new_user = insert(:user, also_known_as: [old_user.ap_id])
  133. follower = insert(:user)
  134. User.follow(follower, old_user)
  135. Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
  136. Pleroma.Tests.ObanHelpers.perform_all()
  137. old_user = refresh_record(old_user)
  138. new_user = refresh_record(new_user)
  139. [notification] = Notification.for_user(follower)
  140. expected = %{
  141. id: to_string(notification.id),
  142. group_key: "ungrouped-#{to_string(notification.id)}",
  143. pleroma: %{is_seen: false, is_muted: false},
  144. type: "move",
  145. account: AccountView.render("show.json", %{user: old_user, for: follower}),
  146. target: AccountView.render("show.json", %{user: new_user, for: follower}),
  147. created_at: Utils.to_masto_date(notification.inserted_at)
  148. }
  149. test_notifications_rendering([notification], follower, [expected])
  150. end
  151. test "EmojiReact notification" do
  152. user = insert(:user)
  153. other_user = insert(:user)
  154. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  155. {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  156. activity = Repo.get(Activity, activity.id)
  157. [notification] = Notification.for_user(user)
  158. assert notification
  159. expected = %{
  160. id: to_string(notification.id),
  161. group_key: "ungrouped-#{to_string(notification.id)}",
  162. pleroma: %{is_seen: false, is_muted: false},
  163. type: "pleroma:emoji_reaction",
  164. emoji: "☕",
  165. account: AccountView.render("show.json", %{user: other_user, for: user}),
  166. status: StatusView.render("show.json", %{activity: activity, for: user}),
  167. created_at: Utils.to_masto_date(notification.inserted_at),
  168. emoji_url: nil
  169. }
  170. test_notifications_rendering([notification], user, [expected])
  171. end
  172. test "EmojiReact custom emoji notification" do
  173. user = insert(:user)
  174. other_user = insert(:user)
  175. note =
  176. insert(:note,
  177. user: user,
  178. data: %{
  179. "reactions" => [
  180. ["👍", [user.ap_id], nil],
  181. ["dinosaur", [user.ap_id], "http://localhost:4001/emoji/dino walking.gif"]
  182. ]
  183. }
  184. )
  185. activity = insert(:note_activity, note: note, user: user)
  186. {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "dinosaur")
  187. activity = Repo.get(Activity, activity.id)
  188. [notification] = Notification.for_user(user)
  189. assert notification
  190. expected = %{
  191. id: to_string(notification.id),
  192. group_key: "ungrouped-#{to_string(notification.id)}",
  193. pleroma: %{is_seen: false, is_muted: false},
  194. type: "pleroma:emoji_reaction",
  195. emoji: ":dinosaur:",
  196. account: AccountView.render("show.json", %{user: other_user, for: user}),
  197. status: StatusView.render("show.json", %{activity: activity, for: user}),
  198. created_at: Utils.to_masto_date(notification.inserted_at),
  199. emoji_url: "http://localhost:4001/emoji/dino walking.gif"
  200. }
  201. test_notifications_rendering([notification], user, [expected])
  202. end
  203. test "Poll notification" do
  204. user = insert(:user)
  205. activity = insert(:question_activity, user: user)
  206. {:ok, [notification]} = Notification.create_poll_notifications(activity)
  207. expected = %{
  208. id: to_string(notification.id),
  209. group_key: "ungrouped-#{to_string(notification.id)}",
  210. pleroma: %{is_seen: false, is_muted: false},
  211. type: "poll",
  212. account:
  213. AccountView.render("show.json", %{
  214. user: user,
  215. for: user
  216. }),
  217. status: StatusView.render("show.json", %{activity: activity, for: user}),
  218. created_at: Utils.to_masto_date(notification.inserted_at)
  219. }
  220. test_notifications_rendering([notification], user, [expected])
  221. end
  222. test "Report notification" do
  223. clear_config([:instance, :moderator_privileges], [:reports_manage_reports])
  224. reporting_user = insert(:user)
  225. reported_user = insert(:user)
  226. moderator_user = insert(:user, is_moderator: true)
  227. {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
  228. {:ok, [notification]} = Notification.create_notifications(activity)
  229. expected = %{
  230. id: to_string(notification.id),
  231. group_key: "ungrouped-#{to_string(notification.id)}",
  232. pleroma: %{is_seen: false, is_muted: false},
  233. type: "pleroma:report",
  234. account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
  235. created_at: Utils.to_masto_date(notification.inserted_at),
  236. report: ReportView.render("show.json", Report.extract_report_info(activity))
  237. }
  238. test_notifications_rendering([notification], moderator_user, [expected])
  239. end
  240. test "Edit notification" do
  241. user = insert(:user)
  242. repeat_user = insert(:user)
  243. {:ok, activity} = CommonAPI.post(user, %{status: "mew"})
  244. {:ok, _} = CommonAPI.repeat(activity.id, repeat_user)
  245. {:ok, update} = CommonAPI.update(activity, user, %{status: "mew mew"})
  246. user = Pleroma.User.get_by_ap_id(user.ap_id)
  247. activity = Pleroma.Activity.normalize(activity)
  248. update = Pleroma.Activity.normalize(update)
  249. {:ok, [notification]} = Notification.create_notifications(update)
  250. expected = %{
  251. id: to_string(notification.id),
  252. group_key: "ungrouped-#{to_string(notification.id)}",
  253. pleroma: %{is_seen: false, is_muted: false},
  254. type: "update",
  255. account: AccountView.render("show.json", %{user: user, for: repeat_user}),
  256. created_at: Utils.to_masto_date(notification.inserted_at),
  257. status: StatusView.render("show.json", %{activity: activity, for: repeat_user})
  258. }
  259. test_notifications_rendering([notification], repeat_user, [expected])
  260. end
  261. test "muted notification" do
  262. user = insert(:user)
  263. another_user = insert(:user)
  264. {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
  265. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  266. {:ok, favorite_activity} = CommonAPI.favorite(create_activity.id, another_user)
  267. {:ok, [notification]} = Notification.create_notifications(favorite_activity)
  268. create_activity = Activity.get_by_id(create_activity.id)
  269. expected = %{
  270. id: to_string(notification.id),
  271. group_key: "ungrouped-#{to_string(notification.id)}",
  272. pleroma: %{is_seen: true, is_muted: true},
  273. type: "favourite",
  274. account: AccountView.render("show.json", %{user: another_user, for: user}),
  275. status: StatusView.render("show.json", %{activity: create_activity, for: user}),
  276. created_at: Utils.to_masto_date(notification.inserted_at)
  277. }
  278. test_notifications_rendering([notification], user, [expected])
  279. end
  280. test "Subscribed status notification" do
  281. user = insert(:user)
  282. subscriber = insert(:user)
  283. User.subscribe(subscriber, user)
  284. {:ok, activity} = CommonAPI.post(user, %{status: "hi"})
  285. {:ok, [notification]} = Notification.create_notifications(activity)
  286. user = User.get_cached_by_id(user.id)
  287. expected = %{
  288. id: to_string(notification.id),
  289. group_key: "ungrouped-#{to_string(notification.id)}",
  290. pleroma: %{is_seen: false, is_muted: false},
  291. type: "status",
  292. account:
  293. AccountView.render("show.json", %{
  294. user: user,
  295. for: subscriber
  296. }),
  297. status: StatusView.render("show.json", %{activity: activity, for: subscriber}),
  298. created_at: Utils.to_masto_date(notification.inserted_at)
  299. }
  300. test_notifications_rendering([notification], subscriber, [expected])
  301. end
  302. end