ニジカ投稿局 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.
 
 
 
 
 

231 lines
6.4 KiB

  1. import express from 'express'
  2. import { logger } from '@server/helpers/logger.js'
  3. import { getFormattedObjects } from '@server/helpers/utils.js'
  4. import { listAvailablePluginsFromIndex } from '@server/lib/plugins/plugin-index.js'
  5. import { PluginManager } from '@server/lib/plugins/plugin-manager.js'
  6. import {
  7. apiRateLimiter,
  8. asyncMiddleware,
  9. authenticate,
  10. availablePluginsSortValidator,
  11. ensureUserHasRight,
  12. openapiOperationDoc,
  13. paginationValidator,
  14. pluginsSortValidator,
  15. setDefaultPagination,
  16. setDefaultSort
  17. } from '@server/middlewares/index.js'
  18. import {
  19. existingPluginValidator,
  20. installOrUpdatePluginValidator,
  21. listAvailablePluginsValidator,
  22. listPluginsValidator,
  23. uninstallPluginValidator,
  24. updatePluginSettingsValidator
  25. } from '@server/middlewares/validators/plugins.js'
  26. import { PluginModel } from '@server/models/server/plugin.js'
  27. import {
  28. HttpStatusCode,
  29. InstallOrUpdatePlugin,
  30. ManagePlugin,
  31. PeertubePluginIndexList,
  32. PublicServerSetting,
  33. RegisteredServerSettings,
  34. UserRight
  35. } from '@peertube/peertube-models'
  36. const pluginRouter = express.Router()
  37. pluginRouter.use(apiRateLimiter)
  38. pluginRouter.get('/available',
  39. openapiOperationDoc({ operationId: 'getAvailablePlugins' }),
  40. authenticate,
  41. ensureUserHasRight(UserRight.MANAGE_PLUGINS),
  42. listAvailablePluginsValidator,
  43. paginationValidator,
  44. availablePluginsSortValidator,
  45. setDefaultSort,
  46. setDefaultPagination,
  47. asyncMiddleware(listAvailablePlugins)
  48. )
  49. pluginRouter.get('/',
  50. openapiOperationDoc({ operationId: 'getPlugins' }),
  51. authenticate,
  52. ensureUserHasRight(UserRight.MANAGE_PLUGINS),
  53. listPluginsValidator,
  54. paginationValidator,
  55. pluginsSortValidator,
  56. setDefaultSort,
  57. setDefaultPagination,
  58. asyncMiddleware(listPlugins)
  59. )
  60. pluginRouter.get('/:npmName/registered-settings',
  61. authenticate,
  62. ensureUserHasRight(UserRight.MANAGE_PLUGINS),
  63. asyncMiddleware(existingPluginValidator),
  64. getPluginRegisteredSettings
  65. )
  66. pluginRouter.get('/:npmName/public-settings',
  67. asyncMiddleware(existingPluginValidator),
  68. getPublicPluginSettings
  69. )
  70. pluginRouter.put('/:npmName/settings',
  71. authenticate,
  72. ensureUserHasRight(UserRight.MANAGE_PLUGINS),
  73. updatePluginSettingsValidator,
  74. asyncMiddleware(existingPluginValidator),
  75. asyncMiddleware(updatePluginSettings)
  76. )
  77. pluginRouter.get('/:npmName',
  78. authenticate,
  79. ensureUserHasRight(UserRight.MANAGE_PLUGINS),
  80. asyncMiddleware(existingPluginValidator),
  81. getPlugin
  82. )
  83. pluginRouter.post('/install',
  84. openapiOperationDoc({ operationId: 'addPlugin' }),
  85. authenticate,
  86. ensureUserHasRight(UserRight.MANAGE_PLUGINS),
  87. installOrUpdatePluginValidator,
  88. asyncMiddleware(installPlugin)
  89. )
  90. pluginRouter.post('/update',
  91. openapiOperationDoc({ operationId: 'updatePlugin' }),
  92. authenticate,
  93. ensureUserHasRight(UserRight.MANAGE_PLUGINS),
  94. installOrUpdatePluginValidator,
  95. asyncMiddleware(updatePlugin)
  96. )
  97. pluginRouter.post('/uninstall',
  98. openapiOperationDoc({ operationId: 'uninstallPlugin' }),
  99. authenticate,
  100. ensureUserHasRight(UserRight.MANAGE_PLUGINS),
  101. uninstallPluginValidator,
  102. asyncMiddleware(uninstallPlugin)
  103. )
  104. // ---------------------------------------------------------------------------
  105. export {
  106. pluginRouter
  107. }
  108. // ---------------------------------------------------------------------------
  109. async function listPlugins (req: express.Request, res: express.Response) {
  110. const pluginType = req.query.pluginType
  111. const uninstalled = req.query.uninstalled
  112. const resultList = await PluginModel.listForApi({
  113. pluginType,
  114. uninstalled,
  115. start: req.query.start,
  116. count: req.query.count,
  117. sort: req.query.sort
  118. })
  119. return res.json(getFormattedObjects(resultList.data, resultList.total))
  120. }
  121. function getPlugin (req: express.Request, res: express.Response) {
  122. const plugin = res.locals.plugin
  123. return res.json(plugin.toFormattedJSON())
  124. }
  125. async function installPlugin (req: express.Request, res: express.Response) {
  126. const body: InstallOrUpdatePlugin = req.body
  127. const fromDisk = !!body.path
  128. const toInstall = body.npmName || body.path
  129. const pluginVersion = body.pluginVersion && body.npmName
  130. ? body.pluginVersion
  131. : undefined
  132. try {
  133. const plugin = await PluginManager.Instance.install({ toInstall, version: pluginVersion, fromDisk })
  134. return res.json(plugin.toFormattedJSON())
  135. } catch (err) {
  136. logger.warn('Cannot install plugin %s.', toInstall, { err })
  137. return res.fail({ message: 'Cannot install plugin ' + toInstall })
  138. }
  139. }
  140. async function updatePlugin (req: express.Request, res: express.Response) {
  141. const body: InstallOrUpdatePlugin = req.body
  142. const fromDisk = !!body.path
  143. const toUpdate = body.npmName || body.path
  144. try {
  145. const plugin = await PluginManager.Instance.update(toUpdate, fromDisk)
  146. return res.json(plugin.toFormattedJSON())
  147. } catch (err) {
  148. logger.warn('Cannot update plugin %s.', toUpdate, { err })
  149. return res.fail({ message: 'Cannot update plugin ' + toUpdate })
  150. }
  151. }
  152. async function uninstallPlugin (req: express.Request, res: express.Response) {
  153. const body: ManagePlugin = req.body
  154. await PluginManager.Instance.uninstall({ npmName: body.npmName })
  155. return res.status(HttpStatusCode.NO_CONTENT_204).end()
  156. }
  157. function getPublicPluginSettings (req: express.Request, res: express.Response) {
  158. const plugin = res.locals.plugin
  159. const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
  160. const publicSettings = plugin.getPublicSettings(registeredSettings)
  161. const json: PublicServerSetting = { publicSettings }
  162. return res.json(json)
  163. }
  164. function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
  165. const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
  166. const json: RegisteredServerSettings = { registeredSettings }
  167. return res.json(json)
  168. }
  169. async function updatePluginSettings (req: express.Request, res: express.Response) {
  170. const plugin = res.locals.plugin
  171. plugin.settings = req.body.settings
  172. await plugin.save()
  173. await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
  174. return res.status(HttpStatusCode.NO_CONTENT_204).end()
  175. }
  176. async function listAvailablePlugins (req: express.Request, res: express.Response) {
  177. const query: PeertubePluginIndexList = req.query
  178. const resultList = await listAvailablePluginsFromIndex(query)
  179. if (!resultList) {
  180. return res.fail({
  181. status: HttpStatusCode.SERVICE_UNAVAILABLE_503,
  182. message: 'Plugin index unavailable. Please retry later'
  183. })
  184. }
  185. return res.json(resultList)
  186. }