From: Chocobozzz Date: Mon, 8 Apr 2019 15:26:01 +0000 (+0200) Subject: Add notification on new instance follower (server side) X-Git-Tag: v1.3.0-rc.1~82 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=883993c81ecc2388d4a4b37b29b81b6de73d264f;p=oweals%2Fpeertube.git Add notification on new instance follower (server side) --- diff --git a/server/controllers/api/users/my-notifications.ts b/server/controllers/api/users/my-notifications.ts index 4edad2a74..f146284e4 100644 --- a/server/controllers/api/users/my-notifications.ts +++ b/server/controllers/api/users/my-notifications.ts @@ -75,7 +75,8 @@ async function updateNotificationSettings (req: express.Request, res: express.Re myVideoImportFinished: body.myVideoImportFinished, newFollow: body.newFollow, newUserRegistration: body.newUserRegistration, - commentMention: body.commentMention + commentMention: body.commentMention, + newInstanceFollower: body.newInstanceFollower } await UserNotificationSettingModel.update(values, query) diff --git a/server/initializers/migrations/0360-notification-instance-follower.ts b/server/initializers/migrations/0360-notification-instance-follower.ts new file mode 100644 index 000000000..05caf8e1d --- /dev/null +++ b/server/initializers/migrations/0360-notification-instance-follower.ts @@ -0,0 +1,40 @@ +import * as Sequelize from 'sequelize' + +async function up (utils: { + transaction: Sequelize.Transaction, + queryInterface: Sequelize.QueryInterface, + sequelize: Sequelize.Sequelize, + db: any +}): Promise { + { + const data = { + type: Sequelize.INTEGER, + defaultValue: null, + allowNull: true + } + await utils.queryInterface.addColumn('userNotificationSetting', 'newInstanceFollower', data) + } + + { + const query = 'UPDATE "userNotificationSetting" SET "newInstanceFollower" = 1' + await utils.sequelize.query(query) + } + + { + const data = { + type: Sequelize.INTEGER, + defaultValue: null, + allowNull: false + } + await utils.queryInterface.changeColumn('userNotificationSetting', 'newInstanceFollower', data) + } +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index 63e810642..c0ad07a52 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -342,6 +342,8 @@ function saveActorAndServerAndModelIfNotExist ( actorCreated.VideoChannel.Account = ownerActor.Account } + actorCreated.Server = server + return actorCreated } } diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts index 140bbe9f1..276a57e60 100644 --- a/server/lib/activitypub/process/process-follow.ts +++ b/server/lib/activitypub/process/process-follow.ts @@ -24,14 +24,16 @@ export { // --------------------------------------------------------------------------- async function processFollow (actor: ActorModel, targetActorURL: string) { - const { actorFollow, created } = await sequelizeTypescript.transaction(async t => { + const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => { const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t) if (!targetActor) throw new Error('Unknown actor') if (targetActor.isOwned() === false) throw new Error('This is not a local actor.') const serverActor = await getServerActor() - if (targetActor.id === serverActor.id && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) { + const isFollowingInstance = targetActor.id === serverActor.id + + if (isFollowingInstance && CONFIG.FOLLOWERS.INSTANCE.ENABLED === false) { logger.info('Rejecting %s because instance followers are disabled.', targetActor.url) return sendReject(actor, targetActor) @@ -50,9 +52,6 @@ async function processFollow (actor: ActorModel, targetActorURL: string) { transaction: t }) - actorFollow.ActorFollower = actor - actorFollow.ActorFollowing = targetActor - if (actorFollow.state !== 'accepted' && CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL === false) { actorFollow.state = 'accepted' await actorFollow.save({ transaction: t }) @@ -64,10 +63,16 @@ async function processFollow (actor: ActorModel, targetActorURL: string) { // Target sends to actor he accepted the follow request if (actorFollow.state === 'accepted') await sendAccept(actorFollow) - return { actorFollow, created } + return { actorFollow, created, isFollowingInstance } }) - if (created) Notifier.Instance.notifyOfNewFollow(actorFollow) + // Rejected + if (!actorFollow) return + + if (created) { + if (isFollowingInstance) Notifier.Instance.notifyOfNewInstanceFollow(actorFollow) + else Notifier.Instance.notifyOfNewUserFollow(actorFollow) + } logger.info('Actor %s is followed by actor %s.', targetActorURL, actor.url) } diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index eec97c27e..aa9083362 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts @@ -129,6 +129,24 @@ class Emailer { return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) } + addNewInstanceFollowerNotification (to: string[], actorFollow: ActorFollowModel) { + const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : '' + + const text = `Hi dear admin,\n\n` + + `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` + + `\n\n` + + `Cheers,\n` + + `PeerTube.` + + const emailPayload: EmailPayload = { + to, + subject: 'New instance follower', + text + } + + return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) + } + myVideoPublishedNotification (to: string[], video: VideoModel) { const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() diff --git a/server/lib/job-queue/handlers/activitypub-follow.ts b/server/lib/job-queue/handlers/activitypub-follow.ts index b4d381062..e7e5ff950 100644 --- a/server/lib/job-queue/handlers/activitypub-follow.ts +++ b/server/lib/job-queue/handlers/activitypub-follow.ts @@ -73,5 +73,5 @@ async function follow (fromActor: ActorModel, targetActor: ActorModel) { return actorFollow }) - if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewFollow(actorFollow) + if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollow) } diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts index 9fe93ec0d..91b71cc64 100644 --- a/server/lib/notifier.ts +++ b/server/lib/notifier.ts @@ -92,18 +92,25 @@ class Notifier { .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) } - notifyOfNewFollow (actorFollow: ActorFollowModel): void { + notifyOfNewUserFollow (actorFollow: ActorFollowModel): void { this.notifyUserOfNewActorFollow(actorFollow) .catch(err => { logger.error( 'Cannot notify owner of channel %s of a new follow by %s.', actorFollow.ActorFollowing.VideoChannel.getDisplayName(), actorFollow.ActorFollower.Account.getDisplayName(), - err + { err } ) }) } + notifyOfNewInstanceFollow (actorFollow: ActorFollowModel): void { + this.notifyAdminsOfNewInstanceFollow(actorFollow) + .catch(err => { + logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }) + }) + } + private async notifySubscribersOfNewVideo (video: VideoModel) { // List all followers that are users const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) @@ -261,6 +268,33 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } + private async notifyAdminsOfNewInstanceFollow (actorFollow: ActorFollowModel) { + const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) + + logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url) + + function settingGetter (user: UserModel) { + return user.NotificationSetting.newInstanceFollower + } + + async function notificationCreator (user: UserModel) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.NEW_INSTANCE_FOLLOWER, + userId: user.id, + actorFollowId: actorFollow.id + }) + notification.ActorFollow = actorFollow + + return notification + } + + function emailSender (emails: string[]) { + return Emailer.Instance.addNewInstanceFollowerNotification(emails, actorFollow) + } + + return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) + } + private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) { const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) if (moderators.length === 0) return diff --git a/server/lib/user.ts b/server/lib/user.ts index 5588b0f76..6fbe3ed03 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts @@ -110,7 +110,8 @@ function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.Tr blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, newUserRegistration: UserNotificationSettingValue.WEB, commentMention: UserNotificationSettingValue.WEB, - newFollow: UserNotificationSettingValue.WEB + newFollow: UserNotificationSettingValue.WEB, + newInstanceFollower: UserNotificationSettingValue.WEB } return UserNotificationSettingModel.create(values, { transaction: t }) diff --git a/server/middlewares/validators/user-notifications.ts b/server/middlewares/validators/user-notifications.ts index 46486e081..3ded8d8cf 100644 --- a/server/middlewares/validators/user-notifications.ts +++ b/server/middlewares/validators/user-notifications.ts @@ -28,8 +28,22 @@ const updateNotificationSettingsValidator = [ .custom(isUserNotificationSettingValid).withMessage('Should have a valid new comment on my video notification setting'), body('videoAbuseAsModerator') .custom(isUserNotificationSettingValid).withMessage('Should have a valid new video abuse as moderator notification setting'), + body('videoAutoBlacklistAsModerator') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid video auto blacklist notification setting'), body('blacklistOnMyVideo') .custom(isUserNotificationSettingValid).withMessage('Should have a valid new blacklist on my video notification setting'), + body('myVideoImportFinished') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid video import finished video notification setting'), + body('myVideoPublished') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid video published notification setting'), + body('commentMention') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid comment mention notification setting'), + body('newFollow') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid new follow notification setting'), + body('newUserRegistration') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid new user registration notification setting'), + body('newInstanceFollower') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid new instance follower notification setting'), (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking updateNotificationSettingsValidator parameters', { parameters: req.body }) diff --git a/server/models/account/user-notification-setting.ts b/server/models/account/user-notification-setting.ts index ba7f739b9..c2fbc6d23 100644 --- a/server/models/account/user-notification-setting.ts +++ b/server/models/account/user-notification-setting.ts @@ -101,6 +101,15 @@ export class UserNotificationSettingModel extends Model throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower') + ) + @Column + newInstanceFollower: UserNotificationSettingValue + @AllowNull(false) @Default(null) @Is( @@ -154,7 +163,8 @@ export class UserNotificationSettingModel extends Model { const actorFollow = this.ActorFollow ? { id: this.ActorFollow.id, + state: this.ActorFollow.state, follower: { id: this.ActorFollow.ActorFollower.Account.id, displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), diff --git a/server/tests/api/check-params/user-notifications.ts b/server/tests/api/check-params/user-notifications.ts index 36eaceac7..4b75f6920 100644 --- a/server/tests/api/check-params/user-notifications.ts +++ b/server/tests/api/check-params/user-notifications.ts @@ -174,7 +174,8 @@ describe('Test user notifications API validators', function () { myVideoPublished: UserNotificationSettingValue.WEB, commentMention: UserNotificationSettingValue.WEB, newFollow: UserNotificationSettingValue.WEB, - newUserRegistration: UserNotificationSettingValue.WEB + newUserRegistration: UserNotificationSettingValue.WEB, + newInstanceFollower: UserNotificationSettingValue.WEB } it('Should fail with missing fields', async function () { diff --git a/server/tests/api/index-1.ts b/server/tests/api/index-1.ts index 80d752f42..75cdd9025 100644 --- a/server/tests/api/index-1.ts +++ b/server/tests/api/index-1.ts @@ -1,2 +1,3 @@ import './check-params' +import './notifications' import './search' diff --git a/server/tests/api/notifications/index.ts b/server/tests/api/notifications/index.ts new file mode 100644 index 000000000..95ac8fc51 --- /dev/null +++ b/server/tests/api/notifications/index.ts @@ -0,0 +1 @@ +export * from './user-notifications' diff --git a/server/tests/api/notifications/user-notifications.ts b/server/tests/api/notifications/user-notifications.ts new file mode 100644 index 000000000..7bff52796 --- /dev/null +++ b/server/tests/api/notifications/user-notifications.ts @@ -0,0 +1,1299 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { + addVideoToBlacklist, + createUser, + doubleFollow, + flushAndRunMultipleServers, + flushTests, + getMyUserInformation, + immutableAssign, + registerUser, + removeVideoFromBlacklist, + reportVideoAbuse, + updateMyUser, + updateVideo, + updateVideoChannel, + userLogin, + wait, + getCustomConfig, + updateCustomConfig, getVideoThreadComments, getVideoCommentThreads, follow +} from '../../../../shared/utils' +import { killallServers, ServerInfo, uploadVideo } from '../../../../shared/utils/index' +import { setAccessTokensToServers } from '../../../../shared/utils/users/login' +import { waitJobs } from '../../../../shared/utils/server/jobs' +import { getUserNotificationSocket } from '../../../../shared/utils/socket/socket-io' +import { + checkCommentMention, + CheckerBaseParams, + checkMyVideoImportIsFinished, + checkNewActorFollow, + checkNewBlacklistOnMyVideo, + checkNewCommentOnMyVideo, + checkNewVideoAbuseForModerators, + checkVideoAutoBlacklistForModerators, + checkNewVideoFromSubscription, + checkUserRegistered, + checkVideoIsPublished, + getLastNotification, + getUserNotifications, + markAsReadNotifications, + updateMyNotificationSettings, + markAsReadAllNotifications, checkNewInstanceFollower +} from '../../../../shared/utils/users/user-notifications' +import { + User, + UserNotification, + UserNotificationSetting, + UserNotificationSettingValue, + UserNotificationType +} from '../../../../shared/models/users' +import { MockSmtpServer } from '../../../../shared/utils/miscs/email' +import { addUserSubscription, removeUserSubscription } from '../../../../shared/utils/users/user-subscriptions' +import { VideoPrivacy } from '../../../../shared/models/videos' +import { getBadVideoUrl, getYoutubeVideoUrl, importVideo } from '../../../../shared/utils/videos/video-imports' +import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/utils/videos/video-comments' +import * as uuidv4 from 'uuid/v4' +import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/utils/users/blocklist' +import { CustomConfig } from '../../../../shared/models/server' +import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model' + +const expect = chai.expect + +async function uploadVideoByRemoteAccount (servers: ServerInfo[], additionalParams: any = {}) { + const name = 'remote video ' + uuidv4() + + const data = Object.assign({ name }, additionalParams) + const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, data) + + await waitJobs(servers) + + return { uuid: res.body.video.uuid, name } +} + +async function uploadVideoByLocalAccount (servers: ServerInfo[], additionalParams: any = {}) { + const name = 'local video ' + uuidv4() + + const data = Object.assign({ name }, additionalParams) + const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, data) + + await waitJobs(servers) + + return { uuid: res.body.video.uuid, name } +} + +describe('Test users notifications', function () { + let servers: ServerInfo[] = [] + let userAccessToken: string + let userNotifications: UserNotification[] = [] + let adminNotifications: UserNotification[] = [] + let adminNotificationsServer2: UserNotification[] = [] + const emails: object[] = [] + let channelId: number + + const allNotificationSettings: UserNotificationSetting = { + newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL + } + + before(async function () { + this.timeout(120000) + + await MockSmtpServer.Instance.collectEmails(emails) + + await flushTests() + + const overrideConfig = { + smtp: { + hostname: 'localhost' + } + } + servers = await flushAndRunMultipleServers(3, overrideConfig) + + // Get the access tokens + await setAccessTokensToServers(servers) + + // Server 1 and server 2 follow each other + await doubleFollow(servers[0], servers[1]) + + await waitJobs(servers) + + const user = { + username: 'user_1', + password: 'super password' + } + await createUser(servers[0].url, servers[0].accessToken, user.username, user.password, 10 * 1000 * 1000) + userAccessToken = await userLogin(servers[0], user) + + await updateMyNotificationSettings(servers[0].url, userAccessToken, allNotificationSettings) + await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, allNotificationSettings) + await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, allNotificationSettings) + + { + const socket = getUserNotificationSocket(servers[ 0 ].url, userAccessToken) + socket.on('new-notification', n => userNotifications.push(n)) + } + { + const socket = getUserNotificationSocket(servers[ 0 ].url, servers[0].accessToken) + socket.on('new-notification', n => adminNotifications.push(n)) + } + { + const socket = getUserNotificationSocket(servers[ 1 ].url, servers[1].accessToken) + socket.on('new-notification', n => adminNotificationsServer2.push(n)) + } + + { + const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken) + channelId = resChannel.body.videoChannels[0].id + } + }) + + describe('New video from my subscription notification', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + }) + + it('Should not send notifications if the user does not follow the video publisher', async function () { + this.timeout(10000) + + await uploadVideoByLocalAccount(servers) + + const notification = await getLastNotification(servers[ 0 ].url, userAccessToken) + expect(notification).to.be.undefined + + expect(emails).to.have.lengthOf(0) + expect(userNotifications).to.have.lengthOf(0) + }) + + it('Should send a new video notification if the user follows the local video publisher', async function () { + this.timeout(15000) + + await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9001') + await waitJobs(servers) + + const { name, uuid } = await uploadVideoByLocalAccount(servers) + await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + }) + + it('Should send a new video notification from a remote account', async function () { + this.timeout(50000) // Server 2 has transcoding enabled + + await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9002') + await waitJobs(servers) + + const { name, uuid } = await uploadVideoByRemoteAccount(servers) + await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + }) + + it('Should send a new video notification on a scheduled publication', async function () { + this.timeout(20000) + + // In 2 seconds + let updateAt = new Date(new Date().getTime() + 2000) + + const data = { + privacy: VideoPrivacy.PRIVATE, + scheduleUpdate: { + updateAt: updateAt.toISOString(), + privacy: VideoPrivacy.PUBLIC + } + } + const { name, uuid } = await uploadVideoByLocalAccount(servers, data) + + await wait(6000) + await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + }) + + it('Should send a new video notification on a remote scheduled publication', async function () { + this.timeout(50000) + + // In 2 seconds + let updateAt = new Date(new Date().getTime() + 2000) + + const data = { + privacy: VideoPrivacy.PRIVATE, + scheduleUpdate: { + updateAt: updateAt.toISOString(), + privacy: VideoPrivacy.PUBLIC + } + } + const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + await waitJobs(servers) + + await wait(6000) + await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + }) + + it('Should not send a notification before the video is published', async function () { + this.timeout(20000) + + let updateAt = new Date(new Date().getTime() + 1000000) + + const data = { + privacy: VideoPrivacy.PRIVATE, + scheduleUpdate: { + updateAt: updateAt.toISOString(), + privacy: VideoPrivacy.PUBLIC + } + } + const { name, uuid } = await uploadVideoByLocalAccount(servers, data) + + await wait(6000) + await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') + }) + + it('Should send a new video notification when a video becomes public', async function () { + this.timeout(10000) + + const data = { privacy: VideoPrivacy.PRIVATE } + const { name, uuid } = await uploadVideoByLocalAccount(servers, data) + + await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') + + await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC }) + + await wait(500) + await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + }) + + it('Should send a new video notification when a remote video becomes public', async function () { + this.timeout(20000) + + const data = { privacy: VideoPrivacy.PRIVATE } + const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + + await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') + + await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC }) + + await waitJobs(servers) + await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + }) + + it('Should not send a new video notification when a video becomes unlisted', async function () { + this.timeout(20000) + + const data = { privacy: VideoPrivacy.PRIVATE } + const { name, uuid } = await uploadVideoByLocalAccount(servers, data) + + await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED }) + + await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') + }) + + it('Should not send a new video notification when a remote video becomes unlisted', async function () { + this.timeout(20000) + + const data = { privacy: VideoPrivacy.PRIVATE } + const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + + await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED }) + + await waitJobs(servers) + await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') + }) + + it('Should send a new video notification after a video import', async function () { + this.timeout(100000) + + const name = 'video import ' + uuidv4() + + const attributes = { + name, + channelId, + privacy: VideoPrivacy.PUBLIC, + targetUrl: getYoutubeVideoUrl() + } + const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) + const uuid = res.body.video.uuid + + await waitJobs(servers) + + await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + }) + }) + + describe('Comment on my video notifications', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + }) + + it('Should not send a new comment notification after a comment on another video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') + }) + + it('Should not send a new comment notification if I comment my own video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') + }) + + it('Should not send a new comment notification if the account is muted', async function () { + this.timeout(10000) + + await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root') + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') + + await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root') + }) + + it('Should send a new comment notification after a local comment on my video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') + }) + + it('Should send a new comment notification after a remote comment on my video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + + await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') + + await waitJobs(servers) + + const resComment = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) + expect(resComment.body.data).to.have.lengthOf(1) + const commentId = resComment.body.data[0].id + + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') + }) + + it('Should send a new comment notification after a local reply on my video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') + const threadId = resThread.body.comment.id + + const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') + }) + + it('Should send a new comment notification after a remote reply on my video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + await waitJobs(servers) + + { + const resThread = await addVideoCommentThread(servers[ 1 ].url, servers[ 1 ].accessToken, uuid, 'comment') + const threadId = resThread.body.comment.id + await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, uuid, threadId, 'reply') + } + + await waitJobs(servers) + + const resThread = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) + expect(resThread.body.data).to.have.lengthOf(1) + const threadId = resThread.body.data[0].id + + const resComments = await getVideoThreadComments(servers[0].url, uuid, threadId) + const tree = resComments.body as VideoCommentThreadTree + + expect(tree.children).to.have.lengthOf(1) + const commentId = tree.children[0].comment.id + + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') + }) + }) + + describe('Mention notifications', function () { + let baseParams: CheckerBaseParams + + before(async () => { + baseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + + await updateMyUser({ + url: servers[0].url, + accessToken: servers[0].accessToken, + displayName: 'super root name' + }) + + await updateMyUser({ + url: servers[1].url, + accessToken: servers[1].accessToken, + displayName: 'super root 2 name' + }) + }) + + it('Should not send a new mention comment notification if I mention the video owner', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') + const commentId = resComment.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') + }) + + it('Should not send a new mention comment notification if I mention myself', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello') + const commentId = resComment.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') + }) + + it('Should not send a new mention notification if the account is muted', async function () { + this.timeout(10000) + + await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root') + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') + const commentId = resComment.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') + + await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root') + }) + + it('Should not send a new mention notification if the remote account mention a local account', async function () { + this.timeout(20000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello') + const threadId = resThread.body.comment.id + + await waitJobs(servers) + await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence') + }) + + it('Should send a new mention notification after local comments', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1') + const threadId = resThread.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence') + + const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1') + const commentId = resComment.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence') + }) + + it('Should send a new mention notification after remote comments', async function () { + this.timeout(20000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'hello @user_1@localhost:9001 1') + const server2ThreadId = resThread.body.comment.id + + await waitJobs(servers) + + const resThread2 = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) + expect(resThread2.body.data).to.have.lengthOf(1) + const server1ThreadId = resThread2.body.data[0].id + await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence') + + const text = '@user_1@localhost:9001 hello 2 @root@localhost:9001' + await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, server2ThreadId, text) + + await waitJobs(servers) + + const resComments = await getVideoThreadComments(servers[0].url, uuid, server1ThreadId) + const tree = resComments.body as VideoCommentThreadTree + + expect(tree.children).to.have.lengthOf(1) + const commentId = tree.children[0].comment.id + + await checkCommentMention(baseParams, uuid, commentId, server1ThreadId, 'super root 2 name', 'presence') + }) + }) + + describe('Video abuse for moderators notification' , function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: adminNotifications, + token: servers[0].accessToken + } + }) + + it('Should send a notification to moderators on local video abuse', async function () { + this.timeout(10000) + + const name = 'video for abuse ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) + const uuid = resVideo.body.video.uuid + + await reportVideoAbuse(servers[0].url, servers[0].accessToken, uuid, 'super reason') + + await waitJobs(servers) + await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence') + }) + + it('Should send a notification to moderators on remote video abuse', async function () { + this.timeout(10000) + + const name = 'video for abuse ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + + await reportVideoAbuse(servers[1].url, servers[1].accessToken, uuid, 'super reason') + + await waitJobs(servers) + await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence') + }) + }) + + describe('Video blacklist on my video', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + }) + + it('Should send a notification to video owner on blacklist', async function () { + this.timeout(10000) + + const name = 'video for abuse ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) + const uuid = resVideo.body.video.uuid + + await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) + + await waitJobs(servers) + await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist') + }) + + it('Should send a notification to video owner on unblacklist', async function () { + this.timeout(10000) + + const name = 'video for abuse ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) + const uuid = resVideo.body.video.uuid + + await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) + + await waitJobs(servers) + await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) + await waitJobs(servers) + + await wait(500) + await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist') + }) + }) + + describe('My video is published', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[1], + emails, + socketNotifications: adminNotificationsServer2, + token: servers[1].accessToken + } + }) + + it('Should not send a notification if transcoding is not enabled', async function () { + this.timeout(10000) + + const { name, uuid } = await uploadVideoByLocalAccount(servers) + await waitJobs(servers) + + await checkVideoIsPublished(baseParams, name, uuid, 'absence') + }) + + it('Should not send a notification if the wait transcoding is false', async function () { + this.timeout(50000) + + await uploadVideoByRemoteAccount(servers, { waitTranscoding: false }) + await waitJobs(servers) + + const notification = await getLastNotification(servers[ 0 ].url, userAccessToken) + if (notification) { + expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED) + } + }) + + it('Should send a notification even if the video is not transcoded in other resolutions', async function () { + this.timeout(50000) + + const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true, fixture: 'video_short_240p.mp4' }) + await waitJobs(servers) + + await checkVideoIsPublished(baseParams, name, uuid, 'presence') + }) + + it('Should send a notification with a transcoded video', async function () { + this.timeout(50000) + + const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true }) + await waitJobs(servers) + + await checkVideoIsPublished(baseParams, name, uuid, 'presence') + }) + + it('Should send a notification when an imported video is transcoded', async function () { + this.timeout(50000) + + const name = 'video import ' + uuidv4() + + const attributes = { + name, + channelId, + privacy: VideoPrivacy.PUBLIC, + targetUrl: getYoutubeVideoUrl(), + waitTranscoding: true + } + const res = await importVideo(servers[1].url, servers[1].accessToken, attributes) + const uuid = res.body.video.uuid + + await waitJobs(servers) + await checkVideoIsPublished(baseParams, name, uuid, 'presence') + }) + + it('Should send a notification when the scheduled update has been proceeded', async function () { + this.timeout(70000) + + // In 2 seconds + let updateAt = new Date(new Date().getTime() + 2000) + + const data = { + privacy: VideoPrivacy.PRIVATE, + scheduleUpdate: { + updateAt: updateAt.toISOString(), + privacy: VideoPrivacy.PUBLIC + } + } + const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + + await wait(6000) + await checkVideoIsPublished(baseParams, name, uuid, 'presence') + }) + + it('Should not send a notification before the video is published', async function () { + this.timeout(20000) + + let updateAt = new Date(new Date().getTime() + 100000) + + const data = { + privacy: VideoPrivacy.PRIVATE, + scheduleUpdate: { + updateAt: updateAt.toISOString(), + privacy: VideoPrivacy.PUBLIC + } + } + const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + + await wait(6000) + await checkVideoIsPublished(baseParams, name, uuid, 'absence') + }) + }) + + describe('My video is imported', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: adminNotifications, + token: servers[0].accessToken + } + }) + + it('Should send a notification when the video import failed', async function () { + this.timeout(70000) + + const name = 'video import ' + uuidv4() + + const attributes = { + name, + channelId, + privacy: VideoPrivacy.PRIVATE, + targetUrl: getBadVideoUrl() + } + const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) + const uuid = res.body.video.uuid + + await waitJobs(servers) + await checkMyVideoImportIsFinished(baseParams, name, uuid, getBadVideoUrl(), false, 'presence') + }) + + it('Should send a notification when the video import succeeded', async function () { + this.timeout(70000) + + const name = 'video import ' + uuidv4() + + const attributes = { + name, + channelId, + privacy: VideoPrivacy.PRIVATE, + targetUrl: getYoutubeVideoUrl() + } + const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) + const uuid = res.body.video.uuid + + await waitJobs(servers) + await checkMyVideoImportIsFinished(baseParams, name, uuid, getYoutubeVideoUrl(), true, 'presence') + }) + }) + + describe('New registration', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: adminNotifications, + token: servers[0].accessToken + } + }) + + it('Should send a notification only to moderators when a user registers on the instance', async function () { + this.timeout(10000) + + await registerUser(servers[0].url, 'user_45', 'password') + + await waitJobs(servers) + + await checkUserRegistered(baseParams, 'user_45', 'presence') + + const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } + await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence') + }) + }) + + describe('New instance follower', function () { + let baseParams: CheckerBaseParams + + before(async () => { + baseParams = { + server: servers[0], + emails, + socketNotifications: adminNotifications, + token: servers[0].accessToken + } + }) + + it('Should send a notification only to admin when there is a new instance follower', async function () { + this.timeout(10000) + + await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) + + await waitJobs(servers) + + await checkNewInstanceFollower(baseParams, 'localhost:9003', 'presence') + + const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } + await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:9003', 'absence') + }) + }) + + describe('New actor follow', function () { + let baseParams: CheckerBaseParams + let myChannelName = 'super channel name' + let myUserName = 'super user name' + + before(async () => { + baseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + + await updateMyUser({ + url: servers[0].url, + accessToken: servers[0].accessToken, + displayName: 'super root name' + }) + + await updateMyUser({ + url: servers[0].url, + accessToken: userAccessToken, + displayName: myUserName + }) + + await updateMyUser({ + url: servers[1].url, + accessToken: servers[1].accessToken, + displayName: 'super root 2 name' + }) + + await updateVideoChannel(servers[0].url, userAccessToken, 'user_1_channel', { displayName: myChannelName }) + }) + + it('Should notify when a local channel is following one of our channel', async function () { + this.timeout(10000) + + await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001') + await waitJobs(servers) + + await checkNewActorFollow(baseParams, 'channel', 'root', 'super root name', myChannelName, 'presence') + + await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001') + }) + + it('Should notify when a remote channel is following one of our channel', async function () { + this.timeout(10000) + + await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001') + await waitJobs(servers) + + await checkNewActorFollow(baseParams, 'channel', 'root', 'super root 2 name', myChannelName, 'presence') + + await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001') + }) + + it('Should notify when a local account is following one of our channel', async function () { + this.timeout(10000) + + await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:9001') + + await waitJobs(servers) + + await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence') + }) + + it('Should notify when a remote account is following one of our channel', async function () { + this.timeout(10000) + + await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:9001') + + await waitJobs(servers) + + await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence') + }) + }) + + describe('Video-related notifications when video auto-blacklist is enabled', function () { + let userBaseParams: CheckerBaseParams + let adminBaseParamsServer1: CheckerBaseParams + let adminBaseParamsServer2: CheckerBaseParams + let videoUUID: string + let videoName: string + let currentCustomConfig: CustomConfig + + before(async () => { + + adminBaseParamsServer1 = { + server: servers[0], + emails, + socketNotifications: adminNotifications, + token: servers[0].accessToken + } + + adminBaseParamsServer2 = { + server: servers[1], + emails, + socketNotifications: adminNotificationsServer2, + token: servers[1].accessToken + } + + userBaseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + + const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken) + currentCustomConfig = resCustomConfig.body + const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, { + autoBlacklist: { + videos: { + ofUsers: { + enabled: true + } + } + } + }) + // enable transcoding otherwise own publish notification after transcoding not expected + autoBlacklistTestsCustomConfig.transcoding.enabled = true + await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig) + + await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001') + await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001') + + }) + + it('Should send notification to moderators on new video with auto-blacklist', async function () { + this.timeout(20000) + + videoName = 'video with auto-blacklist ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName }) + videoUUID = resVideo.body.video.uuid + + await waitJobs(servers) + await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence') + }) + + it('Should not send video publish notification if auto-blacklisted', async function () { + await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence') + }) + + it('Should not send a local user subscription notification if auto-blacklisted', async function () { + await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence') + }) + + it('Should not send a remote user subscription notification if auto-blacklisted', async function () { + await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence') + }) + + it('Should send video published and unblacklist after video unblacklisted', async function () { + this.timeout(20000) + + await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID) + + await waitJobs(servers) + + // FIXME: Can't test as two notifications sent to same user and util only checks last one + // One notification might be better anyways + // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist') + // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence') + }) + + it('Should send a local user subscription notification after removed from blacklist', async function () { + await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence') + }) + + it('Should send a remote user subscription notification after removed from blacklist', async function () { + await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence') + }) + + it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () { + this.timeout(20000) + + let updateAt = new Date(new Date().getTime() + 100000) + + const name = 'video with auto-blacklist and future schedule ' + uuidv4() + + const data = { + name, + privacy: VideoPrivacy.PRIVATE, + scheduleUpdate: { + updateAt: updateAt.toISOString(), + privacy: VideoPrivacy.PUBLIC + } + } + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) + const uuid = resVideo.body.video.uuid + + await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) + + await waitJobs(servers) + await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist') + + // FIXME: Can't test absence as two notifications sent to same user and util only checks last one + // One notification might be better anyways + // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence') + + await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence') + await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence') + }) + + it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () { + this.timeout(20000) + + // In 2 seconds + let updateAt = new Date(new Date().getTime() + 2000) + + const name = 'video with schedule done and still auto-blacklisted ' + uuidv4() + + const data = { + name, + privacy: VideoPrivacy.PRIVATE, + scheduleUpdate: { + updateAt: updateAt.toISOString(), + privacy: VideoPrivacy.PUBLIC + } + } + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) + const uuid = resVideo.body.video.uuid + + await wait(6000) + await checkVideoIsPublished(userBaseParams, name, uuid, 'absence') + await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence') + await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence') + }) + + it('Should not send a notification to moderators on new video without auto-blacklist', async function () { + this.timeout(20000) + + const name = 'video without auto-blacklist ' + uuidv4() + + // admin with blacklist right will not be auto-blacklisted + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence') + }) + + after(async () => { + await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig) + + await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001') + await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001') + }) + }) + + describe('Mark as read', function () { + it('Should mark as read some notifications', async function () { + const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 2, 3) + const ids = res.body.data.map(n => n.id) + + await markAsReadNotifications(servers[ 0 ].url, userAccessToken, ids) + }) + + it('Should have the notifications marked as read', async function () { + const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10) + + const notifications = res.body.data as UserNotification[] + expect(notifications[ 0 ].read).to.be.false + expect(notifications[ 1 ].read).to.be.false + expect(notifications[ 2 ].read).to.be.true + expect(notifications[ 3 ].read).to.be.true + expect(notifications[ 4 ].read).to.be.true + expect(notifications[ 5 ].read).to.be.false + }) + + it('Should only list read notifications', async function () { + const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, false) + + const notifications = res.body.data as UserNotification[] + for (const notification of notifications) { + expect(notification.read).to.be.true + } + }) + + it('Should only list unread notifications', async function () { + const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true) + + const notifications = res.body.data as UserNotification[] + for (const notification of notifications) { + expect(notification.read).to.be.false + } + }) + + it('Should mark as read all notifications', async function () { + await markAsReadAllNotifications(servers[ 0 ].url, userAccessToken) + + const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true) + + expect(res.body.total).to.equal(0) + expect(res.body.data).to.have.lengthOf(0) + }) + }) + + describe('Notification settings', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + }) + + it('Should not have notifications', async function () { + this.timeout(20000) + + await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { + newVideoFromSubscription: UserNotificationSettingValue.NONE + })) + + { + const res = await getMyUserInformation(servers[0].url, userAccessToken) + const info = res.body as User + expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE) + } + + const { name, uuid } = await uploadVideoByLocalAccount(servers) + + const check = { web: true, mail: true } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') + }) + + it('Should only have web notifications', async function () { + this.timeout(20000) + + await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { + newVideoFromSubscription: UserNotificationSettingValue.WEB + })) + + { + const res = await getMyUserInformation(servers[0].url, userAccessToken) + const info = res.body as User + expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB) + } + + const { name, uuid } = await uploadVideoByLocalAccount(servers) + + { + const check = { mail: true, web: false } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') + } + + { + const check = { mail: false, web: true } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') + } + }) + + it('Should only have mail notifications', async function () { + this.timeout(20000) + + await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { + newVideoFromSubscription: UserNotificationSettingValue.EMAIL + })) + + { + const res = await getMyUserInformation(servers[0].url, userAccessToken) + const info = res.body as User + expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL) + } + + const { name, uuid } = await uploadVideoByLocalAccount(servers) + + { + const check = { mail: false, web: true } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') + } + + { + const check = { mail: true, web: false } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') + } + }) + + it('Should have email and web notifications', async function () { + this.timeout(20000) + + await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { + newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL + })) + + { + const res = await getMyUserInformation(servers[0].url, userAccessToken) + const info = res.body as User + expect(info.notificationSettings.newVideoFromSubscription).to.equal( + UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL + ) + } + + const { name, uuid } = await uploadVideoByLocalAccount(servers) + + await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + }) + }) + + after(async function () { + MockSmtpServer.Instance.kill() + + killallServers(servers) + }) +}) diff --git a/server/tests/api/users/index.ts b/server/tests/api/users/index.ts index 52ba6984e..fcd022429 100644 --- a/server/tests/api/users/index.ts +++ b/server/tests/api/users/index.ts @@ -1,5 +1,4 @@ import './users-verification' -import './user-notifications' import './blocklist' import './user-subscriptions' import './users' diff --git a/server/tests/api/users/user-notifications.ts b/server/tests/api/users/user-notifications.ts deleted file mode 100644 index ac47978e2..000000000 --- a/server/tests/api/users/user-notifications.ts +++ /dev/null @@ -1,1272 +0,0 @@ -/* tslint:disable:no-unused-expression */ - -import * as chai from 'chai' -import 'mocha' -import { - addVideoToBlacklist, - createUser, - doubleFollow, - flushAndRunMultipleServers, - flushTests, - getMyUserInformation, - immutableAssign, - registerUser, - removeVideoFromBlacklist, - reportVideoAbuse, - updateMyUser, - updateVideo, - updateVideoChannel, - userLogin, - wait, - getCustomConfig, - updateCustomConfig, getVideoThreadComments, getVideoCommentThreads -} from '../../../../shared/utils' -import { killallServers, ServerInfo, uploadVideo } from '../../../../shared/utils/index' -import { setAccessTokensToServers } from '../../../../shared/utils/users/login' -import { waitJobs } from '../../../../shared/utils/server/jobs' -import { getUserNotificationSocket } from '../../../../shared/utils/socket/socket-io' -import { - checkCommentMention, - CheckerBaseParams, - checkMyVideoImportIsFinished, - checkNewActorFollow, - checkNewBlacklistOnMyVideo, - checkNewCommentOnMyVideo, - checkNewVideoAbuseForModerators, - checkVideoAutoBlacklistForModerators, - checkNewVideoFromSubscription, - checkUserRegistered, - checkVideoIsPublished, - getLastNotification, - getUserNotifications, - markAsReadNotifications, - updateMyNotificationSettings, - markAsReadAllNotifications -} from '../../../../shared/utils/users/user-notifications' -import { - User, - UserNotification, - UserNotificationSetting, - UserNotificationSettingValue, - UserNotificationType -} from '../../../../shared/models/users' -import { MockSmtpServer } from '../../../../shared/utils/miscs/email' -import { addUserSubscription, removeUserSubscription } from '../../../../shared/utils/users/user-subscriptions' -import { VideoPrivacy } from '../../../../shared/models/videos' -import { getBadVideoUrl, getYoutubeVideoUrl, importVideo } from '../../../../shared/utils/videos/video-imports' -import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/utils/videos/video-comments' -import * as uuidv4 from 'uuid/v4' -import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/utils/users/blocklist' -import { CustomConfig } from '../../../../shared/models/server' -import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model' - -const expect = chai.expect - -async function uploadVideoByRemoteAccount (servers: ServerInfo[], additionalParams: any = {}) { - const name = 'remote video ' + uuidv4() - - const data = Object.assign({ name }, additionalParams) - const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, data) - - await waitJobs(servers) - - return { uuid: res.body.video.uuid, name } -} - -async function uploadVideoByLocalAccount (servers: ServerInfo[], additionalParams: any = {}) { - const name = 'local video ' + uuidv4() - - const data = Object.assign({ name }, additionalParams) - const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, data) - - await waitJobs(servers) - - return { uuid: res.body.video.uuid, name } -} - -describe('Test users notifications', function () { - let servers: ServerInfo[] = [] - let userAccessToken: string - let userNotifications: UserNotification[] = [] - let adminNotifications: UserNotification[] = [] - let adminNotificationsServer2: UserNotification[] = [] - const emails: object[] = [] - let channelId: number - - const allNotificationSettings: UserNotificationSetting = { - newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL - } - - before(async function () { - this.timeout(120000) - - await MockSmtpServer.Instance.collectEmails(emails) - - await flushTests() - - const overrideConfig = { - smtp: { - hostname: 'localhost' - } - } - servers = await flushAndRunMultipleServers(2, overrideConfig) - - // Get the access tokens - await setAccessTokensToServers(servers) - - // Server 1 and server 2 follow each other - await doubleFollow(servers[0], servers[1]) - - await waitJobs(servers) - - const user = { - username: 'user_1', - password: 'super password' - } - await createUser(servers[0].url, servers[0].accessToken, user.username, user.password, 10 * 1000 * 1000) - userAccessToken = await userLogin(servers[0], user) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, allNotificationSettings) - await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, allNotificationSettings) - await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, allNotificationSettings) - - { - const socket = getUserNotificationSocket(servers[ 0 ].url, userAccessToken) - socket.on('new-notification', n => userNotifications.push(n)) - } - { - const socket = getUserNotificationSocket(servers[ 0 ].url, servers[0].accessToken) - socket.on('new-notification', n => adminNotifications.push(n)) - } - { - const socket = getUserNotificationSocket(servers[ 1 ].url, servers[1].accessToken) - socket.on('new-notification', n => adminNotificationsServer2.push(n)) - } - - { - const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken) - channelId = resChannel.body.videoChannels[0].id - } - }) - - describe('New video from my subscription notification', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - }) - - it('Should not send notifications if the user does not follow the video publisher', async function () { - this.timeout(10000) - - await uploadVideoByLocalAccount(servers) - - const notification = await getLastNotification(servers[ 0 ].url, userAccessToken) - expect(notification).to.be.undefined - - expect(emails).to.have.lengthOf(0) - expect(userNotifications).to.have.lengthOf(0) - }) - - it('Should send a new video notification if the user follows the local video publisher', async function () { - this.timeout(15000) - - await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9001') - await waitJobs(servers) - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') - }) - - it('Should send a new video notification from a remote account', async function () { - this.timeout(50000) // Server 2 has transcoding enabled - - await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9002') - await waitJobs(servers) - - const { name, uuid } = await uploadVideoByRemoteAccount(servers) - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') - }) - - it('Should send a new video notification on a scheduled publication', async function () { - this.timeout(20000) - - // In 2 seconds - let updateAt = new Date(new Date().getTime() + 2000) - - const data = { - privacy: VideoPrivacy.PRIVATE, - scheduleUpdate: { - updateAt: updateAt.toISOString(), - privacy: VideoPrivacy.PUBLIC - } - } - const { name, uuid } = await uploadVideoByLocalAccount(servers, data) - - await wait(6000) - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') - }) - - it('Should send a new video notification on a remote scheduled publication', async function () { - this.timeout(50000) - - // In 2 seconds - let updateAt = new Date(new Date().getTime() + 2000) - - const data = { - privacy: VideoPrivacy.PRIVATE, - scheduleUpdate: { - updateAt: updateAt.toISOString(), - privacy: VideoPrivacy.PUBLIC - } - } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) - await waitJobs(servers) - - await wait(6000) - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') - }) - - it('Should not send a notification before the video is published', async function () { - this.timeout(20000) - - let updateAt = new Date(new Date().getTime() + 1000000) - - const data = { - privacy: VideoPrivacy.PRIVATE, - scheduleUpdate: { - updateAt: updateAt.toISOString(), - privacy: VideoPrivacy.PUBLIC - } - } - const { name, uuid } = await uploadVideoByLocalAccount(servers, data) - - await wait(6000) - await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') - }) - - it('Should send a new video notification when a video becomes public', async function () { - this.timeout(10000) - - const data = { privacy: VideoPrivacy.PRIVATE } - const { name, uuid } = await uploadVideoByLocalAccount(servers, data) - - await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') - - await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC }) - - await wait(500) - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') - }) - - it('Should send a new video notification when a remote video becomes public', async function () { - this.timeout(20000) - - const data = { privacy: VideoPrivacy.PRIVATE } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) - - await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') - - await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.PUBLIC }) - - await waitJobs(servers) - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') - }) - - it('Should not send a new video notification when a video becomes unlisted', async function () { - this.timeout(20000) - - const data = { privacy: VideoPrivacy.PRIVATE } - const { name, uuid } = await uploadVideoByLocalAccount(servers, data) - - await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED }) - - await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') - }) - - it('Should not send a new video notification when a remote video becomes unlisted', async function () { - this.timeout(20000) - - const data = { privacy: VideoPrivacy.PRIVATE } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) - - await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED }) - - await waitJobs(servers) - await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') - }) - - it('Should send a new video notification after a video import', async function () { - this.timeout(100000) - - const name = 'video import ' + uuidv4() - - const attributes = { - name, - channelId, - privacy: VideoPrivacy.PUBLIC, - targetUrl: getYoutubeVideoUrl() - } - const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) - const uuid = res.body.video.uuid - - await waitJobs(servers) - - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') - }) - }) - - describe('Comment on my video notifications', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - }) - - it('Should not send a new comment notification after a comment on another video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') - }) - - it('Should not send a new comment notification if I comment my own video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') - }) - - it('Should not send a new comment notification if the account is muted', async function () { - this.timeout(10000) - - await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root') - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') - - await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root') - }) - - it('Should send a new comment notification after a local comment on my video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') - }) - - it('Should send a new comment notification after a remote comment on my video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - - await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') - - await waitJobs(servers) - - const resComment = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) - expect(resComment.body.data).to.have.lengthOf(1) - const commentId = resComment.body.data[0].id - - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') - }) - - it('Should send a new comment notification after a local reply on my video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const threadId = resThread.body.comment.id - - const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') - }) - - it('Should send a new comment notification after a remote reply on my video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - await waitJobs(servers) - - { - const resThread = await addVideoCommentThread(servers[ 1 ].url, servers[ 1 ].accessToken, uuid, 'comment') - const threadId = resThread.body.comment.id - await addVideoCommentReply(servers[ 1 ].url, servers[ 1 ].accessToken, uuid, threadId, 'reply') - } - - await waitJobs(servers) - - const resThread = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) - expect(resThread.body.data).to.have.lengthOf(1) - const threadId = resThread.body.data[0].id - - const resComments = await getVideoThreadComments(servers[0].url, uuid, threadId) - const tree = resComments.body as VideoCommentThreadTree - - expect(tree.children).to.have.lengthOf(1) - const commentId = tree.children[0].comment.id - - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') - }) - }) - - describe('Mention notifications', function () { - let baseParams: CheckerBaseParams - - before(async () => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - - await updateMyUser({ - url: servers[0].url, - accessToken: servers[0].accessToken, - displayName: 'super root name' - }) - - await updateMyUser({ - url: servers[1].url, - accessToken: servers[1].accessToken, - displayName: 'super root 2 name' - }) - }) - - it('Should not send a new mention comment notification if I mention the video owner', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') - const commentId = resComment.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') - }) - - it('Should not send a new mention comment notification if I mention myself', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello') - const commentId = resComment.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') - }) - - it('Should not send a new mention notification if the account is muted', async function () { - this.timeout(10000) - - await addAccountToAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root') - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') - const commentId = resComment.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') - - await removeAccountFromAccountBlocklist(servers[ 0 ].url, userAccessToken, 'root') - }) - - it('Should not send a new mention notification if the remote account mention a local account', async function () { - this.timeout(20000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello') - const threadId = resThread.body.comment.id - - await waitJobs(servers) - await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence') - }) - - it('Should send a new mention notification after local comments', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1') - const threadId = resThread.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence') - - const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1') - const commentId = resComment.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence') - }) - - it('Should send a new mention notification after remote comments', async function () { - this.timeout(20000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'hello @user_1@localhost:9001 1') - const server2ThreadId = resThread.body.comment.id - - await waitJobs(servers) - - const resThread2 = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) - expect(resThread2.body.data).to.have.lengthOf(1) - const server1ThreadId = resThread2.body.data[0].id - await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence') - - const text = '@user_1@localhost:9001 hello 2 @root@localhost:9001' - await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, server2ThreadId, text) - - await waitJobs(servers) - - const resComments = await getVideoThreadComments(servers[0].url, uuid, server1ThreadId) - const tree = resComments.body as VideoCommentThreadTree - - expect(tree.children).to.have.lengthOf(1) - const commentId = tree.children[0].comment.id - - await checkCommentMention(baseParams, uuid, commentId, server1ThreadId, 'super root 2 name', 'presence') - }) - }) - - describe('Video abuse for moderators notification' , function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: adminNotifications, - token: servers[0].accessToken - } - }) - - it('Should send a notification to moderators on local video abuse', async function () { - this.timeout(10000) - - const name = 'video for abuse ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) - const uuid = resVideo.body.video.uuid - - await reportVideoAbuse(servers[0].url, servers[0].accessToken, uuid, 'super reason') - - await waitJobs(servers) - await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence') - }) - - it('Should send a notification to moderators on remote video abuse', async function () { - this.timeout(10000) - - const name = 'video for abuse ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - - await reportVideoAbuse(servers[1].url, servers[1].accessToken, uuid, 'super reason') - - await waitJobs(servers) - await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence') - }) - }) - - describe('Video blacklist on my video', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - }) - - it('Should send a notification to video owner on blacklist', async function () { - this.timeout(10000) - - const name = 'video for abuse ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) - const uuid = resVideo.body.video.uuid - - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) - - await waitJobs(servers) - await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist') - }) - - it('Should send a notification to video owner on unblacklist', async function () { - this.timeout(10000) - - const name = 'video for abuse ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) - const uuid = resVideo.body.video.uuid - - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) - - await waitJobs(servers) - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) - await waitJobs(servers) - - await wait(500) - await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist') - }) - }) - - describe('My video is published', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[1], - emails, - socketNotifications: adminNotificationsServer2, - token: servers[1].accessToken - } - }) - - it('Should not send a notification if transcoding is not enabled', async function () { - this.timeout(10000) - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - await waitJobs(servers) - - await checkVideoIsPublished(baseParams, name, uuid, 'absence') - }) - - it('Should not send a notification if the wait transcoding is false', async function () { - this.timeout(50000) - - await uploadVideoByRemoteAccount(servers, { waitTranscoding: false }) - await waitJobs(servers) - - const notification = await getLastNotification(servers[ 0 ].url, userAccessToken) - if (notification) { - expect(notification.type).to.not.equal(UserNotificationType.MY_VIDEO_PUBLISHED) - } - }) - - it('Should send a notification even if the video is not transcoded in other resolutions', async function () { - this.timeout(50000) - - const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true, fixture: 'video_short_240p.mp4' }) - await waitJobs(servers) - - await checkVideoIsPublished(baseParams, name, uuid, 'presence') - }) - - it('Should send a notification with a transcoded video', async function () { - this.timeout(50000) - - const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true }) - await waitJobs(servers) - - await checkVideoIsPublished(baseParams, name, uuid, 'presence') - }) - - it('Should send a notification when an imported video is transcoded', async function () { - this.timeout(50000) - - const name = 'video import ' + uuidv4() - - const attributes = { - name, - channelId, - privacy: VideoPrivacy.PUBLIC, - targetUrl: getYoutubeVideoUrl(), - waitTranscoding: true - } - const res = await importVideo(servers[1].url, servers[1].accessToken, attributes) - const uuid = res.body.video.uuid - - await waitJobs(servers) - await checkVideoIsPublished(baseParams, name, uuid, 'presence') - }) - - it('Should send a notification when the scheduled update has been proceeded', async function () { - this.timeout(70000) - - // In 2 seconds - let updateAt = new Date(new Date().getTime() + 2000) - - const data = { - privacy: VideoPrivacy.PRIVATE, - scheduleUpdate: { - updateAt: updateAt.toISOString(), - privacy: VideoPrivacy.PUBLIC - } - } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) - - await wait(6000) - await checkVideoIsPublished(baseParams, name, uuid, 'presence') - }) - - it('Should not send a notification before the video is published', async function () { - this.timeout(20000) - - let updateAt = new Date(new Date().getTime() + 100000) - - const data = { - privacy: VideoPrivacy.PRIVATE, - scheduleUpdate: { - updateAt: updateAt.toISOString(), - privacy: VideoPrivacy.PUBLIC - } - } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) - - await wait(6000) - await checkVideoIsPublished(baseParams, name, uuid, 'absence') - }) - }) - - describe('My video is imported', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: adminNotifications, - token: servers[0].accessToken - } - }) - - it('Should send a notification when the video import failed', async function () { - this.timeout(70000) - - const name = 'video import ' + uuidv4() - - const attributes = { - name, - channelId, - privacy: VideoPrivacy.PRIVATE, - targetUrl: getBadVideoUrl() - } - const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) - const uuid = res.body.video.uuid - - await waitJobs(servers) - await checkMyVideoImportIsFinished(baseParams, name, uuid, getBadVideoUrl(), false, 'presence') - }) - - it('Should send a notification when the video import succeeded', async function () { - this.timeout(70000) - - const name = 'video import ' + uuidv4() - - const attributes = { - name, - channelId, - privacy: VideoPrivacy.PRIVATE, - targetUrl: getYoutubeVideoUrl() - } - const res = await importVideo(servers[0].url, servers[0].accessToken, attributes) - const uuid = res.body.video.uuid - - await waitJobs(servers) - await checkMyVideoImportIsFinished(baseParams, name, uuid, getYoutubeVideoUrl(), true, 'presence') - }) - }) - - describe('New registration', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: adminNotifications, - token: servers[0].accessToken - } - }) - - it('Should send a notification only to moderators when a user registers on the instance', async function () { - this.timeout(10000) - - await registerUser(servers[0].url, 'user_45', 'password') - - await waitJobs(servers) - - await checkUserRegistered(baseParams, 'user_45', 'presence') - - const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } - await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence') - }) - }) - - describe('New actor follow', function () { - let baseParams: CheckerBaseParams - let myChannelName = 'super channel name' - let myUserName = 'super user name' - - before(async () => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - - await updateMyUser({ - url: servers[0].url, - accessToken: servers[0].accessToken, - displayName: 'super root name' - }) - - await updateMyUser({ - url: servers[0].url, - accessToken: userAccessToken, - displayName: myUserName - }) - - await updateMyUser({ - url: servers[1].url, - accessToken: servers[1].accessToken, - displayName: 'super root 2 name' - }) - - await updateVideoChannel(servers[0].url, userAccessToken, 'user_1_channel', { displayName: myChannelName }) - }) - - it('Should notify when a local channel is following one of our channel', async function () { - this.timeout(10000) - - await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001') - await waitJobs(servers) - - await checkNewActorFollow(baseParams, 'channel', 'root', 'super root name', myChannelName, 'presence') - - await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001') - }) - - it('Should notify when a remote channel is following one of our channel', async function () { - this.timeout(10000) - - await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001') - await waitJobs(servers) - - await checkNewActorFollow(baseParams, 'channel', 'root', 'super root 2 name', myChannelName, 'presence') - - await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001') - }) - - it('Should notify when a local account is following one of our channel', async function () { - this.timeout(10000) - - await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1@localhost:9001') - - await waitJobs(servers) - - await checkNewActorFollow(baseParams, 'account', 'root', 'super root name', myUserName, 'presence') - }) - - it('Should notify when a remote account is following one of our channel', async function () { - this.timeout(10000) - - await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1@localhost:9001') - - await waitJobs(servers) - - await checkNewActorFollow(baseParams, 'account', 'root', 'super root 2 name', myUserName, 'presence') - }) - }) - - describe('Video-related notifications when video auto-blacklist is enabled', function () { - let userBaseParams: CheckerBaseParams - let adminBaseParamsServer1: CheckerBaseParams - let adminBaseParamsServer2: CheckerBaseParams - let videoUUID: string - let videoName: string - let currentCustomConfig: CustomConfig - - before(async () => { - - adminBaseParamsServer1 = { - server: servers[0], - emails, - socketNotifications: adminNotifications, - token: servers[0].accessToken - } - - adminBaseParamsServer2 = { - server: servers[1], - emails, - socketNotifications: adminNotificationsServer2, - token: servers[1].accessToken - } - - userBaseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - - const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken) - currentCustomConfig = resCustomConfig.body - const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, { - autoBlacklist: { - videos: { - ofUsers: { - enabled: true - } - } - } - }) - // enable transcoding otherwise own publish notification after transcoding not expected - autoBlacklistTestsCustomConfig.transcoding.enabled = true - await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig) - - await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001') - await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001') - - }) - - it('Should send notification to moderators on new video with auto-blacklist', async function () { - this.timeout(20000) - - videoName = 'video with auto-blacklist ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName }) - videoUUID = resVideo.body.video.uuid - - await waitJobs(servers) - await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence') - }) - - it('Should not send video publish notification if auto-blacklisted', async function () { - await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence') - }) - - it('Should not send a local user subscription notification if auto-blacklisted', async function () { - await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence') - }) - - it('Should not send a remote user subscription notification if auto-blacklisted', async function () { - await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence') - }) - - it('Should send video published and unblacklist after video unblacklisted', async function () { - this.timeout(20000) - - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID) - - await waitJobs(servers) - - // FIXME: Can't test as two notifications sent to same user and util only checks last one - // One notification might be better anyways - // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist') - // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence') - }) - - it('Should send a local user subscription notification after removed from blacklist', async function () { - await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence') - }) - - it('Should send a remote user subscription notification after removed from blacklist', async function () { - await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence') - }) - - it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () { - this.timeout(20000) - - let updateAt = new Date(new Date().getTime() + 100000) - - const name = 'video with auto-blacklist and future schedule ' + uuidv4() - - const data = { - name, - privacy: VideoPrivacy.PRIVATE, - scheduleUpdate: { - updateAt: updateAt.toISOString(), - privacy: VideoPrivacy.PUBLIC - } - } - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) - const uuid = resVideo.body.video.uuid - - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) - - await waitJobs(servers) - await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist') - - // FIXME: Can't test absence as two notifications sent to same user and util only checks last one - // One notification might be better anyways - // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence') - - await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence') - await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence') - }) - - it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () { - this.timeout(20000) - - // In 2 seconds - let updateAt = new Date(new Date().getTime() + 2000) - - const name = 'video with schedule done and still auto-blacklisted ' + uuidv4() - - const data = { - name, - privacy: VideoPrivacy.PRIVATE, - scheduleUpdate: { - updateAt: updateAt.toISOString(), - privacy: VideoPrivacy.PUBLIC - } - } - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) - const uuid = resVideo.body.video.uuid - - await wait(6000) - await checkVideoIsPublished(userBaseParams, name, uuid, 'absence') - await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence') - await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence') - }) - - it('Should not send a notification to moderators on new video without auto-blacklist', async function () { - this.timeout(20000) - - const name = 'video without auto-blacklist ' + uuidv4() - - // admin with blacklist right will not be auto-blacklisted - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence') - }) - - after(async () => { - await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig) - - await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:9001') - await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:9001') - }) - }) - - describe('Mark as read', function () { - it('Should mark as read some notifications', async function () { - const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 2, 3) - const ids = res.body.data.map(n => n.id) - - await markAsReadNotifications(servers[ 0 ].url, userAccessToken, ids) - }) - - it('Should have the notifications marked as read', async function () { - const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10) - - const notifications = res.body.data as UserNotification[] - expect(notifications[ 0 ].read).to.be.false - expect(notifications[ 1 ].read).to.be.false - expect(notifications[ 2 ].read).to.be.true - expect(notifications[ 3 ].read).to.be.true - expect(notifications[ 4 ].read).to.be.true - expect(notifications[ 5 ].read).to.be.false - }) - - it('Should only list read notifications', async function () { - const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, false) - - const notifications = res.body.data as UserNotification[] - for (const notification of notifications) { - expect(notification.read).to.be.true - } - }) - - it('Should only list unread notifications', async function () { - const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true) - - const notifications = res.body.data as UserNotification[] - for (const notification of notifications) { - expect(notification.read).to.be.false - } - }) - - it('Should mark as read all notifications', async function () { - await markAsReadAllNotifications(servers[ 0 ].url, userAccessToken) - - const res = await getUserNotifications(servers[ 0 ].url, userAccessToken, 0, 10, true) - - expect(res.body.total).to.equal(0) - expect(res.body.data).to.have.lengthOf(0) - }) - }) - - describe('Notification settings', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - }) - - it('Should not have notifications', async function () { - this.timeout(20000) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { - newVideoFromSubscription: UserNotificationSettingValue.NONE - })) - - { - const res = await getMyUserInformation(servers[0].url, userAccessToken) - const info = res.body as User - expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE) - } - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - - const check = { web: true, mail: true } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') - }) - - it('Should only have web notifications', async function () { - this.timeout(20000) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { - newVideoFromSubscription: UserNotificationSettingValue.WEB - })) - - { - const res = await getMyUserInformation(servers[0].url, userAccessToken) - const info = res.body as User - expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB) - } - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - - { - const check = { mail: true, web: false } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') - } - - { - const check = { mail: false, web: true } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') - } - }) - - it('Should only have mail notifications', async function () { - this.timeout(20000) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { - newVideoFromSubscription: UserNotificationSettingValue.EMAIL - })) - - { - const res = await getMyUserInformation(servers[0].url, userAccessToken) - const info = res.body as User - expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL) - } - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - - { - const check = { mail: false, web: true } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') - } - - { - const check = { mail: true, web: false } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') - } - }) - - it('Should have email and web notifications', async function () { - this.timeout(20000) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { - newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL - })) - - { - const res = await getMyUserInformation(servers[0].url, userAccessToken) - const info = res.body as User - expect(info.notificationSettings.newVideoFromSubscription).to.equal( - UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL - ) - } - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') - }) - }) - - after(async function () { - MockSmtpServer.Instance.kill() - - killallServers(servers) - }) -}) diff --git a/shared/models/users/user-notification-setting.model.ts b/shared/models/users/user-notification-setting.model.ts index 57b33e4b8..e2a882b69 100644 --- a/shared/models/users/user-notification-setting.model.ts +++ b/shared/models/users/user-notification-setting.model.ts @@ -15,4 +15,5 @@ export interface UserNotificationSetting { newUserRegistration: UserNotificationSettingValue newFollow: UserNotificationSettingValue commentMention: UserNotificationSettingValue + newInstanceFollower: UserNotificationSettingValue } diff --git a/shared/models/users/user-notification.model.ts b/shared/models/users/user-notification.model.ts index 19892b61a..fafc2b7d7 100644 --- a/shared/models/users/user-notification.model.ts +++ b/shared/models/users/user-notification.model.ts @@ -1,3 +1,5 @@ +import { FollowState } from '../actors' + export enum UserNotificationType { NEW_VIDEO_FROM_SUBSCRIPTION = 1, NEW_COMMENT_ON_MY_VIDEO = 2, @@ -15,7 +17,9 @@ export enum UserNotificationType { NEW_FOLLOW = 10, COMMENT_MENTION = 11, - VIDEO_AUTO_BLACKLIST_FOR_MODERATORS = 12 + VIDEO_AUTO_BLACKLIST_FOR_MODERATORS = 12, + + NEW_INSTANCE_FOLLOWER = 13 } export interface VideoInfo { @@ -73,6 +77,7 @@ export interface UserNotification { actorFollow?: { id: number follower: ActorInfo + state: FollowState following: { type: 'account' | 'channel' name: string diff --git a/shared/utils/users/user-notifications.ts b/shared/utils/users/user-notifications.ts index e3a79f523..495ff80d9 100644 --- a/shared/utils/users/user-notifications.ts +++ b/shared/utils/users/user-notifications.ts @@ -298,6 +298,35 @@ async function checkNewActorFollow ( await checkNotification(base, notificationChecker, emailFinder, type) } +async function checkNewInstanceFollower (base: CheckerBaseParams, followerHost: string, type: CheckerType) { + const notificationType = UserNotificationType.NEW_INSTANCE_FOLLOWER + + function notificationChecker (notification: UserNotification, type: CheckerType) { + if (type === 'presence') { + expect(notification).to.not.be.undefined + expect(notification.type).to.equal(notificationType) + + checkActor(notification.actorFollow.follower) + expect(notification.actorFollow.follower.name).to.equal('peertube') + expect(notification.actorFollow.follower.host).to.equal(followerHost) + + expect(notification.actorFollow.following.name).to.equal('peertube') + } else { + expect(notification).to.satisfy(n => { + return n.type !== notificationType || n.actorFollow.follower.host !== followerHost + }) + } + } + + function emailFinder (email: object) { + const text: string = email[ 'text' ] + + return text.includes('instance has a new follower') && text.includes(followerHost) + } + + await checkNotification(base, notificationChecker, emailFinder, type) +} + async function checkCommentMention ( base: CheckerBaseParams, uuid: string, @@ -462,5 +491,6 @@ export { checkVideoAutoBlacklistForModerators, getUserNotifications, markAsReadNotifications, - getLastNotification + getLastNotification, + checkNewInstanceFollower }