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