aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier/shared/comment/new-comment-for-video-owner.ts
blob: b76fc15bf94bd8ee2fcecffd4ebe3d7945aba5f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { logger } from '@server/helpers/logger'
import { toSafeHtml } from '@server/helpers/markdown'
import { WEBSERVER } from '@server/initializers/constants'
import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
import { UserModel } from '@server/models/user/user'
import { UserNotificationModel } from '@server/models/user/user-notification'
import { MCommentOwnerVideo, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models'
import { UserNotificationType } from '@shared/models'
import { AbstractNotification } from '../common/abstract-notification'

export class NewCommentForVideoOwner extends AbstractNotification <MCommentOwnerVideo> {
  private user: MUserDefault

  async prepare () {
    this.user = await UserModel.loadByVideoId(this.payload.videoId)
  }

  log () {
    logger.info('Notifying owner of a video %s of new comment %s.', this.user.username, this.payload.url)
  }

  isDisabled () {
    if (this.payload.Video.isOwned() === false) return true

    // Not our user or user comments its own video
    if (!this.user || this.payload.Account.userId === this.user.id) return true

    return isBlockedByServerOrAccount(this.payload.Account, this.user.Account)
  }

  getSetting (user: MUserWithNotificationSetting) {
    return user.NotificationSetting.newCommentOnMyVideo
  }

  getTargetUsers () {
    if (!this.user) return []

    return [ this.user ]
  }

  async createNotification (user: MUserWithNotificationSetting) {
    const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
      type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,
      userId: user.id,
      commentId: this.payload.id
    })
    notification.Comment = this.payload

    return notification
  }

  createEmail (to: string) {
    const video = this.payload.Video
    const videoUrl = WEBSERVER.URL + this.payload.Video.getWatchStaticPath()
    const commentUrl = WEBSERVER.URL + this.payload.getCommentStaticPath()
    const commentHtml = toSafeHtml(this.payload.text)

    return {
      template: 'video-comment-new',
      to,
      subject: 'New comment on your video ' + video.name,
      locals: {
        accountName: this.payload.Account.getDisplayName(),
        accountUrl: this.payload.Account.Actor.url,
        comment: this.payload,
        commentHtml,
        video,
        videoUrl,
        action: {
          text: 'View comment',
          url: commentUrl
        }
      }
    }
  }
}