-import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
-import { logger } from '../helpers/logger'
-import { Emailer } from './emailer'
-import { UserNotificationModel } from '../models/account/user-notification'
-import { UserModel } from '../models/account/user'
-import { PeerTubeSocket } from './peertube-socket'
-import { CONFIG } from '../initializers/config'
-import { VideoPrivacy, VideoState } from '../../shared/models/videos'
-import * as Bluebird from 'bluebird'
-import { AccountBlocklistModel } from '../models/account/account-blocklist'
-import {
- MCommentOwnerVideo,
- MVideoAbuseVideo,
- MVideoAccountLight,
- MVideoBlacklistLightVideo,
- MVideoBlacklistVideo,
- MVideoFullLight
-} from '../typings/models/video'
+import { AccountModel } from '@server/models/account/account'
+import { getServerActor } from '@server/models/application/application'
+import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
import {
MUser,
+ MUserAccount,
MUserDefault,
MUserNotifSettingAccount,
MUserWithNotificationSetting,
UserNotificationModelForApi
-} from '@server/typings/models/user'
-import { MActorFollowFull } from '../typings/models'
-import { MVideoImportVideo } from '@server/typings/models/video/video-import'
+} from '@server/types/models/user'
+import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist'
+import { MVideoImportVideo } from '@server/types/models/video/video-import'
+import { UserAbuse } from '@shared/models'
+import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users'
+import { VideoPrivacy, VideoState } from '../../shared/models/videos'
+import { logger } from '../helpers/logger'
+import { CONFIG } from '../initializers/config'
+import { AccountBlocklistModel } from '../models/account/account-blocklist'
+import { UserModel } from '../models/account/user'
+import { UserNotificationModel } from '../models/account/user-notification'
+import { MAbuseFull, MAbuseMessage, MAccountServer, MActorFollowFull } from '../types/models'
+import { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../types/models/video'
+import { isBlockedByServerOrAccount } from './blocklist'
+import { Emailer } from './emailer'
+import { PeerTubeSocket } from './peertube-socket'
class Notifier {
private static instance: Notifier
- private constructor () {}
+ private constructor () {
+ }
notifyOnNewVideoIfNeeded (video: MVideoAccountLight): void {
// Only notify on public and published videos which are not blacklisted
if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.isBlacklisted()) return
this.notifySubscribersOfNewVideo(video)
- .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
+ .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
}
notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void {
if (video.ScheduleVideoUpdate || (video.waitTranscoding && video.state !== VideoState.PUBLISHED)) return
this.notifyOwnedVideoHasBeenPublished(video)
- .catch(err => logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })) // tslint:disable-line:max-line-length
+ .catch(err => {
+ logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })
+ })
}
notifyOnNewComment (comment: MCommentOwnerVideo): void {
.catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
}
- notifyOnNewVideoAbuse (videoAbuse: MVideoAbuseVideo): void {
- this.notifyModeratorsOfNewVideoAbuse(videoAbuse)
- .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
+ notifyOnNewAbuse (parameters: { abuse: UserAbuse, abuseInstance: MAbuseFull, reporter: string }): void {
+ this.notifyModeratorsOfNewAbuse(parameters)
+ .catch(err => logger.error('Cannot notify of new abuse %d.', parameters.abuseInstance.id, { err }))
}
notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void {
this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist)
- .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
+ .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
}
notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void {
this.notifyVideoOwnerOfBlacklist(videoBlacklist)
- .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
+ .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
}
notifyOnVideoUnblacklist (video: MVideoFullLight): void {
notifyOnFinishedVideoImport (videoImport: MVideoImportVideo, success: boolean): void {
this.notifyOwnerVideoImportIsFinished(videoImport, success)
- .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
+ .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
}
notifyOnNewUserRegistration (user: MUserDefault): void {
notifyOfNewUserFollow (actorFollow: MActorFollowFull): 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 }
- )
- })
+ .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 }
+ )
+ })
}
notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void {
})
}
+ notifyOnAbuseStateChange (abuse: MAbuseFull): void {
+ this.notifyReporterOfAbuseStateChange(abuse)
+ .catch(err => {
+ logger.error('Cannot notify reporter of abuse %d state change.', abuse.id, { err })
+ })
+ }
+
+ notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void {
+ this.notifyOfNewAbuseMessage(abuse, message)
+ .catch(err => {
+ logger.error('Cannot notify on new abuse %d message.', abuse.id, { err })
+ })
+ }
+
private async notifySubscribersOfNewVideo (video: MVideoAccountLight) {
// List all followers that are users
const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
// Not our user or user comments its own video
if (!user || comment.Account.userId === user.id) return
- const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, comment.accountId)
- if (accountMuted) return
+ if (await this.isBlockedByServerOrUser(comment.Account, user)) return
logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
if (users.length === 0) return
- const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
+ const serverAccountId = (await getServerActor()).Account.id
+ const sourceAccounts = users.map(u => u.Account.id).concat([ serverAccountId ])
+
+ const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, comment.accountId)
+ const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, comment.Account.Actor.serverId)
logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
function settingGetter (user: MUserNotifSettingAccount) {
- if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
+ const accountId = user.Account.id
+ if (
+ accountMutedHash[accountId] === true || instanceMutedHash[accountId] === true ||
+ accountMutedHash[serverAccountId] === true || instanceMutedHash[serverAccountId] === true
+ ) {
+ return UserNotificationSettingValue.NONE
+ }
return user.NotificationSetting.commentMention
}
if (!user) return
const followerAccount = actorFollow.ActorFollower.Account
+ const followerAccountWithActor = Object.assign(followerAccount, { Actor: actorFollow.ActorFollower })
- const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
- if (accountMuted) return
+ if (await this.isBlockedByServerOrUser(followerAccountWithActor, user)) return
logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) {
const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW)
+ const follower = Object.assign(actorFollow.ActorFollower.Account, { Actor: actorFollow.ActorFollower })
+ if (await this.isBlockedByServerOrUser(follower)) return
+
logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url)
function settingGetter (user: MUserWithNotificationSetting) {
return this.notify({ users: admins, settingGetter, notificationCreator, emailSender })
}
- private async notifyModeratorsOfNewVideoAbuse (videoAbuse: MVideoAbuseVideo) {
- const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
+ private async notifyModeratorsOfNewAbuse (parameters: {
+ abuse: UserAbuse
+ abuseInstance: MAbuseFull
+ reporter: string
+ }) {
+ const { abuse, abuseInstance } = parameters
+
+ const moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES)
if (moderators.length === 0) return
- logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
+ const url = this.getAbuseUrl(abuseInstance)
+
+ logger.info('Notifying %s user/moderators of new abuse %s.', moderators.length, url)
function settingGetter (user: MUserWithNotificationSetting) {
- return user.NotificationSetting.videoAbuseAsModerator
+ return user.NotificationSetting.abuseAsModerator
}
async function notificationCreator (user: MUserWithNotificationSetting) {
- const notification: UserNotificationModelForApi = await UserNotificationModel.create<UserNotificationModelForApi>({
- type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS,
+ const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+ type: UserNotificationType.NEW_ABUSE_FOR_MODERATORS,
userId: user.id,
- videoAbuseId: videoAbuse.id
+ abuseId: abuse.id
})
- notification.VideoAbuse = videoAbuse
+ notification.Abuse = abuseInstance
return notification
}
function emailSender (emails: string[]) {
- return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
+ return Emailer.Instance.addAbuseModeratorsNotification(emails, parameters)
}
return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
}
+ private async notifyReporterOfAbuseStateChange (abuse: MAbuseFull) {
+ // Only notify our users
+ if (abuse.ReporterAccount.isOwned() !== true) return
+
+ const url = this.getAbuseUrl(abuse)
+
+ logger.info('Notifying reporter of abuse % of state change.', url)
+
+ const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId)
+
+ function settingGetter (user: MUserWithNotificationSetting) {
+ return user.NotificationSetting.abuseStateChange
+ }
+
+ async function notificationCreator (user: MUserWithNotificationSetting) {
+ const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+ type: UserNotificationType.ABUSE_STATE_CHANGE,
+ userId: user.id,
+ abuseId: abuse.id
+ })
+ notification.Abuse = abuse
+
+ return notification
+ }
+
+ function emailSender (emails: string[]) {
+ return Emailer.Instance.addAbuseStateChangeNotification(emails, abuse)
+ }
+
+ return this.notify({ users: [ reporter ], settingGetter, notificationCreator, emailSender })
+ }
+
+ private async notifyOfNewAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage) {
+ const url = this.getAbuseUrl(abuse)
+ logger.info('Notifying reporter and moderators of new abuse message on %s.', url)
+
+ const accountMessage = await AccountModel.load(message.accountId)
+
+ function settingGetter (user: MUserWithNotificationSetting) {
+ return user.NotificationSetting.abuseNewMessage
+ }
+
+ async function notificationCreator (user: MUserWithNotificationSetting) {
+ const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+ type: UserNotificationType.ABUSE_NEW_MESSAGE,
+ userId: user.id,
+ abuseId: abuse.id
+ })
+ notification.Abuse = abuse
+
+ return notification
+ }
+
+ function emailSenderReporter (emails: string[]) {
+ return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message, accountMessage })
+ }
+
+ function emailSenderModerators (emails: string[]) {
+ return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message, accountMessage })
+ }
+
+ async function buildReporterOptions () {
+ // Only notify our users
+ if (abuse.ReporterAccount.isOwned() !== true) return undefined
+
+ const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId)
+ // Don't notify my own message
+ if (reporter.Account.id === message.accountId) return undefined
+
+ return { users: [ reporter ], settingGetter, notificationCreator, emailSender: emailSenderReporter }
+ }
+
+ async function buildModeratorsOptions () {
+ let moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES)
+ // Don't notify my own message
+ moderators = moderators.filter(m => m.Account.id !== message.accountId)
+
+ if (moderators.length === 0) return undefined
+
+ return { users: moderators, settingGetter, notificationCreator, emailSender: emailSenderModerators }
+ }
+
+ const options = await Promise.all([
+ buildReporterOptions(),
+ buildModeratorsOptions()
+ ])
+
+ return Promise.all(
+ options
+ .filter(opt => !!opt)
+ .map(opt => this.notify(opt))
+ )
+ }
+
private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) {
const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
if (moderators.length === 0) return
return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
}
- private async notify <T extends MUserWithNotificationSetting> (options: {
- users: T[],
- notificationCreator: (user: T) => Promise<UserNotificationModelForApi>,
- emailSender: (emails: string[]) => Promise<any> | Bluebird<any>,
+ private async notify<T extends MUserWithNotificationSetting> (options: {
+ users: T[]
+ notificationCreator: (user: T) => Promise<UserNotificationModelForApi>
+ emailSender: (emails: string[]) => void
settingGetter: (user: T) => UserNotificationSettingValue
}) {
const emails: string[] = []
}
if (emails.length !== 0) {
- await options.emailSender(emails)
+ options.emailSender(emails)
}
}
return value & UserNotificationSettingValue.WEB
}
+ private isBlockedByServerOrUser (targetAccount: MAccountServer, user?: MUserAccount) {
+ return isBlockedByServerOrAccount(targetAccount, user?.Account)
+ }
+
+ private getAbuseUrl (abuse: MAbuseFull) {
+ return abuse.VideoAbuse?.Video?.url ||
+ abuse.VideoCommentAbuse?.VideoComment?.url ||
+ abuse.FlaggedAccount.Actor.url
+ }
+
static get Instance () {
return this.instance || (this.instance = new this())
}