2018-07-19 10:17:54 -04:00
|
|
|
import * as express from 'express'
|
2018-08-24 09:36:50 -04:00
|
|
|
import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
|
2018-08-23 11:58:39 -04:00
|
|
|
import { getFormattedObjects, getServerActor } from '../../helpers/utils'
|
2018-07-19 10:17:54 -04:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
2018-07-20 08:35:18 -04:00
|
|
|
commonVideosFiltersValidator,
|
2018-07-19 10:17:54 -04:00
|
|
|
optionalAuthenticate,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSearchSort,
|
2018-08-23 11:58:39 -04:00
|
|
|
videoChannelsSearchSortValidator,
|
|
|
|
videoChannelsSearchValidator,
|
|
|
|
videosSearchSortValidator,
|
|
|
|
videosSearchValidator
|
2018-07-19 10:17:54 -04:00
|
|
|
} from '../../middlewares'
|
2018-08-23 11:58:39 -04:00
|
|
|
import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
|
|
|
|
import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
|
2018-08-22 05:51:39 -04:00
|
|
|
import { logger } from '../../helpers/logger'
|
2018-08-23 11:58:39 -04:00
|
|
|
import { VideoChannelModel } from '../../models/video/video-channel'
|
|
|
|
import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
|
2018-07-19 10:17:54 -04:00
|
|
|
|
|
|
|
const searchRouter = express.Router()
|
|
|
|
|
|
|
|
searchRouter.get('/videos',
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
videosSearchSortValidator,
|
|
|
|
setDefaultSearchSort,
|
|
|
|
optionalAuthenticate,
|
2018-07-20 08:35:18 -04:00
|
|
|
commonVideosFiltersValidator,
|
2018-08-23 11:58:39 -04:00
|
|
|
videosSearchValidator,
|
2018-07-19 10:17:54 -04:00
|
|
|
asyncMiddleware(searchVideos)
|
|
|
|
)
|
|
|
|
|
2018-08-23 11:58:39 -04:00
|
|
|
searchRouter.get('/video-channels',
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
videoChannelsSearchSortValidator,
|
|
|
|
setDefaultSearchSort,
|
|
|
|
optionalAuthenticate,
|
|
|
|
videoChannelsSearchValidator,
|
|
|
|
asyncMiddleware(searchVideoChannels)
|
|
|
|
)
|
|
|
|
|
2018-07-19 10:17:54 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export { searchRouter }
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-08-23 11:58:39 -04: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 04:12:30 -04:00
|
|
|
|
|
|
|
// Handle strings like @toto@example.com
|
|
|
|
if (parts.length === 3 && parts[0].length === 0) parts.shift()
|
2018-08-24 05:04:02 -04:00
|
|
|
const isWebfingerSearch = parts.length === 2 && parts.every(p => p.indexOf(' ') === -1)
|
2018-08-23 11:58:39 -04:00
|
|
|
|
2018-08-24 05:04:02 -04:00
|
|
|
if (isURISearch || isWebfingerSearch) return searchVideoChannelURI(search, isWebfingerSearch, res)
|
2018-08-23 11:58:39 -04:00
|
|
|
|
|
|
|
return searchVideoChannelsDB(query, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: express.Response) {
|
|
|
|
const serverActor = await getServerActor()
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
actorId: serverActor.id,
|
|
|
|
search: query.search,
|
|
|
|
start: query.start,
|
|
|
|
count: query.count,
|
|
|
|
sort: query.sort
|
|
|
|
}
|
|
|
|
const resultList = await VideoChannelModel.searchForApi(options)
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
2018-08-24 05:04:02 -04:00
|
|
|
async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) {
|
2018-08-23 11:58:39 -04:00
|
|
|
let videoChannel: VideoChannelModel
|
2018-08-24 05:04:02 -04:00
|
|
|
let uri = search
|
2018-08-23 11:58:39 -04:00
|
|
|
|
2018-08-24 05:04:02 -04:00
|
|
|
if (isWebfingerSearch) uri = await loadActorUrlOrGetFromWebfinger(search)
|
2018-08-23 11:58:39 -04:00
|
|
|
|
2018-08-24 05:04:02 -04:00
|
|
|
if (isUserAbleToSearchRemoteURI(res)) {
|
|
|
|
try {
|
2018-09-19 08:44:20 -04:00
|
|
|
const actor = await getOrCreateActorAndServerAndModel(uri, 'all', true, true)
|
2018-08-24 05:04:02 -04:00
|
|
|
videoChannel = actor.VideoChannel
|
|
|
|
} catch (err) {
|
|
|
|
logger.info('Cannot search remote video channel %s.', uri, { err })
|
|
|
|
}
|
2018-08-23 11:58:39 -04:00
|
|
|
} else {
|
2018-08-24 05:04:02 -04:00
|
|
|
videoChannel = await VideoChannelModel.loadByUrlAndPopulateAccount(uri)
|
2018-08-23 11:58:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
total: videoChannel ? 1 : 0,
|
|
|
|
data: videoChannel ? [ videoChannel.toFormattedJSON() ] : []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-22 05:51:39 -04:00
|
|
|
function searchVideos (req: express.Request, res: express.Response) {
|
2018-07-20 08:35:18 -04:00
|
|
|
const query: VideosSearchQuery = req.query
|
2018-08-23 04:30:53 -04:00
|
|
|
const search = query.search
|
|
|
|
if (search && (search.startsWith('http://') || search.startsWith('https://'))) {
|
2018-08-23 11:58:39 -04:00
|
|
|
return searchVideoURI(search, res)
|
2018-08-22 05:51:39 -04:00
|
|
|
}
|
2018-07-20 08:35:18 -04:00
|
|
|
|
2018-08-22 05:51:39 -04:00
|
|
|
return searchVideosDB(query, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function searchVideosDB (query: VideosSearchQuery, res: express.Response) {
|
2018-08-16 09:25:20 -04:00
|
|
|
const options = Object.assign(query, {
|
|
|
|
includeLocalVideos: true,
|
|
|
|
nsfw: buildNSFWFilter(res, query.nsfw)
|
|
|
|
})
|
2018-07-20 08:35:18 -04:00
|
|
|
const resultList = await VideoModel.searchAndPopulateAccountAndServer(options)
|
2018-07-19 10:17:54 -04:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
2018-08-22 05:51:39 -04:00
|
|
|
|
2018-08-23 11:58:39 -04:00
|
|
|
async function searchVideoURI (url: string, res: express.Response) {
|
2018-08-22 05:51:39 -04:00
|
|
|
let video: VideoModel
|
|
|
|
|
2018-08-22 10:15:35 -04:00
|
|
|
// Check if we can fetch a remote video with the URL
|
2018-08-23 11:58:39 -04:00
|
|
|
if (isUserAbleToSearchRemoteURI(res)) {
|
2018-08-22 10:15:35 -04:00
|
|
|
try {
|
|
|
|
const syncParam = {
|
|
|
|
likes: false,
|
|
|
|
dislikes: false,
|
|
|
|
shares: false,
|
|
|
|
comments: false,
|
|
|
|
thumbnail: true,
|
|
|
|
refreshVideo: false
|
|
|
|
}
|
2018-08-22 05:51:39 -04:00
|
|
|
|
2018-09-19 05:16:23 -04:00
|
|
|
const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
|
2018-08-23 11:58:39 -04:00
|
|
|
video = result ? result.video : undefined
|
2018-08-22 10:15:35 -04:00
|
|
|
} catch (err) {
|
2018-08-24 05:04:02 -04:00
|
|
|
logger.info('Cannot search remote video %s.', url, { err })
|
2018-08-22 10:15:35 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
video = await VideoModel.loadByUrlAndPopulateAccount(url)
|
2018-08-22 05:51:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
total: video ? 1 : 0,
|
|
|
|
data: video ? [ video.toFormattedJSON() ] : []
|
|
|
|
})
|
|
|
|
}
|