aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier/shared/comment/comment-mention.ts
blob: 765cbaad95fecff738cabd260eadaa4f5329c765 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { logger } from '@server/helpers/logger'
import { toSafeHtml } from '@server/helpers/markdown'
import { WEBSERVER } from '@server/initializers/constants'
import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
import { getServerActor } from '@server/models/application/application'
import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
import { UserModel } from '@server/models/user/user'
import { UserNotificationModel } from '@server/models/user/user-notification'
import {
  MCommentOwnerVideo,
  MUserDefault,
  MUserNotifSettingAccount,
  MUserWithNotificationSetting,
  UserNotificationModelForApi
} from '@server/types/models'
import { UserNotificationSettingValue, UserNotificationType } from '@shared/models'
import { AbstractNotification } from '../common'

export class CommentMention extends AbstractNotification <MCommentOwnerVideo, MUserNotifSettingAccount> {
  private users: MUserDefault[]

  private serverAccountId: number

  private accountMutedHash: { [ id: number ]: boolean }
  private instanceMutedHash: { [ id: number ]: boolean }

  async prepare () {
    const extractedUsernames = this.payload.extractMentions()
    logger.debug(
      'Extracted %d username from comment %s.', extractedUsernames.length, this.payload.url,
      { usernames: extractedUsernames, text: this.payload.text }
    )

    this.users = await UserModel.listByUsernames(extractedUsernames)

    if (this.payload.Video.isOwned()) {
      const userException = await UserModel.loadByVideoId(this.payload.videoId)
      this.users = this.users.filter(u => u.id !== userException.id)
    }

    // Don't notify if I mentioned myself
    this.users = this.users.filter(u => u.Account.id !== this.payload.accountId)

    if (this.users.length === 0) return

    this.serverAccountId = (await getServerActor()).Account.id

    const sourceAccounts = this.users.map(u => u.Account.id).concat([ this.serverAccountId ])

    this.accountMutedHash = await AccountBlocklistModel.isAccountMutedByAccounts(sourceAccounts, this.payload.accountId)
    this.instanceMutedHash = await ServerBlocklistModel.isServerMutedByAccounts(sourceAccounts, this.payload.Account.Actor.serverId)
  }

  log () {
    logger.info('Notifying %d users of new comment %s.', this.users.length, this.payload.url)
  }

  getSetting (user: MUserNotifSettingAccount) {
    const accountId = user.Account.id
    if (
      this.accountMutedHash[accountId] === true || this.instanceMutedHash[accountId] === true ||
      this.accountMutedHash[this.serverAccountId] === true || this.instanceMutedHash[this.serverAccountId] === true
    ) {
      return UserNotificationSettingValue.NONE
    }

    return user.NotificationSetting.commentMention
  }

  getTargetUsers () {
    return this.users
  }

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

    return notification
  }

  createEmail (to: string) {
    const comment = this.payload

    const accountName = comment.Account.getDisplayName()
    const video = comment.Video
    const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath()
    const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
    const commentHtml = toSafeHtml(comment.text)

    return {
      template: 'video-comment-mention',
      to,
      subject: 'Mention on video ' + video.name,
      locals: {
        comment,
        commentHtml,
        video,
        videoUrl,
        accountName,
        action: {
          text: 'View comment',
          url: commentUrl
        }
      }
    }
  }
}