aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/notifier/shared/follow/follow-for-user.ts
blob: 2d0f675a822f4163f5bde9bb5358e3fa181599f9 (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
import { logger } from '@server/helpers/logger'
import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
import { UserModel } from '@server/models/user/user'
import { UserNotificationModel } from '@server/models/user/user-notification'
import { MActorFollowFull, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models'
import { UserNotificationType } from '@shared/models'
import { AbstractNotification } from '../common/abstract-notification'

export class FollowForUser extends AbstractNotification <MActorFollowFull> {
  private followType: 'account' | 'channel'
  private user: MUserDefault

  async prepare () {
    // Account follows one of our account?
    this.followType = 'channel'
    this.user = await UserModel.loadByChannelActorId(this.actorFollow.ActorFollowing.id)

    // Account follows one of our channel?
    if (!this.user) {
      this.user = await UserModel.loadByAccountActorId(this.actorFollow.ActorFollowing.id)
      this.followType = 'account'
    }
  }

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

    const followerAccount = this.actorFollow.ActorFollower.Account
    const followerAccountWithActor = Object.assign(followerAccount, { Actor: this.actorFollow.ActorFollower })

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

  log () {
    logger.info('Notifying user %s of new follower: %s.', this.user.username, this.actorFollow.ActorFollower.Account.getDisplayName())
  }

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

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

    return [ this.user ]
  }

  async createNotification (user: MUserWithNotificationSetting) {
    const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
      type: UserNotificationType.NEW_FOLLOW,
      userId: user.id,
      actorFollowId: this.actorFollow.id
    })
    notification.ActorFollow = this.actorFollow

    return notification
  }

  async createEmail (to: string) {
    const following = this.actorFollow.ActorFollowing
    const follower = this.actorFollow.ActorFollower

    const followingName = (following.VideoChannel || following.Account).getDisplayName()

    return {
      template: 'follower-on-channel',
      to,
      subject: `New follower on your channel ${followingName}`,
      locals: {
        followerName: follower.Account.getDisplayName(),
        followerUrl: follower.url,
        followingName,
        followingUrl: following.url,
        followType: this.followType
      }
    }
  }

  private get actorFollow () {
    return this.payload
  }
}