search.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import express from 'express'
  2. import { CONFIG } from '@server/initializers/config.js'
  3. import { AccountBlocklistModel } from '@server/models/account/account-blocklist.js'
  4. import { getServerActor } from '@server/models/application/application.js'
  5. import { ServerBlocklistModel } from '@server/models/server/server-blocklist.js'
  6. import { SearchTargetQuery } from '@peertube/peertube-models'
  7. function isSearchIndexSearch (query: SearchTargetQuery) {
  8. if (query.searchTarget === 'search-index') return true
  9. const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
  10. if (searchIndexConfig.ENABLED !== true) return false
  11. if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true
  12. return false
  13. }
  14. async function buildMutedForSearchIndex (res: express.Response) {
  15. const serverActor = await getServerActor()
  16. const accountIds = [ serverActor.Account.id ]
  17. if (res.locals.oauth) {
  18. accountIds.push(res.locals.oauth.token.User.Account.id)
  19. }
  20. const [ blockedHosts, blockedAccounts ] = await Promise.all([
  21. ServerBlocklistModel.listHostsBlockedBy(accountIds),
  22. AccountBlocklistModel.listHandlesBlockedBy(accountIds)
  23. ])
  24. return {
  25. blockedHosts,
  26. blockedAccounts
  27. }
  28. }
  29. function isURISearch (search: string) {
  30. if (!search) return false
  31. return search.startsWith('http://') || search.startsWith('https://')
  32. }
  33. export {
  34. isSearchIndexSearch,
  35. buildMutedForSearchIndex,
  36. isURISearch
  37. }