ActivityPub-federated video streaming platform using P2P directly in your web browser
Add notification on new instance follower (server side)
Chocobozzz883993c81ecc
diff --git a/server/controllers/api/users/my-notifications.ts b/server/controllers/api/users/my-notifications.tsindex 4edad2a749..f146284e4a 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.RemyVideoImportFinished: 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.tsnew file mode 100644index 0000000000..05caf8e1d8--- /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<void> {+ {+ 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.tsindex 63e8106421..c0ad07a525 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.tsindex 140bbe9f13..276a57e607 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 requestif (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.tsindex eec97c27ee..aa90833624 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.tsindex b4d3810625..e7e5ff950b 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.tsindex 9fe93ec0d1..91b71cc64d 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 usersconst 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) returndiff --git a/server/lib/user.ts b/server/lib/user.tsindex 5588b0f769..6fbe3ed03f 100644--- a/server/lib/user.ts+++ b/server/lib/user.ts@@ -110,7 +110,8 @@ function createDefaultUserNotificationSettings (user: UserModel, t: Sequelize.TrblacklistOnMyVideo: 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.tsindex 46486e0813..3ded8d8cf9 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.tsindex ba7f739b9c..c2fbc6d23c 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<UserNotificationSettingM@ColumnnewUserRegistration: UserNotificationSettingValue+ @AllowNull(false)+ @Default(null)+ @Is(+ 'UserNotificationSettingNewInstanceFollower',+ value => throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower')+ )+ @Column+ newInstanceFollower: UserNotificationSettingValue+@AllowNull(false)@Default(null)@Is(@@ -154,7 +163,8 @@ export class UserNotificationSettingModel extends Model<UserNotificationSettingMmyVideoImportFinished: this.myVideoImportFinished,newUserRegistration: this.newUserRegistration,commentMention: this.commentMention,- newFollow: this.newFollow+ newFollow: this.newFollow,+ newInstanceFollower: this.newInstanceFollower}}}diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.tsindex 6cdbb827bc..ccf8277ab1 100644--- a/server/models/account/user-notification.ts+++ b/server/models/account/user-notification.ts@@ -418,6 +418,7 @@ export class UserNotificationModel extends Model<UserNotificationModel> {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.tsindex 36eaceac7b..4b75f6920b 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.tsindex 80d752f421..75cdd90254 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.tsnew file mode 100644index 0000000000..95ac8fc519--- /dev/null+++ b/server/tests/api/notifications/index.ts@@ -0,0 +1 @@+export * from './user-notifications'diff --git a/server/tests/api/users/user-notifications.ts b/server/tests/api/notifications/user-notifications.tssimilarity index 97%rename from server/tests/api/users/user-notifications.tsrename to server/tests/api/notifications/user-notifications.tsindex ac47978e2b..7bff527962 100644--- a/server/tests/api/users/user-notifications.ts+++ b/server/tests/api/notifications/user-notifications.ts@@ -19,7 +19,7 @@ import {userLogin,wait,getCustomConfig,- updateCustomConfig, getVideoThreadComments, getVideoCommentThreads+ updateCustomConfig, getVideoThreadComments, getVideoCommentThreads, follow} from '../../../../shared/utils'import { killallServers, ServerInfo, uploadVideo } from '../../../../shared/utils/index'import { setAccessTokensToServers } from '../../../../shared/utils/users/login'@@ -41,7 +41,7 @@ import {getUserNotifications,markAsReadNotifications,updateMyNotificationSettings,- markAsReadAllNotifications+ markAsReadAllNotifications, checkNewInstanceFollower} from '../../../../shared/utils/users/user-notifications'import {User,@@ -103,7 +103,8 @@ describe('Test users notifications', function () {myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,- newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL+ newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,+ newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL}before(async function () {@@ -118,7 +119,7 @@ describe('Test users notifications', function () {hostname: 'localhost'}}- servers = await flushAndRunMultipleServers(2, overrideConfig)+ servers = await flushAndRunMultipleServers(3, overrideConfig)// Get the access tokensawait setAccessTokensToServers(servers)@@ -861,6 +862,32 @@ describe('Test users notifications', function () {})})+ 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: CheckerBaseParamslet myChannelName = 'super channel name'diff --git a/server/tests/api/users/index.ts b/server/tests/api/users/index.tsindex 52ba6984eb..fcd0224294 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/shared/models/users/user-notification-setting.model.ts b/shared/models/users/user-notification-setting.model.tsindex 57b33e4b84..e2a882b691 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: UserNotificationSettingValuenewFollow: UserNotificationSettingValuecommentMention: UserNotificationSettingValue+ newInstanceFollower: UserNotificationSettingValue}diff --git a/shared/models/users/user-notification.model.ts b/shared/models/users/user-notification.model.tsindex 19892b61a5..fafc2b7d74 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: numberfollower: ActorInfo+ state: FollowStatefollowing: {type: 'account' | 'channel'name: stringdiff --git a/shared/utils/users/user-notifications.ts b/shared/utils/users/user-notifications.tsindex e3a79f523e..495ff80d9b 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}