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

54 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-06-03 14:30:09 +02:00
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { PeerTubeRequestError } from '@server/helpers/requests'
2021-06-17 16:02:38 +02:00
import { JobQueue } from '@server/lib/job-queue'
import { MVideoPlaylist, MVideoPlaylistOwner } from '@server/types/models'
2021-07-16 10:42:24 +02:00
import { HttpStatusCode } from '@shared/models'
2021-06-03 14:30:09 +02:00
import { createOrUpdateVideoPlaylist } from './create-update'
import { fetchRemoteVideoPlaylist } from './shared'
2021-06-17 16:02:38 +02:00
function scheduleRefreshIfNeeded (playlist: MVideoPlaylist) {
if (!playlist.isOutdated()) return
2022-08-08 15:48:17 +02:00
JobQueue.Instance.createJobAsync({ type: 'activitypub-refresher', payload: { type: 'video-playlist', url: playlist.url } })
2021-06-17 16:02:38 +02:00
}
2021-06-03 14:30:09 +02: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 16:19:09 +02:00
logger.info('Refreshing playlist %s.', videoPlaylist.url, lTags())
2021-06-03 14:30:09 +02: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 16:02:38 +02:00
await createOrUpdateVideoPlaylist(playlistObject)
2021-06-03 14:30:09 +02: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 16:02:38 +02:00
scheduleRefreshIfNeeded,
2021-06-03 14:30:09 +02:00
refreshVideoPlaylistIfNeeded
}