search-videos.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import express from 'express'
  2. import { sanitizeUrl } from '@server/helpers/core-utils.js'
  3. import { pickSearchVideoQuery } from '@server/helpers/query.js'
  4. import { doJSONRequest } from '@server/helpers/requests.js'
  5. import { CONFIG } from '@server/initializers/config.js'
  6. import { WEBSERVER } from '@server/initializers/constants.js'
  7. import { findLatestAPRedirection } from '@server/lib/activitypub/activity.js'
  8. import { getOrCreateAPVideo } from '@server/lib/activitypub/videos/index.js'
  9. import { Hooks } from '@server/lib/plugins/hooks.js'
  10. import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search.js'
  11. import { getServerActor } from '@server/models/application/application.js'
  12. import { HttpStatusCode, ResultList, Video, VideosSearchQueryAfterSanitize } from '@peertube/peertube-models'
  13. import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../../helpers/express-utils.js'
  14. import { logger } from '../../../helpers/logger.js'
  15. import { getFormattedObjects } from '../../../helpers/utils.js'
  16. import {
  17. asyncMiddleware,
  18. commonVideosFiltersValidator,
  19. openapiOperationDoc,
  20. optionalAuthenticate,
  21. paginationValidator,
  22. setDefaultPagination,
  23. setDefaultSearchSort,
  24. videosSearchSortValidator,
  25. videosSearchValidator
  26. } from '../../../middlewares/index.js'
  27. import { guessAdditionalAttributesFromQuery } from '../../../models/video/formatter/index.js'
  28. import { VideoModel } from '../../../models/video/video.js'
  29. import { MVideoAccountLightBlacklistAllFiles } from '../../../types/models/index.js'
  30. import { searchLocalUrl } from './shared/index.js'
  31. const searchVideosRouter = express.Router()
  32. searchVideosRouter.get('/videos',
  33. openapiOperationDoc({ operationId: 'searchVideos' }),
  34. paginationValidator,
  35. setDefaultPagination,
  36. videosSearchSortValidator,
  37. setDefaultSearchSort,
  38. optionalAuthenticate,
  39. commonVideosFiltersValidator,
  40. videosSearchValidator,
  41. asyncMiddleware(searchVideos)
  42. )
  43. // ---------------------------------------------------------------------------
  44. export { searchVideosRouter }
  45. // ---------------------------------------------------------------------------
  46. function searchVideos (req: express.Request, res: express.Response) {
  47. const query = pickSearchVideoQuery(req.query)
  48. const search = query.search
  49. if (isURISearch(search)) {
  50. return searchVideoURI(search, res)
  51. }
  52. if (isSearchIndexSearch(query)) {
  53. return searchVideosIndex(query, res)
  54. }
  55. return searchVideosDB(query, res)
  56. }
  57. async function searchVideosIndex (query: VideosSearchQueryAfterSanitize, res: express.Response) {
  58. const result = await buildMutedForSearchIndex(res)
  59. let body = { ...query, ...result }
  60. // Use the default instance NSFW policy if not specified
  61. if (!body.nsfw) {
  62. const nsfwPolicy = res.locals.oauth
  63. ? res.locals.oauth.token.User.nsfwPolicy
  64. : CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
  65. body.nsfw = nsfwPolicy === 'do_not_list'
  66. ? 'false'
  67. : 'both'
  68. }
  69. body = await Hooks.wrapObject(body, 'filter:api.search.videos.index.list.params')
  70. const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
  71. try {
  72. logger.debug('Doing videos search index request on %s.', url, { body })
  73. const { body: searchIndexResult } = await doJSONRequest<ResultList<Video>>(url, { method: 'POST', json: body })
  74. const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.videos.index.list.result')
  75. return res.json(jsonResult)
  76. } catch (err) {
  77. logger.warn('Cannot use search index to make video search.', { err })
  78. return res.fail({
  79. status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
  80. message: 'Cannot use search index to make video search'
  81. })
  82. }
  83. }
  84. async function searchVideosDB (query: VideosSearchQueryAfterSanitize, res: express.Response) {
  85. const serverActor = await getServerActor()
  86. const apiOptions = await Hooks.wrapObject({
  87. ...query,
  88. displayOnlyForFollower: {
  89. actorId: serverActor.id,
  90. orLocalVideos: true
  91. },
  92. nsfw: buildNSFWFilter(res, query.nsfw),
  93. user: res.locals.oauth
  94. ? res.locals.oauth.token.User
  95. : undefined
  96. }, 'filter:api.search.videos.local.list.params')
  97. const resultList = await Hooks.wrapPromiseFun(
  98. VideoModel.searchAndPopulateAccountAndServer,
  99. apiOptions,
  100. 'filter:api.search.videos.local.list.result'
  101. )
  102. return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
  103. }
  104. async function searchVideoURI (url: string, res: express.Response) {
  105. let video: MVideoAccountLightBlacklistAllFiles
  106. // Check if we can fetch a remote video with the URL
  107. if (isUserAbleToSearchRemoteURI(res)) {
  108. try {
  109. const syncParam = {
  110. rates: false,
  111. shares: false,
  112. comments: false,
  113. refreshVideo: false
  114. }
  115. const result = await getOrCreateAPVideo({
  116. videoObject: await findLatestAPRedirection(url),
  117. syncParam
  118. })
  119. video = result ? result.video : undefined
  120. } catch (err) {
  121. logger.info('Cannot search remote video %s.', url, { err })
  122. }
  123. } else {
  124. video = await searchLocalUrl(sanitizeLocalUrl(url), url => VideoModel.loadByUrlAndPopulateAccount(url))
  125. }
  126. return res.json({
  127. total: video ? 1 : 0,
  128. data: video ? [ video.toFormattedJSON() ] : []
  129. })
  130. }
  131. function sanitizeLocalUrl (url: string) {
  132. if (!url) return ''
  133. // Handle alternative video URLs
  134. return url.replace(new RegExp('^' + WEBSERVER.URL + '/w/'), WEBSERVER.URL + '/videos/watch/')
  135. }