accounts.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import * as express from 'express'
  2. import { getFormattedObjects, getServerActor } from '../../helpers/utils'
  3. import {
  4. asyncMiddleware,
  5. authenticate,
  6. commonVideosFiltersValidator,
  7. optionalAuthenticate,
  8. paginationValidator,
  9. setDefaultPagination,
  10. setDefaultSort,
  11. videoPlaylistsSortValidator,
  12. videoRatesSortValidator,
  13. videoRatingValidator
  14. } from '../../middlewares'
  15. import {
  16. accountNameWithHostGetValidator,
  17. accountsSortValidator,
  18. ensureAuthUserOwnsAccountValidator,
  19. videosSortValidator,
  20. videoChannelsSortValidator
  21. } from '../../middlewares/validators'
  22. import { AccountModel } from '../../models/account/account'
  23. import { AccountVideoRateModel } from '../../models/account/account-video-rate'
  24. import { VideoModel } from '../../models/video/video'
  25. import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
  26. import { VideoChannelModel } from '../../models/video/video-channel'
  27. import { JobQueue } from '../../lib/job-queue'
  28. import { logger } from '../../helpers/logger'
  29. import { VideoPlaylistModel } from '../../models/video/video-playlist'
  30. import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
  31. const accountsRouter = express.Router()
  32. accountsRouter.get('/',
  33. paginationValidator,
  34. accountsSortValidator,
  35. setDefaultSort,
  36. setDefaultPagination,
  37. asyncMiddleware(listAccounts)
  38. )
  39. accountsRouter.get('/:accountName',
  40. asyncMiddleware(accountNameWithHostGetValidator),
  41. getAccount
  42. )
  43. accountsRouter.get('/:accountName/videos',
  44. asyncMiddleware(accountNameWithHostGetValidator),
  45. paginationValidator,
  46. videosSortValidator,
  47. setDefaultSort,
  48. setDefaultPagination,
  49. optionalAuthenticate,
  50. commonVideosFiltersValidator,
  51. asyncMiddleware(listAccountVideos)
  52. )
  53. accountsRouter.get('/:accountName/video-channels',
  54. asyncMiddleware(accountNameWithHostGetValidator),
  55. paginationValidator,
  56. videoChannelsSortValidator,
  57. setDefaultSort,
  58. setDefaultPagination,
  59. asyncMiddleware(listAccountChannels)
  60. )
  61. accountsRouter.get('/:accountName/video-playlists',
  62. optionalAuthenticate,
  63. asyncMiddleware(accountNameWithHostGetValidator),
  64. paginationValidator,
  65. videoPlaylistsSortValidator,
  66. setDefaultSort,
  67. setDefaultPagination,
  68. commonVideoPlaylistFiltersValidator,
  69. asyncMiddleware(listAccountPlaylists)
  70. )
  71. accountsRouter.get('/:accountName/ratings',
  72. authenticate,
  73. asyncMiddleware(accountNameWithHostGetValidator),
  74. ensureAuthUserOwnsAccountValidator,
  75. paginationValidator,
  76. videoRatesSortValidator,
  77. setDefaultSort,
  78. setDefaultPagination,
  79. videoRatingValidator,
  80. asyncMiddleware(listAccountRatings)
  81. )
  82. // ---------------------------------------------------------------------------
  83. export {
  84. accountsRouter
  85. }
  86. // ---------------------------------------------------------------------------
  87. function getAccount (req: express.Request, res: express.Response) {
  88. const account = res.locals.account
  89. if (account.isOutdated()) {
  90. JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
  91. .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
  92. }
  93. return res.json(account.toFormattedJSON())
  94. }
  95. async function listAccounts (req: express.Request, res: express.Response) {
  96. const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
  97. return res.json(getFormattedObjects(resultList.data, resultList.total))
  98. }
  99. async function listAccountChannels (req: express.Request, res: express.Response) {
  100. const options = {
  101. accountId: res.locals.account.id,
  102. start: req.query.start,
  103. count: req.query.count,
  104. sort: req.query.sort
  105. }
  106. const resultList = await VideoChannelModel.listByAccount(options)
  107. return res.json(getFormattedObjects(resultList.data, resultList.total))
  108. }
  109. async function listAccountPlaylists (req: express.Request, res: express.Response) {
  110. const serverActor = await getServerActor()
  111. // Allow users to see their private/unlisted video playlists
  112. let privateAndUnlisted = false
  113. if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
  114. privateAndUnlisted = true
  115. }
  116. const resultList = await VideoPlaylistModel.listForApi({
  117. followerActorId: serverActor.id,
  118. start: req.query.start,
  119. count: req.query.count,
  120. sort: req.query.sort,
  121. accountId: res.locals.account.id,
  122. privateAndUnlisted,
  123. type: req.query.playlistType
  124. })
  125. return res.json(getFormattedObjects(resultList.data, resultList.total))
  126. }
  127. async function listAccountVideos (req: express.Request, res: express.Response) {
  128. const account = res.locals.account
  129. const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
  130. const resultList = await VideoModel.listForApi({
  131. followerActorId,
  132. start: req.query.start,
  133. count: req.query.count,
  134. sort: req.query.sort,
  135. includeLocalVideos: true,
  136. categoryOneOf: req.query.categoryOneOf,
  137. licenceOneOf: req.query.licenceOneOf,
  138. languageOneOf: req.query.languageOneOf,
  139. tagsOneOf: req.query.tagsOneOf,
  140. tagsAllOf: req.query.tagsAllOf,
  141. filter: req.query.filter,
  142. nsfw: buildNSFWFilter(res, req.query.nsfw),
  143. withFiles: false,
  144. accountId: account.id,
  145. user: res.locals.oauth ? res.locals.oauth.token.User : undefined
  146. })
  147. return res.json(getFormattedObjects(resultList.data, resultList.total))
  148. }
  149. async function listAccountRatings (req: express.Request, res: express.Response) {
  150. const account = res.locals.account
  151. const resultList = await AccountVideoRateModel.listByAccountForApi({
  152. accountId: account.id,
  153. start: req.query.start,
  154. count: req.query.count,
  155. sort: req.query.sort,
  156. type: req.query.rating
  157. })
  158. return res.json(getFormattedObjects(resultList.rows, resultList.count))
  159. }