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/comment | |
parent | 2bee9db56ade2b3b1bb0efa8716840d87efdb93f (diff) | |
download | PeerTube-d26836cd95e981d636006652927773c7943e77ce.tar.gz PeerTube-d26836cd95e981d636006652927773c7943e77ce.tar.zst PeerTube-d26836cd95e981d636006652927773c7943e77ce.zip |
Refactor notifier
Diffstat (limited to 'server/lib/notifier/shared/comment')
-rw-r--r-- | server/lib/notifier/shared/comment/comment-mention.ts | 111 | ||||
-rw-r--r-- | server/lib/notifier/shared/comment/index.ts | 2 | ||||
-rw-r--r-- | server/lib/notifier/shared/comment/new-comment-for-video-owner.ts | 76 |
3 files changed, 189 insertions, 0 deletions
diff --git a/server/lib/notifier/shared/comment/comment-mention.ts b/server/lib/notifier/shared/comment/comment-mention.ts new file mode 100644 index 000000000..4f84d8dea --- /dev/null +++ b/server/lib/notifier/shared/comment/comment-mention.ts | |||
@@ -0,0 +1,111 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { toSafeHtml } from '@server/helpers/markdown' | ||
3 | import { WEBSERVER } from '@server/initializers/constants' | ||
4 | import { AccountBlocklistModel } from '@server/models/account/account-blocklist' | ||
5 | import { getServerActor } from '@server/models/application/application' | ||
6 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' | ||
7 | import { UserModel } from '@server/models/user/user' | ||
8 | import { UserNotificationModel } from '@server/models/user/user-notification' | ||
9 | import { | ||
10 | MCommentOwnerVideo, | ||
11 | MUserDefault, | ||
12 | MUserNotifSettingAccount, | ||
13 | MUserWithNotificationSetting, | ||
14 | UserNotificationModelForApi | ||
15 | } from '@server/types/models' | ||
16 | import { UserNotificationSettingValue, UserNotificationType } from '@shared/models' | ||
17 | import { AbstractNotification } from '../common' | ||
18 | |||
19 | export class CommentMention extends AbstractNotification <MCommentOwnerVideo, MUserNotifSettingAccount> { | ||
20 | private users: MUserDefault[] | ||
21 | |||
22 | private serverAccountId: number | ||
23 | |||
24 | private accountMutedHash: { [ id: number ]: boolean } | ||
25 | private instanceMutedHash: { [ id: number ]: boolean } | ||
26 | |||
27 | async prepare () { | ||
28 | const extractedUsernames = this.payload.extractMentions() | ||
29 | logger.debug( | ||
30 | 'Extracted %d username from comment %s.', extractedUsernames.length, this.payload.url, | ||
31 | { usernames: extractedUsernames, text: this.payload.text } | ||
32 | ) | ||
33 | |||
34 | this.users = await UserModel.listByUsernames(extractedUsernames) | ||
35 | |||
36 | if (this.payload.Video.isOwned()) { | ||
37 | const userException = await UserModel.loadByVideoId(this.payload.videoId) | ||
38 | this.users = this.users.filter(u => u.id !== userException.id) | ||
39 | } | ||
40 | |||
41 | // Don't notify if I mentioned myself | ||
42 | this.users = this.users.filter(u => u.Account.id !== this.payload.accountId) | ||
43 | |||
44 | if (this.users.length === 0) return | ||
45 | |||
46 | this.serverAccountId = (await getServerActor()).Account.id | ||
47 | |||
48 | const sourceAccounts = this.users.map(u => u.Account.id).concat([ this.serverAccountId ]) | ||
49 | |||
50 | this.accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, this.payload.accountId) | ||
51 | this.instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, this.payload.Account.Actor.serverId) | ||
52 | } | ||
53 | |||
54 | log () { | ||
55 | logger.info('Notifying %d users of new comment %s.', this.users.length, this.payload.url) | ||
56 | } | ||
57 | |||
58 | getSetting (user: MUserNotifSettingAccount) { | ||
59 | const accountId = user.Account.id | ||
60 | if ( | ||
61 | this.accountMutedHash[accountId] === true || this.instanceMutedHash[accountId] === true || | ||
62 | this.accountMutedHash[this.serverAccountId] === true || this.instanceMutedHash[this.serverAccountId] === true | ||
63 | ) { | ||
64 | return UserNotificationSettingValue.NONE | ||
65 | } | ||
66 | |||
67 | return user.NotificationSetting.commentMention | ||
68 | } | ||
69 | |||
70 | getTargetUsers () { | ||
71 | return this.users | ||
72 | } | ||
73 | |||
74 | async createNotification (user: MUserWithNotificationSetting) { | ||
75 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | ||
76 | type: UserNotificationType.COMMENT_MENTION, | ||
77 | userId: user.id, | ||
78 | commentId: this.payload.id | ||
79 | }) | ||
80 | notification.Comment = this.payload | ||
81 | |||
82 | return notification | ||
83 | } | ||
84 | |||
85 | createEmail (to: string) { | ||
86 | const comment = this.payload | ||
87 | |||
88 | const accountName = comment.Account.getDisplayName() | ||
89 | const video = comment.Video | ||
90 | const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() | ||
91 | const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath() | ||
92 | const commentHtml = toSafeHtml(comment.text) | ||
93 | |||
94 | return { | ||
95 | template: 'video-comment-mention', | ||
96 | to, | ||
97 | subject: 'Mention on video ' + video.name, | ||
98 | locals: { | ||
99 | comment, | ||
100 | commentHtml, | ||
101 | video, | ||
102 | videoUrl, | ||
103 | accountName, | ||
104 | action: { | ||
105 | text: 'View comment', | ||
106 | url: commentUrl | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | } | ||
diff --git a/server/lib/notifier/shared/comment/index.ts b/server/lib/notifier/shared/comment/index.ts new file mode 100644 index 000000000..ae01a9646 --- /dev/null +++ b/server/lib/notifier/shared/comment/index.ts | |||
@@ -0,0 +1,2 @@ | |||
1 | export * from './comment-mention' | ||
2 | export * from './new-comment-for-video-owner' | ||
diff --git a/server/lib/notifier/shared/comment/new-comment-for-video-owner.ts b/server/lib/notifier/shared/comment/new-comment-for-video-owner.ts new file mode 100644 index 000000000..b76fc15bf --- /dev/null +++ b/server/lib/notifier/shared/comment/new-comment-for-video-owner.ts | |||
@@ -0,0 +1,76 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { toSafeHtml } from '@server/helpers/markdown' | ||
3 | import { WEBSERVER } from '@server/initializers/constants' | ||
4 | import { isBlockedByServerOrAccount } from '@server/lib/blocklist' | ||
5 | import { UserModel } from '@server/models/user/user' | ||
6 | import { UserNotificationModel } from '@server/models/user/user-notification' | ||
7 | import { MCommentOwnerVideo, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models' | ||
8 | import { UserNotificationType } from '@shared/models' | ||
9 | import { AbstractNotification } from '../common/abstract-notification' | ||
10 | |||
11 | export class NewCommentForVideoOwner extends AbstractNotification <MCommentOwnerVideo> { | ||
12 | private user: MUserDefault | ||
13 | |||
14 | async prepare () { | ||
15 | this.user = await UserModel.loadByVideoId(this.payload.videoId) | ||
16 | } | ||
17 | |||
18 | log () { | ||
19 | logger.info('Notifying owner of a video %s of new comment %s.', this.user.username, this.payload.url) | ||
20 | } | ||
21 | |||
22 | isDisabled () { | ||
23 | if (this.payload.Video.isOwned() === false) return true | ||
24 | |||
25 | // Not our user or user comments its own video | ||
26 | if (!this.user || this.payload.Account.userId === this.user.id) return true | ||
27 | |||
28 | return isBlockedByServerOrAccount(this.payload.Account, this.user.Account) | ||
29 | } | ||
30 | |||
31 | getSetting (user: MUserWithNotificationSetting) { | ||
32 | return user.NotificationSetting.newCommentOnMyVideo | ||
33 | } | ||
34 | |||
35 | getTargetUsers () { | ||
36 | if (!this.user) return [] | ||
37 | |||
38 | return [ this.user ] | ||
39 | } | ||
40 | |||
41 | async createNotification (user: MUserWithNotificationSetting) { | ||
42 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | ||
43 | type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, | ||
44 | userId: user.id, | ||
45 | commentId: this.payload.id | ||
46 | }) | ||
47 | notification.Comment = this.payload | ||
48 | |||
49 | return notification | ||
50 | } | ||
51 | |||
52 | createEmail (to: string) { | ||
53 | const video = this.payload.Video | ||
54 | const videoUrl = WEBSERVER.URL + this.payload.Video.getWatchStaticPath() | ||
55 | const commentUrl = WEBSERVER.URL + this.payload.getCommentStaticPath() | ||
56 | const commentHtml = toSafeHtml(this.payload.text) | ||
57 | |||
58 | return { | ||
59 | template: 'video-comment-new', | ||
60 | to, | ||
61 | subject: 'New comment on your video ' + video.name, | ||
62 | locals: { | ||
63 | accountName: this.payload.Account.getDisplayName(), | ||
64 | accountUrl: this.payload.Account.Actor.url, | ||
65 | comment: this.payload, | ||
66 | commentHtml, | ||
67 | video, | ||
68 | videoUrl, | ||
69 | action: { | ||
70 | text: 'View comment', | ||
71 | url: commentUrl | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | } | ||