ActivityPub-federated video streaming platform using P2P directly in your web browser
Add notification plugin hook
Chocobozzz785f1897a429
diff --git a/server/lib/notifier/notifier.ts b/server/lib/notifier/notifier.tsindex a6f13780b4..d1c4c02153 100644--- a/server/lib/notifier/notifier.ts+++ b/server/lib/notifier/notifier.ts@@ -7,12 +7,12 @@ import { MAbuseFull, MAbuseMessage, MActorFollowFull, MApplication, MPlugin } frimport { MCommentOwnerVideo, MVideoAccountLight, MVideoFullLight } from '../../types/models/video'import { JobQueue } from '../job-queue'import { PeerTubeSocket } from '../peertube-socket'+import { Hooks } from '../plugins/hooks'import {AbstractNotification,AbuseStateChangeForReporter,AutoFollowForInstance,CommentMention,- StudioEditionFinishedForOwner,FollowForInstance,FollowForUser,ImportFinishedForOwner,@@ -31,6 +31,7 @@ import {OwnedPublicationAfterScheduleUpdate,OwnedPublicationAfterTranscoding,RegistrationForModerators,+ StudioEditionFinishedForOwner,UnblacklistForOwner} from './shared'@@ -222,15 +223,21 @@ class Notifier {for (const user of users) {const setting = object.getSetting(user)- if (this.isWebNotificationEnabled(setting)) {- const notification = await object.createNotification(user)+ const webNotificationEnabled = this.isWebNotificationEnabled(setting)+ const emailNotificationEnabled = this.isEmailEnabled(user, setting)+ const notification = object.createNotification(user)++ if (webNotificationEnabled) {+ await notification.save()PeerTubeSocket.Instance.sendNotification(user.id, notification)}- if (this.isEmailEnabled(user, setting)) {+ if (emailNotificationEnabled) {toEmails.push(user.email)}++ Hooks.runAction('action:notifier.notification.created', { webNotificationEnabled, emailNotificationEnabled, user, notification })}for (const to of toEmails) {diff --git a/server/lib/notifier/shared/abuse/abstract-new-abuse-message.ts b/server/lib/notifier/shared/abuse/abstract-new-abuse-message.tsindex a7292de695..1dc1ccfc23 100644--- a/server/lib/notifier/shared/abuse/abstract-new-abuse-message.ts+++ b/server/lib/notifier/shared/abuse/abstract-new-abuse-message.ts@@ -21,8 +21,8 @@ export abstract class AbstractNewAbuseMessage extends AbstractNotification <NewAreturn user.NotificationSetting.abuseNewMessage}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.ABUSE_NEW_MESSAGE,userId: user.id,abuseId: this.abuse.iddiff --git a/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts b/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.tsindex d92f7a13ba..97e896c6a6 100644--- a/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts+++ b/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts@@ -32,8 +32,8 @@ export class AbuseStateChangeForReporter extends AbstractNotification <MAbuseFulreturn [ this.user ]}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.ABUSE_STATE_CHANGE,userId: user.id,abuseId: this.abuse.iddiff --git a/server/lib/notifier/shared/abuse/new-abuse-for-moderators.ts b/server/lib/notifier/shared/abuse/new-abuse-for-moderators.tsindex c3c7c55156..7d86fb55f5 100644--- a/server/lib/notifier/shared/abuse/new-abuse-for-moderators.ts+++ b/server/lib/notifier/shared/abuse/new-abuse-for-moderators.ts@@ -28,8 +28,8 @@ export class NewAbuseForModerators extends AbstractNotification <NewAbusePayloadreturn this.moderators}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.NEW_ABUSE_FOR_MODERATORS,userId: user.id,abuseId: this.payload.abuseInstance.iddiff --git a/server/lib/notifier/shared/blacklist/new-auto-blacklist-for-moderators.ts b/server/lib/notifier/shared/blacklist/new-auto-blacklist-for-moderators.tsindex a92a49a0c7..ad2cc00ea9 100644--- a/server/lib/notifier/shared/blacklist/new-auto-blacklist-for-moderators.ts+++ b/server/lib/notifier/shared/blacklist/new-auto-blacklist-for-moderators.ts@@ -26,8 +26,8 @@ export class NewAutoBlacklistForModerators extends AbstractNotification <MVideoBreturn this.moderators}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS,userId: user.id,videoBlacklistId: this.payload.iddiff --git a/server/lib/notifier/shared/blacklist/new-blacklist-for-owner.ts b/server/lib/notifier/shared/blacklist/new-blacklist-for-owner.tsindex 45bc30eb29..342b69ec78 100644--- a/server/lib/notifier/shared/blacklist/new-blacklist-for-owner.ts+++ b/server/lib/notifier/shared/blacklist/new-blacklist-for-owner.ts@@ -28,8 +28,8 @@ export class NewBlacklistForOwner extends AbstractNotification <MVideoBlacklistVreturn [ this.user ]}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.BLACKLIST_ON_MY_VIDEO,userId: user.id,videoBlacklistId: this.payload.iddiff --git a/server/lib/notifier/shared/blacklist/unblacklist-for-owner.ts b/server/lib/notifier/shared/blacklist/unblacklist-for-owner.tsindex 21f5a1c2d2..e6f90e23cd 100644--- a/server/lib/notifier/shared/blacklist/unblacklist-for-owner.ts+++ b/server/lib/notifier/shared/blacklist/unblacklist-for-owner.ts@@ -28,8 +28,8 @@ export class UnblacklistForOwner extends AbstractNotification <MVideoFullLight>return [ this.user ]}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO,userId: user.id,videoId: this.payload.iddiff --git a/server/lib/notifier/shared/comment/comment-mention.ts b/server/lib/notifier/shared/comment/comment-mention.tsindex ecd1687b47..3074e97db6 100644--- a/server/lib/notifier/shared/comment/comment-mention.ts+++ b/server/lib/notifier/shared/comment/comment-mention.ts@@ -71,8 +71,8 @@ export class CommentMention extends AbstractNotification <MCommentOwnerVideo, MUreturn this.users}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.COMMENT_MENTION,userId: user.id,commentId: this.payload.iddiff --git a/server/lib/notifier/shared/comment/new-comment-for-video-owner.ts b/server/lib/notifier/shared/comment/new-comment-for-video-owner.tsindex 7575027038..4f96439a30 100644--- a/server/lib/notifier/shared/comment/new-comment-for-video-owner.ts+++ b/server/lib/notifier/shared/comment/new-comment-for-video-owner.ts@@ -38,8 +38,8 @@ export class NewCommentForVideoOwner extends AbstractNotification <MCommentOwnerreturn [ this.user ]}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO,userId: user.id,commentId: this.payload.iddiff --git a/server/lib/notifier/shared/common/abstract-notification.ts b/server/lib/notifier/shared/common/abstract-notification.tsindex 53e2e02d53..79403611e7 100644--- a/server/lib/notifier/shared/common/abstract-notification.ts+++ b/server/lib/notifier/shared/common/abstract-notification.ts@@ -13,7 +13,7 @@ export abstract class AbstractNotification <T, U = MUserWithNotificationSetting>abstract getSetting (user: U): UserNotificationSettingValueabstract getTargetUsers (): U[]- abstract createNotification (user: U): Promise<UserNotificationModelForApi>+ abstract createNotification (user: U): UserNotificationModelForApiabstract createEmail (to: string): EmailPayload | Promise<EmailPayload>isDisabled (): boolean | Promise<boolean> {diff --git a/server/lib/notifier/shared/follow/auto-follow-for-instance.ts b/server/lib/notifier/shared/follow/auto-follow-for-instance.tsindex 01d2b35631..ab9747ba88 100644--- a/server/lib/notifier/shared/follow/auto-follow-for-instance.ts+++ b/server/lib/notifier/shared/follow/auto-follow-for-instance.ts@@ -24,8 +24,8 @@ export class AutoFollowForInstance extends AbstractNotification <MActorFollowFulreturn this.admins}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.AUTO_INSTANCE_FOLLOWING,userId: user.id,actorFollowId: this.actorFollow.iddiff --git a/server/lib/notifier/shared/follow/follow-for-instance.ts b/server/lib/notifier/shared/follow/follow-for-instance.tsindex a4a2fbf537..777a12ef49 100644--- a/server/lib/notifier/shared/follow/follow-for-instance.ts+++ b/server/lib/notifier/shared/follow/follow-for-instance.ts@@ -32,8 +32,8 @@ export class FollowForInstance extends AbstractNotification <MActorFollowFull> {return this.admins}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.NEW_INSTANCE_FOLLOWER,userId: user.id,actorFollowId: this.actorFollow.iddiff --git a/server/lib/notifier/shared/follow/follow-for-user.ts b/server/lib/notifier/shared/follow/follow-for-user.tsindex e579d44877..697c82cddd 100644--- a/server/lib/notifier/shared/follow/follow-for-user.ts+++ b/server/lib/notifier/shared/follow/follow-for-user.ts@@ -45,8 +45,8 @@ export class FollowForUser extends AbstractNotification <MActorFollowFull> {return [ this.user ]}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.NEW_FOLLOW,userId: user.id,actorFollowId: this.actorFollow.iddiff --git a/server/lib/notifier/shared/instance/new-peertube-version-for-admins.ts b/server/lib/notifier/shared/instance/new-peertube-version-for-admins.tsindex 6b5ac808a0..f5646c6669 100644--- a/server/lib/notifier/shared/instance/new-peertube-version-for-admins.ts+++ b/server/lib/notifier/shared/instance/new-peertube-version-for-admins.ts@@ -30,8 +30,8 @@ export class NewPeerTubeVersionForAdmins extends AbstractNotification <NewPeerTureturn this.admins}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.NEW_PEERTUBE_VERSION,userId: user.id,applicationId: this.payload.application.iddiff --git a/server/lib/notifier/shared/instance/new-plugin-version-for-admins.ts b/server/lib/notifier/shared/instance/new-plugin-version-for-admins.tsindex 874b10a3de..547c6726c0 100644--- a/server/lib/notifier/shared/instance/new-plugin-version-for-admins.ts+++ b/server/lib/notifier/shared/instance/new-plugin-version-for-admins.ts@@ -26,8 +26,8 @@ export class NewPluginVersionForAdmins extends AbstractNotification <MPlugin> {return this.admins}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.NEW_PLUGIN_VERSION,userId: user.id,pluginId: this.plugin.iddiff --git a/server/lib/notifier/shared/instance/registration-for-moderators.ts b/server/lib/notifier/shared/instance/registration-for-moderators.tsindex 2a48ef2fa5..e924674242 100644--- a/server/lib/notifier/shared/instance/registration-for-moderators.ts+++ b/server/lib/notifier/shared/instance/registration-for-moderators.ts@@ -25,8 +25,8 @@ export class RegistrationForModerators extends AbstractNotification <MUserDefaulreturn this.moderators}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.NEW_USER_REGISTRATION,userId: user.id,accountId: this.payload.Account.iddiff --git a/server/lib/notifier/shared/video-publication/abstract-owned-video-publication.ts b/server/lib/notifier/shared/video-publication/abstract-owned-video-publication.tsindex 37435f898f..a940cde69d 100644--- a/server/lib/notifier/shared/video-publication/abstract-owned-video-publication.ts+++ b/server/lib/notifier/shared/video-publication/abstract-owned-video-publication.ts@@ -27,8 +27,8 @@ export abstract class AbstractOwnedVideoPublication extends AbstractNotificationreturn [ this.user ]}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.MY_VIDEO_PUBLISHED,userId: user.id,videoId: this.payload.iddiff --git a/server/lib/notifier/shared/video-publication/import-finished-for-owner.ts b/server/lib/notifier/shared/video-publication/import-finished-for-owner.tsindex 9f374b6f95..3bd64692fd 100644--- a/server/lib/notifier/shared/video-publication/import-finished-for-owner.ts+++ b/server/lib/notifier/shared/video-publication/import-finished-for-owner.ts@@ -32,8 +32,8 @@ export class ImportFinishedForOwner extends AbstractNotification <ImportFinishedreturn [ this.user ]}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: this.payload.success? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS: UserNotificationType.MY_VIDEO_IMPORT_ERROR,diff --git a/server/lib/notifier/shared/video-publication/new-video-for-subscribers.ts b/server/lib/notifier/shared/video-publication/new-video-for-subscribers.tsindex 4253a0930f..df7a5561d7 100644--- a/server/lib/notifier/shared/video-publication/new-video-for-subscribers.ts+++ b/server/lib/notifier/shared/video-publication/new-video-for-subscribers.ts@@ -30,8 +30,8 @@ export class NewVideoForSubscribers extends AbstractNotification <MVideoAccountLreturn this.users}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION,userId: user.id,videoId: this.payload.iddiff --git a/server/lib/notifier/shared/video-publication/studio-edition-finished-for-owner.ts b/server/lib/notifier/shared/video-publication/studio-edition-finished-for-owner.tsindex ee30272450..f36399f05a 100644--- a/server/lib/notifier/shared/video-publication/studio-edition-finished-for-owner.ts+++ b/server/lib/notifier/shared/video-publication/studio-edition-finished-for-owner.ts@@ -27,8 +27,8 @@ export class StudioEditionFinishedForOwner extends AbstractNotification <MVideoFreturn [ this.user ]}- async createNotification (user: MUserWithNotificationSetting) {- const notification = await UserNotificationModel.create<UserNotificationModelForApi>({+ createNotification (user: MUserWithNotificationSetting) {+ const notification = UserNotificationModel.build<UserNotificationModelForApi>({type: UserNotificationType.MY_VIDEO_STUDIO_EDITION_FINISHED,userId: user.id,videoId: this.payload.iddiff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.jsindex 8d7584eba2..b3febfa12b 100644--- a/server/tests/fixtures/peertube-plugin-test/main.js+++ b/server/tests/fixtures/peertube-plugin-test/main.js@@ -1,6 +1,7 @@async function register ({ registerHook, registerSetting, settingsManager, storageManager, peertubeHelpers }) {const actionHooks = ['action:application.listening',+ 'action:notifier.notification.created','action:api.video.updated','action:api.video.deleted',diff --git a/server/tests/plugins/action-hooks.ts b/server/tests/plugins/action-hooks.tsindex 209db95a44..405f81d7c3 100644--- a/server/tests/plugins/action-hooks.ts+++ b/server/tests/plugins/action-hooks.ts@@ -17,8 +17,8 @@ describe('Test plugin action hooks', function () {let videoUUID: stringlet threadId: number- function checkHook (hook: ServerHookName) {- return servers[0].servers.waitUntilLog('Run hook ' + hook)+ function checkHook (hook: ServerHookName, strictCount = true) {+ return servers[0].servers.waitUntilLog('Run hook ' + hook, 1, strictCount)}before(async function () {@@ -225,6 +225,13 @@ describe('Test plugin action hooks', function () {})})+ describe('Notification hook', function () {++ it('Should run action:notifier.notification.created', async function () {+ await checkHook('action:notifier.notification.created', false)+ })+ })+after(async function () {await cleanupTests(servers)})diff --git a/shared/models/plugins/server/server-hook.model.ts b/shared/models/plugins/server/server-hook.model.tsindex 5f3a5be10d..c8e879323e 100644--- a/shared/models/plugins/server/server-hook.model.ts+++ b/shared/models/plugins/server/server-hook.model.ts@@ -112,6 +112,9 @@ export const serverActionHookObject = {// Fired when the application has been loaded and is listening HTTP requests'action:application.listening': true,+ // Fired when a new notification is created+ 'action:notifier.notification.created': true,+// API actions hooks give access to the original express `req` and `res` parameters// Fired when a local video is updated