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/abuse/abuse-state-change-for-reporter.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/abuse/abuse-state-change-for-reporter.ts')
-rw-r--r-- | server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts b/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts new file mode 100644 index 000000000..968b5bca9 --- /dev/null +++ b/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts | |||
@@ -0,0 +1,74 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { WEBSERVER } from '@server/initializers/constants' | ||
3 | import { getAbuseTargetUrl } from '@server/lib/activitypub/url' | ||
4 | import { UserModel } from '@server/models/user/user' | ||
5 | import { UserNotificationModel } from '@server/models/user/user-notification' | ||
6 | import { MAbuseFull, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models' | ||
7 | import { AbuseState, UserNotificationType } from '@shared/models' | ||
8 | import { AbstractNotification } from '../common/abstract-notification' | ||
9 | |||
10 | export class AbuseStateChangeForReporter extends AbstractNotification <MAbuseFull> { | ||
11 | |||
12 | private user: MUserDefault | ||
13 | |||
14 | async prepare () { | ||
15 | const reporter = this.abuse.ReporterAccount | ||
16 | if (reporter.isOwned() !== true) return | ||
17 | |||
18 | this.user = await UserModel.loadByAccountActorId(this.abuse.ReporterAccount.actorId) | ||
19 | } | ||
20 | |||
21 | log () { | ||
22 | logger.info('Notifying reporter of abuse % of state change.', getAbuseTargetUrl(this.abuse)) | ||
23 | } | ||
24 | |||
25 | getSetting (user: MUserWithNotificationSetting) { | ||
26 | return user.NotificationSetting.abuseStateChange | ||
27 | } | ||
28 | |||
29 | getTargetUsers () { | ||
30 | if (!this.user) return [] | ||
31 | |||
32 | return [ this.user ] | ||
33 | } | ||
34 | |||
35 | async createNotification (user: MUserWithNotificationSetting) { | ||
36 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | ||
37 | type: UserNotificationType.ABUSE_STATE_CHANGE, | ||
38 | userId: user.id, | ||
39 | abuseId: this.abuse.id | ||
40 | }) | ||
41 | notification.Abuse = this.abuse | ||
42 | |||
43 | return notification | ||
44 | } | ||
45 | |||
46 | createEmail (to: string) { | ||
47 | const text = this.abuse.state === AbuseState.ACCEPTED | ||
48 | ? 'Report #' + this.abuse.id + ' has been accepted' | ||
49 | : 'Report #' + this.abuse.id + ' has been rejected' | ||
50 | |||
51 | const abuseUrl = WEBSERVER.URL + '/my-account/abuses?search=%23' + this.abuse.id | ||
52 | |||
53 | const action = { | ||
54 | text, | ||
55 | url: abuseUrl | ||
56 | } | ||
57 | |||
58 | return { | ||
59 | template: 'abuse-state-change', | ||
60 | to, | ||
61 | subject: text, | ||
62 | locals: { | ||
63 | action, | ||
64 | abuseId: this.abuse.id, | ||
65 | abuseUrl, | ||
66 | isAccepted: this.abuse.state === AbuseState.ACCEPTED | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | private get abuse () { | ||
72 | return this.payload | ||
73 | } | ||
74 | } | ||