diff options
Diffstat (limited to 'server/lib/emailer.ts')
-rw-r--r-- | server/lib/emailer.ts | 116 |
1 files changed, 74 insertions, 42 deletions
diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index 074d4ad44..d766e655b 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts | |||
@@ -1,5 +1,4 @@ | |||
1 | import { createTransport, Transporter } from 'nodemailer' | 1 | import { createTransport, Transporter } from 'nodemailer' |
2 | import { UserRight } from '../../shared/models/users' | ||
3 | import { isTestInstance } from '../helpers/core-utils' | 2 | import { isTestInstance } from '../helpers/core-utils' |
4 | import { bunyanLogger, logger } from '../helpers/logger' | 3 | import { bunyanLogger, logger } from '../helpers/logger' |
5 | import { CONFIG } from '../initializers' | 4 | import { CONFIG } from '../initializers' |
@@ -8,6 +7,9 @@ import { VideoModel } from '../models/video/video' | |||
8 | import { JobQueue } from './job-queue' | 7 | import { JobQueue } from './job-queue' |
9 | import { EmailPayload } from './job-queue/handlers/email' | 8 | import { EmailPayload } from './job-queue/handlers/email' |
10 | import { readFileSync } from 'fs-extra' | 9 | import { readFileSync } from 'fs-extra' |
10 | import { VideoCommentModel } from '../models/video/video-comment' | ||
11 | import { VideoAbuseModel } from '../models/video/video-abuse' | ||
12 | import { VideoBlacklistModel } from '../models/video/video-blacklist' | ||
11 | 13 | ||
12 | class Emailer { | 14 | class Emailer { |
13 | 15 | ||
@@ -79,50 +81,57 @@ class Emailer { | |||
79 | } | 81 | } |
80 | } | 82 | } |
81 | 83 | ||
82 | addForgetPasswordEmailJob (to: string, resetPasswordUrl: string) { | 84 | addNewVideoFromSubscriberNotification (to: string[], video: VideoModel) { |
85 | const channelName = video.VideoChannel.getDisplayName() | ||
86 | const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() | ||
87 | |||
83 | const text = `Hi dear user,\n\n` + | 88 | const text = `Hi dear user,\n\n` + |
84 | `It seems you forgot your password on ${CONFIG.WEBSERVER.HOST}! ` + | 89 | `Your subscription ${channelName} just published a new video: ${video.name}` + |
85 | `Please follow this link to reset it: ${resetPasswordUrl}\n\n` + | 90 | `\n\n` + |
86 | `If you are not the person who initiated this request, please ignore this email.\n\n` + | 91 | `You can view it on ${videoUrl} ` + |
92 | `\n\n` + | ||
87 | `Cheers,\n` + | 93 | `Cheers,\n` + |
88 | `PeerTube.` | 94 | `PeerTube.` |
89 | 95 | ||
90 | const emailPayload: EmailPayload = { | 96 | const emailPayload: EmailPayload = { |
91 | to: [ to ], | 97 | to, |
92 | subject: 'Reset your PeerTube password', | 98 | subject: channelName + ' just published a new video', |
93 | text | 99 | text |
94 | } | 100 | } |
95 | 101 | ||
96 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | 102 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) |
97 | } | 103 | } |
98 | 104 | ||
99 | addVerifyEmailJob (to: string, verifyEmailUrl: string) { | 105 | addNewCommentOnMyVideoNotification (to: string[], comment: VideoCommentModel) { |
100 | const text = `Welcome to PeerTube,\n\n` + | 106 | const accountName = comment.Account.getDisplayName() |
101 | `To start using PeerTube on ${CONFIG.WEBSERVER.HOST} you must verify your email! ` + | 107 | const video = comment.Video |
102 | `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` + | 108 | const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath() |
103 | `If you are not the person who initiated this request, please ignore this email.\n\n` + | 109 | |
110 | const text = `Hi dear user,\n\n` + | ||
111 | `A new comment has been posted by ${accountName} on your video ${video.name}` + | ||
112 | `\n\n` + | ||
113 | `You can view it on ${commentUrl} ` + | ||
114 | `\n\n` + | ||
104 | `Cheers,\n` + | 115 | `Cheers,\n` + |
105 | `PeerTube.` | 116 | `PeerTube.` |
106 | 117 | ||
107 | const emailPayload: EmailPayload = { | 118 | const emailPayload: EmailPayload = { |
108 | to: [ to ], | 119 | to, |
109 | subject: 'Verify your PeerTube email', | 120 | subject: 'New comment on your video ' + video.name, |
110 | text | 121 | text |
111 | } | 122 | } |
112 | 123 | ||
113 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | 124 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) |
114 | } | 125 | } |
115 | 126 | ||
116 | async addVideoAbuseReportJob (videoId: number) { | 127 | async addVideoAbuseModeratorsNotification (to: string[], videoAbuse: VideoAbuseModel) { |
117 | const video = await VideoModel.load(videoId) | 128 | const videoUrl = CONFIG.WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath() |
118 | if (!video) throw new Error('Unknown Video id during Abuse report.') | ||
119 | 129 | ||
120 | const text = `Hi,\n\n` + | 130 | const text = `Hi,\n\n` + |
121 | `Your instance received an abuse for the following video ${video.url}\n\n` + | 131 | `${CONFIG.WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` + |
122 | `Cheers,\n` + | 132 | `Cheers,\n` + |
123 | `PeerTube.` | 133 | `PeerTube.` |
124 | 134 | ||
125 | const to = await UserModel.listEmailsWithRight(UserRight.MANAGE_VIDEO_ABUSES) | ||
126 | const emailPayload: EmailPayload = { | 135 | const emailPayload: EmailPayload = { |
127 | to, | 136 | to, |
128 | subject: '[PeerTube] Received a video abuse', | 137 | subject: '[PeerTube] Received a video abuse', |
@@ -132,16 +141,12 @@ class Emailer { | |||
132 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | 141 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) |
133 | } | 142 | } |
134 | 143 | ||
135 | async addVideoBlacklistReportJob (videoId: number, reason?: string) { | 144 | async addVideoBlacklistNotification (to: string[], videoBlacklist: VideoBlacklistModel) { |
136 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) | 145 | const videoName = videoBlacklist.Video.name |
137 | if (!video) throw new Error('Unknown Video id during Blacklist report.') | 146 | const videoUrl = CONFIG.WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() |
138 | // It's not our user | ||
139 | if (video.remote === true) return | ||
140 | 147 | ||
141 | const user = await UserModel.loadById(video.VideoChannel.Account.userId) | 148 | const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : '' |
142 | 149 | const blockedString = `Your video ${videoName} (${videoUrl} on ${CONFIG.WEBSERVER.HOST} has been blacklisted${reasonString}.` | |
143 | const reasonString = reason ? ` for the following reason: ${reason}` : '' | ||
144 | const blockedString = `Your video ${video.name} on ${CONFIG.WEBSERVER.HOST} has been blacklisted${reasonString}.` | ||
145 | 150 | ||
146 | const text = 'Hi,\n\n' + | 151 | const text = 'Hi,\n\n' + |
147 | blockedString + | 152 | blockedString + |
@@ -149,33 +154,26 @@ class Emailer { | |||
149 | 'Cheers,\n' + | 154 | 'Cheers,\n' + |
150 | `PeerTube.` | 155 | `PeerTube.` |
151 | 156 | ||
152 | const to = user.email | ||
153 | const emailPayload: EmailPayload = { | 157 | const emailPayload: EmailPayload = { |
154 | to: [ to ], | 158 | to, |
155 | subject: `[PeerTube] Video ${video.name} blacklisted`, | 159 | subject: `[PeerTube] Video ${videoName} blacklisted`, |
156 | text | 160 | text |
157 | } | 161 | } |
158 | 162 | ||
159 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | 163 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) |
160 | } | 164 | } |
161 | 165 | ||
162 | async addVideoUnblacklistReportJob (videoId: number) { | 166 | async addVideoUnblacklistNotification (to: string[], video: VideoModel) { |
163 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) | 167 | const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() |
164 | if (!video) throw new Error('Unknown Video id during Blacklist report.') | ||
165 | // It's not our user | ||
166 | if (video.remote === true) return | ||
167 | |||
168 | const user = await UserModel.loadById(video.VideoChannel.Account.userId) | ||
169 | 168 | ||
170 | const text = 'Hi,\n\n' + | 169 | const text = 'Hi,\n\n' + |
171 | `Your video ${video.name} on ${CONFIG.WEBSERVER.HOST} has been unblacklisted.` + | 170 | `Your video ${video.name} (${videoUrl}) on ${CONFIG.WEBSERVER.HOST} has been unblacklisted.` + |
172 | '\n\n' + | 171 | '\n\n' + |
173 | 'Cheers,\n' + | 172 | 'Cheers,\n' + |
174 | `PeerTube.` | 173 | `PeerTube.` |
175 | 174 | ||
176 | const to = user.email | ||
177 | const emailPayload: EmailPayload = { | 175 | const emailPayload: EmailPayload = { |
178 | to: [ to ], | 176 | to, |
179 | subject: `[PeerTube] Video ${video.name} unblacklisted`, | 177 | subject: `[PeerTube] Video ${video.name} unblacklisted`, |
180 | text | 178 | text |
181 | } | 179 | } |
@@ -183,6 +181,40 @@ class Emailer { | |||
183 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | 181 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) |
184 | } | 182 | } |
185 | 183 | ||
184 | addForgetPasswordEmailJob (to: string, resetPasswordUrl: string) { | ||
185 | const text = `Hi dear user,\n\n` + | ||
186 | `It seems you forgot your password on ${CONFIG.WEBSERVER.HOST}! ` + | ||
187 | `Please follow this link to reset it: ${resetPasswordUrl}\n\n` + | ||
188 | `If you are not the person who initiated this request, please ignore this email.\n\n` + | ||
189 | `Cheers,\n` + | ||
190 | `PeerTube.` | ||
191 | |||
192 | const emailPayload: EmailPayload = { | ||
193 | to: [ to ], | ||
194 | subject: 'Reset your PeerTube password', | ||
195 | text | ||
196 | } | ||
197 | |||
198 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | ||
199 | } | ||
200 | |||
201 | addVerifyEmailJob (to: string, verifyEmailUrl: string) { | ||
202 | const text = `Welcome to PeerTube,\n\n` + | ||
203 | `To start using PeerTube on ${CONFIG.WEBSERVER.HOST} you must verify your email! ` + | ||
204 | `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` + | ||
205 | `If you are not the person who initiated this request, please ignore this email.\n\n` + | ||
206 | `Cheers,\n` + | ||
207 | `PeerTube.` | ||
208 | |||
209 | const emailPayload: EmailPayload = { | ||
210 | to: [ to ], | ||
211 | subject: 'Verify your PeerTube email', | ||
212 | text | ||
213 | } | ||
214 | |||
215 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | ||
216 | } | ||
217 | |||
186 | addUserBlockJob (user: UserModel, blocked: boolean, reason?: string) { | 218 | addUserBlockJob (user: UserModel, blocked: boolean, reason?: string) { |
187 | const reasonString = reason ? ` for the following reason: ${reason}` : '' | 219 | const reasonString = reason ? ` for the following reason: ${reason}` : '' |
188 | const blockedWord = blocked ? 'blocked' : 'unblocked' | 220 | const blockedWord = blocked ? 'blocked' : 'unblocked' |
@@ -205,7 +237,7 @@ class Emailer { | |||
205 | } | 237 | } |
206 | 238 | ||
207 | sendMail (to: string[], subject: string, text: string) { | 239 | sendMail (to: string[], subject: string, text: string) { |
208 | if (!this.transporter) { | 240 | if (!this.enabled) { |
209 | throw new Error('Cannot send mail because SMTP is not configured.') | 241 | throw new Error('Cannot send mail because SMTP is not configured.') |
210 | } | 242 | } |
211 | 243 | ||