1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
6 use Pleroma.DataCase, async: false
10 alias Pleroma.Chat.MessageReference
11 alias Pleroma.Notification
15 alias Pleroma.Web.AdminAPI.Report
16 alias Pleroma.Web.AdminAPI.ReportView
17 alias Pleroma.Web.CommonAPI
18 alias Pleroma.Web.CommonAPI.Utils
19 alias Pleroma.Web.MastodonAPI.AccountView
20 alias Pleroma.Web.MastodonAPI.NotificationView
21 alias Pleroma.Web.MastodonAPI.StatusView
22 alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
23 import Pleroma.Factory
26 Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config)
30 defp test_notifications_rendering(notifications, user, expected_result) do
31 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
33 assert expected_result == result
36 NotificationView.render("index.json", %{
37 notifications: notifications,
42 assert expected_result == result
45 test "ChatMessage notification" do
47 recipient = insert(:user)
48 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
50 {:ok, [notification]} = Notification.create_notifications(activity)
52 object = Object.normalize(activity, fetch: false)
53 chat = Chat.get(recipient.id, user.ap_id)
55 cm_ref = MessageReference.for_chat_and_object(chat, object)
58 id: to_string(notification.id),
59 pleroma: %{is_seen: false, is_muted: false},
60 type: "pleroma:chat_mention",
61 account: AccountView.render("show.json", %{user: user, for: recipient}),
62 chat_message: MessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
63 created_at: Utils.to_masto_date(notification.inserted_at)
66 test_notifications_rendering([notification], recipient, [expected])
69 test "Mention notification" do
71 mentioned_user = insert(:user)
72 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
73 {:ok, [notification]} = Notification.create_notifications(activity)
74 user = User.get_cached_by_id(user.id)
77 id: to_string(notification.id),
78 pleroma: %{is_seen: false, is_muted: false},
81 AccountView.render("show.json", %{
85 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
86 created_at: Utils.to_masto_date(notification.inserted_at)
89 test_notifications_rendering([notification], mentioned_user, [expected])
92 test "Favourite notification" do
94 another_user = insert(:user)
95 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
96 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
97 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
98 create_activity = Activity.get_by_id(create_activity.id)
101 id: to_string(notification.id),
102 pleroma: %{is_seen: false, is_muted: false},
104 account: AccountView.render("show.json", %{user: another_user, for: user}),
105 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
106 created_at: Utils.to_masto_date(notification.inserted_at)
109 test_notifications_rendering([notification], user, [expected])
112 test "Reblog notification" do
114 another_user = insert(:user)
115 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
116 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
117 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
118 reblog_activity = Activity.get_by_id(create_activity.id)
121 id: to_string(notification.id),
122 pleroma: %{is_seen: false, is_muted: false},
124 account: AccountView.render("show.json", %{user: another_user, for: user}),
125 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
126 created_at: Utils.to_masto_date(notification.inserted_at)
129 test_notifications_rendering([notification], user, [expected])
132 test "Follow notification" do
133 follower = insert(:user)
134 followed = insert(:user)
135 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
136 notification = Notification |> Repo.one() |> Repo.preload(:activity)
139 id: to_string(notification.id),
140 pleroma: %{is_seen: false, is_muted: false},
142 account: AccountView.render("show.json", %{user: follower, for: followed}),
143 created_at: Utils.to_masto_date(notification.inserted_at)
146 test_notifications_rendering([notification], followed, [expected])
148 User.perform(:delete, follower)
149 refute Repo.one(Notification)
152 test "Move notification" do
153 old_user = insert(:user)
154 new_user = insert(:user, also_known_as: [old_user.ap_id])
155 follower = insert(:user)
157 User.follow(follower, old_user)
158 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
159 Pleroma.Tests.ObanHelpers.perform_all()
161 old_user = refresh_record(old_user)
162 new_user = refresh_record(new_user)
164 [notification] = Notification.for_user(follower)
167 id: to_string(notification.id),
168 pleroma: %{is_seen: false, is_muted: false},
170 account: AccountView.render("show.json", %{user: old_user, for: follower}),
171 target: AccountView.render("show.json", %{user: new_user, for: follower}),
172 created_at: Utils.to_masto_date(notification.inserted_at)
175 test_notifications_rendering([notification], follower, [expected])
178 test "EmojiReact notification" do
180 other_user = insert(:user)
182 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
183 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
185 activity = Repo.get(Activity, activity.id)
187 [notification] = Notification.for_user(user)
192 id: to_string(notification.id),
193 pleroma: %{is_seen: false, is_muted: false},
194 type: "pleroma:emoji_reaction",
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),
202 test_notifications_rendering([notification], user, [expected])
205 test "EmojiReact custom emoji notification" do
207 other_user = insert(:user)
214 ["👍", [user.ap_id], nil],
215 ["dinosaur", [user.ap_id], "http://localhost:4001/emoji/dino walking.gif"]
220 activity = insert(:note_activity, note: note, user: user)
222 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "dinosaur")
224 activity = Repo.get(Activity, activity.id)
226 [notification] = Notification.for_user(user)
231 id: to_string(notification.id),
232 pleroma: %{is_seen: false, is_muted: false},
233 type: "pleroma:emoji_reaction",
235 account: AccountView.render("show.json", %{user: other_user, for: user}),
236 status: StatusView.render("show.json", %{activity: activity, for: user}),
237 created_at: Utils.to_masto_date(notification.inserted_at),
238 emoji_url: "http://localhost:4001/emoji/dino walking.gif"
241 test_notifications_rendering([notification], user, [expected])
244 test "Poll notification" do
246 activity = insert(:question_activity, user: user)
247 {:ok, [notification]} = Notification.create_poll_notifications(activity)
250 id: to_string(notification.id),
251 pleroma: %{is_seen: false, is_muted: false},
254 AccountView.render("show.json", %{
258 status: StatusView.render("show.json", %{activity: activity, for: user}),
259 created_at: Utils.to_masto_date(notification.inserted_at)
262 test_notifications_rendering([notification], user, [expected])
265 test "Report notification" do
266 clear_config([:instance, :moderator_privileges], [:reports_manage_reports])
268 reporting_user = insert(:user)
269 reported_user = insert(:user)
270 moderator_user = insert(:user, is_moderator: true)
272 {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
273 {:ok, [notification]} = Notification.create_notifications(activity)
276 id: to_string(notification.id),
277 pleroma: %{is_seen: false, is_muted: false},
278 type: "pleroma:report",
279 account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
280 created_at: Utils.to_masto_date(notification.inserted_at),
281 report: ReportView.render("show.json", Report.extract_report_info(activity))
284 test_notifications_rendering([notification], moderator_user, [expected])
287 test "Edit notification" do
289 repeat_user = insert(:user)
291 {:ok, activity} = CommonAPI.post(user, %{status: "mew"})
292 {:ok, _} = CommonAPI.repeat(activity.id, repeat_user)
293 {:ok, update} = CommonAPI.update(user, activity, %{status: "mew mew"})
295 user = Pleroma.User.get_by_ap_id(user.ap_id)
296 activity = Pleroma.Activity.normalize(activity)
297 update = Pleroma.Activity.normalize(update)
299 {:ok, [notification]} = Notification.create_notifications(update)
302 id: to_string(notification.id),
303 pleroma: %{is_seen: false, is_muted: false},
305 account: AccountView.render("show.json", %{user: user, for: repeat_user}),
306 created_at: Utils.to_masto_date(notification.inserted_at),
307 status: StatusView.render("show.json", %{activity: activity, for: repeat_user})
310 test_notifications_rendering([notification], repeat_user, [expected])
313 test "muted notification" do
315 another_user = insert(:user)
317 {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
318 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
319 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
320 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
321 create_activity = Activity.get_by_id(create_activity.id)
324 id: to_string(notification.id),
325 pleroma: %{is_seen: true, is_muted: true},
327 account: AccountView.render("show.json", %{user: another_user, for: user}),
328 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
329 created_at: Utils.to_masto_date(notification.inserted_at)
332 test_notifications_rendering([notification], user, [expected])