2018-07-19 09:17:54 -05:00
|
|
|
import * as express from 'express'
|
2020-05-29 09:16:24 -05:00
|
|
|
import { sanitizeUrl } from '@server/helpers/core-utils'
|
2021-03-08 07:24:11 -06:00
|
|
|
import { doJSONRequest } from '@server/helpers/requests'
|
2020-05-29 09:16:24 -05:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2021-06-02 08:47:05 -05:00
|
|
|
import { getOrCreateAPVideo } from '@server/lib/activitypub/videos'
|
2021-03-24 03:16:48 -05:00
|
|
|
import { Hooks } from '@server/lib/plugins/hooks'
|
2020-05-29 09:16:24 -05:00
|
|
|
import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
|
|
|
|
import { getServerActor } from '@server/models/application/application'
|
|
|
|
import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
|
2020-12-07 07:32:36 -06:00
|
|
|
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
|
2020-05-29 09:16:24 -05:00
|
|
|
import { ResultList, Video, VideoChannel } from '@shared/models'
|
|
|
|
import { SearchTargetQuery } from '@shared/models/search/search-target-query.model'
|
|
|
|
import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
|
2018-08-24 08:36:50 -05:00
|
|
|
import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
|
2020-05-29 09:16:24 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
2020-04-23 02:32:53 -05:00
|
|
|
import { getFormattedObjects } from '../../helpers/utils'
|
2020-05-29 09:16:24 -05:00
|
|
|
import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
|
2021-06-03 09:02:29 -05:00
|
|
|
import { getOrCreateAPActor } from '../../lib/activitypub/actors'
|
2018-07-19 09:17:54 -05:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
2018-07-20 07:35:18 -05:00
|
|
|
commonVideosFiltersValidator,
|
2018-07-19 09:17:54 -05:00
|
|
|
optionalAuthenticate,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSearchSort,
|
2020-07-27 10:00:39 -05:00
|
|
|
videoChannelsListSearchValidator,
|
2021-03-24 03:16:48 -05:00
|
|
|
videoChannelsSearchSortValidator,
|
2018-08-23 10:58:39 -05:00
|
|
|
videosSearchSortValidator,
|
|
|
|
videosSearchValidator
|
2018-07-19 09:17:54 -05:00
|
|
|
} from '../../middlewares'
|
2020-05-29 09:16:24 -05:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
2018-08-23 10:58:39 -05:00
|
|
|
import { VideoChannelModel } from '../../models/video/video-channel'
|
2020-06-18 03:45:25 -05:00
|
|
|
import { MChannelAccountDefault, MVideoAccountLightBlacklistAllFiles } from '../../types/models'
|
2018-07-19 09:17:54 -05:00
|
|
|
|
|
|
|
const searchRouter = express.Router()
|
|
|
|
|
|
|
|
searchRouter.get('/videos',
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
videosSearchSortValidator,
|
|
|
|
setDefaultSearchSort,
|
|
|
|
optionalAuthenticate,
|
2018-07-20 07:35:18 -05:00
|
|
|
commonVideosFiltersValidator,
|
2018-08-23 10:58:39 -05:00
|
|
|
videosSearchValidator,
|
2018-07-19 09:17:54 -05:00
|
|
|
asyncMiddleware(searchVideos)
|
|
|
|
)
|
|
|
|
|
2018-08-23 10:58:39 -05:00
|
|
|
searchRouter.get('/video-channels',
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
videoChannelsSearchSortValidator,
|
|
|
|
setDefaultSearchSort,
|
|
|
|
optionalAuthenticate,
|
2020-07-27 10:00:39 -05:00
|
|
|
videoChannelsListSearchValidator,
|
2018-08-23 10:58:39 -05:00
|
|
|
asyncMiddleware(searchVideoChannels)
|
|
|
|
)
|
|
|
|
|
2018-07-19 09:17:54 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export { searchRouter }
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-08-23 10:58:39 -05:00
|
|
|
function searchVideoChannels (req: express.Request, res: express.Response) {
|
|
|
|
const query: VideoChannelsSearchQuery = req.query
|
|
|
|
const search = query.search
|
|
|
|
|
|
|
|
const isURISearch = search.startsWith('http://') || search.startsWith('https://')
|
|
|
|
|
|
|
|
const parts = search.split('@')
|
2018-09-17 03:12:30 -05:00
|
|
|
|
|
|
|
// Handle strings like @toto@example.com
|
|
|
|
if (parts.length === 3 && parts[0].length === 0) parts.shift()
|
2020-02-28 09:03:39 -06:00
|
|
|
const isWebfingerSearch = parts.length === 2 && parts.every(p => p && !p.includes(' '))
|
2018-08-23 10:58:39 -05:00
|
|
|
|
2018-08-24 04:04:02 -05:00
|
|
|
if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
|
2018-08-23 10:58:39 -05:00
|
|
|
|
2019-05-28 03:04:07 -05:00
|
|
|
// @username -> username to search in DB
|
|
|
|
if (query.search.startsWith('@')) query.search = query.search.replace(/^@/, '')
|
2020-05-29 09:16:24 -05:00
|
|
|
|
2020-06-10 03:58:44 -05:00
|
|
|
if (isSearchIndexSearch(query)) {
|
2020-05-29 09:16:24 -05:00
|
|
|
return searchVideoChannelsIndex(query, res)
|
|
|
|
}
|
|
|
|
|
2018-08-23 10:58:39 -05:00
|
|
|
return searchVideoChannelsDB(query, res)
|
|
|
|
}
|
|
|
|
|
2020-05-29 09:16:24 -05:00
|
|
|
async function searchVideoChannelsIndex (query: VideoChannelsSearchQuery, res: express.Response) {
|
|
|
|
const result = await buildMutedForSearchIndex(res)
|
|
|
|
|
2021-03-24 03:16:48 -05:00
|
|
|
const body = await Hooks.wrapObject(Object.assign(query, result), 'filter:api.search.video-channels.index.list.params')
|
2020-05-29 09:16:24 -05:00
|
|
|
|
|
|
|
const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-channels'
|
|
|
|
|
|
|
|
try {
|
2020-12-10 09:38:12 -06:00
|
|
|
logger.debug('Doing video channels search index request on %s.', url, { body })
|
|
|
|
|
2021-03-10 02:25:26 -06:00
|
|
|
const { body: searchIndexResult } = await doJSONRequest<ResultList<VideoChannel>>(url, { method: 'POST', json: body })
|
2021-03-24 03:16:48 -05:00
|
|
|
const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.video-channels.index.list.result')
|
2020-05-29 09:16:24 -05:00
|
|
|
|
2021-03-24 03:16:48 -05:00
|
|
|
return res.json(jsonResult)
|
2020-05-29 09:16:24 -05:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot use search index to make video channels search.', { err })
|
|
|
|
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
|
|
|
|
message: 'Cannot use search index to make video channels search'
|
|
|
|
})
|
2020-05-29 09:16:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 10:58:39 -05:00
|
|
|
async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
|
|
|
|
const serverActor = await getServerActor()
|
|
|
|
|
2021-03-24 03:16:48 -05:00
|
|
|
const apiOptions = await Hooks.wrapObject({
|
2018-08-23 10:58:39 -05:00
|
|
|
actorId: serverActor.id,
|
|
|
|
search: query.search,
|
|
|
|
start: query.start,
|
|
|
|
count: query.count,
|
|
|
|
sort: query.sort
|
2021-03-24 03:16:48 -05:00
|
|
|
}, 'filter:api.search.video-channels.local.list.params')
|
|
|
|
|
|
|
|
const resultList = await Hooks.wrapPromiseFun(
|
|
|
|
VideoChannelModel.searchForApi,
|
|
|
|
apiOptions,
|
|
|
|
'filter:api.search.video-channels.local.list.result'
|
|
|
|
)
|
2018-08-23 10:58:39 -05:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
2018-08-24 04:04:02 -05:00
|
|
|
async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
|
2019-08-15 04:53:26 -05:00
|
|
|
let videoChannel: MChannelAccountDefault
|
2018-08-24 04:04:02 -05:00
|
|
|
let uri = search
|
2018-08-23 10:58:39 -05:00
|
|
|
|
2019-05-28 03:04:07 -05:00
|
|
|
if (isWebfingerSearch) {
|
|
|
|
try {
|
|
|
|
uri = await loadActorUrlOrGetFromWebfinger(search)
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot load actor URL or get from webfinger.', { search, err })
|
|
|
|
|
|
|
|
return res.json({ total: 0, data: [] })
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 10:58:39 -05:00
|
|
|
|
2018-08-24 04:04:02 -05:00
|
|
|
if (isUserAbleToSearchRemoteURI(res)) {
|
|
|
|
try {
|
2021-06-03 09:02:29 -05:00
|
|
|
const actor = await getOrCreateAPActor(uri, 'all', true, true)
|
2018-08-24 04:04:02 -05:00
|
|
|
videoChannel = actor.VideoChannel
|
|
|
|
} catch (err) {
|
|
|
|
logger.info('Cannot search remote video channel %s.', uri, { err })
|
|
|
|
}
|
2018-08-23 10:58:39 -05:00
|
|
|
} else {
|
2018-08-24 04:04:02 -05:00
|
|
|
videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
|
2018-08-23 10:58:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
total: videoChannel ? 1 : 0,
|
|
|
|
data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-22 04:51:39 -05:00
|
|
|
function searchVideos (req: express.Request, res: express.Response) {
|
2018-07-20 07:35:18 -05:00
|
|
|
const query: VideosSearchQuery = req.query
|
2018-08-23 03:30:53 -05:00
|
|
|
const search = query.search
|
2020-05-29 09:16:24 -05:00
|
|
|
|
2018-08-23 03:30:53 -05:00
|
|
|
if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
|
2018-08-23 10:58:39 -05:00
|
|
|
return searchVideoURI(search, res)
|
2018-08-22 04:51:39 -05:00
|
|
|
}
|
2018-07-20 07:35:18 -05:00
|
|
|
|
2020-06-10 03:58:44 -05:00
|
|
|
if (isSearchIndexSearch(query)) {
|
2020-05-29 09:16:24 -05:00
|
|
|
return searchVideosIndex(query, res)
|
|
|
|
}
|
|
|
|
|
2018-08-22 04:51:39 -05:00
|
|
|
return searchVideosDB(query, res)
|
|
|
|
}
|
|
|
|
|
2020-05-29 09:16:24 -05:00
|
|
|
async function searchVideosIndex (query: VideosSearchQuery, res: express.Response) {
|
|
|
|
const result = await buildMutedForSearchIndex(res)
|
|
|
|
|
2021-03-24 03:16:48 -05:00
|
|
|
let body: VideosSearchQuery = Object.assign(query, result)
|
2020-06-12 09:24:58 -05:00
|
|
|
|
|
|
|
// Use the default instance NSFW policy if not specified
|
|
|
|
if (!body.nsfw) {
|
2020-06-15 07:40:35 -05:00
|
|
|
const nsfwPolicy = res.locals.oauth
|
|
|
|
? res.locals.oauth.token.User.nsfwPolicy
|
|
|
|
: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
|
|
|
|
|
|
|
|
body.nsfw = nsfwPolicy === 'do_not_list'
|
2020-06-12 09:24:58 -05:00
|
|
|
? 'false'
|
|
|
|
: 'both'
|
|
|
|
}
|
2020-05-29 09:16:24 -05:00
|
|
|
|
2021-03-24 03:16:48 -05:00
|
|
|
body = await Hooks.wrapObject(body, 'filter:api.search.videos.index.list.params')
|
|
|
|
|
2020-05-29 09:16:24 -05:00
|
|
|
const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/videos'
|
|
|
|
|
|
|
|
try {
|
2020-12-10 09:38:12 -06:00
|
|
|
logger.debug('Doing videos search index request on %s.', url, { body })
|
|
|
|
|
2021-03-10 02:25:26 -06:00
|
|
|
const { body: searchIndexResult } = await doJSONRequest<ResultList<Video>>(url, { method: 'POST', json: body })
|
2021-03-24 03:16:48 -05:00
|
|
|
const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.videos.index.list.result')
|
2020-05-29 09:16:24 -05:00
|
|
|
|
2021-03-24 03:16:48 -05:00
|
|
|
return res.json(jsonResult)
|
2020-05-29 09:16:24 -05:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot use search index to make video search.', { err })
|
|
|
|
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
|
|
|
|
message: 'Cannot use search index to make video search'
|
|
|
|
})
|
2020-05-29 09:16:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 04:51:39 -05:00
|
|
|
async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
|
2021-03-24 03:16:48 -05:00
|
|
|
const apiOptions = await Hooks.wrapObject(Object.assign(query, {
|
2018-08-16 08:25:20 -05:00
|
|
|
includeLocalVideos: true,
|
2018-10-05 04:15:06 -05:00
|
|
|
nsfw: buildNSFWFilter(res, query.nsfw),
|
2018-10-10 04:46:50 -05:00
|
|
|
filter: query.filter,
|
2018-10-12 08:26:04 -05:00
|
|
|
user: res.locals.oauth ? res.locals.oauth.token.User : undefined
|
2021-03-24 03:16:48 -05:00
|
|
|
}), 'filter:api.search.videos.local.list.params')
|
|
|
|
|
|
|
|
const resultList = await Hooks.wrapPromiseFun(
|
|
|
|
VideoModel.searchAndPopulateAccountAndServer,
|
|
|
|
apiOptions,
|
|
|
|
'filter:api.search.videos.local.list.result'
|
|
|
|
)
|
2018-07-19 09:17:54 -05:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
2018-08-22 04:51:39 -05:00
|
|
|
|
2018-08-23 10:58:39 -05:00
|
|
|
async function searchVideoURI (url: string, res: express.Response) {
|
2019-08-20 06:52:49 -05:00
|
|
|
let video: MVideoAccountLightBlacklistAllFiles
|
2018-08-22 04:51:39 -05:00
|
|
|
|
2018-08-22 09:15:35 -05:00
|
|
|
// Check if we can fetch a remote video with the URL
|
2018-08-23 10:58:39 -05:00
|
|
|
if (isUserAbleToSearchRemoteURI(res)) {
|
2018-08-22 09:15:35 -05:00
|
|
|
try {
|
|
|
|
const syncParam = {
|
|
|
|
likes: false,
|
|
|
|
dislikes: false,
|
|
|
|
shares: false,
|
|
|
|
comments: false,
|
|
|
|
thumbnail: true,
|
|
|
|
refreshVideo: false
|
|
|
|
}
|
2018-08-22 04:51:39 -05:00
|
|
|
|
2021-06-02 08:47:05 -05:00
|
|
|
const result = await getOrCreateAPVideo({ videoObject: url, syncParam })
|
2018-08-23 10:58:39 -05:00
|
|
|
video = result ? result.video : undefined
|
2018-08-22 09:15:35 -05:00
|
|
|
} catch (err) {
|
2018-08-24 04:04:02 -05:00
|
|
|
logger.info('Cannot search remote video %s.', url, { err })
|
2018-08-22 09:15:35 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
video = await VideoModel.loadByUrlAndPopulateAccount(url)
|
2018-08-22 04:51:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
total: video ? 1 : 0,
|
|
|
|
data: video ? [ video.toFormattedJSON() ] : []
|
|
|
|
})
|
|
|
|
}
|
2020-05-29 09:16:24 -05:00
|
|
|
|
2020-06-10 03:58:44 -05:00
|
|
|
function isSearchIndexSearch (query: SearchTargetQuery) {
|
2020-05-29 09:16:24 -05:00
|
|
|
if (query.searchTarget === 'search-index') return true
|
|
|
|
|
|
|
|
const searchIndexConfig = CONFIG.SEARCH.SEARCH_INDEX
|
|
|
|
|
|
|
|
if (searchIndexConfig.ENABLED !== true) return false
|
|
|
|
|
|
|
|
if (searchIndexConfig.DISABLE_LOCAL_SEARCH) return true
|
|
|
|
if (searchIndexConfig.IS_DEFAULT_SEARCH && !query.searchTarget) return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildMutedForSearchIndex (res: express.Response) {
|
|
|
|
const serverActor = await getServerActor()
|
|
|
|
const accountIds = [ serverActor.Account.id ]
|
|
|
|
|
|
|
|
if (res.locals.oauth) {
|
|
|
|
accountIds.push(res.locals.oauth.token.User.Account.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
const [ blockedHosts, blockedAccounts ] = await Promise.all([
|
|
|
|
ServerBlocklistModel.listHostsBlockedBy(accountIds),
|
|
|
|
AccountBlocklistModel.listHandlesBlockedBy(accountIds)
|
|
|
|
])
|
|
|
|
|
|
|
|
return {
|
|
|
|
blockedHosts,
|
|
|
|
blockedAccounts
|
|
|
|
}
|
|
|
|
}
|