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"})
242 {:ok, emoji_react, _} =
243 Builder.emoji_react(other_user, Object.normalize(activity, fetch: false), ":100a:")
245 remoteUrl = "http://evil.website/emoji/100a.png"
246 [tag] = emoji_react["tag"]
247 tag = put_in(tag["id"], remoteUrl)
248 tag = put_in(tag["icon"]["url"], remoteUrl)
249 emoji_react = put_in(emoji_react["tag"], [tag])
251 {:ok, _activity, _} = Pipeline.common_pipeline(emoji_react, local: false)
253 activity = Repo.get(Activity, activity.id)
255 [notification] = Notification.for_user(user)
260 id: to_string(notification.id),
261 pleroma: %{is_seen: false, is_muted: false},
262 type: "pleroma:emoji_reaction",
264 emoji_url: if(testProxy, do: MediaProxy.encode_url(remoteUrl), else: remoteUrl),
265 account: AccountView.render("show.json", %{user: other_user, for: user}),
266 status: StatusView.render("show.json", %{activity: activity, for: user}),
267 created_at: Utils.to_masto_date(notification.inserted_at)
270 test_notifications_rendering([notification], user, [expected])
274 test "Poll notification" do
276 activity = insert(:question_activity, user: user)
277 {:ok, [notification]} = Notification.create_poll_notifications(activity)
280 id: to_string(notification.id),
281 pleroma: %{is_seen: false, is_muted: false},
284 AccountView.render("show.json", %{
288 status: StatusView.render("show.json", %{activity: activity, for: user}),
289 created_at: Utils.to_masto_date(notification.inserted_at)
292 test_notifications_rendering([notification], user, [expected])
295 test "Report notification" do
296 reporting_user = insert(:user)
297 reported_user = insert(:user)
298 {:ok, moderator_user} = insert(:user) |> User.admin_api_update(%{is_moderator: true})
300 {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
301 {:ok, [notification]} = Notification.create_notifications(activity)
304 id: to_string(notification.id),
305 pleroma: %{is_seen: false, is_muted: false},
306 type: "pleroma:report",
307 account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
308 created_at: Utils.to_masto_date(notification.inserted_at),
309 report: ReportView.render("show.json", Report.extract_report_info(activity))
312 test_notifications_rendering([notification], moderator_user, [expected])
315 test "muted notification" do
317 another_user = insert(:user)
319 {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
320 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
321 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
322 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
323 create_activity = Activity.get_by_id(create_activity.id)
326 id: to_string(notification.id),
327 pleroma: %{is_seen: true, is_muted: true},
329 account: AccountView.render("show.json", %{user: another_user, for: user}),
330 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
331 created_at: Utils.to_masto_date(notification.inserted_at)
334 test_notifications_rendering([notification], user, [expected])