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/new-comment-for-video-owner.ts | |
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/new-comment-for-video-owner.ts')
-rw-r--r-- | server/lib/notifier/shared/comment/new-comment-for-video-owner.ts | 76 |
1 files changed, 76 insertions, 0 deletions
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 | } | ||