PeerTube/server/lib/activitypub/playlists/refresh.ts

54 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-06-03 07:30:09 -05:00
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { PeerTubeRequestError } from '@server/helpers/requests'
2021-06-17 09:02:38 -05:00
import { JobQueue } from '@server/lib/job-queue'
import { MVideoPlaylist, MVideoPlaylistOwner } from '@server/types/models'
2021-07-16 03:42:24 -05:00
import { HttpStatusCode } from '@shared/models'
2021-06-03 07:30:09 -05:00
import { createOrUpdateVideoPlaylist } from './create-update'
import { fetchRemoteVideoPlaylist } from './shared'
2021-06-17 09:02:38 -05:00
function scheduleRefreshIfNeeded (playlist: MVideoPlaylist) {
if (!playlist.isOutdated()) return
JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video-playlist', url: playlist.url } })
}
2021-06-03 07:30:09 -05:00
async function refreshVideoPlaylistIfNeeded (videoPlaylist: MVideoPlaylistOwner): Promise<MVideoPlaylistOwner> {
if (!videoPlaylist.isOutdated()) return videoPlaylist
const lTags = loggerTagsFactory('ap', 'video-playlist', 'refresh', videoPlaylist.uuid, videoPlaylist.url)
2021-06-08 09:19:09 -05:00
logger.info('Refreshing playlist %s.', videoPlaylist.url, lTags())
2021-06-03 07:30:09 -05:00
try {
const { playlistObject } = await fetchRemoteVideoPlaylist(videoPlaylist.url)
if (playlistObject === undefined) {
logger.warn('Cannot refresh remote playlist %s: invalid body.', videoPlaylist.url, lTags())
await videoPlaylist.setAsRefreshed()
return videoPlaylist
}
2021-06-17 09:02:38 -05:00
await createOrUpdateVideoPlaylist(playlistObject)
2021-06-03 07:30:09 -05:00
return videoPlaylist
} catch (err) {
if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) {
logger.info('Cannot refresh not existing playlist %s. Deleting it.', videoPlaylist.url, lTags())
await videoPlaylist.destroy()
return undefined
}
logger.warn('Cannot refresh video playlist %s.', videoPlaylist.url, { err, ...lTags() })
await videoPlaylist.setAsRefreshed()
return videoPlaylist
}
}
export {
2021-06-17 09:02:38 -05:00
scheduleRefreshIfNeeded,
2021-06-03 07:30:09 -05:00
refreshVideoPlaylistIfNeeded
}