diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-03 14:30:09 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:40:32 +0200 |
commit | 49af5ac8c2653cb0ef23479c9d3256c5b724d49d (patch) | |
tree | 6783df1833b13e141cfd5dc0177531887c4a4e2e /server/lib/activitypub/playlists/refresh.ts | |
parent | 9777fe9eebe53debdf45091cab98f72a5987e05a (diff) | |
download | PeerTube-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.ts | 44 |
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 @@ | |||
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 | } | ||