myVideoImportFinished: body.myVideoImportFinished,
newFollow: body.newFollow,
newUserRegistration: body.newUserRegistration,
- commentMention: body.commentMention
+ commentMention: body.commentMention,
+ newInstanceFollower: body.newInstanceFollower
}
await UserNotificationSettingModel.update(values, query)
--- /dev/null
+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
+}
actorCreated.VideoChannel.Account = ownerActor.Account
}
+ actorCreated.Server = server
+
return actorCreated
}
}
// ---------------------------------------------------------------------------
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)
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 })
// 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)
}
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()
return actorFollow
})
- if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewFollow(actorFollow)
+ if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollow)
}
.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)
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
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 })
.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 })
@Column
newUserRegistration: UserNotificationSettingValue
+ @AllowNull(false)
+ @Default(null)
+ @Is(
+ 'UserNotificationSettingNewInstanceFollower',
+ value => throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower')
+ )
+ @Column
+ newInstanceFollower: UserNotificationSettingValue
+
@AllowNull(false)
@Default(null)
@Is(
myVideoImportFinished: this.myVideoImportFinished,
newUserRegistration: this.newUserRegistration,
commentMention: this.commentMention,
- newFollow: this.newFollow
+ newFollow: this.newFollow,
+ newInstanceFollower: this.newInstanceFollower
}
}
}
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(),
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 () {
import './check-params'
+import './notifications'
import './search'
--- /dev/null
+export * from './user-notifications'
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'
getUserNotifications,
markAsReadNotifications,
updateMyNotificationSettings,
- markAsReadAllNotifications
+ markAsReadAllNotifications, checkNewInstanceFollower
} from '../../../../shared/utils/users/user-notifications'
import {
User,
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 () {
hostname: 'localhost'
}
}
- servers = await flushAndRunMultipleServers(2, overrideConfig)
+ servers = await flushAndRunMultipleServers(3, overrideConfig)
// Get the access tokens
await setAccessTokensToServers(servers)
})
})
+ 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'
import './users-verification'
-import './user-notifications'
import './blocklist'
import './user-subscriptions'
import './users'
newUserRegistration: UserNotificationSettingValue
newFollow: UserNotificationSettingValue
commentMention: UserNotificationSettingValue
+ newInstanceFollower: UserNotificationSettingValue
}
+import { FollowState } from '../actors'
+
export enum UserNotificationType {
NEW_VIDEO_FROM_SUBSCRIPTION = 1,
NEW_COMMENT_ON_MY_VIDEO = 2,
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 {
actorFollow?: {
id: number
follower: ActorInfo
+ state: FollowState
following: {
type: 'account' | 'channel'
name: string
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,
checkVideoAutoBlacklistForModerators,
getUserNotifications,
markAsReadNotifications,
- getLastNotification
+ getLastNotification,
+ checkNewInstanceFollower
}