From cef534ed53e4518fe0acf581bfe880788d42fc36 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 26 Dec 2018 10:36:24 +0100 Subject: Add user notification base code --- shared/models/users/index.ts | 2 + .../users/user-notification-setting.model.ts | 13 ++ shared/models/users/user-notification.model.ts | 47 +++++ shared/models/users/user.model.ts | 2 + shared/utils/server/jobs.ts | 15 +- shared/utils/socket/socket-io.ts | 13 ++ shared/utils/users/user-notifications.ts | 232 +++++++++++++++++++++ 7 files changed, 318 insertions(+), 6 deletions(-) create mode 100644 shared/models/users/user-notification-setting.model.ts create mode 100644 shared/models/users/user-notification.model.ts create mode 100644 shared/utils/socket/socket-io.ts create mode 100644 shared/utils/users/user-notifications.ts (limited to 'shared') diff --git a/shared/models/users/index.ts b/shared/models/users/index.ts index 7114741e0..cd07cf320 100644 --- a/shared/models/users/index.ts +++ b/shared/models/users/index.ts @@ -1,6 +1,8 @@ export * from './user.model' export * from './user-create.model' export * from './user-login.model' +export * from './user-notification.model' +export * from './user-notification-setting.model' export * from './user-refresh-token.model' export * from './user-update.model' export * from './user-update-me.model' diff --git a/shared/models/users/user-notification-setting.model.ts b/shared/models/users/user-notification-setting.model.ts new file mode 100644 index 000000000..7cecd70a2 --- /dev/null +++ b/shared/models/users/user-notification-setting.model.ts @@ -0,0 +1,13 @@ +export enum UserNotificationSettingValue { + NONE = 1, + WEB_NOTIFICATION = 2, + EMAIL = 3, + WEB_NOTIFICATION_AND_EMAIL = 4 +} + +export interface UserNotificationSetting { + newVideoFromSubscription: UserNotificationSettingValue + newCommentOnMyVideo: UserNotificationSettingValue + videoAbuseAsModerator: UserNotificationSettingValue + blacklistOnMyVideo: UserNotificationSettingValue +} diff --git a/shared/models/users/user-notification.model.ts b/shared/models/users/user-notification.model.ts new file mode 100644 index 000000000..39beb2350 --- /dev/null +++ b/shared/models/users/user-notification.model.ts @@ -0,0 +1,47 @@ +export enum UserNotificationType { + NEW_VIDEO_FROM_SUBSCRIPTION = 1, + NEW_COMMENT_ON_MY_VIDEO = 2, + NEW_VIDEO_ABUSE_FOR_MODERATORS = 3, + BLACKLIST_ON_MY_VIDEO = 4, + UNBLACKLIST_ON_MY_VIDEO = 5 +} + +interface VideoInfo { + id: number + uuid: string + name: string +} + +export interface UserNotification { + id: number + type: UserNotificationType + read: boolean + + video?: VideoInfo & { + channel: { + id: number + displayName: string + } + } + + comment?: { + id: number + account: { + id: number + displayName: string + } + } + + videoAbuse?: { + id: number + video: VideoInfo + } + + videoBlacklist?: { + id: number + video: VideoInfo + } + + createdAt: string + updatedAt: string +} diff --git a/shared/models/users/user.model.ts b/shared/models/users/user.model.ts index 2aabff494..af783d389 100644 --- a/shared/models/users/user.model.ts +++ b/shared/models/users/user.model.ts @@ -2,6 +2,7 @@ import { Account } from '../actors' import { VideoChannel } from '../videos/channel/video-channel.model' import { UserRole } from './user-role' import { NSFWPolicyType } from '../videos/nsfw-policy.type' +import { UserNotificationSetting } from './user-notification-setting.model' export interface User { id: number @@ -19,6 +20,7 @@ export interface User { videoQuotaDaily: number createdAt: Date account: Account + notificationSettings?: UserNotificationSetting videoChannels?: VideoChannel[] blocked: boolean 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) { else servers = serversArg as ServerInfo[] const states: JobState[] = [ 'waiting', 'active', 'delayed' ] - const tasks: Promise[] = [] - let pendingRequests: boolean + let pendingRequests = false - do { + function tasksBuilder () { + const tasks: Promise[] = [] pendingRequests = false // Check if each server has pending request @@ -54,13 +54,16 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { } } - await Promise.all(tasks) + return tasks + } + + do { + await Promise.all(tasksBuilder()) // Retry, in case of new jobs were created if (pendingRequests === false) { await wait(2000) - - await Promise.all(tasks) + await Promise.all(tasksBuilder()) } 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 @@ +import * as io from 'socket.io-client' + +function getUserNotificationSocket (serverUrl: string, accessToken: string) { + return io(serverUrl + '/user-notifications', { + query: { accessToken } + }) +} + +// --------------------------------------------------------------------------- + +export { + getUserNotificationSocket +} 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 @@ +/* tslint:disable:no-unused-expression */ + +import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' +import { UserNotification, UserNotificationSetting, UserNotificationType } from '../../models/users' +import { ServerInfo } from '..' +import { expect } from 'chai' + +function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) { + const path = '/api/v1/users/me/notification-settings' + + return makePutBodyRequest({ + url, + path, + token, + fields: settings, + statusCodeExpected + }) +} + +function getUserNotifications (url: string, token: string, start: number, count: number, sort = '-createdAt', statusCodeExpected = 200) { + const path = '/api/v1/users/me/notifications' + + return makeGetRequest({ + url, + path, + token, + query: { + start, + count, + sort + }, + statusCodeExpected + }) +} + +function markAsReadNotifications (url: string, token: string, ids: number[], statusCodeExpected = 204) { + const path = '/api/v1/users/me/notifications/read' + + return makePostBodyRequest({ + url, + path, + token, + fields: { ids }, + statusCodeExpected + }) +} + +async function getLastNotification (serverUrl: string, accessToken: string) { + const res = await getUserNotifications(serverUrl, accessToken, 0, 1, '-createdAt') + + if (res.body.total === 0) return undefined + + return res.body.data[0] as UserNotification +} + +type CheckerBaseParams = { + server: ServerInfo + emails: object[] + socketNotifications: UserNotification[] + token: string, + check?: { web: boolean, mail: boolean } +} + +type CheckerType = 'presence' | 'absence' + +async function checkNotification ( + base: CheckerBaseParams, + lastNotificationChecker: (notification: UserNotification) => void, + socketNotificationFinder: (notification: UserNotification) => boolean, + emailNotificationFinder: (email: object) => boolean, + checkType: 'presence' | 'absence' +) { + const check = base.check || { web: true, mail: true } + + if (check.web) { + const notification = await getLastNotification(base.server.url, base.token) + lastNotificationChecker(notification) + + const socketNotification = base.socketNotifications.find(n => socketNotificationFinder(n)) + + if (checkType === 'presence') expect(socketNotification, 'The socket notification is absent.').to.not.be.undefined + else expect(socketNotification, 'The socket notification is present.').to.be.undefined + } + + if (check.mail) { + // Last email + const email = base.emails + .slice() + .reverse() + .find(e => emailNotificationFinder(e)) + + if (checkType === 'presence') expect(email, 'The email is present.').to.not.be.undefined + else expect(email, 'The email is absent.').to.be.undefined + } +} + +async function checkNewVideoFromSubscription (base: CheckerBaseParams, videoName: string, videoUUID: string, type: CheckerType) { + const notificationType = UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION + + function lastNotificationChecker (notification: UserNotification) { + if (type === 'presence') { + expect(notification).to.not.be.undefined + expect(notification.type).to.equal(notificationType) + expect(notification.video.name).to.equal(videoName) + } else { + expect(notification.video).to.satisfy(v => v === undefined || v.name !== videoName) + } + } + + function socketFinder (notification: UserNotification) { + return notification.type === notificationType && notification.video.name === videoName + } + + function emailFinder (email: object) { + return email[ 'text' ].indexOf(videoUUID) !== -1 + } + + await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, type) +} + +let lastEmailCount = 0 +async function checkNewCommentOnMyVideo (base: CheckerBaseParams, uuid: string, commentId: number, threadId: number, type: CheckerType) { + const notificationType = UserNotificationType.NEW_COMMENT_ON_MY_VIDEO + + function lastNotificationChecker (notification: UserNotification) { + if (type === 'presence') { + expect(notification).to.not.be.undefined + expect(notification.type).to.equal(notificationType) + expect(notification.comment.id).to.equal(commentId) + expect(notification.comment.account.displayName).to.equal('root') + } else { + expect(notification).to.satisfy((n: UserNotification) => { + return n === undefined || n.comment === undefined || n.comment.id !== commentId + }) + } + } + + function socketFinder (notification: UserNotification) { + return notification.type === notificationType && + notification.comment.id === commentId && + notification.comment.account.displayName === 'root' + } + + const commentUrl = `http://localhost:9001/videos/watch/${uuid};threadId=${threadId}` + function emailFinder (email: object) { + return email[ 'text' ].indexOf(commentUrl) !== -1 + } + + await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, type) + + if (type === 'presence') { + // We cannot detect email duplicates, so check we received another email + expect(base.emails).to.have.length.above(lastEmailCount) + lastEmailCount = base.emails.length + } +} + +async function checkNewVideoAbuseForModerators (base: CheckerBaseParams, videoUUID: string, videoName: string, type: CheckerType) { + const notificationType = UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS + + function lastNotificationChecker (notification: UserNotification) { + if (type === 'presence') { + expect(notification).to.not.be.undefined + expect(notification.type).to.equal(notificationType) + expect(notification.videoAbuse.video.uuid).to.equal(videoUUID) + expect(notification.videoAbuse.video.name).to.equal(videoName) + } else { + expect(notification).to.satisfy((n: UserNotification) => { + return n === undefined || n.videoAbuse === undefined || n.videoAbuse.video.uuid !== videoUUID + }) + } + } + + function socketFinder (notification: UserNotification) { + return notification.type === notificationType && notification.videoAbuse.video.uuid === videoUUID + } + + function emailFinder (email: object) { + const text = email[ 'text' ] + return text.indexOf(videoUUID) !== -1 && text.indexOf('abuse') !== -1 + } + + await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, type) +} + +async function checkNewBlacklistOnMyVideo ( + base: CheckerBaseParams, + videoUUID: string, + videoName: string, + blacklistType: 'blacklist' | 'unblacklist' +) { + const notificationType = blacklistType === 'blacklist' + ? UserNotificationType.BLACKLIST_ON_MY_VIDEO + : UserNotificationType.UNBLACKLIST_ON_MY_VIDEO + + function lastNotificationChecker (notification: UserNotification) { + expect(notification).to.not.be.undefined + expect(notification.type).to.equal(notificationType) + + const video = blacklistType === 'blacklist' ? notification.videoBlacklist.video : notification.video + + expect(video.uuid).to.equal(videoUUID) + expect(video.name).to.equal(videoName) + } + + function socketFinder (notification: UserNotification) { + return notification.type === notificationType && (notification.video || notification.videoBlacklist.video).uuid === videoUUID + } + + function emailFinder (email: object) { + const text = email[ 'text' ] + return text.indexOf(videoUUID) !== -1 && text.indexOf(' ' + blacklistType) !== -1 + } + + await checkNotification(base, lastNotificationChecker, socketFinder, emailFinder, 'presence') +} + +// --------------------------------------------------------------------------- + +export { + CheckerBaseParams, + CheckerType, + checkNotification, + checkNewVideoFromSubscription, + checkNewCommentOnMyVideo, + checkNewBlacklistOnMyVideo, + updateMyNotificationSettings, + checkNewVideoAbuseForModerators, + getUserNotifications, + markAsReadNotifications, + getLastNotification +} -- cgit v1.2.3