aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/utils
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-12-26 10:36:24 +0100
committerChocobozzz <chocobozzz@cpy.re>2019-01-09 11:15:15 +0100
commitcef534ed53e4518fe0acf581bfe880788d42fc36 (patch)
tree115b51ea5136849a2336d44915c7780649f25dc2 /shared/utils
parent1de1d05f4c61fe059fa5e24e79c92582f0e7e4b3 (diff)
downloadPeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.tar.gz
PeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.tar.zst
PeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.zip
Add user notification base code
Diffstat (limited to 'shared/utils')
-rw-r--r--shared/utils/server/jobs.ts15
-rw-r--r--shared/utils/socket/socket-io.ts13
-rw-r--r--shared/utils/users/user-notifications.ts232
3 files changed, 254 insertions, 6 deletions
diff --git a/shared/utils/server/jobs.ts b/shared/utils/server/jobs.ts
index f4623f896..6218c0b66 100644
--- a/shared/utils/server/jobs.ts
+++ b/shared/utils/server/jobs.ts
@@ -35,10 +35,10 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) {
35 else servers = serversArg as ServerInfo[] 35 else servers = serversArg as ServerInfo[]
36 36
37 const states: JobState[] = [ 'waiting', 'active', 'delayed' ] 37 const states: JobState[] = [ 'waiting', 'active', 'delayed' ]
38 const tasks: Promise<any>[] = [] 38 let pendingRequests = false
39 let pendingRequests: boolean
40 39
41 do { 40 function tasksBuilder () {
41 const tasks: Promise<any>[] = []
42 pendingRequests = false 42 pendingRequests = false
43 43
44 // Check if each server has pending request 44 // Check if each server has pending request
@@ -54,13 +54,16 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) {
54 } 54 }
55 } 55 }
56 56
57 await Promise.all(tasks) 57 return tasks
58 }
59
60 do {
61 await Promise.all(tasksBuilder())
58 62
59 // Retry, in case of new jobs were created 63 // Retry, in case of new jobs were created
60 if (pendingRequests === false) { 64 if (pendingRequests === false) {
61 await wait(2000) 65 await wait(2000)
62 66 await Promise.all(tasksBuilder())
63 await Promise.all(tasks)
64 } 67 }
65 68
66 if (pendingRequests) { 69 if (pendingRequests) {
diff --git a/shared/utils/socket/socket-io.ts b/shared/utils/socket/socket-io.ts
new file mode 100644
index 000000000..854ab71af
--- /dev/null
+++ b/shared/utils/socket/socket-io.ts
@@ -0,0 +1,13 @@
1import * as io from 'socket.io-client'
2
3function getUserNotificationSocket (serverUrl: string, accessToken: string) {
4 return io(serverUrl + '/user-notifications', {
5 query: { accessToken }
6 })
7}
8
9// ---------------------------------------------------------------------------
10
11export {
12 getUserNotificationSocket
13}
diff --git a/shared/utils/users/user-notifications.ts b/shared/utils/users/user-notifications.ts
new file mode 100644
index 000000000..dbe87559e
--- /dev/null
+++ b/shared/utils/users/user-notifications.ts
@@ -0,0 +1,232 @@
1/* tslint:disable:no-unused-expression */
2
3import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
4import { UserNotification, UserNotificationSetting, UserNotificationType } from '../../models/users'
5import { ServerInfo } from '..'
6import { expect } from 'chai'
7
8function 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
20function 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
36function 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
48async 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
56type CheckerBaseParams = {
57 server: ServerInfo
58 emails: object[]
59 socketNotifications: UserNotification[]
60 token: string,
61 check?: { web: boolean, mail: boolean }
62}
63
64type CheckerType = 'presence' | 'absence'
65
66async 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
97async 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
121let lastEmailCount = 0
122async 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
158async 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
186async 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
220export {
221 CheckerBaseParams,
222 CheckerType,
223 checkNotification,
224 checkNewVideoFromSubscription,
225 checkNewCommentOnMyVideo,
226 checkNewBlacklistOnMyVideo,
227 updateMyNotificationSettings,
228 checkNewVideoAbuseForModerators,
229 getUserNotifications,
230 markAsReadNotifications,
231 getLastNotification
232}