]>
Commit | Line | Data |
---|---|---|
49af5ac8 C |
1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
2 | import { PeerTubeRequestError } from '@server/helpers/requests' | |
3 | import { MVideoPlaylistOwner } from '@server/types/models' | |
4 | import { HttpStatusCode } from '@shared/core-utils' | |
5 | import { createOrUpdateVideoPlaylist } from './create-update' | |
6 | import { fetchRemoteVideoPlaylist } from './shared' | |
7 | ||
8 | async function refreshVideoPlaylistIfNeeded (videoPlaylist: MVideoPlaylistOwner): Promise<MVideoPlaylistOwner> { | |
9 | if (!videoPlaylist.isOutdated()) return videoPlaylist | |
10 | ||
11 | const lTags = loggerTagsFactory('ap', 'video-playlist', 'refresh', videoPlaylist.uuid, videoPlaylist.url) | |
12 | ||
13 | try { | |
14 | const { playlistObject } = await fetchRemoteVideoPlaylist(videoPlaylist.url) | |
15 | ||
16 | if (playlistObject === undefined) { | |
17 | logger.warn('Cannot refresh remote playlist %s: invalid body.', videoPlaylist.url, lTags()) | |
18 | ||
19 | await videoPlaylist.setAsRefreshed() | |
20 | return videoPlaylist | |
21 | } | |
22 | ||
23 | const byAccount = videoPlaylist.OwnerAccount | |
24 | await createOrUpdateVideoPlaylist(playlistObject, byAccount, playlistObject.to) | |
25 | ||
26 | return videoPlaylist | |
27 | } catch (err) { | |
28 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { | |
29 | logger.info('Cannot refresh not existing playlist %s. Deleting it.', videoPlaylist.url, lTags()) | |
30 | ||
31 | await videoPlaylist.destroy() | |
32 | return undefined | |
33 | } | |
34 | ||
35 | logger.warn('Cannot refresh video playlist %s.', videoPlaylist.url, { err, ...lTags() }) | |
36 | ||
37 | await videoPlaylist.setAsRefreshed() | |
38 | return videoPlaylist | |
39 | } | |
40 | } | |
41 | ||
42 | export { | |
43 | refreshVideoPlaylistIfNeeded | |
44 | } |