2021-08-27 14:32:44 +02:00
|
|
|
import { map } from 'bluebird'
|
2021-06-03 14:30:09 +02:00
|
|
|
import { isArray } from '@server/helpers/custom-validators/misc'
|
2022-09-09 12:05:35 +02:00
|
|
|
import { retryTransactionWrapper } from '@server/helpers/database-utils'
|
2021-06-03 14:30:09 +02:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
|
|
|
import { CRAWL_REQUEST_CONCURRENCY } from '@server/initializers/constants'
|
|
|
|
import { sequelizeTypescript } from '@server/initializers/database'
|
2021-06-04 09:19:01 +02:00
|
|
|
import { updatePlaylistMiniatureFromUrl } from '@server/lib/thumbnail'
|
2021-06-03 14:30:09 +02:00
|
|
|
import { VideoPlaylistModel } from '@server/models/video/video-playlist'
|
|
|
|
import { VideoPlaylistElementModel } from '@server/models/video/video-playlist-element'
|
|
|
|
import { FilteredModelAttributes } from '@server/types'
|
2021-06-17 16:02:38 +02:00
|
|
|
import { MThumbnail, MVideoPlaylist, MVideoPlaylistFull, MVideoPlaylistVideosLength } from '@server/types/models'
|
2021-06-03 14:30:09 +02:00
|
|
|
import { PlaylistObject } from '@shared/models'
|
2022-03-23 14:24:50 +01:00
|
|
|
import { AttributesOnly } from '@shared/typescript-utils'
|
|
|
|
import { getAPId } from '../activity'
|
2021-06-03 16:02:29 +02:00
|
|
|
import { getOrCreateAPActor } from '../actors'
|
2021-06-03 14:30:09 +02:00
|
|
|
import { crawlCollectionPage } from '../crawl'
|
|
|
|
import { getOrCreateAPVideo } from '../videos'
|
|
|
|
import {
|
|
|
|
fetchRemotePlaylistElement,
|
|
|
|
fetchRemoteVideoPlaylist,
|
|
|
|
playlistElementObjectToDBAttributes,
|
|
|
|
playlistObjectToDBAttributes
|
|
|
|
} from './shared'
|
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('ap', 'video-playlist')
|
|
|
|
|
2021-06-17 16:02:38 +02:00
|
|
|
async function createAccountPlaylists (playlistUrls: string[]) {
|
2021-08-27 14:32:44 +02:00
|
|
|
await map(playlistUrls, async playlistUrl => {
|
2021-06-03 14:30:09 +02:00
|
|
|
try {
|
|
|
|
const exists = await VideoPlaylistModel.doesPlaylistExist(playlistUrl)
|
|
|
|
if (exists === true) return
|
|
|
|
|
|
|
|
const { playlistObject } = await fetchRemoteVideoPlaylist(playlistUrl)
|
|
|
|
|
|
|
|
if (playlistObject === undefined) {
|
|
|
|
throw new Error(`Cannot refresh remote playlist ${playlistUrl}: invalid body.`)
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:02:38 +02:00
|
|
|
return createOrUpdateVideoPlaylist(playlistObject)
|
2021-06-03 14:30:09 +02:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot add playlist element %s.', playlistUrl, { err, ...lTags(playlistUrl) })
|
|
|
|
}
|
|
|
|
}, { concurrency: CRAWL_REQUEST_CONCURRENCY })
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:02:38 +02:00
|
|
|
async function createOrUpdateVideoPlaylist (playlistObject: PlaylistObject, to?: string[]) {
|
|
|
|
const playlistAttributes = playlistObjectToDBAttributes(playlistObject, to || playlistObject.to)
|
2021-06-03 14:30:09 +02:00
|
|
|
|
2021-06-17 16:02:38 +02:00
|
|
|
await setVideoChannel(playlistObject, playlistAttributes)
|
2021-06-03 14:30:09 +02:00
|
|
|
|
2021-06-17 16:02:38 +02:00
|
|
|
const [ upsertPlaylist ] = await VideoPlaylistModel.upsert<MVideoPlaylistVideosLength>(playlistAttributes, { returning: true })
|
2021-06-03 14:30:09 +02:00
|
|
|
|
|
|
|
const playlistElementUrls = await fetchElementUrls(playlistObject)
|
|
|
|
|
|
|
|
// Refetch playlist from DB since elements fetching could be long in time
|
|
|
|
const playlist = await VideoPlaylistModel.loadWithAccountAndChannel(upsertPlaylist.id, null)
|
|
|
|
|
2021-06-17 11:09:54 +02:00
|
|
|
await updatePlaylistThumbnail(playlistObject, playlist)
|
2021-06-03 14:30:09 +02:00
|
|
|
|
2021-06-17 16:02:38 +02:00
|
|
|
const elementsLength = await rebuildVideoPlaylistElements(playlistElementUrls, playlist)
|
|
|
|
playlist.setVideosLength(elementsLength)
|
|
|
|
|
|
|
|
return playlist
|
2021-06-03 14:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
createAccountPlaylists,
|
|
|
|
createOrUpdateVideoPlaylist
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-06-17 16:02:38 +02:00
|
|
|
async function setVideoChannel (playlistObject: PlaylistObject, playlistAttributes: AttributesOnly<VideoPlaylistModel>) {
|
|
|
|
if (!isArray(playlistObject.attributedTo) || playlistObject.attributedTo.length !== 1) {
|
|
|
|
throw new Error('Not attributed to for playlist object ' + getAPId(playlistObject))
|
|
|
|
}
|
2021-06-03 14:30:09 +02:00
|
|
|
|
2021-06-17 16:02:38 +02:00
|
|
|
const actor = await getOrCreateAPActor(playlistObject.attributedTo[0], 'all')
|
2021-06-03 14:30:09 +02:00
|
|
|
|
|
|
|
if (!actor.VideoChannel) {
|
|
|
|
logger.warn('Playlist "attributedTo" %s is not a video channel.', playlistObject.id, { playlistObject, ...lTags(playlistObject.id) })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
playlistAttributes.videoChannelId = actor.VideoChannel.id
|
2021-06-17 16:02:38 +02:00
|
|
|
playlistAttributes.ownerAccountId = actor.VideoChannel.Account.id
|
2021-06-03 14:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchElementUrls (playlistObject: PlaylistObject) {
|
|
|
|
let accItems: string[] = []
|
|
|
|
await crawlCollectionPage<string>(playlistObject.id, items => {
|
|
|
|
accItems = accItems.concat(items)
|
|
|
|
|
|
|
|
return Promise.resolve()
|
|
|
|
})
|
|
|
|
|
|
|
|
return accItems
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updatePlaylistThumbnail (playlistObject: PlaylistObject, playlist: MVideoPlaylistFull) {
|
|
|
|
if (playlistObject.icon) {
|
2021-06-17 11:09:54 +02:00
|
|
|
let thumbnailModel: MThumbnail
|
|
|
|
|
|
|
|
try {
|
|
|
|
thumbnailModel = await updatePlaylistMiniatureFromUrl({ downloadUrl: playlistObject.icon.url, playlist })
|
|
|
|
await playlist.setAndSaveThumbnail(thumbnailModel, undefined)
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot set thumbnail of %s.', playlistObject.id, { err, ...lTags(playlistObject.id, playlist.uuid, playlist.url) })
|
|
|
|
|
|
|
|
if (thumbnailModel) await thumbnailModel.removeThumbnail()
|
|
|
|
}
|
2021-06-03 14:30:09 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Playlist does not have an icon, destroy existing one
|
|
|
|
if (playlist.hasThumbnail()) {
|
|
|
|
await playlist.Thumbnail.destroy()
|
|
|
|
playlist.Thumbnail = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function rebuildVideoPlaylistElements (elementUrls: string[], playlist: MVideoPlaylist) {
|
|
|
|
const elementsToCreate = await buildElementsDBAttributes(elementUrls, playlist)
|
|
|
|
|
2022-09-09 12:05:35 +02:00
|
|
|
await retryTransactionWrapper(() => sequelizeTypescript.transaction(async t => {
|
2021-06-03 14:30:09 +02:00
|
|
|
await VideoPlaylistElementModel.deleteAllOf(playlist.id, t)
|
|
|
|
|
|
|
|
for (const element of elementsToCreate) {
|
|
|
|
await VideoPlaylistElementModel.create(element, { transaction: t })
|
|
|
|
}
|
2022-09-09 12:05:35 +02:00
|
|
|
}))
|
2021-06-03 14:30:09 +02:00
|
|
|
|
|
|
|
logger.info('Rebuilt playlist %s with %s elements.', playlist.url, elementsToCreate.length, lTags(playlist.uuid, playlist.url))
|
|
|
|
|
2021-06-17 16:02:38 +02:00
|
|
|
return elementsToCreate.length
|
2021-06-03 14:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function buildElementsDBAttributes (elementUrls: string[], playlist: MVideoPlaylist) {
|
|
|
|
const elementsToCreate: FilteredModelAttributes<VideoPlaylistElementModel>[] = []
|
|
|
|
|
2021-08-27 14:32:44 +02:00
|
|
|
await map(elementUrls, async elementUrl => {
|
2021-06-03 14:30:09 +02:00
|
|
|
try {
|
|
|
|
const { elementObject } = await fetchRemotePlaylistElement(elementUrl)
|
|
|
|
|
|
|
|
const { video } = await getOrCreateAPVideo({ videoObject: { id: elementObject.url }, fetchType: 'only-video' })
|
|
|
|
|
|
|
|
elementsToCreate.push(playlistElementObjectToDBAttributes(elementObject, playlist, video))
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot add playlist element %s.', elementUrl, { err, ...lTags(playlist.uuid, playlist.url) })
|
|
|
|
}
|
|
|
|
}, { concurrency: CRAWL_REQUEST_CONCURRENCY })
|
|
|
|
|
|
|
|
return elementsToCreate
|
|
|
|
}
|