Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Add ability for plugins to send emails

Chocobozzz962acec26f16
diff --git a/packages/models/src/server/emailer.model.ts b/packages/models/src/server/emailer.model.ts
index 2ba8967d22..bc5f80c2c5 100644
--- a/packages/models/src/server/emailer.model.ts
+++ b/packages/models/src/server/emailer.model.ts
@@ -1,63 +1,39 @@
-export type To = { email: string, language: string }
-type From = string | { name?: string, address: string }
-
-interface Base {
- to: To[] | To
-
- from?: From
- subject?: string
- replyTo?: string
-}
-
-interface MailTemplate extends Base {
- template: string
- locals?: { [key: string]: any }
- text?: undefined
-}
+export type MailTo = { email: string, language: string }
+export type MailFrom = string | { name?: string, address: string }
export interface MailAction {
url: string
text: string
}
-interface MailText extends Base {
- text: string
+interface MailBase {
+ to: MailTo[] | MailTo
- locals?: Partial<SendEmailDefaultLocalsOptions> & {
- title?: string
- action?: MailAction
- }
+ from?: MailFrom
+ subject?: string
+ replyTo?: string
}
-interface SendEmailDefaultLocalsOptions {
- instanceName: string
- text: string
- subject: string
-
- fg: string
- bg: string
- onPrimary: string
- primary: string
- language: string
- logoUrl: string
+export interface MailBaseLocals {
+ title?: string
+ action?: MailAction
}
-interface SendEmailDefaultMessageOptions {
- to: string[] | string
- from: From
- subject: string
- replyTo: string
-}
+interface SendMailTemplateOptions extends MailBase, Partial<MailBaseLocals> {
+ template: string
+ locals?: Record<string, any>
-export type SendEmailDefaultOptions = {
- template: 'common'
+ // text is forbidden if template is used
+ text?: undefined
+}
- message: SendEmailDefaultMessageOptions
+interface SendMailTextOptions extends MailBase, Partial<MailBaseLocals> {
+ text: string
- locals: SendEmailDefaultLocalsOptions & {
- WEBSERVER: any
- signature: string
- }
+ // locals is forbidden if template is used
+ locals?: undefined
+ // template is forbidden if template is used
+ template?: undefined
}
-export type SendEmailOptions = MailTemplate | MailText
+export type SendEmailOptions = SendMailTemplateOptions | SendMailTextOptions
diff --git a/packages/tests/fixtures/peertube-plugin-test-four/main.js b/packages/tests/fixtures/peertube-plugin-test-four/main.js
index 4aa8cb0275..ddee2edb9d 100644
--- a/packages/tests/fixtures/peertube-plugin-test-four/main.js
+++ b/packages/tests/fixtures/peertube-plugin-test-four/main.js
@@ -157,6 +157,16 @@ async function register ({
return res.sendStatus(201)
})
+
+ router.post('/send-email', async (req, res) => {
+ await peertubeHelpers.email.createJob({
+ to: { email: req.body.to, language: 'en' },
+ subject: req.body.subject,
+ text: req.body.text
+ })
+
+ return res.sendStatus(201)
+ })
}
}
diff --git a/packages/tests/src/plugins/plugin-helpers.ts b/packages/tests/src/plugins/plugin-helpers.ts
index 5f4f5da3d5..06ce829b29 100644
--- a/packages/tests/src/plugins/plugin-helpers.ts
+++ b/packages/tests/src/plugins/plugin-helpers.ts
@@ -5,6 +5,7 @@ import { pathExists } from 'fs-extra/esm'
import { HttpStatusCode } from '@peertube/peertube-models'
import {
cleanupTests,
+ ConfigCommand,
createMultipleServers,
doubleFollow,
makeGetRequest,
@@ -15,6 +16,7 @@ import {
setAccessTokensToServers,
waitJobs
} from '@peertube/peertube-server-commands'
+import { MockSmtpServer } from '@tests/shared/mock-servers/mock-email.js'
import { checkVideoFilesWereRemoved } from '@tests/shared/videos.js'
function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) {
@@ -32,10 +34,14 @@ function postCommand (server: PeerTubeServer, command: string, bodyArg?: object)
describe('Test plugin helpers', function () {
let servers: PeerTubeServer[]
+ const emails: object[] = []
+
before(async function () {
this.timeout(60000)
- servers = await createMultipleServers(2)
+ const emailPort = await MockSmtpServer.Instance.collectEmails(emails)
+
+ servers = await createMultipleServers(2, ConfigCommand.getEmailOverrideConfig(emailPort))
await setAccessTokensToServers(servers)
await doubleFollow(servers[0], servers[1])
@@ -117,6 +123,30 @@ describe('Test plugin helpers', function () {
})
})
+ describe('Email', function () {
+ it('Should send an email', async function () {
+ await makePostBodyRequest({
+ url: servers[0].url,
+ path: '/plugins/test-four/router/send-email',
+ fields: {
+ subject: 'Email sent by a plugin',
+ text: 'Hello from plugin four'
+ },
+ expectedStatus: HttpStatusCode.CREATED_201
+ })
+
+ await waitJobs(servers)
+
+ expect(emails).to.have.lengthOf(1)
+
+ const email = emails[0]
+ expect(email['to'][0]['address']).to.equal('[email protected]')
+ expect(email['subject']).to.contain('Email sent by a plugin')
+ expect(email['text']).to.contain('Hello from plugin four')
+ })
+ })
+
describe('Plugin', function () {
it('Should get the base static route', async function () {
const res = await makeGetRequest({
diff --git a/server/core/lib/emailer.ts b/server/core/lib/emailer.ts
index 5120796921..b5f983b445 100644
--- a/server/core/lib/emailer.ts
+++ b/server/core/lib/emailer.ts
@@ -1,5 +1,13 @@
import { arrayify } from '@peertube/peertube-core-utils'
-import { EmailPayload, MailAction, SendEmailDefaultOptions, To, UserExportState, UserRegistrationState } from '@peertube/peertube-models'
+import {
+ EmailPayload,
+ MailAction,
+ MailBaseLocals,
+ MailFrom,
+ MailTo,
+ UserExportState,
+ UserRegistrationState
+} from '@peertube/peertube-models'
import { getFilenameWithoutExt, isTestOrDevInstance, root } from '@peertube/peertube-node-utils'
import { t } from '@server/helpers/i18n.js'
import { toSafeMailHtml } from '@server/helpers/markdown.js'
@@ -8,7 +16,6 @@ import { UserModel } from '@server/models/user/user.js'
import { readFileSync } from 'fs'
import { readdir, readFile } from 'fs/promises'
import handlebars, { HelperOptions } from 'handlebars'
-import merge from 'lodash-es/merge.js'
import { createTransport, Transporter } from 'nodemailer'
import { join } from 'path'
import { bunyanLogger, logger } from '../helpers/logger.js'
@@ -20,6 +27,35 @@ import { JobQueue } from './job-queue/index.js'
import { Hooks } from './plugins/hooks.js'
import { ServerConfigManager } from './server-config-manager.js'
+interface MailMessage {
+ to: string[] | string
+ from: MailFrom
+ subject: string
+ replyTo: string
+}
+
+interface SendMailOptions {
+ template: string
+
+ message: MailMessage
+
+ locals: MailBaseLocals & {
+ text: string
+ subject: string
+
+ WEBSERVER: typeof WEBSERVER
+ signature: string
+
+ instanceName: string
+ fg: string
+ bg: string
+ onPrimary: string
+ primary: string
+ language: string
+ logoUrl: string
+ }
+}
+
export class Emailer {
private static instance: Emailer
@@ -264,7 +300,7 @@ export class Emailer {
// ---------------------------------------------------------------------------
- async addUserExportCompletedOrErroredJob (userExport: Pick<MUserExport, 'userId' | 'state' | 'error'>, toOverride?: To) {
+ async addUserExportCompletedOrErroredJob (userExport: Pick<MUserExport, 'userId' | 'state' | 'error'>, toOverride?: MailTo) {
let template: string
let subject: string
@@ -293,7 +329,7 @@ export class Emailer {
return JobQueue.Instance.createJobAsync({ type: 'email', payload: emailPayload })
}
- async addUserImportErroredJob (userImport: Pick<MUserImport, 'userId' | 'error'>, toOverride?: To) {
+ async addUserImportErroredJob (userImport: Pick<MUserImport, 'userId' | 'error'>, toOverride?: MailTo) {
const to = toOverride ?? await UserModel.loadForEmail(userImport.userId)
const emailPayload: EmailPayload = {
@@ -311,7 +347,7 @@ export class Emailer {
return JobQueue.Instance.createJobAsync({ type: 'email', payload: emailPayload })
}
- async addUserImportSuccessJob (userImport: Pick<MUserImport, 'userId' | 'resultSummary'>, toOverride?: To) {
+ async addUserImportSuccessJob (userImport: Pick<MUserImport, 'userId' | 'resultSummary'>, toOverride?: MailTo) {
const to = toOverride ?? await UserModel.loadForEmail(userImport.userId)
const emailPayload: EmailPayload = {
@@ -378,6 +414,7 @@ export class Emailer {
transport: this.transporter,
subjectPrefix: this.buildSubjectPrefix()
})
+
const subject = await Hooks.wrapObject(
options.subject,
'filter:email.subject.result',
@@ -387,33 +424,34 @@ export class Emailer {
const errors: Error[] = []
for (const to of arrayify(options.to)) {
- const baseOptions: SendEmailDefaultOptions = {
- template: 'common',
+ const sendMailOptions: SendMailOptions = {
+ template: options.template ?? 'common',
message: {
to: to.email,
from: options.from,
subject,
replyTo: options.replyTo
},
- locals: { // default variables available in all templates
+ locals: {
WEBSERVER,
instanceName: CONFIG.INSTANCE.NAME,
- text: options.text,
subject,
+ title: options.text,
+ action: options.action,
+ text: options.text, // If MailText
signature: this.buildSignature(),
+ language: to.language,
+ logoUrl: ServerConfigManager.Instance.getLogoUrl(await getServerActor(), 192),
+
...this.buildEmailTheme(),
- language: to.language,
- logoUrl: ServerConfigManager.Instance.getLogoUrl(await getServerActor(), 192)
+ ...(options.locals ?? {})
}
}
- // overridden/new variables given for a specific template in the payload
- const sendOptions = merge(baseOptions, options)
-
try {
- const res = await email.send(sendOptions)
+ const res = await email.send(sendMailOptions)
logger.debug('Sent email.', { res })
} catch (err) {
diff --git a/server/core/lib/notifier/shared/abuse/abstract-new-abuse-message.ts b/server/core/lib/notifier/shared/abuse/abstract-new-abuse-message.ts
index efbdb81940..65dcbf6194 100644
--- a/server/core/lib/notifier/shared/abuse/abstract-new-abuse-message.ts
+++ b/server/core/lib/notifier/shared/abuse/abstract-new-abuse-message.ts
@@ -1,4 +1,4 @@
-import { To, UserNotificationType } from '@peertube/peertube-models'
+import { MailTo, UserNotificationType } from '@peertube/peertube-models'
import { t } from '@server/helpers/i18n.js'
import { getAdminAbuseUrl, getUserAbuseUrl } from '@server/lib/client-urls.js'
import { AccountModel } from '@server/models/account/account.js'
@@ -39,7 +39,7 @@ export abstract class AbstractNewAbuseMessage extends AbstractNotification<NewAb
return notification
}
- protected createEmailFor (to: To, target: 'moderator' | 'reporter') {
+ protected createEmailFor (to: MailTo, target: 'moderator' | 'reporter') {
const text = t('New message on report #{id}', to.language, { id: this.abuse.id })
const abuseUrl = target === 'moderator'
? getAdminAbuseUrl(this.abuse)
diff --git a/server/core/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts b/server/core/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts
index 3e601e8667..7f12aa4e92 100644
--- a/server/core/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts
+++ b/server/core/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts
@@ -62,8 +62,8 @@ export class AbuseStateChangeForReporter extends AbstractNotification<MAbuseFull
template: 'abuse-state-change',
to,
subject: text,
+ action,
locals: {
- action,
abuseId: this.abuse.id,
abuseUrl,
isAccepted: this.abuse.state === AbuseState.ACCEPTED
diff --git a/server/core/lib/notifier/shared/abuse/new-abuse-for-moderators.ts b/server/core/lib/notifier/shared/abuse/new-abuse-for-moderators.ts
index ea6274cdf1..f5f87d9155 100644
--- a/server/core/lib/notifier/shared/abuse/new-abuse-for-moderators.ts
+++ b/server/core/lib/notifier/shared/abuse/new-abuse-for-moderators.ts
@@ -1,4 +1,4 @@
-import { To, UserAbuse, UserNotificationType, UserRight } from '@peertube/peertube-models'
+import { MailTo, UserAbuse, UserNotificationType, UserRight } from '@peertube/peertube-models'
import { t } from '@server/helpers/i18n.js'
import { logger } from '@server/helpers/logger.js'
import { WEBSERVER } from '@server/initializers/constants.js'
@@ -52,7 +52,7 @@ export class NewAbuseForModerators extends AbstractNotification<NewAbusePayload>
return this.createAccountAbuseEmail(to)
}
- private createVideoAbuseEmail (to: To) {
+ private createVideoAbuseEmail (to: MailTo) {
const video = this.payload.abuseInstance.VideoAbuse.Video
const channel = video.VideoChannel
@@ -75,7 +75,7 @@ export class NewAbuseForModerators extends AbstractNotification<NewAbusePayload>
}
}
- private createCommentAbuseEmail (to: To) {
+ private createCommentAbuseEmail (to: MailTo) {
const comment = this.payload.abuseInstance.VideoCommentAbuse.VideoComment
return {
@@ -95,7 +95,7 @@ export class NewAbuseForModerators extends AbstractNotification<NewAbusePayload>
}
}
- private createAccountAbuseEmail (to: To) {
+ private createAccountAbuseEmail (to: MailTo) {
const account = this.payload.abuseInstance.FlaggedAccount
const accountUrl = account.getClientUrl()
@@ -114,7 +114,7 @@ export class NewAbuseForModerators extends AbstractNotification<NewAbusePayload>
}
}
- private buildEmailAction (to: To) {
+ private buildEmailAction (to: MailTo) {
return {
text: t('View report #' + this.payload.abuseInstance.id, to.language),
url: getAdminAbuseUrl(this.payload.abuseInstance)
diff --git a/server/core/lib/notifier/shared/blacklist/new-auto-blacklist-for-moderators.ts b/server/core/lib/notifier/shared/blacklist/new-auto-blacklist-for-moderators.ts
index ea9c913ccf..6ab7771c00 100644
--- a/server/core/lib/notifier/shared/blacklist/new-auto-blacklist-for-moderators.ts
+++ b/server/core/lib/notifier/shared/blacklist/new-auto-blacklist-for-moderators.ts
@@ -52,15 +52,15 @@ export class NewAutoBlacklistForModerators extends AbstractNotification<MVideoBl
template: 'video-auto-blacklist-new',
to: { email: user.email, language: user.getLanguage() },
subject: tu('A new video is pending moderation', user),
+ action: {
+ text: tu('Review video', user),
+ url: videoAutoBlacklistUrl
+ },
locals: {
channelDisplayName: channel.getDisplayName(),
channelUrl: channel.getClientUrl(),
videoUrl: WEBSERVER.URL + video.getWatchStaticPath(),
- videoName: video.name,
- action: {
- text: tu('Review video', user),
- url: videoAutoBlacklistUrl
- }
+ videoName: video.name
}
}
}
diff --git a/server/core/lib/notifier/shared/blacklist/new-blacklist-for-owner.ts b/server/core/lib/notifier/shared/blacklist/new-blacklist-for-owner.ts
index 279ca929a6..4b04d002e5 100644
--- a/server/core/lib/notifier/shared/blacklist/new-blacklist-for-owner.ts
+++ b/server/core/lib/notifier/shared/blacklist/new-blacklist-for-owner.ts
@@ -1,7 +1,6 @@
import { UserNotificationType } from '@peertube/peertube-models'
import { tu } from '@server/helpers/i18n.js'
import { logger } from '@server/helpers/logger.js'
-import { CONFIG } from '@server/initializers/config.js'
import { WEBSERVER } from '@server/initializers/constants.js'
import { UserNotificationModel } from '@server/models/user/user-notification.js'
import { UserModel } from '@server/models/user/user.js'
@@ -55,14 +54,13 @@ export class NewBlacklistForOwner extends AbstractNotification<MVideoBlacklistVi
template: 'video-owner-blacklist-new',
to,
subject: tu('Your video has been blocked', user),
+ action: {
+ text: tu('View video', user),
+ url: videoUrl
+ },
locals: {
- instanceName: CONFIG.INSTANCE.NAME,
videoName,
- reason: this.payload.reason,
- action: {
- text: tu('View video', user),
- url: videoUrl
- }
+ reason: this.payload.reason
}
}
}
diff --git a/server/core/lib/notifier/shared/blacklist/unblacklist-for-owner.ts b/server/core/lib/notifier/shared/blacklist/unblacklist-for-owner.ts
index a96412c134..babc407598 100644
--- a/server/core/lib/notifier/shared/blacklist/unblacklist-for-owner.ts
+++ b/server/core/lib/notifier/shared/blacklist/unblacklist-for-owner.ts
@@ -1,7 +1,6 @@
import { UserNotificationType } from '@peertube/peertube-models'
import { tu } from '@server/helpers/i18n.js'
import { logger } from '@server/helpers/logger.js'
-import { CONFIG } from '@server/initializers/config.js'
import { WEBSERVER } from '@server/initializers/constants.js'
import { UserNotificationModel } from '@server/models/user/user-notification.js'
import { UserModel } from '@server/models/user/user.js'
@@ -50,13 +49,12 @@ export class UnblacklistForOwner extends AbstractNotification<MVideoAccountLight
template: 'video-owner-unblacklist',
to,
subject: tu('Your video has been unblocked', user),
+ action: {
+ text: tu('View video', user),
+ url: videoUrl
+ },
locals: {
- instanceName: CONFIG.INSTANCE.NAME,
- videoName: video.name,
- action: {
- text: tu('View video', user),
- url: videoUrl
- }
+ videoName: video.name
}
}
}
diff --git a/server/core/lib/notifier/shared/caption/video-transcription-generated-for-owner.ts b/server/core/lib/notifier/shared/caption/video-transcription-generated-for-owner.ts
index 2d0d4dd012..bcb0a2dc7a 100644
--- a/server/core/lib/notifier/shared/caption/video-transcription-generated-for-owner.ts
+++ b/server/core/lib/notifier/shared/caption/video-transcription-generated-for-owner.ts
@@ -60,11 +60,9 @@ export class VideoTranscriptionGeneratedForOwner extends AbstractNotification<MV
language,
videoName: video.name
}),
- locals: {
- action: {
- text: t('View video', userLanguage),
- url: videoUrl
- }
+ action: {
+ text: t('View video', userLanguage),
+ url: videoUrl
}
}
}
diff --git a/server/core/lib/notifier/shared/change-ownership/channel-change-ownership-accepted.ts b/server/core/lib/notifier/shared/change-ownership/channel-change-ownership-accepted.ts
index d0b22095d5..36148c6c54 100644
--- a/server/core/lib/notifier/shared/change-ownership/channel-change-ownership-accepted.ts
+++ b/server/core/lib/notifier/shared/change-ownership/channel-change-ownership-accepted.ts
@@ -28,11 +28,9 @@ export class ChannelChangeOwnershipAccepted extends AbstractChannelChangeOwnersh
nextOwner: this.payload.NextOwner.getDisplayName(),
channelName: this.payload.VideoChannel.name
}),
- locals: {
- action: {
- text: t('View your notifications', language),
- url: WEBSERVER.URL + '/my-account/notifications'
- }
+ action: {
+ text: t('View your notifications', language),
+ url: WEBSERVER.URL + '/my-account/notifications'
}
}
}
diff --git a/server/core/lib/notifier/shared/change-ownership/channel-change-ownership-rejected.ts b/server/core/lib/notifier/shared/change-ownership/channel-change-ownership-rejected.ts
index dde22b619e..6da5408f36 100644
--- a/server/core/lib/notifier/shared/change-ownership/channel-change-ownership-rejected.ts
+++ b/server/core/lib/notifier/shared/change-ownership/channel-change-ownership-rejected.ts
@@ -28,11 +28,9 @@ export class ChannelChangeOwnershipRejected extends AbstractChannelChangeOwnersh
nextOwner: this.payload.NextOwner.getDisplayName(),
channelName: this.payload.VideoChannel.name
}),
- locals: {
- action: {
- text: t('View your notifications', language),
- url: WEBSERVER.URL + '/my-account/notifications'
- }
+ action: {
+ text: t('View your notifications', language),
+ url: WEBSERVER.URL + '/my-account/notifications'
}
}
}
diff --git a/server/core/lib/notifier/shared/change-ownership/request-channel-change-ownership.ts b/server/core/lib/notifier/shared/change-ownership/request-channel-change-ownership.ts
index a07da43d2a..b86072e354 100644
--- a/server/core/lib/notifier/shared/change-ownership/request-channel-change-ownership.ts
+++ b/server/core/lib/notifier/shared/change-ownership/request-channel-change-ownership.ts
@@ -59,11 +59,9 @@ export class RequestChannelChangeOwnership extends AbstractNotification<MChangeO
initiator: this.payload.Initiator.getDisplayName(),
channelName: this.payload.VideoChannel.name
}),
- locals: {
- action: {
- text: t('Review the request', language),
- url: WEBSERVER.URL + '/my-account/notifications'
- }
+ action: {
+ text: t('Review the request', language),
+ url: WEBSERVER.URL + '/my-account/notifications'
}
}
}
diff --git a/server/core/lib/notifier/shared/change-ownership/request-video-change-ownership.ts b/server/core/lib/notifier/shared/change-ownership/request-video-change-ownership.ts
index ab97117bd7..e743d964b0 100644
--- a/server/core/lib/notifier/shared/change-ownership/request-video-change-ownership.ts
+++ b/server/core/lib/notifier/shared/change-ownership/request-video-change-ownership.ts
@@ -2,12 +2,12 @@ import { UserNotificationSettingValue, UserNotificationType } from '@peertube/pe
import { t } from '@server/helpers/i18n.js'
import { logger } from '@server/helpers/logger.js'
import { WEBSERVER } from '@server/initializers/constants.js'
+import { AccountBlocklistModel } from '@server/models/blocklist/account-blocklist.js'
import { UserModel } from '@server/models/user/user.js'
import { MUserDefault, MUserWithNotificationSetting } from '@server/types/models/index.js'
import { MChangeOwnershipFull } from '@server/types/models/video/change-ownership.js'
import { AbstractNotification } from '../common/abstract-notification.js'
import { buildChangeOwnershipNotification } from './change-ownership-utils.js'
-import { AccountBlocklistModel } from '@server/models/blocklist/account-blocklist.js'
export class RequestVideoChangeOwnership extends AbstractNotification<MChangeOwnershipFull> {
private user: MUserDefault
@@ -59,11 +59,9 @@ export class RequestVideoChangeOwnership extends AbstractNotification<MChangeOwn
initiator: this.payload.Initiator.getDisplayName(),
videoName: this.payload.Video.name
}),
- locals: {
- action: {
- text: t('Review the request', language),
- url: WEBSERVER.URL + '/my-library/ownership'
- }
+ action: {
+ text: t('Review the request', language),
+ url: WEBSERVER.URL + '/my-library/ownership'
}
}
}
diff --git a/server/core/lib/notifier/shared/change-ownership/video-change-ownership-accepted.ts b/server/core/lib/notifier/shared/change-ownership/video-change-ownership-accepted.ts
index c3aeaf8cf2..41dfdb158e 100644
--- a/server/core/lib/notifier/shared/change-ownership/video-change-ownership-accepted.ts
+++ b/server/core/lib/notifier/shared/change-ownership/video-change-ownership-accepted.ts
@@ -25,11 +25,9 @@ export class VideoChangeOwnershipAccepted extends AbstractVideoChangeOwnershipRe
nextOwner: this.payload.NextOwner.getDisplayName(),
videoName: this.payload.Video.name
}),
- locals: {
- action: {
- text: t('Review ownership changes', language),
- url: WEBSERVER.URL + '/my-library/ownership'
- }
+ action: {
+ text: t('Review ownership changes', language),
+ url: WEBSERVER.URL + '/my-library/ownership'
}
}
}
diff --git a/server/core/lib/notifier/shared/change-ownership/video-change-ownership-rejected.ts b/server/core/lib/notifier/shared/change-ownership/video-change-ownership-rejected.ts
index 149f557c74..674e55b050 100644
--- a/server/core/lib/notifier/shared/change-ownership/video-change-ownership-rejected.ts
+++ b/server/core/lib/notifier/shared/change-ownership/video-change-ownership-rejected.ts
@@ -25,11 +25,9 @@ export class VideoChangeOwnershipRejected extends AbstractVideoChangeOwnershipRe
nextOwner: this.payload.NextOwner.getDisplayName(),
videoName: this.payload.Video.name
}),
- locals: {
- action: {
- text: t('Review ownership changes', language),
- url: WEBSERVER.URL + '/my-library/ownership'
- }
+ action: {
+ text: t('Review ownership changes', language),
+ url: WEBSERVER.URL + '/my-library/ownership'
}
}
}
diff --git a/server/core/lib/notifier/shared/channel/accepted-to-collaborate-to-channel.ts b/server/core/lib/notifier/shared/channel/accepted-to-collaborate-to-channel.ts
index cae3cbc7e6..b6123906ac 100644
--- a/server/core/lib/notifier/shared/channel/accepted-to-collaborate-to-channel.ts
+++ b/server/core/lib/notifier/shared/channel/accepted-to-collaborate-to-channel.ts
@@ -57,11 +57,9 @@ export class AcceptedToCollaborateToChannel extends AbstractNotification<Notific
to,
subject: text,
text,
- locals: {
- action: {
- text: t('Manage your channel', userLanguage),
- url: channel.getClientManageUrl()
- }
+ action: {
+ text: t('Manage your channel', userLanguage),
+ url: channel.getClientManageUrl()
}
}
}
diff --git a/server/core/lib/notifier/shared/channel/invited-to-collaborate-to-channel.ts b/server/core/lib/notifier/shared/channel/invited-to-collaborate-to-channel.ts
index cf31dcf2d4..16616b4f6e 100644
--- a/server/core/lib/notifier/shared/channel/invited-to-collaborate-to-channel.ts
+++ b/server/core/lib/notifier/shared/channel/invited-to-collaborate-to-channel.ts
@@ -65,11 +65,9 @@ export class InvitedToCollaborateToChannel extends AbstractNotification<Notifica
to,
subject: text,
text,
- locals: {
- action: {
- text: t('Review the invitation', userLanguage),
- url: WEBSERVER.URL + '/my-account/notifications'
- }
+ action: {
+ text: t('Review the invitation', userLanguage),
+ url: WEBSERVER.URL + '/my-account/notifications'
}
}
}
diff --git a/server/core/lib/notifier/shared/channel/refused-to-collaborate-to-channel.ts b/server/core/lib/notifier/shared/channel/refused-to-collaborate-to-channel.ts
index 4f8442502a..44ca44dc4e 100644
--- a/server/core/lib/notifier/shared/channel/refused-to-collaborate-to-channel.ts
+++ b/server/core/lib/notifier/shared/channel/refused-to-collaborate-to-channel.ts
@@ -57,11 +57,9 @@ export class RefusedToCollaborateToChannel extends AbstractNotification<Notifica
to,
subject: text,
text,
- locals: {
- action: {
- text: t('Manage your channel', userLanguage),
- url: channel.getClientManageUrl()
- }
+ action: {
+ text: t('Manage your channel', userLanguage),
+ url: channel.getClientManageUrl()
}
}
}
diff --git a/server/core/lib/notifier/shared/comment/comment-mention.ts b/server/core/lib/notifier/shared/comment/comment-mention.ts
index 35e1d68c91..696ab8429a 100644
--- a/server/core/lib/notifier/shared/comment/comment-mention.ts
+++ b/server/core/lib/notifier/shared/comment/comment-mention.ts
@@ -104,17 +104,17 @@ export class CommentMention extends AbstractNotification<MCommentOwnerVideo, MUs
template: 'video-comment-mention',
to,
subject: tu('Mention on video {videoName}', user, { videoName: video.name }),
+ action: {
+ text: tu('View comment', user),
+ url: commentUrl
+ },
locals: {
comment,
commentHtml,
video,
videoUrl,
accountName,
- accountUrl: comment.Account.getClientUrl(),
- action: {
- text: tu('View comment', user),
- url: commentUrl
- }
+ accountUrl: comment.Account.getClientUrl()
}
}
}
diff --git a/server/core/lib/notifier/shared/comment/new-comment-for-video-owner.ts b/server/core/lib/notifier/shared/comment/new-comment-for-video-owner.ts
index 4c946e643a..1b2a664950 100644
--- a/server/core/lib/notifier/shared/comment/new-comment-for-video-owner.ts
+++ b/server/core/lib/notifier/shared/comment/new-comment-for-video-owner.ts
@@ -67,6 +67,15 @@ export class NewCommentForVideoOwner extends AbstractNotification<MCommentOwnerV
template: 'video-comment-new',
to,
subject: t('New comment on your video', to.language),
+
+ action: {
+ text: comment.heldForReview
+ ? t('Review comment', to.language)
+ : t('View comment', to.language),
+
+ url: commentUrl
+ },
+
locals: {
accountName: this.payload.Account.getDisplayName(),
accountUrl: this.payload.Account.getClientUrl(),
@@ -74,14 +83,7 @@ export class NewCommentForVideoOwner extends AbstractNotification<MCommentOwnerV
commentHtml,
video,
videoUrl,
- requiresApproval: this.payload.heldForReview,
-
- action: {
- text: comment.heldForReview
- ? t('Review comment', to.language)
- : t('View comment', to.language),
- url: commentUrl
- }
+ requiresApproval: this.payload.heldForReview
}
}
}
diff --git a/server/core/lib/notifier/shared/follow/auto-follow-for-instance.ts b/server/core/lib/notifier/shared/follow/auto-follow-for-instance.ts
index 49ca97edca..3efade61d9 100644
--- a/server/core/lib/notifier/shared/follow/auto-follow-for-instance.ts
+++ b/server/core/lib/notifier/shared/follow/auto-follow-for-instance.ts
@@ -46,11 +46,9 @@ export class AutoFollowForInstance extends AbstractNotification<MActorFollowFull
to,
subject: t('Auto platform follow', to.language),
text: t('Your platform automatically followed {identifier}', to.language, { identifier: subscription.getIdentifier() }),
- locals: {
- action: {
- text: t('View subscription', to.language),
- url: instanceFollowingUrl
- }
+ action: {
+ text: t('View subscription', to.language),
+ url: instanceFollowingUrl
}
}
}
diff --git a/server/core/lib/notifier/shared/follow/follow-for-instance.ts b/server/core/lib/notifier/shared/follow/follow-for-instance.ts
index 4e60b4974f..ec5ceee859 100644
--- a/server/core/lib/notifier/shared/follow/follow-for-instance.ts
+++ b/server/core/lib/notifier/shared/follow/follow-for-instance.ts
@@ -59,11 +59,9 @@ export class FollowForInstance extends AbstractNotification<MActorFollowFull> {
to,
subject: t('New follower for {instanceName}', language, { instanceName: CONFIG.INSTANCE.NAME }),
text,
- locals: {
- action: {
- text: t('Review followers', language),
- url: WEBSERVER.URL + '/admin/follows/followers-list'
- }
+ action: {
+ text: t('Review followers', language),
+ url: WEBSERVER.URL + '/admin/follows/followers-list'
}
}
}
diff --git a/server/core/lib/notifier/shared/instance/direct-registration-for-moderators.ts b/server/core/lib/notifier/shared/instance/direct-registration-for-moderators.ts
index f7bbf94438..179513a649 100644
--- a/server/core/lib/notifier/shared/instance/direct-registration-for-moderators.ts
+++ b/server/core/lib/notifier/shared/instance/direct-registration-for-moderators.ts
@@ -45,15 +45,15 @@ export class DirectRegistrationForModerators extends AbstractNotification<MUserD
template: 'user-registered',
to,
subject: t('A new user registered on {instanceName}', to.language, { instanceName: CONFIG.INSTANCE.NAME }),
+ action: {
+ text: t('View users', to.language),
+ url: adminUsersListUrl
+ },
locals: {
userUsername: this.payload.username,
userEmail: this.payload.email,
userPendingEmail: this.payload.pendingEmail,
- accountUrl: this.payload.Account.getClientUrl(),
- action: {
- type: t('View users', to.language),
- url: adminUsersListUrl
- }
+ accountUrl: this.payload.Account.getClientUrl()
}
}
}
diff --git a/server/core/lib/notifier/shared/instance/registration-request-for-moderators.ts b/server/core/lib/notifier/shared/instance/registration-request-for-moderators.ts
index e6281f7ea1..a34ae3d04d 100644
--- a/server/core/lib/notifier/shared/instance/registration-request-for-moderators.ts
+++ b/server/core/lib/notifier/shared/instance/registration-request-for-moderators.ts
@@ -1,7 +1,6 @@
import { UserNotificationType, UserRight } from '@peertube/peertube-models'
import { t } from '@server/helpers/i18n.js'
import { logger } from '@server/helpers/logger.js'
-import { CONFIG } from '@server/initializers/config.js'
import { adminRegistrationsListUrl } from '@server/lib/client-urls.js'
import { UserNotificationModel } from '@server/models/user/user-notification.js'
import { UserModel } from '@server/models/user/user.js'
@@ -46,13 +45,12 @@ export class RegistrationRequestForModerators extends AbstractNotification<MRegi
template: 'user-registration-request',
to,
subject: t('A new user wants to register: {username}', to.language, { username: this.payload.username }),
+ action: {
+ url: adminRegistrationsListUrl,
+ text: t('View registration request', language)
+ },
locals: {
- registration: this.payload,
- instanceName: CONFIG.INSTANCE.NAME,
- action: {
- url: adminRegistrationsListUrl,
- text: t('View registration request', language)
- }
+ registration: this.payload
}
}
}
diff --git a/server/core/lib/notifier/shared/video-publication/abstract-owned-video-publication.ts b/server/core/lib/notifier/shared/video-publication/abstract-owned-video-publication.ts
index 2cebab7bb1..49d91dc728 100644
--- a/server/core/lib/notifier/shared/video-publication/abstract-owned-video-publication.ts
+++ b/server/core/lib/notifier/shared/video-publication/abstract-owned-video-publication.ts
@@ -54,13 +54,11 @@ export abstract class AbstractOwnedVideoPublication extends AbstractNotification
return {
to,
subject: t('Your video has been published', language),
+ title: t('Your video is live', language),
text: t('Your video {videoName} has been published.', language, { videoName: this.payload.name }),
- locals: {
- title: t('Your video is live', language),
- action: {
- text: t('View video', language),
- url: videoUrl
- }
+ action: {
+ text: t('View video', language),
+ url: videoUrl
}
}
}
diff --git a/server/core/lib/notifier/shared/video-publication/import-finished-for-owner.ts b/server/core/lib/notifier/shared/video-publication/import-finished-for-owner.ts
index f8fb50f81f..2c854ec116 100644
--- a/server/core/lib/notifier/shared/video-publication/import-finished-for-owner.ts
+++ b/server/core/lib/notifier/shared/video-publication/import-finished-for-owner.ts
@@ -1,4 +1,4 @@
-import { To, UserNotificationType } from '@peertube/peertube-models'
+import { MailTo, UserNotificationType } from '@peertube/peertube-models'
import { t } from '@server/helpers/i18n.js'
import { logger } from '@server/helpers/logger.js'
import { WEBSERVER } from '@server/initializers/constants.js'
@@ -56,7 +56,7 @@ export class ImportFinishedForOwner extends AbstractNotification<ImportFinishedF
return this.createFailEmail(to)
}
- private createSuccessEmail (to: To) {
+ private createSuccessEmail (to: MailTo) {
const videoUrl = WEBSERVER.URL + this.videoImport.Video.getWatchStaticPath()
const language = to.language
const targetId = this.videoImport.getTargetIdentifier()
@@ -65,16 +65,14 @@ export class ImportFinishedForOwner extends AbstractNotification<ImportFinishedF
to,
subject: t('Your video import is complete', language),
text: t('Your video {targetId} has just finished importing.', language, { targetId }),
- locals: {
- action: {
- text: t('View video', language),
- url: videoUrl
- }
+ action: {
+ text: t('View video', language),
+ url: videoUrl
}
}
}
- private createFailEmail (to: To) {
+ private createFailEmail (to: MailTo) {
const language = to.language
const targetId = this.videoImport.getTargetIdentifier()
@@ -83,13 +81,11 @@ export class ImportFinishedForOwner extends AbstractNotification<ImportFinishedF
return {
to,
subject: t('Your video import encountered an error', language),
+ title: t('Import failed', language),
text,
- locals: {
- title: t('Import failed', language),
- action: {
- text: t('Review imports', language),
- url: myVideoImportsUrl
- }
+ action: {
+ text: t('Review imports', language),
+ url: myVideoImportsUrl
}
}
}
diff --git a/server/core/lib/notifier/shared/video-publication/studio-edition-finished-for-owner.ts b/server/core/lib/notifier/shared/video-publication/studio-edition-finished-for-owner.ts
index b6b2fb5890..a2cf651bca 100644
--- a/server/core/lib/notifier/shared/video-publication/studio-edition-finished-for-owner.ts
+++ b/server/core/lib/notifier/shared/video-publication/studio-edition-finished-for-owner.ts
@@ -47,12 +47,10 @@ export class StudioEditionFinishedForOwner extends AbstractNotification<MVideoFu
to,
subject: t('Edition of your video has finished', to.language),
text: t('Edition of your video {videoName} has finished.', to.language, { videoName: this.payload.name }),
- locals: {
- title: t('Video edition has finished', to.language),
- action: {
- text: t('View video', to.language),
- url: videoUrl
- }
+ title: t('Video edition has finished', to.language),
+ action: {
+ text: t('View video', to.language),
+ url: videoUrl
}
}
}
diff --git a/server/core/lib/plugins/plugin-helpers-builder.ts b/server/core/lib/plugins/plugin-helpers-builder.ts
index a049b3126f..29a997bc29 100644
--- a/server/core/lib/plugins/plugin-helpers-builder.ts
+++ b/server/core/lib/plugins/plugin-helpers-builder.ts
@@ -21,6 +21,7 @@ import express from 'express'
import { Server } from 'http'
import { join } from 'path'
import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist.js'
+import { JobQueue } from '../job-queue/index.js'
import { PeerTubeSocket } from '../peertube-socket.js'
import { ServerConfigManager } from '../server-config-manager.js'
import { blacklistVideo, unblacklistVideo } from '../video-blacklist.js'
@@ -45,7 +46,9 @@ function buildPluginHelpers (httpServer: Server, pluginModel: MPlugin, npmName:
user: buildUserHelpers(),
- automaticTags: buildAutomaticTagsHelpers()
+ automaticTags: buildAutomaticTagsHelpers(),
+
+ email: buildEmailHelpers()
}
}
@@ -309,3 +312,23 @@ function buildAutomaticTagsHelpers () {
}
}
}
+
+function buildEmailHelpers () {
+ return {
+ createJob: async (payload: Parameters<PeerTubeHelpers['email']['createJob']>[0]) => {
+ await JobQueue.Instance.createJob({
+ type: 'email',
+ payload: {
+ to: { email: payload.to.email, language: payload.to.language || CONFIG.INSTANCE.DEFAULT_LANGUAGE },
+
+ text: payload.text,
+
+ subject: payload.subject,
+ title: payload.title,
+
+ action: payload.action
+ }
+ })
+ }
+ }
+}
diff --git a/server/core/types/plugins/register-server-option.model.ts b/server/core/types/plugins/register-server-option.model.ts
index cd60321774..b41cc1b6a0 100644
--- a/server/core/types/plugins/register-server-option.model.ts
+++ b/server/core/types/plugins/register-server-option.model.ts
@@ -134,6 +134,29 @@ export type PeerTubeHelpers = {
// PeerTube >= 8.3
getServerVideoAutomaticTags: (options: { videoId: number }) => Promise<MAutomaticTag[]>
}
+
+ email: {
+ // PeerTube >= 8.3
+ createJob: (payload: {
+ // Language of the email to send. If not provided, the default language of the instance will be used
+ to: { email: string, language?: string }
+
+ // Email subject
+ subject: string
+
+ // Optional title, injected in HTML email
+ title?: string
+
+ // Text inside the HTML email
+ text: string
+
+ // Button that can be added at the end of the email
+ action?: {
+ url: string
+ text: string
+ }
+ }) => Promise<void>
+ }
}
export type RegisterServerOptions = {
diff --git a/support/doc/plugins/guide.md b/support/doc/plugins/guide.md
index ca6ae92fa5..67cd09b6b6 100644
--- a/support/doc/plugins/guide.md
+++ b/support/doc/plugins/guide.md
@@ -574,6 +574,15 @@ async function register ({
{
const video = await peertubeHelpers.videos.loadByUrl('...')
}
+
+ // Send an email
+ {
+ await peertubeHelpers.email.createJob({
+ to: { email: '[email protected]', language: 'en' },
+ subject: 'Hello from my plugin',
+ text: 'Plugin body text'
+ })
+ }
}
```