]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { sanitizeUrl } from '@server/helpers/core-utils' | |
3 | import { isUserAbleToSearchRemoteURI } from '@server/helpers/express-utils' | |
4 | import { logger } from '@server/helpers/logger' | |
5 | import { pickSearchPlaylistQuery } from '@server/helpers/query' | |
6 | import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests' | |
7 | import { getFormattedObjects } from '@server/helpers/utils' | |
8 | import { CONFIG } from '@server/initializers/config' | |
9 | import { WEBSERVER } from '@server/initializers/constants' | |
10 | import { getOrCreateAPVideoPlaylist } from '@server/lib/activitypub/playlists/get' | |
11 | import { Hooks } from '@server/lib/plugins/hooks' | |
12 | import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@server/lib/search' | |
13 | import { getServerActor } from '@server/models/application/application' | |
14 | import { VideoPlaylistModel } from '@server/models/video/video-playlist' | |
15 | import { MVideoPlaylistFullSummary } from '@server/types/models' | |
16 | import { HttpStatusCode, ResultList, VideoPlaylist, VideoPlaylistsSearchQueryAfterSanitize } from '@shared/models' | |
17 | import { | |
18 | asyncMiddleware, | |
19 | openapiOperationDoc, | |
20 | optionalAuthenticate, | |
21 | paginationValidator, | |
22 | setDefaultPagination, | |
23 | setDefaultSearchSort, | |
24 | videoPlaylistsListSearchValidator, | |
25 | videoPlaylistsSearchSortValidator | |
26 | } from '../../../middlewares' | |
27 | import { searchLocalUrl } from './shared' | |
28 | ||
29 | const searchPlaylistsRouter = express.Router() | |
30 | ||
31 | searchPlaylistsRouter.get('/video-playlists', | |
32 | openapiOperationDoc({ operationId: 'searchPlaylists' }), | |
33 | paginationValidator, | |
34 | setDefaultPagination, | |
35 | videoPlaylistsSearchSortValidator, | |
36 | setDefaultSearchSort, | |
37 | optionalAuthenticate, | |
38 | videoPlaylistsListSearchValidator, | |
39 | asyncMiddleware(searchVideoPlaylists) | |
40 | ) | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | export { searchPlaylistsRouter } | |
45 | ||
46 | // --------------------------------------------------------------------------- | |
47 | ||
48 | function searchVideoPlaylists (req: express.Request, res: express.Response) { | |
49 | const query = pickSearchPlaylistQuery(req.query) | |
50 | const search = query.search | |
51 | ||
52 | if (isURISearch(search)) return searchVideoPlaylistsURI(search, res) | |
53 | ||
54 | if (isSearchIndexSearch(query)) { | |
55 | return searchVideoPlaylistsIndex(query, res) | |
56 | } | |
57 | ||
58 | return searchVideoPlaylistsDB(query, res) | |
59 | } | |
60 | ||
61 | async function searchVideoPlaylistsIndex (query: VideoPlaylistsSearchQueryAfterSanitize, res: express.Response) { | |
62 | const result = await buildMutedForSearchIndex(res) | |
63 | ||
64 | const body = await Hooks.wrapObject(Object.assign(query, result), 'filter:api.search.video-playlists.index.list.params') | |
65 | ||
66 | const url = sanitizeUrl(CONFIG.SEARCH.SEARCH_INDEX.URL) + '/api/v1/search/video-playlists' | |
67 | ||
68 | try { | |
69 | logger.debug('Doing video playlists search index request on %s.', url, { body }) | |
70 | ||
71 | const { body: searchIndexResult } = await doJSONRequest<ResultList<VideoPlaylist>>(url, { method: 'POST', json: body }) | |
72 | const jsonResult = await Hooks.wrapObject(searchIndexResult, 'filter:api.search.video-playlists.index.list.result') | |
73 | ||
74 | return res.json(jsonResult) | |
75 | } catch (err) { | |
76 | logger.warn('Cannot use search index to make video playlists search.', { err }) | |
77 | ||
78 | return res.fail({ | |
79 | status: HttpStatusCode.INTERNAL_SERVER_ERROR_500, | |
80 | message: 'Cannot use search index to make video playlists search' | |
81 | }) | |
82 | } | |
83 | } | |
84 | ||
85 | async function searchVideoPlaylistsDB (query: VideoPlaylistsSearchQueryAfterSanitize, res: express.Response) { | |
86 | const serverActor = await getServerActor() | |
87 | ||
88 | const apiOptions = await Hooks.wrapObject({ | |
89 | ...query, | |
90 | ||
91 | followerActorId: serverActor.id | |
92 | }, 'filter:api.search.video-playlists.local.list.params') | |
93 | ||
94 | const resultList = await Hooks.wrapPromiseFun( | |
95 | VideoPlaylistModel.searchForApi, | |
96 | apiOptions, | |
97 | 'filter:api.search.video-playlists.local.list.result' | |
98 | ) | |
99 | ||
100 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
101 | } | |
102 | ||
103 | async function searchVideoPlaylistsURI (search: string, res: express.Response) { | |
104 | let videoPlaylist: MVideoPlaylistFullSummary | |
105 | ||
106 | if (isUserAbleToSearchRemoteURI(res)) { | |
107 | try { | |
108 | const url = await findLatestRedirection(search, { activityPub: true }) | |
109 | ||
110 | videoPlaylist = await getOrCreateAPVideoPlaylist(url) | |
111 | } catch (err) { | |
112 | logger.info('Cannot search remote video playlist %s.', search, { err }) | |
113 | } | |
114 | } else { | |
115 | videoPlaylist = await searchLocalUrl(sanitizeLocalUrl(search), url => VideoPlaylistModel.loadByUrlWithAccountAndChannelSummary(url)) | |
116 | } | |
117 | ||
118 | return res.json({ | |
119 | total: videoPlaylist ? 1 : 0, | |
120 | data: videoPlaylist ? [ videoPlaylist.toFormattedJSON() ] : [] | |
121 | }) | |
122 | } | |
123 | ||
124 | function sanitizeLocalUrl (url: string) { | |
125 | if (!url) return '' | |
126 | ||
127 | // Handle alternative channel URLs | |
128 | return url.replace(new RegExp('^' + WEBSERVER.URL + '/videos/watch/playlist/'), WEBSERVER.URL + '/video-playlists/') | |
129 | .replace(new RegExp('^' + WEBSERVER.URL + '/w/p/'), WEBSERVER.URL + '/video-playlists/') | |
130 | } |