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.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
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 "ChatMessage notification" do
42 recipient = insert(:user)
43 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
45 {:ok, [notification]} = Notification.create_notifications(activity)
47 object = Object.normalize(activity, fetch: false)
48 chat = Chat.get(recipient.id, user.ap_id)
50 cm_ref = MessageReference.for_chat_and_object(chat, object)
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)
61 test_notifications_rendering([notification], recipient, [expected])
64 test "Mention notification" do
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)
72 id: to_string(notification.id),
73 pleroma: %{is_seen: false, is_muted: false},
76 AccountView.render("show.json", %{
80 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
81 created_at: Utils.to_masto_date(notification.inserted_at)
84 test_notifications_rendering([notification], mentioned_user, [expected])
87 test "Favourite notification" do
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)
96 id: to_string(notification.id),
97 pleroma: %{is_seen: false, is_muted: false},
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)
104 test_notifications_rendering([notification], user, [expected])
107 test "Reblog notification" do
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)
116 id: to_string(notification.id),
117 pleroma: %{is_seen: false, is_muted: false},
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)
124 test_notifications_rendering([notification], user, [expected])
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)
134 id: to_string(notification.id),
135 pleroma: %{is_seen: false, is_muted: false},
137 account: AccountView.render("show.json", %{user: follower, for: followed}),
138 created_at: Utils.to_masto_date(notification.inserted_at)
141 test_notifications_rendering([notification], followed, [expected])
143 User.perform(:delete, follower)
144 refute Repo.one(Notification)
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)
152 User.follow(follower, old_user)
153 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
154 Pleroma.Tests.ObanHelpers.perform_all()
156 old_user = refresh_record(old_user)
157 new_user = refresh_record(new_user)
159 [notification] = Notification.for_user(follower)
162 id: to_string(notification.id),
163 pleroma: %{is_seen: false, is_muted: false},
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)
170 test_notifications_rendering([notification], follower, [expected])
173 test "EmojiReact notification" do
175 other_user = insert(:user)
177 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
178 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
180 activity = Repo.get(Activity, activity.id)
182 [notification] = Notification.for_user(user)
187 id: to_string(notification.id),
188 pleroma: %{is_seen: false, is_muted: false},
189 type: "pleroma:emoji_reaction",
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)
196 test_notifications_rendering([notification], user, [expected])
199 test "Report notification" do
200 reporting_user = insert(:user)
201 reported_user = insert(:user)
202 {:ok, moderator_user} = insert(:user) |> User.admin_api_update(%{is_moderator: true})
204 {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
205 {:ok, [notification]} = Notification.create_notifications(activity)
208 id: to_string(notification.id),
209 pleroma: %{is_seen: false, is_muted: false},
210 type: "pleroma:report",
211 account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
212 created_at: Utils.to_masto_date(notification.inserted_at),
213 report: ReportView.render("show.json", Report.extract_report_info(activity))
216 test_notifications_rendering([notification], moderator_user, [expected])
219 test "muted notification" do
221 another_user = insert(:user)
223 {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
224 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
225 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
226 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
227 create_activity = Activity.get_by_id(create_activity.id)
230 id: to_string(notification.id),
231 pleroma: %{is_seen: true, is_muted: true},
233 account: AccountView.render("show.json", %{user: another_user, for: user}),
234 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
235 created_at: Utils.to_masto_date(notification.inserted_at)
238 test_notifications_rendering([notification], user, [expected])