1 import { logger } from '@server/helpers/logger'
2 import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
3 import { UserModel } from '@server/models/user/user'
4 import { UserNotificationModel } from '@server/models/user/user-notification'
5 import { MActorFollowFull, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models'
6 import { UserNotificationType } from '@shared/models'
7 import { AbstractNotification } from '../common/abstract-notification'
9 export class FollowForUser extends AbstractNotification <MActorFollowFull> {
10 private followType: 'account' | 'channel'
11 private user: MUserDefault
14 // Account follows one of our account?
15 this.followType = 'channel'
16 this.user = await UserModel.loadByChannelActorId(this.actorFollow.ActorFollowing.id)
18 // Account follows one of our channel?
20 this.user = await UserModel.loadByAccountActorId(this.actorFollow.ActorFollowing.id)
21 this.followType = 'account'
26 if (this.payload.ActorFollowing.isOwned() === false) return true
28 const followerAccount = this.actorFollow.ActorFollower.Account
29 const followerAccountWithActor = Object.assign(followerAccount, { Actor: this.actorFollow.ActorFollower })
31 return isBlockedByServerOrAccount(followerAccountWithActor, this.user.Account)
35 logger.info('Notifying user %s of new follower: %s.', this.user.username, this.actorFollow.ActorFollower.Account.getDisplayName())
38 getSetting (user: MUserWithNotificationSetting) {
39 return user.NotificationSetting.newFollow
43 if (!this.user) return []
48 async createNotification (user: MUserWithNotificationSetting) {
49 const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
50 type: UserNotificationType.NEW_FOLLOW,
52 actorFollowId: this.actorFollow.id
54 notification.ActorFollow = this.actorFollow
59 createEmail (to: string) {
60 const following = this.actorFollow.ActorFollowing
61 const follower = this.actorFollow.ActorFollower
63 const followingName = (following.VideoChannel || following.Account).getDisplayName()
66 template: 'follower-on-channel',
68 subject: `New follower on your channel ${followingName}`,
70 followerName: follower.Account.getDisplayName(),
71 followerUrl: follower.url,
73 followingUrl: following.url,
74 followType: this.followType
79 private get actorFollow () {