ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

356 lines
14 KiB

  1. import { UserNotificationSettingValue, UserNotificationSettingValueType } from '@peertube/peertube-models'
  2. import { MRegistration, MUser, MUserDefault } from '@server/types/models/user/index.js'
  3. import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist.js'
  4. import { logger, loggerTagsFactory } from '../../helpers/logger.js'
  5. import { CONFIG } from '../../initializers/config.js'
  6. import {
  7. MAbuseFull,
  8. MAbuseMessage,
  9. MActorFollowFull,
  10. MApplication,
  11. MCommentOwnerVideo,
  12. MPlugin,
  13. MVideoAccountLight,
  14. MVideoCaptionVideo,
  15. MVideoFullLight
  16. } from '../../types/models/index.js'
  17. import { JobQueue } from '../job-queue/index.js'
  18. import { PeerTubeSocket } from '../peertube-socket.js'
  19. import { Hooks } from '../plugins/hooks.js'
  20. import {
  21. AbstractNotification,
  22. AbuseStateChangeForReporter,
  23. AutoFollowForInstance,
  24. CommentMention,
  25. DirectRegistrationForModerators,
  26. FollowForInstance,
  27. FollowForUser,
  28. ImportFinishedForOwner,
  29. ImportFinishedForOwnerPayload,
  30. NewAbuseForModerators,
  31. NewAbuseMessageForModerators,
  32. NewAbuseMessageForReporter,
  33. NewAbusePayload,
  34. NewAutoBlacklistForModerators,
  35. NewBlacklistForOwner,
  36. NewCommentForVideoOwner,
  37. NewPeerTubeVersionForAdmins,
  38. NewPluginVersionForAdmins,
  39. NewVideoOrLiveForSubscribers,
  40. OwnedPublicationAfterAutoUnblacklist,
  41. OwnedPublicationAfterScheduleUpdate,
  42. OwnedPublicationAfterTranscoding,
  43. RegistrationRequestForModerators,
  44. StudioEditionFinishedForOwner,
  45. UnblacklistForOwner,
  46. VideoTranscriptionGeneratedForOwner
  47. } from './shared/index.js'
  48. const lTags = loggerTagsFactory('notifier')
  49. class Notifier {
  50. private readonly notificationModels = {
  51. newVideoOrLive: [ NewVideoOrLiveForSubscribers ],
  52. publicationAfterTranscoding: [ OwnedPublicationAfterTranscoding ],
  53. publicationAfterScheduleUpdate: [ OwnedPublicationAfterScheduleUpdate ],
  54. publicationAfterAutoUnblacklist: [ OwnedPublicationAfterAutoUnblacklist ],
  55. newComment: [ CommentMention, NewCommentForVideoOwner ],
  56. commentApproval: [ CommentMention ],
  57. newAbuse: [ NewAbuseForModerators ],
  58. newBlacklist: [ NewBlacklistForOwner ],
  59. unblacklist: [ UnblacklistForOwner ],
  60. importFinished: [ ImportFinishedForOwner ],
  61. directRegistration: [ DirectRegistrationForModerators ],
  62. registrationRequest: [ RegistrationRequestForModerators ],
  63. userFollow: [ FollowForUser ],
  64. instanceFollow: [ FollowForInstance ],
  65. autoInstanceFollow: [ AutoFollowForInstance ],
  66. newAutoBlacklist: [ NewAutoBlacklistForModerators ],
  67. abuseStateChange: [ AbuseStateChangeForReporter ],
  68. newAbuseMessage: [ NewAbuseMessageForReporter, NewAbuseMessageForModerators ],
  69. newPeertubeVersion: [ NewPeerTubeVersionForAdmins ],
  70. newPluginVersion: [ NewPluginVersionForAdmins ],
  71. videoStudioEditionFinished: [ StudioEditionFinishedForOwner ],
  72. videoTranscriptionGenerated: [ VideoTranscriptionGeneratedForOwner ]
  73. }
  74. private static instance: Notifier
  75. private constructor () {
  76. }
  77. notifyOnNewVideoOrLiveIfNeeded (video: MVideoAccountLight): void {
  78. const models = this.notificationModels.newVideoOrLive
  79. logger.debug('Notify on new video or live if needed', { video: video.url, ...lTags() })
  80. this.sendNotifications(models, video)
  81. .catch(err => logger.error('Cannot notify subscribers of new video %s.', video.url, { err }))
  82. }
  83. notifyOnVideoPublishedAfterTranscoding (video: MVideoFullLight): void {
  84. const models = this.notificationModels.publicationAfterTranscoding
  85. logger.debug('Notify on published video after transcoding', { video: video.url, ...lTags() })
  86. this.sendNotifications(models, video)
  87. .catch(err => logger.error('Cannot notify owner that its video %s has been published after transcoding.', video.url, { err }))
  88. }
  89. notifyOnVideoPublishedAfterScheduledUpdate (video: MVideoFullLight): void {
  90. const models = this.notificationModels.publicationAfterScheduleUpdate
  91. logger.debug('Notify on published video after scheduled update', { video: video.url, ...lTags() })
  92. this.sendNotifications(models, video)
  93. .catch(err => logger.error('Cannot notify owner that its video %s has been published after scheduled update.', video.url, { err }))
  94. }
  95. notifyOnVideoPublishedAfterRemovedFromAutoBlacklist (video: MVideoFullLight): void {
  96. const models = this.notificationModels.publicationAfterAutoUnblacklist
  97. logger.debug('Notify on published video after being removed from auto blacklist', { video: video.url, ...lTags() })
  98. this.sendNotifications(models, video)
  99. .catch(err => {
  100. logger.error('Cannot notify owner that its video %s has been published after removed from auto-blacklist.', video.url, { err })
  101. })
  102. }
  103. notifyOnNewComment (comment: MCommentOwnerVideo): void {
  104. const models = this.notificationModels.newComment
  105. logger.debug('Notify on new comment', { comment: comment.url, ...lTags() })
  106. this.sendNotifications(models, comment)
  107. .catch(err => logger.error('Cannot notify of new comment %s.', comment.url, { err }))
  108. }
  109. notifyOnNewCommentApproval (comment: MCommentOwnerVideo): void {
  110. const models = this.notificationModels.commentApproval
  111. logger.debug('Notify on comment approval', { comment: comment.url, ...lTags() })
  112. this.sendNotifications(models, comment)
  113. .catch(err => logger.error('Cannot notify on comment approval %s.', comment.url, { err }))
  114. }
  115. notifyOnNewAbuse (payload: NewAbusePayload): void {
  116. const models = this.notificationModels.newAbuse
  117. logger.debug('Notify on new abuse', { abuse: payload.abuseInstance.id, ...lTags() })
  118. this.sendNotifications(models, payload)
  119. .catch(err => logger.error('Cannot notify of new abuse %d.', payload.abuseInstance.id, { err }))
  120. }
  121. notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void {
  122. const models = this.notificationModels.newAutoBlacklist
  123. logger.debug('Notify on video auto blacklist', { video: videoBlacklist?.Video?.url, ...lTags() })
  124. this.sendNotifications(models, videoBlacklist)
  125. .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err }))
  126. }
  127. notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void {
  128. const models = this.notificationModels.newBlacklist
  129. logger.debug('Notify on video manual blacklist', { video: videoBlacklist?.Video?.url, ...lTags() })
  130. this.sendNotifications(models, videoBlacklist)
  131. .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err }))
  132. }
  133. notifyOnVideoUnblacklist (video: MVideoFullLight): void {
  134. const models = this.notificationModels.unblacklist
  135. logger.debug('Notify on video unblacklist', { video: video.url, ...lTags() })
  136. this.sendNotifications(models, video)
  137. .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err }))
  138. }
  139. notifyOnFinishedVideoImport (payload: ImportFinishedForOwnerPayload): void {
  140. const models = this.notificationModels.importFinished
  141. logger.debug('Notify on finished video import', { import: payload.videoImport.getTargetIdentifier(), ...lTags() })
  142. this.sendNotifications(models, payload)
  143. .catch(err => {
  144. logger.error('Cannot notify owner that its video import %s is finished.', payload.videoImport.getTargetIdentifier(), { err })
  145. })
  146. }
  147. notifyOnNewDirectRegistration (user: MUserDefault): void {
  148. const models = this.notificationModels.directRegistration
  149. logger.debug('Notify on new direct registration', { user: user.username, ...lTags() })
  150. this.sendNotifications(models, user)
  151. .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err }))
  152. }
  153. notifyOnNewRegistrationRequest (registration: MRegistration): void {
  154. const models = this.notificationModels.registrationRequest
  155. logger.debug('Notify on new registration request', { registration: registration.username, ...lTags() })
  156. this.sendNotifications(models, registration)
  157. .catch(err => logger.error('Cannot notify moderators of new registration request (%s).', registration.username, { err }))
  158. }
  159. notifyOfNewUserFollow (actorFollow: MActorFollowFull): void {
  160. const models = this.notificationModels.userFollow
  161. const following = actorFollow?.ActorFollowing?.VideoChannel?.getDisplayName()
  162. const follower = actorFollow?.ActorFollower?.Account?.getDisplayName()
  163. logger.debug('Notify on new user follow', { following, follower, ...lTags() })
  164. this.sendNotifications(models, actorFollow)
  165. .catch(err => {
  166. logger.error('Cannot notify owner of channel %s of a new follow by %s.', following, follower, { err })
  167. })
  168. }
  169. notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void {
  170. const models = this.notificationModels.instanceFollow
  171. logger.debug('Notify on new instance follow', { follower: actorFollow.ActorFollower.url, ...lTags() })
  172. this.sendNotifications(models, actorFollow)
  173. .catch(err => logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }))
  174. }
  175. notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void {
  176. const models = this.notificationModels.autoInstanceFollow
  177. logger.debug('Notify on new instance auto following', { following: actorFollow.ActorFollowing.url, ...lTags() })
  178. this.sendNotifications(models, actorFollow)
  179. .catch(err => logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err }))
  180. }
  181. notifyOnAbuseStateChange (abuse: MAbuseFull): void {
  182. const models = this.notificationModels.abuseStateChange
  183. logger.debug('Notify on abuse state change', { abuse: abuse.id, ...lTags() })
  184. this.sendNotifications(models, abuse)
  185. .catch(err => logger.error('Cannot notify of abuse %d state change.', abuse.id, { err }))
  186. }
  187. notifyOnAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage): void {
  188. const models = this.notificationModels.newAbuseMessage
  189. logger.debug('Notify on abuse message', { abuse: abuse.id, message, ...lTags() })
  190. this.sendNotifications(models, { abuse, message })
  191. .catch(err => logger.error('Cannot notify on new abuse %d message.', abuse.id, { err }))
  192. }
  193. notifyOfNewPeerTubeVersion (application: MApplication, latestVersion: string) {
  194. const models = this.notificationModels.newPeertubeVersion
  195. logger.debug('Notify on new peertube version', { currentVersion: application.version, latestVersion, ...lTags() })
  196. this.sendNotifications(models, { application, latestVersion })
  197. .catch(err => logger.error('Cannot notify on new PeerTube version %s.', latestVersion, { err }))
  198. }
  199. notifyOfNewPluginVersion (plugin: MPlugin) {
  200. const models = this.notificationModels.newPluginVersion
  201. logger.debug('Notify on new plugin version', { plugin: plugin.name, ...lTags() })
  202. this.sendNotifications(models, plugin)
  203. .catch(err => logger.error('Cannot notify on new plugin version %s.', plugin.name, { err }))
  204. }
  205. notifyOfFinishedVideoStudioEdition (video: MVideoFullLight) {
  206. const models = this.notificationModels.videoStudioEditionFinished
  207. logger.debug('Notify on finished video studio edition', { video: video.url, ...lTags() })
  208. this.sendNotifications(models, video)
  209. .catch(err => logger.error('Cannot notify on finished studio edition %s.', video.url, { err }))
  210. }
  211. notifyOfGeneratedVideoTranscription (caption: MVideoCaptionVideo) {
  212. const models = this.notificationModels.videoTranscriptionGenerated
  213. const video = caption.Video
  214. logger.debug('Notify on generated video transcription', { language: caption.language, video: video.url, ...lTags() })
  215. this.sendNotifications(models, caption)
  216. .catch(err => logger.error('Cannot notify on generated video transcription %s of video %s.', caption.language, video.url, { err }))
  217. }
  218. private async notify <T> (object: AbstractNotification<T>) {
  219. await object.prepare()
  220. const users = object.getTargetUsers()
  221. if (users.length === 0) return
  222. if (await object.isDisabled()) return
  223. object.log()
  224. const toEmails: string[] = []
  225. for (const user of users) {
  226. const setting = object.getSetting(user)
  227. const webNotificationEnabled = this.isWebNotificationEnabled(setting)
  228. const emailNotificationEnabled = this.isEmailEnabled(user, setting)
  229. const notification = object.createNotification(user)
  230. if (webNotificationEnabled) {
  231. await notification.save()
  232. PeerTubeSocket.Instance.sendNotification(user.id, notification)
  233. }
  234. if (emailNotificationEnabled) {
  235. toEmails.push(user.email)
  236. }
  237. Hooks.runAction('action:notifier.notification.created', { webNotificationEnabled, emailNotificationEnabled, user, notification })
  238. }
  239. for (const to of toEmails) {
  240. const payload = await object.createEmail(to)
  241. JobQueue.Instance.createJobAsync({ type: 'email', payload })
  242. }
  243. }
  244. private isEmailEnabled (user: MUser, value: UserNotificationSettingValueType) {
  245. if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION === true && user.emailVerified === false) return false
  246. return value & UserNotificationSettingValue.EMAIL
  247. }
  248. private isWebNotificationEnabled (value: UserNotificationSettingValueType) {
  249. return value & UserNotificationSettingValue.WEB
  250. }
  251. private async sendNotifications <T> (models: (new (payload: T) => AbstractNotification<T>)[], payload: T) {
  252. for (const model of models) {
  253. // eslint-disable-next-line new-cap
  254. await this.notify(new model(payload))
  255. }
  256. }
  257. static get Instance () {
  258. return this.instance || (this.instance = new this())
  259. }
  260. }
  261. // ---------------------------------------------------------------------------
  262. export {
  263. Notifier
  264. }