diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-30 16:51:27 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-08-02 10:39:51 +0200 |
commit | d26836cd95e981d636006652927773c7943e77ce (patch) | |
tree | 934a4a835bfddbf1c2c7da98d84ebd7623d60d49 /server/lib/notifier/shared/follow | |
parent | 2bee9db56ade2b3b1bb0efa8716840d87efdb93f (diff) | |
download | PeerTube-d26836cd95e981d636006652927773c7943e77ce.tar.gz PeerTube-d26836cd95e981d636006652927773c7943e77ce.tar.zst PeerTube-d26836cd95e981d636006652927773c7943e77ce.zip |
Refactor notifier
Diffstat (limited to 'server/lib/notifier/shared/follow')
4 files changed, 204 insertions, 0 deletions
diff --git a/server/lib/notifier/shared/follow/auto-follow-for-instance.ts b/server/lib/notifier/shared/follow/auto-follow-for-instance.ts new file mode 100644 index 000000000..16cc62984 --- /dev/null +++ b/server/lib/notifier/shared/follow/auto-follow-for-instance.ts | |||
@@ -0,0 +1,51 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { UserModel } from '@server/models/user/user' | ||
3 | import { UserNotificationModel } from '@server/models/user/user-notification' | ||
4 | import { MActorFollowFull, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models' | ||
5 | import { UserNotificationType, UserRight } from '@shared/models' | ||
6 | import { AbstractNotification } from '../common/abstract-notification' | ||
7 | |||
8 | export class AutoFollowForInstance extends AbstractNotification <MActorFollowFull> { | ||
9 | private admins: MUserDefault[] | ||
10 | |||
11 | async prepare () { | ||
12 | this.admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) | ||
13 | } | ||
14 | |||
15 | log () { | ||
16 | logger.info('Notifying %d administrators of auto instance following: %s.', this.admins.length, this.actorFollow.ActorFollowing.url) | ||
17 | } | ||
18 | |||
19 | getSetting (user: MUserWithNotificationSetting) { | ||
20 | return user.NotificationSetting.autoInstanceFollowing | ||
21 | } | ||
22 | |||
23 | getTargetUsers () { | ||
24 | return this.admins | ||
25 | } | ||
26 | |||
27 | async createNotification (user: MUserWithNotificationSetting) { | ||
28 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | ||
29 | type: UserNotificationType.AUTO_INSTANCE_FOLLOWING, | ||
30 | userId: user.id, | ||
31 | actorFollowId: this.actorFollow.id | ||
32 | }) | ||
33 | notification.ActorFollow = this.actorFollow | ||
34 | |||
35 | return notification | ||
36 | } | ||
37 | |||
38 | async createEmail (to: string) { | ||
39 | const instanceUrl = this.actorFollow.ActorFollowing.url | ||
40 | |||
41 | return { | ||
42 | to, | ||
43 | subject: 'Auto instance following', | ||
44 | text: `Your instance automatically followed a new instance: <a href="${instanceUrl}">${instanceUrl}</a>.` | ||
45 | } | ||
46 | } | ||
47 | |||
48 | private get actorFollow () { | ||
49 | return this.payload | ||
50 | } | ||
51 | } | ||
diff --git a/server/lib/notifier/shared/follow/follow-for-instance.ts b/server/lib/notifier/shared/follow/follow-for-instance.ts new file mode 100644 index 000000000..9ab269cf1 --- /dev/null +++ b/server/lib/notifier/shared/follow/follow-for-instance.ts | |||
@@ -0,0 +1,68 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { WEBSERVER } from '@server/initializers/constants' | ||
3 | import { isBlockedByServerOrAccount } from '@server/lib/blocklist' | ||
4 | import { UserModel } from '@server/models/user/user' | ||
5 | import { UserNotificationModel } from '@server/models/user/user-notification' | ||
6 | import { MActorFollowFull, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models' | ||
7 | import { UserNotificationType, UserRight } from '@shared/models' | ||
8 | import { AbstractNotification } from '../common/abstract-notification' | ||
9 | |||
10 | export class FollowForInstance extends AbstractNotification <MActorFollowFull> { | ||
11 | private admins: MUserDefault[] | ||
12 | |||
13 | async prepare () { | ||
14 | this.admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) | ||
15 | } | ||
16 | |||
17 | isDisabled () { | ||
18 | const follower = Object.assign(this.actorFollow.ActorFollower.Account, { Actor: this.actorFollow.ActorFollower }) | ||
19 | |||
20 | return isBlockedByServerOrAccount(follower) | ||
21 | } | ||
22 | |||
23 | log () { | ||
24 | logger.info('Notifying %d administrators of new instance follower: %s.', this.admins.length, this.actorFollow.ActorFollower.url) | ||
25 | } | ||
26 | |||
27 | getSetting (user: MUserWithNotificationSetting) { | ||
28 | return user.NotificationSetting.newInstanceFollower | ||
29 | } | ||
30 | |||
31 | getTargetUsers () { | ||
32 | return this.admins | ||
33 | } | ||
34 | |||
35 | async createNotification (user: MUserWithNotificationSetting) { | ||
36 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | ||
37 | type: UserNotificationType.NEW_INSTANCE_FOLLOWER, | ||
38 | userId: user.id, | ||
39 | actorFollowId: this.actorFollow.id | ||
40 | }) | ||
41 | notification.ActorFollow = this.actorFollow | ||
42 | |||
43 | return notification | ||
44 | } | ||
45 | |||
46 | async createEmail (to: string) { | ||
47 | const awaitingApproval = this.actorFollow.state === 'pending' | ||
48 | ? ' awaiting manual approval.' | ||
49 | : '' | ||
50 | |||
51 | return { | ||
52 | to, | ||
53 | subject: 'New instance follower', | ||
54 | text: `Your instance has a new follower: ${this.actorFollow.ActorFollower.url}${awaitingApproval}.`, | ||
55 | locals: { | ||
56 | title: 'New instance follower', | ||
57 | action: { | ||
58 | text: 'Review followers', | ||
59 | url: WEBSERVER.URL + '/admin/follows/followers-list' | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | private get actorFollow () { | ||
66 | return this.payload | ||
67 | } | ||
68 | } | ||
diff --git a/server/lib/notifier/shared/follow/follow-for-user.ts b/server/lib/notifier/shared/follow/follow-for-user.ts new file mode 100644 index 000000000..2d0f675a8 --- /dev/null +++ b/server/lib/notifier/shared/follow/follow-for-user.ts | |||
@@ -0,0 +1,82 @@ | |||
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' | ||
8 | |||
9 | export class FollowForUser extends AbstractNotification <MActorFollowFull> { | ||
10 | private followType: 'account' | 'channel' | ||
11 | private user: MUserDefault | ||
12 | |||
13 | async prepare () { | ||
14 | // Account follows one of our account? | ||
15 | this.followType = 'channel' | ||
16 | this.user = await UserModel.loadByChannelActorId(this.actorFollow.ActorFollowing.id) | ||
17 | |||
18 | // Account follows one of our channel? | ||
19 | if (!this.user) { | ||
20 | this.user = await UserModel.loadByAccountActorId(this.actorFollow.ActorFollowing.id) | ||
21 | this.followType = 'account' | ||
22 | } | ||
23 | } | ||
24 | |||
25 | async isDisabled () { | ||
26 | if (this.payload.ActorFollowing.isOwned() === false) return true | ||
27 | |||
28 | const followerAccount = this.actorFollow.ActorFollower.Account | ||
29 | const followerAccountWithActor = Object.assign(followerAccount, { Actor: this.actorFollow.ActorFollower }) | ||
30 | |||
31 | return isBlockedByServerOrAccount(followerAccountWithActor, this.user.Account) | ||
32 | } | ||
33 | |||
34 | log () { | ||
35 | logger.info('Notifying user %s of new follower: %s.', this.user.username, this.actorFollow.ActorFollower.Account.getDisplayName()) | ||
36 | } | ||
37 | |||
38 | getSetting (user: MUserWithNotificationSetting) { | ||
39 | return user.NotificationSetting.newFollow | ||
40 | } | ||
41 | |||
42 | getTargetUsers () { | ||
43 | if (!this.user) return [] | ||
44 | |||
45 | return [ this.user ] | ||
46 | } | ||
47 | |||
48 | async createNotification (user: MUserWithNotificationSetting) { | ||
49 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | ||
50 | type: UserNotificationType.NEW_FOLLOW, | ||
51 | userId: user.id, | ||
52 | actorFollowId: this.actorFollow.id | ||
53 | }) | ||
54 | notification.ActorFollow = this.actorFollow | ||
55 | |||
56 | return notification | ||
57 | } | ||
58 | |||
59 | async createEmail (to: string) { | ||
60 | const following = this.actorFollow.ActorFollowing | ||
61 | const follower = this.actorFollow.ActorFollower | ||
62 | |||
63 | const followingName = (following.VideoChannel || following.Account).getDisplayName() | ||
64 | |||
65 | return { | ||
66 | template: 'follower-on-channel', | ||
67 | to, | ||
68 | subject: `New follower on your channel ${followingName}`, | ||
69 | locals: { | ||
70 | followerName: follower.Account.getDisplayName(), | ||
71 | followerUrl: follower.url, | ||
72 | followingName, | ||
73 | followingUrl: following.url, | ||
74 | followType: this.followType | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | |||
79 | private get actorFollow () { | ||
80 | return this.payload | ||
81 | } | ||
82 | } | ||
diff --git a/server/lib/notifier/shared/follow/index.ts b/server/lib/notifier/shared/follow/index.ts new file mode 100644 index 000000000..27f5289d9 --- /dev/null +++ b/server/lib/notifier/shared/follow/index.ts | |||
@@ -0,0 +1,3 @@ | |||
1 | export * from './auto-follow-for-instance' | ||
2 | export * from './follow-for-instance' | ||
3 | export * from './follow-for-user' | ||