remove `unread_conversation_count` from User
[akkoma] / test / pleroma / web / mastodon_api / views / notification_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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.CommonAPI
16 alias Pleroma.Web.CommonAPI.Utils
17 alias Pleroma.Web.MastodonAPI.AccountView
18 alias Pleroma.Web.MastodonAPI.NotificationView
19 alias Pleroma.Web.MastodonAPI.StatusView
20 alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
21 import Pleroma.Factory
22
23 defp test_notifications_rendering(notifications, user, expected_result) do
24 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
25
26 assert expected_result == result
27
28 result =
29 NotificationView.render("index.json", %{
30 notifications: notifications,
31 for: user,
32 relationships: nil
33 })
34
35 assert expected_result == result
36 end
37
38 test "ChatMessage notification" do
39 user = insert(:user)
40 recipient = insert(:user)
41 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
42
43 {:ok, [notification]} = Notification.create_notifications(activity)
44
45 object = Object.normalize(activity)
46 chat = Chat.get(recipient.id, user.ap_id)
47
48 cm_ref = MessageReference.for_chat_and_object(chat, object)
49
50 expected = %{
51 id: to_string(notification.id),
52 pleroma: %{is_seen: false, is_muted: false},
53 type: "pleroma:chat_mention",
54 account: AccountView.render("show.json", %{user: user, for: recipient}),
55 chat_message: MessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
56 created_at: Utils.to_masto_date(notification.inserted_at)
57 }
58
59 test_notifications_rendering([notification], recipient, [expected])
60 end
61
62 test "Mention notification" do
63 user = insert(:user)
64 mentioned_user = insert(:user)
65 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
66 {:ok, [notification]} = Notification.create_notifications(activity)
67 user = User.get_cached_by_id(user.id)
68
69 expected = %{
70 id: to_string(notification.id),
71 pleroma: %{is_seen: false, is_muted: false},
72 type: "mention",
73 account:
74 AccountView.render("show.json", %{
75 user: user,
76 for: mentioned_user
77 }),
78 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
79 created_at: Utils.to_masto_date(notification.inserted_at)
80 }
81
82 test_notifications_rendering([notification], mentioned_user, [expected])
83 end
84
85 test "Favourite notification" do
86 user = insert(:user)
87 another_user = insert(:user)
88 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
89 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
90 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
91 create_activity = Activity.get_by_id(create_activity.id)
92
93 expected = %{
94 id: to_string(notification.id),
95 pleroma: %{is_seen: false, is_muted: false},
96 type: "favourite",
97 account: AccountView.render("show.json", %{user: another_user, for: user}),
98 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
99 created_at: Utils.to_masto_date(notification.inserted_at)
100 }
101
102 test_notifications_rendering([notification], user, [expected])
103 end
104
105 test "Reblog notification" do
106 user = insert(:user)
107 another_user = insert(:user)
108 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
109 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
110 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
111 reblog_activity = Activity.get_by_id(create_activity.id)
112
113 expected = %{
114 id: to_string(notification.id),
115 pleroma: %{is_seen: false, is_muted: false},
116 type: "reblog",
117 account: AccountView.render("show.json", %{user: another_user, for: user}),
118 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
119 created_at: Utils.to_masto_date(notification.inserted_at)
120 }
121
122 test_notifications_rendering([notification], user, [expected])
123 end
124
125 test "Follow notification" do
126 follower = insert(:user)
127 followed = insert(:user)
128 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
129 notification = Notification |> Repo.one() |> Repo.preload(:activity)
130
131 expected = %{
132 id: to_string(notification.id),
133 pleroma: %{is_seen: false, is_muted: false},
134 type: "follow",
135 account: AccountView.render("show.json", %{user: follower, for: followed}),
136 created_at: Utils.to_masto_date(notification.inserted_at)
137 }
138
139 test_notifications_rendering([notification], followed, [expected])
140
141 User.perform(:delete, follower)
142 refute Repo.one(Notification)
143 end
144
145 @tag capture_log: true
146 test "Move notification" do
147 old_user = insert(:user)
148 new_user = insert(:user, also_known_as: [old_user.ap_id])
149 follower = insert(:user)
150
151 old_user_url = old_user.ap_id
152
153 body =
154 File.read!("test/fixtures/users_mock/localhost.json")
155 |> String.replace("{{nickname}}", old_user.nickname)
156 |> Jason.encode!()
157
158 Tesla.Mock.mock(fn
159 %{method: :get, url: ^old_user_url} ->
160 %Tesla.Env{status: 200, body: body}
161 end)
162
163 User.follow(follower, old_user)
164 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
165 Pleroma.Tests.ObanHelpers.perform_all()
166
167 old_user = refresh_record(old_user)
168 new_user = refresh_record(new_user)
169
170 [notification] = Notification.for_user(follower)
171
172 expected = %{
173 id: to_string(notification.id),
174 pleroma: %{is_seen: false, is_muted: false},
175 type: "move",
176 account: AccountView.render("show.json", %{user: old_user, for: follower}),
177 target: AccountView.render("show.json", %{user: new_user, for: follower}),
178 created_at: Utils.to_masto_date(notification.inserted_at)
179 }
180
181 test_notifications_rendering([notification], follower, [expected])
182 end
183
184 test "EmojiReact notification" do
185 user = insert(:user)
186 other_user = insert(:user)
187
188 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
189 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
190
191 activity = Repo.get(Activity, activity.id)
192
193 [notification] = Notification.for_user(user)
194
195 assert notification
196
197 expected = %{
198 id: to_string(notification.id),
199 pleroma: %{is_seen: false, is_muted: false},
200 type: "pleroma:emoji_reaction",
201 emoji: "☕",
202 account: AccountView.render("show.json", %{user: other_user, for: user}),
203 status: StatusView.render("show.json", %{activity: activity, for: user}),
204 created_at: Utils.to_masto_date(notification.inserted_at)
205 }
206
207 test_notifications_rendering([notification], user, [expected])
208 end
209
210 test "muted notification" do
211 user = insert(:user)
212 another_user = insert(:user)
213
214 {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
215 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
216 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
217 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
218 create_activity = Activity.get_by_id(create_activity.id)
219
220 expected = %{
221 id: to_string(notification.id),
222 pleroma: %{is_seen: true, is_muted: true},
223 type: "favourite",
224 account: AccountView.render("show.json", %{user: another_user, for: user}),
225 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
226 created_at: Utils.to_masto_date(notification.inserted_at)
227 }
228
229 test_notifications_rendering([notification], user, [expected])
230 end
231 end