]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/utils/users/user-notifications.ts
Fix notification icon position
[github/Chocobozzz/PeerTube.git] / shared / utils / users / user-notifications.ts
1 /* tslint:disable:no-unused-expression */
2
3 import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
4 import { UserNotification, UserNotificationSetting, UserNotificationType } from '../../models/users'
5 import { ServerInfo } from '..'
6 import { expect } from 'chai'
7
8 function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) {
9 const path = '/api/v1/users/me/notification-settings'
10
11 return makePutBodyRequest({
12 url,
13 path,
14 token,
15 fields: settings,
16 statusCodeExpected
17 })
18 }
19
20 function getUserNotifications (url: string, token: string, start: number, count: number, sort = '-createdAt', statusCodeExpected = 200) {
21 const path = '/api/v1/users/me/notifications'
22
23 return makeGetRequest({
24 url,
25 path,
26 token,
27 query: {
28 start,
29 count,
30 sort
31 },
32 statusCodeExpected
33 })
34 }
35
36 function markAsReadNotifications (url: string, token: string, ids: number[], statusCodeExpected = 204) {
37 const path = '/api/v1/users/me/notifications/read'
38
39 return makePostBodyRequest({
40 url,
41 path,
42 token,
43 fields: { ids },
44 statusCodeExpected
45 })
46 }
47
48 async function getLastNotification (serverUrl: string, accessToken: string) {
49 const res = await getUserNotifications(serverUrl, accessToken, 0, 1, '-createdAt')
50
51 if (res.body.total === 0) return undefined
52
53 return res.body.data[0] as UserNotification
54 }
55
56 type CheckerBaseParams = {
57 server: ServerInfo
58 emails: object[]
59 socketNotifications: UserNotification[]
60 token: string,
61 check?: { web: boolean, mail: boolean }
62 }
63
64 type CheckerType = 'presence' | 'absence'
65
66 async function checkNotification (
67 base: CheckerBaseParams,
68 lastNotificationChecker: (notification: UserNotification) => void,
69 socketNotificationFinder: (notification: UserNotification) => boolean,
70 emailNotificationFinder: (email: object) => boolean,
71 checkType: 'presence' | 'absence'
72 ) {
73 const check = base.check || { web: true, mail: true }
74
75 if (check.web) {
76 const notification = await getLastNotification(base.server.url, base.token)
77 lastNotificationChecker(notification)
78
79 const socketNotification = base.socketNotifications.find(n => socketNotificationFinder(n))
80
81 if (checkType === 'presence') expect(socketNotification, 'The socket notification is absent.').to.not.be.undefined
82 else expect(socketNotification, 'The socket notification is present.').to.be.undefined
83 }
84
85 if (check.mail) {
86 // Last email
87 const email = base.emails
88 .slice()
89 .reverse()
90 .find(e => emailNotificationFinder(e))
91
92 if (checkType === 'presence') expect(email, 'The email is present.').to.not.be.undefined
93 else expect(email, 'The email is absent.').to.be.undefined
94 }
95 }
96
97 async function checkNewVideoFromSubscription (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) {
98 const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION
99
100 function lastNotificationChecker (notification: UserNotification) {
101 if (type === 'presence') {
102 expect(notification).to.not.be.undefined
103 expect(notification.type).to.equal(notificationType)
104 expect(notification.video.name).to.equal(videoName)
105 } else {
106 expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName)
107 }
108 }
109
110 function socketFinder (notification: UserNotification) {
111 return notification.type === notificationType && notification.video.name === videoName
112 }
113
114 function emailFinder (email: object) {
115 return email[ 'text' ].indexOf(videoUUID) !== -1
116 }
117
118 await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, type)
119 }
120
121 let lastEmailCount = 0
122 async function checkNewCommentOnMyVideo (base: CheckerBaseParams, uuid: string, commentId: number, threadId: number, type: CheckerType) {
123 const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO
124
125 function lastNotificationChecker (notification: UserNotification) {
126 if (type === 'presence') {
127 expect(notification).to.not.be.undefined
128 expect(notification.type).to.equal(notificationType)
129 expect(notification.comment.id).to.equal(commentId)
130 expect(notification.comment.account.displayName).to.equal('root')
131 } else {
132 expect(notification).to.satisfy((n: UserNotification) => {
133 return n === undefined || n.comment === undefined || n.comment.id !== commentId
134 })
135 }
136 }
137
138 function socketFinder (notification: UserNotification) {
139 return notification.type === notificationType &&
140 notification.comment.id === commentId &&
141 notification.comment.account.displayName === 'root'
142 }
143
144 const commentUrl = `http://localhost:9001/videos/watch/${uuid};threadId=${threadId}`
145 function emailFinder (email: object) {
146 return email[ 'text' ].indexOf(commentUrl) !== -1
147 }
148
149 await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, type)
150
151 if (type === 'presence') {
152 // We cannot detect email duplicates, so check we received another email
153 expect(base.emails).to.have.length.above(lastEmailCount)
154 lastEmailCount = base.emails.length
155 }
156 }
157
158 async function checkNewVideoAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) {
159 const notificationType = UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS
160
161 function lastNotificationChecker (notification: UserNotification) {
162 if (type === 'presence') {
163 expect(notification).to.not.be.undefined
164 expect(notification.type).to.equal(notificationType)
165 expect(notification.videoAbuse.video.uuid).to.equal(videoUUID)
166 expect(notification.videoAbuse.video.name).to.equal(videoName)
167 } else {
168 expect(notification).to.satisfy((n: UserNotification) => {
169 return n === undefined || n.videoAbuse === undefined || n.videoAbuse.video.uuid !== videoUUID
170 })
171 }
172 }
173
174 function socketFinder (notification: UserNotification) {
175 return notification.type === notificationType && notification.videoAbuse.video.uuid === videoUUID
176 }
177
178 function emailFinder (email: object) {
179 const text = email[ 'text' ]
180 return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1
181 }
182
183 await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, type)
184 }
185
186 async function checkNewBlacklistOnMyVideo (
187 base: CheckerBaseParams,
188 videoUUID: string,
189 videoName: string,
190 blacklistType: 'blacklist' | 'unblacklist'
191 ) {
192 const notificationType = blacklistType === 'blacklist'
193 ? UserNotificationType.BLACKLIST_ON_MY_VIDEO
194 : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO
195
196 function lastNotificationChecker (notification: UserNotification) {
197 expect(notification).to.not.be.undefined
198 expect(notification.type).to.equal(notificationType)
199
200 const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video
201
202 expect(video.uuid).to.equal(videoUUID)
203 expect(video.name).to.equal(videoName)
204 }
205
206 function socketFinder (notification: UserNotification) {
207 return notification.type === notificationType && (notification.video || notification.videoBlacklist.video).uuid === videoUUID
208 }
209
210 function emailFinder (email: object) {
211 const text = email[ 'text' ]
212 return text.indexOf(videoUUID) !== -1 && text.indexOf(' ' + blacklistType) !== -1
213 }
214
215 await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, 'presence')
216 }
217
218 // ---------------------------------------------------------------------------
219
220 export {
221 CheckerBaseParams,
222 CheckerType,
223 checkNotification,
224 checkNewVideoFromSubscription,
225 checkNewCommentOnMyVideo,
226 checkNewBlacklistOnMyVideo,
227 updateMyNotificationSettings,
228 checkNewVideoAbuseForModerators,
229 getUserNotifications,
230 markAsReadNotifications,
231 getLastNotification
232 }