]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/notifier.ts
Update CHANGELOG.md
[github/Chocobozzz/PeerTube.git] / server / lib / notifier.ts
index a21b50b2dfa5222a3e58c42f233d03420e82bcef..d1b3313467ccb61e8f0d630826afa3c36b20707e 100644 (file)
@@ -11,6 +11,10 @@ import { VideoPrivacy, VideoState } from '../../shared/models/videos'
 import { VideoAbuseModel } from '../models/video/video-abuse'
 import { VideoBlacklistModel } from '../models/video/video-blacklist'
 import * as Bluebird from 'bluebird'
+import { VideoImportModel } from '../models/video/video-import'
+import { AccountBlocklistModel } from '../models/account/account-blocklist'
+import { ActorFollowModel } from '../models/activitypub/actor-follow'
+import { AccountModel } from '../models/account/account'
 
 class Notifier {
 
@@ -26,9 +30,20 @@ class Notifier {
       .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
+
+    this.notifyOwnedVideoHasBeenPublished(video)
+        .catch(err => logger.error('Cannot notify owner that its video %s has been published.', video.url, { err }))
+  }
+
   notifyOnNewComment (comment: VideoCommentModel): void {
     this.notifyVideoOwnerOfNewComment(comment)
-        .catch(err => logger.error('Cannot notify of new comment %s.', comment.url, { err }))
+        .catch(err => logger.error('Cannot notify video owner of new comment %s.', comment.url, { err }))
+
+    this.notifyOfCommentMention(comment)
+        .catch(err => logger.error('Cannot notify mentions of comment %s.', comment.url, { err }))
   }
 
   notifyOnNewVideoAbuse (videoAbuse: VideoAbuseModel): void {
@@ -46,6 +61,28 @@ class Notifier {
         .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', video.url, { err }))
   }
 
+  notifyOnFinishedVideoImport (videoImport: VideoImportModel, success: boolean): void {
+    this.notifyOwnerVideoImportIsFinished(videoImport, success)
+      .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err }))
+  }
+
+  notifyOnNewUserRegistration (user: UserModel): void {
+    this.notifyModeratorsOfNewUserRegistration(user)
+        .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
+  }
+
+  notifyOfNewFollow (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
+        )
+      })
+  }
+
   private async notifySubscribersOfNewVideo (video: VideoModel) {
     // List all followers that are users
     const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId)
@@ -75,11 +112,16 @@ class Notifier {
   }
 
   private async notifyVideoOwnerOfNewComment (comment: VideoCommentModel) {
+    if (comment.Video.isOwned() === false) return
+
     const user = await UserModel.loadByVideoId(comment.videoId)
 
     // 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
+
     logger.info('Notifying user %s of new comment %s.', user.username, comment.url)
 
     function settingGetter (user: UserModel) {
@@ -104,11 +146,100 @@ class Notifier {
     return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
   }
 
-  private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
-    const users = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
+  private async notifyOfCommentMention (comment: VideoCommentModel) {
+    const usernames = comment.extractMentions()
+    let users = await UserModel.listByUsernames(usernames)
+
+    if (comment.Video.isOwned()) {
+      const userException = await UserModel.loadByVideoId(comment.videoId)
+      users = users.filter(u => u.id !== userException.id)
+    }
+
+    // Don't notify if I mentioned myself
+    users = users.filter(u => u.Account.id !== comment.accountId)
+
     if (users.length === 0) return
 
-    logger.info('Notifying %s user/moderators of new video abuse %s.', users.length, videoAbuse.Video.url)
+    const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(users.map(u => u.Account.id), comment.accountId)
+
+    logger.info('Notifying %d users of new comment %s.', users.length, comment.url)
+
+    function settingGetter (user: UserModel) {
+      if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE
+
+      return user.NotificationSetting.commentMention
+    }
+
+    async function notificationCreator (user: UserModel) {
+      const notification = await UserNotificationModel.create({
+        type: UserNotificationType.COMMENT_MENTION,
+        userId: user.id,
+        commentId: comment.id
+      })
+      notification.Comment = comment
+
+      return notification
+    }
+
+    function emailSender (emails: string[]) {
+      return Emailer.Instance.addNewCommentMentionNotification(emails, comment)
+    }
+
+    return this.notify({ users, settingGetter, notificationCreator, emailSender })
+  }
+
+  private async notifyUserOfNewActorFollow (actorFollow: ActorFollowModel) {
+    if (actorFollow.ActorFollowing.isOwned() === false) return
+
+    // Account follows one of our account?
+    let followType: 'account' | 'channel' = 'channel'
+    let user = await UserModel.loadByChannelActorId(actorFollow.ActorFollowing.id)
+
+    // Account follows one of our channel?
+    if (!user) {
+      user = await UserModel.loadByAccountActorId(actorFollow.ActorFollowing.id)
+      followType = 'account'
+    }
+
+    if (!user) return
+
+    if (!actorFollow.ActorFollower.Account || !actorFollow.ActorFollower.Account.name) {
+      actorFollow.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as AccountModel
+    }
+    const followerAccount = actorFollow.ActorFollower.Account
+
+    const accountMuted = await AccountBlocklistModel.isAccountMutedBy(user.Account.id, followerAccount.id)
+    if (accountMuted) return
+
+    logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName())
+
+    function settingGetter (user: UserModel) {
+      return user.NotificationSetting.newFollow
+    }
+
+    async function notificationCreator (user: UserModel) {
+      const notification = await UserNotificationModel.create({
+        type: UserNotificationType.NEW_FOLLOW,
+        userId: user.id,
+        actorFollowId: actorFollow.id
+      })
+      notification.ActorFollow = actorFollow
+
+      return notification
+    }
+
+    function emailSender (emails: string[]) {
+      return Emailer.Instance.addNewFollowNotification(emails, actorFollow, followType)
+    }
+
+    return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
+  }
+
+  private async notifyModeratorsOfNewVideoAbuse (videoAbuse: VideoAbuseModel) {
+    const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES)
+    if (moderators.length === 0) return
+
+    logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url)
 
     function settingGetter (user: UserModel) {
       return user.NotificationSetting.videoAbuseAsModerator
@@ -129,7 +260,7 @@ class Notifier {
       return Emailer.Instance.addVideoAbuseModeratorsNotification(emails, videoAbuse)
     }
 
-    return this.notify({ users, settingGetter, notificationCreator, emailSender })
+    return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
   }
 
   private async notifyVideoOwnerOfBlacklist (videoBlacklist: VideoBlacklistModel) {
@@ -188,6 +319,95 @@ class Notifier {
     return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
   }
 
+  private async notifyOwnedVideoHasBeenPublished (video: VideoModel) {
+    const user = await UserModel.loadByVideoId(video.id)
+    if (!user) return
+
+    logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url)
+
+    function settingGetter (user: UserModel) {
+      return user.NotificationSetting.myVideoPublished
+    }
+
+    async function notificationCreator (user: UserModel) {
+      const notification = await UserNotificationModel.create({
+        type: UserNotificationType.MY_VIDEO_PUBLISHED,
+        userId: user.id,
+        videoId: video.id
+      })
+      notification.Video = video
+
+      return notification
+    }
+
+    function emailSender (emails: string[]) {
+      return Emailer.Instance.myVideoPublishedNotification(emails, video)
+    }
+
+    return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
+  }
+
+  private async notifyOwnerVideoImportIsFinished (videoImport: VideoImportModel, success: boolean) {
+    const user = await UserModel.loadByVideoImportId(videoImport.id)
+    if (!user) return
+
+    logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier())
+
+    function settingGetter (user: UserModel) {
+      return user.NotificationSetting.myVideoImportFinished
+    }
+
+    async function notificationCreator (user: UserModel) {
+      const notification = await UserNotificationModel.create({
+        type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
+        userId: user.id,
+        videoImportId: videoImport.id
+      })
+      notification.VideoImport = videoImport
+
+      return notification
+    }
+
+    function emailSender (emails: string[]) {
+      return success
+        ? Emailer.Instance.myVideoImportSuccessNotification(emails, videoImport)
+        : Emailer.Instance.myVideoImportErrorNotification(emails, videoImport)
+    }
+
+    return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender })
+  }
+
+  private async notifyModeratorsOfNewUserRegistration (registeredUser: UserModel) {
+    const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS)
+    if (moderators.length === 0) return
+
+    logger.info(
+      'Notifying %s moderators of new user registration of %s.',
+      moderators.length, registeredUser.Account.Actor.preferredUsername
+    )
+
+    function settingGetter (user: UserModel) {
+      return user.NotificationSetting.newUserRegistration
+    }
+
+    async function notificationCreator (user: UserModel) {
+      const notification = await UserNotificationModel.create({
+        type: UserNotificationType.NEW_USER_REGISTRATION,
+        userId: user.id,
+        accountId: registeredUser.Account.id
+      })
+      notification.Account = registeredUser.Account
+
+      return notification
+    }
+
+    function emailSender (emails: string[]) {
+      return Emailer.Instance.addNewUserRegistrationNotification(emails, registeredUser)
+    }
+
+    return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender })
+  }
+
   private async notify (options: {
     users: UserModel[],
     notificationCreator: (user: UserModel) => Promise<UserNotificationModel>,
@@ -216,11 +436,11 @@ class Notifier {
   private isEmailEnabled (user: UserModel, value: UserNotificationSettingValue) {
     if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified !== true) return false
 
-    return value === UserNotificationSettingValue.EMAIL || value === UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
+    return value & UserNotificationSettingValue.EMAIL
   }
 
   private isWebNotificationEnabled (value: UserNotificationSettingValue) {
-    return value === UserNotificationSettingValue.WEB_NOTIFICATION || value === UserNotificationSettingValue.WEB_NOTIFICATION_AND_EMAIL
+    return value & UserNotificationSettingValue.WEB
   }
 
   static get Instance () {