plugins.ts 6.3 KB

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