plugin-helpers.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { PeerTubeHelpers } from '@server/types/plugins'
  2. import { sequelizeTypescript } from '@server/initializers/database'
  3. import { buildLogger } from '@server/helpers/logger'
  4. import { VideoModel } from '@server/models/video/video'
  5. import { WEBSERVER } from '@server/initializers/constants'
  6. import { ServerModel } from '@server/models/server/server'
  7. import { getServerActor } from '@server/models/application/application'
  8. import { addServerInBlocklist, removeServerFromBlocklist, addAccountInBlocklist, removeAccountFromBlocklist } from '../blocklist'
  9. import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
  10. import { AccountModel } from '@server/models/account/account'
  11. import { VideoBlacklistCreate } from '@shared/models'
  12. import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
  13. import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
  14. import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
  15. function buildPluginHelpers (npmName: string): PeerTubeHelpers {
  16. const logger = buildPluginLogger(npmName)
  17. const database = buildDatabaseHelpers()
  18. const videos = buildVideosHelpers()
  19. const config = buildConfigHelpers()
  20. const server = buildServerHelpers()
  21. const moderation = buildModerationHelpers()
  22. return {
  23. logger,
  24. database,
  25. videos,
  26. config,
  27. moderation,
  28. server
  29. }
  30. }
  31. export {
  32. buildPluginHelpers
  33. }
  34. // ---------------------------------------------------------------------------
  35. function buildPluginLogger (npmName: string) {
  36. return buildLogger(npmName)
  37. }
  38. function buildDatabaseHelpers () {
  39. return {
  40. query: sequelizeTypescript.query.bind(sequelizeTypescript)
  41. }
  42. }
  43. function buildServerHelpers () {
  44. return {
  45. getServerActor: () => getServerActor()
  46. }
  47. }
  48. function buildVideosHelpers () {
  49. return {
  50. loadByUrl: (url: string) => {
  51. return VideoModel.loadByUrl(url)
  52. },
  53. removeVideo: (id: number) => {
  54. return sequelizeTypescript.transaction(async t => {
  55. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t)
  56. await video.destroy({ transaction: t })
  57. })
  58. }
  59. }
  60. }
  61. function buildModerationHelpers () {
  62. return {
  63. blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
  64. const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
  65. await addServerInBlocklist(options.byAccountId, serverToBlock.id)
  66. },
  67. unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
  68. const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
  69. if (!serverBlock) return
  70. await removeServerFromBlocklist(serverBlock)
  71. },
  72. blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
  73. const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
  74. if (!accountToBlock) return
  75. await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
  76. },
  77. unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
  78. const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
  79. if (!targetAccount) return
  80. const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
  81. if (!accountBlock) return
  82. await removeAccountFromBlocklist(accountBlock)
  83. },
  84. blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
  85. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
  86. if (!video) return
  87. await blacklistVideo(video, options.createOptions)
  88. },
  89. unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
  90. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
  91. if (!video) return
  92. const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
  93. if (!videoBlacklist) return
  94. await unblacklistVideo(videoBlacklist, video)
  95. }
  96. }
  97. }
  98. function buildConfigHelpers () {
  99. return {
  100. getWebserverUrl () {
  101. return WEBSERVER.URL
  102. }
  103. }
  104. }