]>
Commit | Line | Data |
---|---|---|
418d092a C |
1 | import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' |
2 | import { crawlCollectionPage } from './crawl' | |
e8bafea3 | 3 | import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' |
418d092a C |
4 | import { isArray } from '../../helpers/custom-validators/misc' |
5 | import { getOrCreateActorAndServerAndModel } from './actor' | |
6 | import { logger } from '../../helpers/logger' | |
7 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | |
e8bafea3 | 8 | import { doRequest } from '../../helpers/requests' |
418d092a C |
9 | import { checkUrlsSameHost } from '../../helpers/activitypub' |
10 | import * as Bluebird from 'bluebird' | |
11 | import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object' | |
12 | import { getOrCreateVideoAndAccountAndChannel } from './videos' | |
13 | import { isPlaylistElementObjectValid, isPlaylistObjectValid } from '../../helpers/custom-validators/activitypub/playlist' | |
14 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' | |
418d092a | 15 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' |
74dc3bca | 16 | import { sequelizeTypescript } from '../../initializers/database' |
3acc5084 | 17 | import { createPlaylistMiniatureFromUrl } from '../thumbnail' |
26d6bf65 C |
18 | import { FilteredModelAttributes } from '../../types/sequelize' |
19 | import { MAccountDefault, MAccountId, MVideoId } from '../../types/models' | |
20 | import { MVideoPlaylist, MVideoPlaylistId, MVideoPlaylistOwner } from '../../types/models/video/video-playlist' | |
f2eb23cd | 21 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
418d092a | 22 | |
453e83ea | 23 | function playlistObjectToDBAttributes (playlistObject: PlaylistObject, byAccount: MAccountId, to: string[]) { |
bdd428a6 C |
24 | const privacy = to.includes(ACTIVITY_PUB.PUBLIC) |
25 | ? VideoPlaylistPrivacy.PUBLIC | |
26 | : VideoPlaylistPrivacy.UNLISTED | |
418d092a C |
27 | |
28 | return { | |
29 | name: playlistObject.name, | |
30 | description: playlistObject.content, | |
31 | privacy, | |
32 | url: playlistObject.id, | |
33 | uuid: playlistObject.uuid, | |
34 | ownerAccountId: byAccount.id, | |
df0b219d C |
35 | videoChannelId: null, |
36 | createdAt: new Date(playlistObject.published), | |
37 | updatedAt: new Date(playlistObject.updated) | |
418d092a C |
38 | } |
39 | } | |
40 | ||
453e83ea | 41 | function playlistElementObjectToDBAttributes (elementObject: PlaylistElementObject, videoPlaylist: MVideoPlaylistId, video: MVideoId) { |
418d092a C |
42 | return { |
43 | position: elementObject.position, | |
44 | url: elementObject.id, | |
45 | startTimestamp: elementObject.startTimestamp || null, | |
46 | stopTimestamp: elementObject.stopTimestamp || null, | |
47 | videoPlaylistId: videoPlaylist.id, | |
48 | videoId: video.id | |
49 | } | |
50 | } | |
51 | ||
453e83ea | 52 | async function createAccountPlaylists (playlistUrls: string[], account: MAccountDefault) { |
418d092a C |
53 | await Bluebird.map(playlistUrls, async playlistUrl => { |
54 | try { | |
55 | const exists = await VideoPlaylistModel.doesPlaylistExist(playlistUrl) | |
56 | if (exists === true) return | |
57 | ||
58 | // Fetch url | |
59 | const { body } = await doRequest<PlaylistObject>({ | |
60 | uri: playlistUrl, | |
61 | json: true, | |
62 | activityPub: true | |
63 | }) | |
64 | ||
65 | if (!isPlaylistObjectValid(body)) { | |
66 | throw new Error(`Invalid playlist object when fetch account playlists: ${JSON.stringify(body)}`) | |
67 | } | |
68 | ||
69 | if (!isArray(body.to)) { | |
70 | throw new Error('Playlist does not have an audience.') | |
71 | } | |
72 | ||
73 | return createOrUpdateVideoPlaylist(body, account, body.to) | |
74 | } catch (err) { | |
75 | logger.warn('Cannot add playlist element %s.', playlistUrl, { err }) | |
76 | } | |
77 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
78 | } | |
79 | ||
453e83ea | 80 | async function createOrUpdateVideoPlaylist (playlistObject: PlaylistObject, byAccount: MAccountId, to: string[]) { |
418d092a C |
81 | const playlistAttributes = playlistObjectToDBAttributes(playlistObject, byAccount, to) |
82 | ||
83 | if (isArray(playlistObject.attributedTo) && playlistObject.attributedTo.length === 1) { | |
84 | const actor = await getOrCreateActorAndServerAndModel(playlistObject.attributedTo[0]) | |
85 | ||
86 | if (actor.VideoChannel) { | |
87 | playlistAttributes.videoChannelId = actor.VideoChannel.id | |
88 | } else { | |
89 | logger.warn('Attributed to of video playlist %s is not a video channel.', playlistObject.id, { playlistObject }) | |
90 | } | |
91 | } | |
92 | ||
453e83ea | 93 | const [ playlist ] = await VideoPlaylistModel.upsert<MVideoPlaylist>(playlistAttributes, { returning: true }) |
418d092a C |
94 | |
95 | let accItems: string[] = [] | |
96 | await crawlCollectionPage<string>(playlistObject.id, items => { | |
97 | accItems = accItems.concat(items) | |
98 | ||
99 | return Promise.resolve() | |
100 | }) | |
101 | ||
e8bafea3 C |
102 | const refreshedPlaylist = await VideoPlaylistModel.loadWithAccountAndChannel(playlist.id, null) |
103 | ||
104 | if (playlistObject.icon) { | |
418d092a | 105 | try { |
a35a2279 | 106 | const thumbnailModel = await createPlaylistMiniatureFromUrl({ downloadUrl: playlistObject.icon.url, playlist: refreshedPlaylist }) |
3acc5084 | 107 | await refreshedPlaylist.setAndSaveThumbnail(thumbnailModel, undefined) |
418d092a C |
108 | } catch (err) { |
109 | logger.warn('Cannot generate thumbnail of %s.', playlistObject.id, { err }) | |
110 | } | |
65af03a2 C |
111 | } else if (refreshedPlaylist.hasThumbnail()) { |
112 | await refreshedPlaylist.Thumbnail.destroy() | |
113 | refreshedPlaylist.Thumbnail = null | |
418d092a C |
114 | } |
115 | ||
e8bafea3 | 116 | return resetVideoPlaylistElements(accItems, refreshedPlaylist) |
418d092a C |
117 | } |
118 | ||
453e83ea | 119 | async function refreshVideoPlaylistIfNeeded (videoPlaylist: MVideoPlaylistOwner): Promise<MVideoPlaylistOwner> { |
9f79ade6 C |
120 | if (!videoPlaylist.isOutdated()) return videoPlaylist |
121 | ||
122 | try { | |
123 | const { statusCode, playlistObject } = await fetchRemoteVideoPlaylist(videoPlaylist.url) | |
f2eb23cd | 124 | if (statusCode === HttpStatusCode.NOT_FOUND_404) { |
9f79ade6 C |
125 | logger.info('Cannot refresh remote video playlist %s: it does not exist anymore. Deleting it.', videoPlaylist.url) |
126 | ||
127 | await videoPlaylist.destroy() | |
128 | return undefined | |
129 | } | |
130 | ||
131 | if (playlistObject === undefined) { | |
132 | logger.warn('Cannot refresh remote playlist %s: invalid body.', videoPlaylist.url) | |
133 | ||
134 | await videoPlaylist.setAsRefreshed() | |
135 | return videoPlaylist | |
136 | } | |
137 | ||
138 | const byAccount = videoPlaylist.OwnerAccount | |
139 | await createOrUpdateVideoPlaylist(playlistObject, byAccount, playlistObject.to) | |
140 | ||
141 | return videoPlaylist | |
142 | } catch (err) { | |
143 | logger.warn('Cannot refresh video playlist %s.', videoPlaylist.url, { err }) | |
144 | ||
145 | await videoPlaylist.setAsRefreshed() | |
146 | return videoPlaylist | |
147 | } | |
148 | } | |
149 | ||
418d092a C |
150 | // --------------------------------------------------------------------------- |
151 | ||
152 | export { | |
153 | createAccountPlaylists, | |
154 | playlistObjectToDBAttributes, | |
155 | playlistElementObjectToDBAttributes, | |
9f79ade6 C |
156 | createOrUpdateVideoPlaylist, |
157 | refreshVideoPlaylistIfNeeded | |
418d092a C |
158 | } |
159 | ||
160 | // --------------------------------------------------------------------------- | |
161 | ||
453e83ea | 162 | async function resetVideoPlaylistElements (elementUrls: string[], playlist: MVideoPlaylist) { |
3acc5084 | 163 | const elementsToCreate: FilteredModelAttributes<VideoPlaylistElementModel>[] = [] |
418d092a C |
164 | |
165 | await Bluebird.map(elementUrls, async elementUrl => { | |
166 | try { | |
167 | // Fetch url | |
168 | const { body } = await doRequest<PlaylistElementObject>({ | |
169 | uri: elementUrl, | |
170 | json: true, | |
171 | activityPub: true | |
172 | }) | |
173 | ||
174 | if (!isPlaylistElementObjectValid(body)) throw new Error(`Invalid body in video get playlist element ${elementUrl}`) | |
175 | ||
176 | if (checkUrlsSameHost(body.id, elementUrl) !== true) { | |
177 | throw new Error(`Playlist element url ${elementUrl} host is different from the AP object id ${body.id}`) | |
178 | } | |
179 | ||
180 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: { id: body.url }, fetchType: 'only-video' }) | |
181 | ||
182 | elementsToCreate.push(playlistElementObjectToDBAttributes(body, playlist, video)) | |
183 | } catch (err) { | |
184 | logger.warn('Cannot add playlist element %s.', elementUrl, { err }) | |
185 | } | |
186 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
187 | ||
188 | await sequelizeTypescript.transaction(async t => { | |
189 | await VideoPlaylistElementModel.deleteAllOf(playlist.id, t) | |
190 | ||
191 | for (const element of elementsToCreate) { | |
192 | await VideoPlaylistElementModel.create(element, { transaction: t }) | |
193 | } | |
194 | }) | |
195 | ||
196 | logger.info('Reset playlist %s with %s elements.', playlist.url, elementsToCreate.length) | |
197 | ||
198 | return undefined | |
199 | } | |
200 | ||
9f79ade6 C |
201 | async function fetchRemoteVideoPlaylist (playlistUrl: string): Promise<{ statusCode: number, playlistObject: PlaylistObject }> { |
202 | const options = { | |
203 | uri: playlistUrl, | |
204 | method: 'GET', | |
205 | json: true, | |
206 | activityPub: true | |
207 | } | |
208 | ||
209 | logger.info('Fetching remote playlist %s.', playlistUrl) | |
210 | ||
bdd428a6 | 211 | const { response, body } = await doRequest<any>(options) |
9f79ade6 C |
212 | |
213 | if (isPlaylistObjectValid(body) === false || checkUrlsSameHost(body.id, playlistUrl) !== true) { | |
214 | logger.debug('Remote video playlist JSON is not valid.', { body }) | |
215 | return { statusCode: response.statusCode, playlistObject: undefined } | |
216 | } | |
217 | ||
218 | return { statusCode: response.statusCode, playlistObject: body } | |
219 | } |