]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/notifier.ts
Fix video import CLI script
[github/Chocobozzz/PeerTube.git] / server / lib / notifier.ts
index d1b3313467ccb61e8f0d630826afa3c36b20707e..c1e63fa8faad41de2f5cc3c67a27e257e46d255f 100644 (file)
@@ -6,7 +6,7 @@ import { UserNotificationModel } from '../models/account/user-notification'
 import { VideoCommentModel } from '../models/video/video-comment'
 import { UserModel } from '../models/account/user'
 import { PeerTubeSocket } from './peertube-socket'
-import { CONFIG } from '../initializers/constants'
+import { CONFIG } from '../initializers/config'
 import { VideoPrivacy, VideoState } from '../../shared/models/videos'
 import { VideoAbuseModel } from '../models/video/video-abuse'
 import { VideoBlacklistModel } from '../models/video/video-blacklist'
@@ -23,19 +23,35 @@ class Notifier {
   private constructor () {}
 
   notifyOnNewVideo (video: VideoModel): void {
-    // Only notify on public and published videos
-    if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED) return
+    // Only notify on public and published videos which are not blacklisted
+    if (video.privacy !== VideoPrivacy.PUBLIC || video.state !== VideoState.PUBLISHED || video.VideoBlacklist) return
 
     this.notifySubscribersOfNewVideo(video)
       .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
   }
 
-  notifyOnPendingVideoPublished (video: VideoModel): void {
-    // Only notify on public videos that has been published while the user waited transcoding/scheduled update
-    if (video.waitTranscoding === false && !video.ScheduleVideoUpdate) return
+  notifyOnVideoPublishedAfterTranscoding (video: VideoModel): void {
+    // don't notify if didn't wait for transcoding or video is still blacklisted/waiting for scheduled update
+    if (!video.waitTranscoding || video.VideoBlacklist || video.ScheduleVideoUpdate) return
 
     this.notifyOwnedVideoHasBeenPublished(video)
-        .catch(err => logger.error('Cannot notify owner that its video %s has been published.', video.url, { err }))
+        .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
+  }
+
+  notifyOnVideoPublishedAfterScheduledUpdate (video: VideoModel): void {
+    // don't notify if video is still blacklisted or waiting for transcoding
+    if (video.VideoBlacklist || (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 scheduled update.', video.url, { err }))
+  }
+
+  notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: VideoModel): void {
+    // don't notify if video is still waiting for transcoding or scheduled update
+    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
   }
 
   notifyOnNewComment (comment: VideoCommentModel): void {
@@ -51,6 +67,11 @@ class Notifier {
       .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err }))
   }
 
+  notifyOnVideoAutoBlacklist (video: VideoModel): void {
+    this.notifyModeratorsOfVideoAutoBlacklist(video)
+      .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', video.url, { err }))
+  }
+
   notifyOnVideoBlacklist (videoBlacklist: VideoBlacklistModel): void {
     this.notifyVideoOwnerOfBlacklist(videoBlacklist)
       .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
@@ -58,7 +79,7 @@ class Notifier {
 
   notifyOnVideoUnblacklist (video: VideoModel): void {
     this.notifyVideoOwnerOfUnblacklist(video)
-        .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', video.url, { err }))
+        .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
   }
 
   notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void {
@@ -71,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)
@@ -147,8 +175,13 @@ class Notifier {
   }
 
   private async notifyOfCommentMention (comment: VideoCommentModel) {
-    const usernames = comment.extractMentions()
-    let users = await UserModel.listByUsernames(usernames)
+    const extractedUsernames = comment.extractMentions()
+    logger.debug(
+      'Extracted %d username from comment %s.', extractedUsernames.length, comment.url,
+      { usernames: extractedUsernames, text: comment.text }
+    )
+
+    let users = await UserModel.listByUsernames(extractedUsernames)
 
     if (comment.Video.isOwned()) {
       const userException = await UserModel.loadByVideoId(comment.videoId)
@@ -235,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
@@ -263,6 +323,34 @@ class Notifier {
     return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
   }
 
+  private async notifyModeratorsOfVideoAutoBlacklist (video: VideoModel) {
+    const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST)
+    if (moderators.length === 0) return
+
+    logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, video.url)
+
+    function settingGetter (user: UserModel) {
+      return user.NotificationSetting.videoAutoBlacklistAsModerator
+    }
+    async function notificationCreator (user: UserModel) {
+
+      const notification = await UserNotificationModel.create({
+        type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,
+        userId: user.id,
+        videoId: video.id
+      })
+      notification.Video = video
+
+      return notification
+    }
+
+    function emailSender (emails: string[]) {
+      return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, video)
+    }
+
+    return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
+  }
+
   private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
     const user = await UserModel.loadByVideoId(videoBlacklist.videoId)
     if (!user) return
@@ -434,7 +522,7 @@ class Notifier {
   }
 
   private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
-    if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified !== true) return false
+    if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
 
     return value & UserNotificationSettingValue.EMAIL
   }