aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/playlists/refresh.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-03 14:30:09 +0200
committerChocobozzz <me@florianbigard.com>2021-06-03 16:40:32 +0200
commit49af5ac8c2653cb0ef23479c9d3256c5b724d49d (patch)
tree6783df1833b13e141cfd5dc0177531887c4a4e2e /server/lib/activitypub/playlists/refresh.ts
parent9777fe9eebe53debdf45091cab98f72a5987e05a (diff)
downloadPeerTube-49af5ac8c2653cb0ef23479c9d3256c5b724d49d.tar.gz
PeerTube-49af5ac8c2653cb0ef23479c9d3256c5b724d49d.tar.zst
PeerTube-49af5ac8c2653cb0ef23479c9d3256c5b724d49d.zip
Refactor AP playlists
Diffstat (limited to 'server/lib/activitypub/playlists/refresh.ts')
-rw-r--r--server/lib/activitypub/playlists/refresh.ts44
1 files changed, 44 insertions, 0 deletions
diff --git a/server/lib/activitypub/playlists/refresh.ts b/server/lib/activitypub/playlists/refresh.ts
new file mode 100644
index 000000000..ff9e5471a
--- /dev/null
+++ b/server/lib/activitypub/playlists/refresh.ts
@@ -0,0 +1,44 @@
1import { logger, loggerTagsFactory } from '@server/helpers/logger'
2import { PeerTubeRequestError } from '@server/helpers/requests'
3import { MVideoPlaylistOwner } from '@server/types/models'
4import { HttpStatusCode } from '@shared/core-utils'
5import { createOrUpdateVideoPlaylist } from './create-update'
6import { fetchRemoteVideoPlaylist } from './shared'
7
8async 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
42export {
43 refreshVideoPlaylistIfNeeded
44}