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