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

209 lines
5.8 KiB

  1. async function register ({
  2. peertubeHelpers,
  3. registerHook,
  4. getRouter
  5. }) {
  6. const logger = peertubeHelpers.logger
  7. logger.info('Hello world from plugin four')
  8. {
  9. const username = 'root'
  10. const results = await peertubeHelpers.database.query(
  11. 'SELECT "email" from "user" WHERE "username" = $username',
  12. {
  13. type: 'SELECT',
  14. bind: { username }
  15. }
  16. )
  17. logger.info('root email is ' + results[0]['email'])
  18. }
  19. {
  20. registerHook({
  21. target: 'action:api.video.viewed',
  22. handler: async ({ video }) => {
  23. const videoFromDB1 = await peertubeHelpers.videos.loadByUrl(video.url)
  24. const videoFromDB2 = await peertubeHelpers.videos.loadByIdOrUUID(video.id)
  25. const videoFromDB3 = await peertubeHelpers.videos.loadByIdOrUUID(video.uuid)
  26. if (videoFromDB1.uuid !== videoFromDB2.uuid || videoFromDB2.uuid !== videoFromDB3.uuid) return
  27. const videoWithFiles = await peertubeHelpers.videos.loadByIdOrUUIDWithFiles(video.id)
  28. if (videoWithFiles.getHLSPlaylist().getMasterPlaylistUrl(videoWithFiles) === null) {
  29. logger.error('Video with files could not be loaded.')
  30. return
  31. }
  32. logger.info('video from DB uuid is %s.', videoFromDB1.uuid)
  33. await peertubeHelpers.videos.removeVideo(video.id)
  34. logger.info('Video deleted by plugin four.')
  35. }
  36. })
  37. }
  38. {
  39. const serverActor = await peertubeHelpers.server.getServerActor()
  40. logger.info('server actor name is %s', serverActor.preferredUsername)
  41. }
  42. {
  43. logger.info('server url is %s', peertubeHelpers.config.getWebserverUrl())
  44. }
  45. {
  46. const actions = {
  47. blockServer,
  48. unblockServer,
  49. blockAccount,
  50. unblockAccount,
  51. blacklist,
  52. unblacklist
  53. }
  54. const router = getRouter()
  55. router.post('/commander', async (req, res) => {
  56. try {
  57. await actions[req.body.command](peertubeHelpers, req.body)
  58. res.sendStatus(204)
  59. } catch (err) {
  60. logger.error('Error in commander.', { err })
  61. res.sendStatus(500)
  62. }
  63. })
  64. router.get('/server-config', async (req, res) => {
  65. const serverConfig = await peertubeHelpers.config.getServerConfig()
  66. return res.json({ serverConfig })
  67. })
  68. router.get('/server-listening-config', async (req, res) => {
  69. const config = await peertubeHelpers.config.getServerListeningConfig()
  70. return res.json({ config })
  71. })
  72. router.get('/static-route', async (req, res) => {
  73. const staticRoute = peertubeHelpers.plugin.getBaseStaticRoute()
  74. return res.json({ staticRoute })
  75. })
  76. router.get('/router-route', async (req, res) => {
  77. const routerRoute = peertubeHelpers.plugin.getBaseRouterRoute()
  78. return res.json({ routerRoute })
  79. })
  80. router.get('/user/:id', async (req, res) => {
  81. const user = await peertubeHelpers.user.loadById(req.params.id)
  82. if (!user) return res.status(404).end()
  83. return res.json({
  84. username: user.username
  85. })
  86. })
  87. router.get('/user', async (req, res) => {
  88. const user = await peertubeHelpers.user.getAuthUser(res)
  89. if (!user) return res.sendStatus(404)
  90. const isAdmin = user.role === 0
  91. const isModerator = user.role === 1
  92. const isUser = user.role === 2
  93. return res.json({
  94. id: user.id,
  95. username: user.username,
  96. displayName: user.Account.name,
  97. isAdmin,
  98. isModerator,
  99. isUser
  100. })
  101. })
  102. router.get('/video-files/:id', async (req, res) => {
  103. const details = await peertubeHelpers.videos.getFiles(req.params.id)
  104. if (!details) return res.sendStatus(404)
  105. return res.json(details)
  106. })
  107. router.get('/ffprobe', async (req, res) => {
  108. const result = await peertubeHelpers.videos.ffprobe(req.query.path)
  109. if (!result) return res.sendStatus(404)
  110. return res.json(result)
  111. })
  112. router.post('/send-notification', async (req, res) => {
  113. peertubeHelpers.socket.sendNotification(req.body.userId, {
  114. type: 1,
  115. userId: req.body.userId
  116. })
  117. return res.sendStatus(201)
  118. })
  119. router.post('/send-video-live-new-state/:uuid', async (req, res) => {
  120. const video = await peertubeHelpers.videos.loadByIdOrUUID(req.params.uuid)
  121. peertubeHelpers.socket.sendVideoLiveNewState(video)
  122. return res.sendStatus(201)
  123. })
  124. }
  125. }
  126. async function unregister () {
  127. return
  128. }
  129. module.exports = {
  130. register,
  131. unregister
  132. }
  133. // ###########################################################################
  134. async function blockServer (peertubeHelpers, body) {
  135. const serverActor = await peertubeHelpers.server.getServerActor()
  136. await peertubeHelpers.moderation.blockServer({ byAccountId: serverActor.Account.id, hostToBlock: body.hostToBlock })
  137. }
  138. async function unblockServer (peertubeHelpers, body) {
  139. const serverActor = await peertubeHelpers.server.getServerActor()
  140. await peertubeHelpers.moderation.unblockServer({ byAccountId: serverActor.Account.id, hostToUnblock: body.hostToUnblock })
  141. }
  142. async function blockAccount (peertubeHelpers, body) {
  143. const serverActor = await peertubeHelpers.server.getServerActor()
  144. await peertubeHelpers.moderation.blockAccount({ byAccountId: serverActor.Account.id, handleToBlock: body.handleToBlock })
  145. }
  146. async function unblockAccount (peertubeHelpers, body) {
  147. const serverActor = await peertubeHelpers.server.getServerActor()
  148. await peertubeHelpers.moderation.unblockAccount({ byAccountId: serverActor.Account.id, handleToUnblock: body.handleToUnblock })
  149. }
  150. async function blacklist (peertubeHelpers, body) {
  151. await peertubeHelpers.moderation.blacklistVideo({
  152. videoIdOrUUID: body.videoUUID,
  153. createOptions: body
  154. })
  155. }
  156. async function unblacklist (peertubeHelpers, body) {
  157. await peertubeHelpers.moderation.unblacklistVideo({ videoIdOrUUID: body.videoUUID })
  158. }