1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
9 alias Pleroma.Notification
13 alias Pleroma.Web.ActivityPub.Builder
14 alias Pleroma.Web.ActivityPub.Pipeline
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.MediaProxy
23 import Pleroma.Factory
25 defp test_notifications_rendering(notifications, user, expected_result) do
26 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
28 assert expected_result == result
31 NotificationView.render("index.json", %{
32 notifications: notifications,
37 assert expected_result == result
40 test "Mention notification" do
42 mentioned_user = insert(:user)
43 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
44 {:ok, [notification]} = Notification.create_notifications(activity)
45 user = User.get_cached_by_id(user.id)
48 id: to_string(notification.id),
49 pleroma: %{is_seen: false, is_muted: false},
52 AccountView.render("show.json", %{
56 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
57 created_at: Utils.to_masto_date(notification.inserted_at)
60 test_notifications_rendering([notification], mentioned_user, [expected])
63 test "Favourite notification" do
65 another_user = insert(:user)
66 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
67 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
68 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
69 create_activity = Activity.get_by_id(create_activity.id)
72 id: to_string(notification.id),
73 pleroma: %{is_seen: false, is_muted: false},
75 account: AccountView.render("show.json", %{user: another_user, for: user}),
76 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
77 created_at: Utils.to_masto_date(notification.inserted_at)
80 test_notifications_rendering([notification], user, [expected])
83 test "Reblog notification" do
85 another_user = insert(:user)
86 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
87 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
88 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
89 reblog_activity = Activity.get_by_id(create_activity.id)
92 id: to_string(notification.id),
93 pleroma: %{is_seen: false, is_muted: false},
95 account: AccountView.render("show.json", %{user: another_user, for: user}),
96 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
97 created_at: Utils.to_masto_date(notification.inserted_at)
100 test_notifications_rendering([notification], user, [expected])
103 test "Follow notification" do
104 follower = insert(:user)
105 followed = insert(:user)
106 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
107 notification = Notification |> Repo.one() |> Repo.preload(:activity)
110 id: to_string(notification.id),
111 pleroma: %{is_seen: false, is_muted: false},
113 account: AccountView.render("show.json", %{user: follower, for: followed}),
114 created_at: Utils.to_masto_date(notification.inserted_at)
117 test_notifications_rendering([notification], followed, [expected])
119 User.perform(:delete, follower)
120 refute Repo.one(Notification)
123 test "Move notification" do
124 old_user = insert(:user)
125 new_user = insert(:user, also_known_as: [old_user.ap_id])
126 follower = insert(:user)
128 User.follow(follower, old_user)
129 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
130 Pleroma.Tests.ObanHelpers.perform_all()
132 old_user = refresh_record(old_user)
133 new_user = refresh_record(new_user)
135 [notification] = Notification.for_user(follower)
138 id: to_string(notification.id),
139 pleroma: %{is_seen: false, is_muted: false},
141 account: AccountView.render("show.json", %{user: old_user, for: follower}),
142 target: AccountView.render("show.json", %{user: new_user, for: follower}),
143 created_at: Utils.to_masto_date(notification.inserted_at)
146 test_notifications_rendering([notification], follower, [expected])
149 test "EmojiReact notification" do
151 other_user = insert(:user)
153 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
154 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
156 activity = Repo.get(Activity, activity.id)
158 [notification] = Notification.for_user(user)
163 id: to_string(notification.id),
164 pleroma: %{is_seen: false, is_muted: false},
165 type: "pleroma:emoji_reaction",
168 account: AccountView.render("show.json", %{user: other_user, for: user}),
169 status: StatusView.render("show.json", %{activity: activity, for: user}),
170 created_at: Utils.to_masto_date(notification.inserted_at)
173 test_notifications_rendering([notification], user, [expected])
176 test "EmojiReact notification with custom emoji" do
178 other_user = insert(:user)
180 {:ok, activity} = CommonAPI.post(user, %{status: "#morb"})
181 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":100a:")
183 activity = Repo.get(Activity, activity.id)
185 [notification] = Notification.for_user(user)
190 id: to_string(notification.id),
191 pleroma: %{is_seen: false, is_muted: false},
192 type: "pleroma:emoji_reaction",
194 emoji_url: "http://localhost:4001/emoji/100a.png",
195 account: AccountView.render("show.json", %{user: other_user, for: user}),
196 status: StatusView.render("show.json", %{activity: activity, for: user}),
197 created_at: Utils.to_masto_date(notification.inserted_at)
200 test_notifications_rendering([notification], user, [expected])
203 test "EmojiReact notification with remote custom emoji" do
204 proxyBaseUrl = "https://cache.pleroma.social"
205 clear_config([:media_proxy, :base_url], proxyBaseUrl)
207 for testProxy <- [true, false] do
208 clear_config([:media_proxy, :enabled], testProxy)
211 other_user = insert(:user, local: false)
213 {:ok, activity} = CommonAPI.post(user, %{status: "#morb"})
215 {:ok, emoji_react, _} =
216 Builder.emoji_react(other_user, Object.normalize(activity, fetch: false), ":100a:")
218 remoteUrl = "http://evil.website/emoji/100a.png"
219 [tag] = emoji_react["tag"]
220 tag = put_in(tag["id"], remoteUrl)
221 tag = put_in(tag["icon"]["url"], remoteUrl)
222 emoji_react = put_in(emoji_react["tag"], [tag])
224 {:ok, _activity, _} = Pipeline.common_pipeline(emoji_react, local: false)
226 activity = Repo.get(Activity, activity.id)
228 [notification] = Notification.for_user(user)
233 id: to_string(notification.id),
234 pleroma: %{is_seen: false, is_muted: false},
235 type: "pleroma:emoji_reaction",
237 emoji_url: if(testProxy, do: MediaProxy.encode_url(remoteUrl), else: remoteUrl),
238 account: AccountView.render("show.json", %{user: other_user, for: user}),
239 status: StatusView.render("show.json", %{activity: activity, for: user}),
240 created_at: Utils.to_masto_date(notification.inserted_at)
243 test_notifications_rendering([notification], user, [expected])
247 test "Poll notification" do
249 activity = insert(:question_activity, user: user)
250 {:ok, [notification]} = Notification.create_poll_notifications(activity)
253 id: to_string(notification.id),
254 pleroma: %{is_seen: false, is_muted: false},
257 AccountView.render("show.json", %{
261 status: StatusView.render("show.json", %{activity: activity, for: user}),
262 created_at: Utils.to_masto_date(notification.inserted_at)
265 test_notifications_rendering([notification], user, [expected])
268 test "Report notification" do
269 reporting_user = insert(:user)
270 reported_user = insert(:user)
271 {:ok, moderator_user} = insert(:user) |> User.admin_api_update(%{is_moderator: true})
273 {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
274 {:ok, [notification]} = Notification.create_notifications(activity)
277 id: to_string(notification.id),
278 pleroma: %{is_seen: false, is_muted: false},
279 type: "pleroma:report",
280 account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
281 created_at: Utils.to_masto_date(notification.inserted_at),
282 report: ReportView.render("show.json", Report.extract_report_info(activity))
285 test_notifications_rendering([notification], moderator_user, [expected])
288 test "Edit notification" do
290 repeat_user = insert(:user)
292 {:ok, activity} = CommonAPI.post(user, %{status: "mew"})
293 {:ok, _} = CommonAPI.repeat(activity.id, repeat_user)
294 {:ok, update} = CommonAPI.update(user, activity, %{status: "mew mew"})
296 user = Pleroma.User.get_by_ap_id(user.ap_id)
297 activity = Pleroma.Activity.normalize(activity)
298 update = Pleroma.Activity.normalize(update)
300 {:ok, [notification]} = Notification.create_notifications(update)
303 id: to_string(notification.id),
304 pleroma: %{is_seen: false, is_muted: false},
306 account: AccountView.render("show.json", %{user: user, for: repeat_user}),
307 created_at: Utils.to_masto_date(notification.inserted_at),
308 status: StatusView.render("show.json", %{activity: activity, for: repeat_user})
311 test_notifications_rendering([notification], repeat_user, [expected])
314 test "muted notification" do
316 another_user = insert(:user)
318 {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
319 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
320 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
321 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
322 create_activity = Activity.get_by_id(create_activity.id)
325 id: to_string(notification.id),
326 pleroma: %{is_seen: true, is_muted: true},
328 account: AccountView.render("show.json", %{user: another_user, for: user}),
329 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
330 created_at: Utils.to_masto_date(notification.inserted_at)
333 test_notifications_rendering([notification], user, [expected])