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
10 alias Pleroma.Chat.MessageReference
11 alias Pleroma.Notification
15 alias Pleroma.Web.ActivityPub.Builder
16 alias Pleroma.Web.ActivityPub.Pipeline
17 alias Pleroma.Web.AdminAPI.Report
18 alias Pleroma.Web.AdminAPI.ReportView
19 alias Pleroma.Web.CommonAPI
20 alias Pleroma.Web.CommonAPI.Utils
21 alias Pleroma.Web.MastodonAPI.AccountView
22 alias Pleroma.Web.MastodonAPI.NotificationView
23 alias Pleroma.Web.MastodonAPI.StatusView
24 alias Pleroma.Web.MediaProxy
25 alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
26 import Pleroma.Factory
28 defp test_notifications_rendering(notifications, user, expected_result) do
29 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
31 assert expected_result == result
34 NotificationView.render("index.json", %{
35 notifications: notifications,
40 assert expected_result == result
43 test "ChatMessage notification" do
45 recipient = insert(:user)
46 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
48 {:ok, [notification]} = Notification.create_notifications(activity)
50 object = Object.normalize(activity, fetch: false)
51 chat = Chat.get(recipient.id, user.ap_id)
53 cm_ref = MessageReference.for_chat_and_object(chat, object)
56 id: to_string(notification.id),
57 pleroma: %{is_seen: false, is_muted: false},
58 type: "pleroma:chat_mention",
59 account: AccountView.render("show.json", %{user: user, for: recipient}),
60 chat_message: MessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
61 created_at: Utils.to_masto_date(notification.inserted_at)
64 test_notifications_rendering([notification], recipient, [expected])
67 test "Mention notification" do
69 mentioned_user = insert(:user)
70 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
71 {:ok, [notification]} = Notification.create_notifications(activity)
72 user = User.get_cached_by_id(user.id)
75 id: to_string(notification.id),
76 pleroma: %{is_seen: false, is_muted: false},
79 AccountView.render("show.json", %{
83 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
84 created_at: Utils.to_masto_date(notification.inserted_at)
87 test_notifications_rendering([notification], mentioned_user, [expected])
90 test "Favourite notification" do
92 another_user = insert(:user)
93 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
94 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
95 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
96 create_activity = Activity.get_by_id(create_activity.id)
99 id: to_string(notification.id),
100 pleroma: %{is_seen: false, is_muted: false},
102 account: AccountView.render("show.json", %{user: another_user, for: user}),
103 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
104 created_at: Utils.to_masto_date(notification.inserted_at)
107 test_notifications_rendering([notification], user, [expected])
110 test "Reblog notification" do
112 another_user = insert(:user)
113 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
114 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
115 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
116 reblog_activity = Activity.get_by_id(create_activity.id)
119 id: to_string(notification.id),
120 pleroma: %{is_seen: false, is_muted: false},
122 account: AccountView.render("show.json", %{user: another_user, for: user}),
123 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
124 created_at: Utils.to_masto_date(notification.inserted_at)
127 test_notifications_rendering([notification], user, [expected])
130 test "Follow notification" do
131 follower = insert(:user)
132 followed = insert(:user)
133 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
134 notification = Notification |> Repo.one() |> Repo.preload(:activity)
137 id: to_string(notification.id),
138 pleroma: %{is_seen: false, is_muted: false},
140 account: AccountView.render("show.json", %{user: follower, for: followed}),
141 created_at: Utils.to_masto_date(notification.inserted_at)
144 test_notifications_rendering([notification], followed, [expected])
146 User.perform(:delete, follower)
147 refute Repo.one(Notification)
150 test "Move notification" do
151 old_user = insert(:user)
152 new_user = insert(:user, also_known_as: [old_user.ap_id])
153 follower = insert(:user)
155 User.follow(follower, old_user)
156 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
157 Pleroma.Tests.ObanHelpers.perform_all()
159 old_user = refresh_record(old_user)
160 new_user = refresh_record(new_user)
162 [notification] = Notification.for_user(follower)
165 id: to_string(notification.id),
166 pleroma: %{is_seen: false, is_muted: false},
168 account: AccountView.render("show.json", %{user: old_user, for: follower}),
169 target: AccountView.render("show.json", %{user: new_user, for: follower}),
170 created_at: Utils.to_masto_date(notification.inserted_at)
173 test_notifications_rendering([notification], follower, [expected])
176 test "EmojiReact notification" do
178 other_user = insert(:user)
180 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
181 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
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",
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 custom emoji" do
205 other_user = insert(:user)
207 {:ok, activity} = CommonAPI.post(user, %{status: "#morb"})
208 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":100a:")
210 activity = Repo.get(Activity, activity.id)
212 [notification] = Notification.for_user(user)
217 id: to_string(notification.id),
218 pleroma: %{is_seen: false, is_muted: false},
219 type: "pleroma:emoji_reaction",
221 emoji_url: "http://localhost:4001/emoji/100a.png",
222 account: AccountView.render("show.json", %{user: other_user, for: user}),
223 status: StatusView.render("show.json", %{activity: activity, for: user}),
224 created_at: Utils.to_masto_date(notification.inserted_at)
227 test_notifications_rendering([notification], user, [expected])
230 test "EmojiReact notification with remote custom emoji" do
231 proxyBaseUrl = "https://cache.pleroma.social"
232 clear_config([:media_proxy, :base_url], proxyBaseUrl)
234 for testProxy <- [true, false] do
235 clear_config([:media_proxy, :enabled], testProxy)
238 other_user = insert(:user, local: false)
240 {:ok, activity} = CommonAPI.post(user, %{status: "#morb"})
241 {:ok, emoji_react, _} = Builder.emoji_react(other_user, Object.normalize(activity, fetch: false), ":100a:")
243 remoteUrl = "http://evil.website/emoji/100a.png"
244 [tag] = emoji_react["tag"]
245 tag = put_in(tag["id"], remoteUrl)
246 tag = put_in(tag["icon"]["url"], remoteUrl)
247 emoji_react = put_in(emoji_react["tag"], [tag])
249 {:ok, _activity, _} = Pipeline.common_pipeline(emoji_react, local: false)
251 activity = Repo.get(Activity, activity.id)
253 [notification] = Notification.for_user(user)
258 id: to_string(notification.id),
259 pleroma: %{is_seen: false, is_muted: false},
260 type: "pleroma:emoji_reaction",
262 emoji_url: (if testProxy, do: MediaProxy.encode_url(remoteUrl), else: remoteUrl),
263 account: AccountView.render("show.json", %{user: other_user, for: user}),
264 status: StatusView.render("show.json", %{activity: activity, for: user}),
265 created_at: Utils.to_masto_date(notification.inserted_at)
267 test_notifications_rendering([notification], user, [expected])
271 test "Poll notification" do
273 activity = insert(:question_activity, user: user)
274 {:ok, [notification]} = Notification.create_poll_notifications(activity)
277 id: to_string(notification.id),
278 pleroma: %{is_seen: false, is_muted: false},
281 AccountView.render("show.json", %{
285 status: StatusView.render("show.json", %{activity: activity, for: user}),
286 created_at: Utils.to_masto_date(notification.inserted_at)
289 test_notifications_rendering([notification], user, [expected])
292 test "Report notification" do
293 reporting_user = insert(:user)
294 reported_user = insert(:user)
295 {:ok, moderator_user} = insert(:user) |> User.admin_api_update(%{is_moderator: true})
297 {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
298 {:ok, [notification]} = Notification.create_notifications(activity)
301 id: to_string(notification.id),
302 pleroma: %{is_seen: false, is_muted: false},
303 type: "pleroma:report",
304 account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
305 created_at: Utils.to_masto_date(notification.inserted_at),
306 report: ReportView.render("show.json", Report.extract_report_info(activity))
309 test_notifications_rendering([notification], moderator_user, [expected])
312 test "muted notification" do
314 another_user = insert(:user)
316 {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
317 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
318 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
319 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
320 create_activity = Activity.get_by_id(create_activity.id)
323 id: to_string(notification.id),
324 pleroma: %{is_seen: true, is_muted: true},
326 account: AccountView.render("show.json", %{user: another_user, for: user}),
327 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
328 created_at: Utils.to_masto_date(notification.inserted_at)
331 test_notifications_rendering([notification], user, [expected])