]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/playlist.ts
Generate a name for thumbnails
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / playlist.ts
1 import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object'
2 import { crawlCollectionPage } from './crawl'
3 import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
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'
8 import { doRequest } from '../../helpers/requests'
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'
15 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
16 import { sequelizeTypescript } from '../../initializers/database'
17 import { createPlaylistMiniatureFromUrl } from '../thumbnail'
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'
21 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
22
23 function playlistObjectToDBAttributes (playlistObject: PlaylistObject, byAccount: MAccountId, to: string[]) {
24 const privacy = to.includes(ACTIVITY_PUB.PUBLIC)
25 ? VideoPlaylistPrivacy.PUBLIC
26 : VideoPlaylistPrivacy.UNLISTED
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,
35 videoChannelId: null,
36 createdAt: new Date(playlistObject.published),
37 updatedAt: new Date(playlistObject.updated)
38 }
39 }
40
41 function playlistElementObjectToDBAttributes (elementObject: PlaylistElementObject, videoPlaylist: MVideoPlaylistId, video: MVideoId) {
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
52 async function createAccountPlaylists (playlistUrls: string[], account: MAccountDefault) {
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
80 async function createOrUpdateVideoPlaylist (playlistObject: PlaylistObject, byAccount: MAccountId, to: string[]) {
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
93 const [ playlist ] = await VideoPlaylistModel.upsert<MVideoPlaylist>(playlistAttributes, { returning: true })
94
95 let accItems: string[] = []
96 await crawlCollectionPage<string>(playlistObject.id, items => {
97 accItems = accItems.concat(items)
98
99 return Promise.resolve()
100 })
101
102 logger.info('toto', { playlist, id: playlist.id })
103
104 const refreshedPlaylist = await VideoPlaylistModel.loadWithAccountAndChannel(playlist.id, null)
105
106 if (playlistObject.icon) {
107 try {
108 const thumbnailModel = await createPlaylistMiniatureFromUrl(playlistObject.icon.url, refreshedPlaylist)
109 await refreshedPlaylist.setAndSaveThumbnail(thumbnailModel, undefined)
110 } catch (err) {
111 logger.warn('Cannot generate thumbnail of %s.', playlistObject.id, { err })
112 }
113 } else if (refreshedPlaylist.hasThumbnail()) {
114 await refreshedPlaylist.Thumbnail.destroy()
115 refreshedPlaylist.Thumbnail = null
116 }
117
118 return resetVideoPlaylistElements(accItems, refreshedPlaylist)
119 }
120
121 async function refreshVideoPlaylistIfNeeded (videoPlaylist: MVideoPlaylistOwner): Promise<MVideoPlaylistOwner> {
122 if (!videoPlaylist.isOutdated()) return videoPlaylist
123
124 try {
125 const { statusCode, playlistObject } = await fetchRemoteVideoPlaylist(videoPlaylist.url)
126 if (statusCode === HttpStatusCode.NOT_FOUND_404) {
127 logger.info('Cannot refresh remote video playlist %s: it does not exist anymore. Deleting it.', videoPlaylist.url)
128
129 await videoPlaylist.destroy()
130 return undefined
131 }
132
133 if (playlistObject === undefined) {
134 logger.warn('Cannot refresh remote playlist %s: invalid body.', videoPlaylist.url)
135
136 await videoPlaylist.setAsRefreshed()
137 return videoPlaylist
138 }
139
140 const byAccount = videoPlaylist.OwnerAccount
141 await createOrUpdateVideoPlaylist(playlistObject, byAccount, playlistObject.to)
142
143 return videoPlaylist
144 } catch (err) {
145 logger.warn('Cannot refresh video playlist %s.', videoPlaylist.url, { err })
146
147 await videoPlaylist.setAsRefreshed()
148 return videoPlaylist
149 }
150 }
151
152 // ---------------------------------------------------------------------------
153
154 export {
155 createAccountPlaylists,
156 playlistObjectToDBAttributes,
157 playlistElementObjectToDBAttributes,
158 createOrUpdateVideoPlaylist,
159 refreshVideoPlaylistIfNeeded
160 }
161
162 // ---------------------------------------------------------------------------
163
164 async function resetVideoPlaylistElements (elementUrls: string[], playlist: MVideoPlaylist) {
165 const elementsToCreate: FilteredModelAttributes<VideoPlaylistElementModel>[] = []
166
167 await Bluebird.map(elementUrls, async elementUrl => {
168 try {
169 // Fetch url
170 const { body } = await doRequest<PlaylistElementObject>({
171 uri: elementUrl,
172 json: true,
173 activityPub: true
174 })
175
176 if (!isPlaylistElementObjectValid(body)) throw new Error(`Invalid body in video get playlist element ${elementUrl}`)
177
178 if (checkUrlsSameHost(body.id, elementUrl) !== true) {
179 throw new Error(`Playlist element url ${elementUrl} host is different from the AP object id ${body.id}`)
180 }
181
182 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: { id: body.url }, fetchType: 'only-video' })
183
184 elementsToCreate.push(playlistElementObjectToDBAttributes(body, playlist, video))
185 } catch (err) {
186 logger.warn('Cannot add playlist element %s.', elementUrl, { err })
187 }
188 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
189
190 await sequelizeTypescript.transaction(async t => {
191 await VideoPlaylistElementModel.deleteAllOf(playlist.id, t)
192
193 for (const element of elementsToCreate) {
194 await VideoPlaylistElementModel.create(element, { transaction: t })
195 }
196 })
197
198 logger.info('Reset playlist %s with %s elements.', playlist.url, elementsToCreate.length)
199
200 return undefined
201 }
202
203 async function fetchRemoteVideoPlaylist (playlistUrl: string): Promise<{ statusCode: number, playlistObject: PlaylistObject }> {
204 const options = {
205 uri: playlistUrl,
206 method: 'GET',
207 json: true,
208 activityPub: true
209 }
210
211 logger.info('Fetching remote playlist %s.', playlistUrl)
212
213 const { response, body } = await doRequest<any>(options)
214
215 if (isPlaylistObjectValid(body) === false || checkUrlsSameHost(body.id, playlistUrl) !== true) {
216 logger.debug('Remote video playlist JSON is not valid.', { body })
217 return { statusCode: response.statusCode, playlistObject: undefined }
218 }
219
220 return { statusCode: response.statusCode, playlistObject: body }
221 }