Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

241 行
8.4KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
  5. use Pleroma.DataCase
  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. defp test_notifications_rendering(notifications, user, expected_result) do
  23. result = NotificationView.render("index.json", %{notifications: notifications, for: user})
  24. assert expected_result == result
  25. result =
  26. NotificationView.render("index.json", %{
  27. notifications: notifications,
  28. for: user,
  29. relationships: nil
  30. })
  31. assert expected_result == result
  32. end
  33. test "ChatMessage notification" do
  34. user = insert(:user)
  35. recipient = insert(:user)
  36. {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
  37. {:ok, [notification]} = Notification.create_notifications(activity)
  38. object = Object.normalize(activity, fetch: false)
  39. chat = Chat.get(recipient.id, user.ap_id)
  40. cm_ref = MessageReference.for_chat_and_object(chat, object)
  41. expected = %{
  42. id: to_string(notification.id),
  43. pleroma: %{is_seen: false, is_muted: false},
  44. type: "pleroma:chat_mention",
  45. account: AccountView.render("show.json", %{user: user, for: recipient}),
  46. chat_message: MessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
  47. created_at: Utils.to_masto_date(notification.inserted_at)
  48. }
  49. test_notifications_rendering([notification], recipient, [expected])
  50. end
  51. test "Mention notification" do
  52. user = insert(:user)
  53. mentioned_user = insert(:user)
  54. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
  55. {:ok, [notification]} = Notification.create_notifications(activity)
  56. user = User.get_cached_by_id(user.id)
  57. expected = %{
  58. id: to_string(notification.id),
  59. pleroma: %{is_seen: false, is_muted: false},
  60. type: "mention",
  61. account:
  62. AccountView.render("show.json", %{
  63. user: user,
  64. for: mentioned_user
  65. }),
  66. status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
  67. created_at: Utils.to_masto_date(notification.inserted_at)
  68. }
  69. test_notifications_rendering([notification], mentioned_user, [expected])
  70. end
  71. test "Favourite notification" do
  72. user = insert(:user)
  73. another_user = insert(:user)
  74. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  75. {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
  76. {:ok, [notification]} = Notification.create_notifications(favorite_activity)
  77. create_activity = Activity.get_by_id(create_activity.id)
  78. expected = %{
  79. id: to_string(notification.id),
  80. pleroma: %{is_seen: false, is_muted: false},
  81. type: "favourite",
  82. account: AccountView.render("show.json", %{user: another_user, for: user}),
  83. status: StatusView.render("show.json", %{activity: create_activity, for: user}),
  84. created_at: Utils.to_masto_date(notification.inserted_at)
  85. }
  86. test_notifications_rendering([notification], user, [expected])
  87. end
  88. test "Reblog notification" do
  89. user = insert(:user)
  90. another_user = insert(:user)
  91. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  92. {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
  93. {:ok, [notification]} = Notification.create_notifications(reblog_activity)
  94. reblog_activity = Activity.get_by_id(create_activity.id)
  95. expected = %{
  96. id: to_string(notification.id),
  97. pleroma: %{is_seen: false, is_muted: false},
  98. type: "reblog",
  99. account: AccountView.render("show.json", %{user: another_user, for: user}),
  100. status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
  101. created_at: Utils.to_masto_date(notification.inserted_at)
  102. }
  103. test_notifications_rendering([notification], user, [expected])
  104. end
  105. test "Follow notification" do
  106. follower = insert(:user)
  107. followed = insert(:user)
  108. {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
  109. notification = Notification |> Repo.one() |> Repo.preload(:activity)
  110. expected = %{
  111. id: to_string(notification.id),
  112. pleroma: %{is_seen: false, is_muted: false},
  113. type: "follow",
  114. account: AccountView.render("show.json", %{user: follower, for: followed}),
  115. created_at: Utils.to_masto_date(notification.inserted_at)
  116. }
  117. test_notifications_rendering([notification], followed, [expected])
  118. User.perform(:delete, follower)
  119. refute Repo.one(Notification)
  120. end
  121. test "Move notification" do
  122. old_user = insert(:user)
  123. new_user = insert(:user, also_known_as: [old_user.ap_id])
  124. follower = insert(:user)
  125. User.follow(follower, old_user)
  126. Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
  127. Pleroma.Tests.ObanHelpers.perform_all()
  128. old_user = refresh_record(old_user)
  129. new_user = refresh_record(new_user)
  130. [notification] = Notification.for_user(follower)
  131. expected = %{
  132. id: to_string(notification.id),
  133. pleroma: %{is_seen: false, is_muted: false},
  134. type: "move",
  135. account: AccountView.render("show.json", %{user: old_user, for: follower}),
  136. target: AccountView.render("show.json", %{user: new_user, for: follower}),
  137. created_at: Utils.to_masto_date(notification.inserted_at)
  138. }
  139. test_notifications_rendering([notification], follower, [expected])
  140. end
  141. test "EmojiReact notification" do
  142. user = insert(:user)
  143. other_user = insert(:user)
  144. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  145. {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  146. activity = Repo.get(Activity, activity.id)
  147. [notification] = Notification.for_user(user)
  148. assert notification
  149. expected = %{
  150. id: to_string(notification.id),
  151. pleroma: %{is_seen: false, is_muted: false},
  152. type: "pleroma:emoji_reaction",
  153. emoji: "☕",
  154. account: AccountView.render("show.json", %{user: other_user, for: user}),
  155. status: StatusView.render("show.json", %{activity: activity, for: user}),
  156. created_at: Utils.to_masto_date(notification.inserted_at)
  157. }
  158. test_notifications_rendering([notification], user, [expected])
  159. end
  160. test "Report notification" do
  161. reporting_user = insert(:user)
  162. reported_user = insert(:user)
  163. {:ok, moderator_user} = insert(:user) |> User.admin_api_update(%{is_moderator: true})
  164. {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
  165. {:ok, [notification]} = Notification.create_notifications(activity)
  166. expected = %{
  167. id: to_string(notification.id),
  168. pleroma: %{is_seen: false, is_muted: false},
  169. type: "pleroma:report",
  170. account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
  171. created_at: Utils.to_masto_date(notification.inserted_at),
  172. report: ReportView.render("show.json", Report.extract_report_info(activity))
  173. }
  174. test_notifications_rendering([notification], moderator_user, [expected])
  175. end
  176. test "muted notification" do
  177. user = insert(:user)
  178. another_user = insert(:user)
  179. {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
  180. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  181. {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
  182. {:ok, [notification]} = Notification.create_notifications(favorite_activity)
  183. create_activity = Activity.get_by_id(create_activity.id)
  184. expected = %{
  185. id: to_string(notification.id),
  186. pleroma: %{is_seen: true, is_muted: true},
  187. type: "favourite",
  188. account: AccountView.render("show.json", %{user: another_user, for: user}),
  189. status: StatusView.render("show.json", %{activity: create_activity, for: user}),
  190. created_at: Utils.to_masto_date(notification.inserted_at)
  191. }
  192. test_notifications_rendering([notification], user, [expected])
  193. end
  194. end