X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Fupdate-host.ts;h=1d17ce1521d38783071400d03e94df738d67db09;hb=118626c8752bee7b05c4e0b668852e1aba2416f1;hp=3fb5f1972dd85b6491fed4d9979533743dbeefe7;hpb=da854ddd502cd70685ef779c673b9e63757b8aa0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/update-host.ts b/scripts/update-host.ts index 3fb5f1972..1d17ce152 100755 --- a/scripts/update-host.ts +++ b/scripts/update-host.ts @@ -1,40 +1,140 @@ -import { getServerActor } from '../server/helpers/utils' -import { initDatabaseModels } from '../server/initializers' -import { ActorFollowModel } from '../server/models/activitypub/actor-follow' +import { updateTorrentMetadata } from '@server/helpers/webtorrent' +import { getServerActor } from '@server/models/application/application' +import { WEBSERVER } from '../server/initializers/constants' +import { initDatabaseModels } from '../server/initializers/database' +import { + getLocalAccountActivityPubUrl, + getLocalVideoActivityPubUrl, + getLocalVideoAnnounceActivityPubUrl, + getLocalVideoChannelActivityPubUrl, + getLocalVideoCommentActivityPubUrl +} from '../server/lib/activitypub/url' +import { AccountModel } from '../server/models/account/account' +import { ActorModel } from '../server/models/actor/actor' +import { ActorFollowModel } from '../server/models/actor/actor-follow' import { VideoModel } from '../server/models/video/video' +import { VideoChannelModel } from '../server/models/video/video-channel' +import { VideoCommentModel } from '../server/models/video/video-comment' +import { VideoShareModel } from '../server/models/video/video-share' -initDatabaseModels(true) - .then(() => { - return getServerActor() +run() + .then(() => process.exit(0)) + .catch(err => { + console.error(err) + process.exit(-1) }) - .then(serverAccount => { - return ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined) - }) - .then(res => { - return res.total > 0 - }) - .then(hasFollowing => { + +async function run () { + await initDatabaseModels(true) + + const serverAccount = await getServerActor() + + { + const res = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined) + const hasFollowing = res.total > 0 + if (hasFollowing === true) { - console.log('Cannot update host because you follow other servers!') - process.exit(-1) + throw new Error('Cannot update host because you follow other servers!') } + } + + console.log('Updating actors.') - console.log('Updating torrent files.') - return VideoModel.list() + const actors: ActorModel[] = await ActorModel.unscoped().findAll({ + include: [ + { + model: VideoChannelModel.unscoped(), + required: false + }, + { + model: AccountModel.unscoped(), + required: false + } + ] }) - .then(videos => { - const tasks: Promise[] = [] + for (const actor of actors) { + if (actor.isOwned() === false) continue - videos.forEach(video => { - console.log('Updating video ' + video.uuid) + console.log('Updating actor ' + actor.url) - video.VideoFiles.forEach(file => { - tasks.push(video.createTorrentAndSetInfoHash(file)) - }) - }) + const newUrl = actor.Account + ? getLocalAccountActivityPubUrl(actor.preferredUsername) + : getLocalVideoChannelActivityPubUrl(actor.preferredUsername) - return Promise.all(tasks) + actor.url = newUrl + actor.inboxUrl = newUrl + '/inbox' + actor.outboxUrl = newUrl + '/outbox' + actor.sharedInboxUrl = WEBSERVER.URL + '/inbox' + actor.followersUrl = newUrl + '/followers' + actor.followingUrl = newUrl + '/following' + + await actor.save() + } + + console.log('Updating video shares.') + + const videoShares: VideoShareModel[] = await VideoShareModel.findAll({ + include: [ VideoModel.unscoped(), ActorModel.unscoped() ] }) - .then(() => { - process.exit(0) + for (const videoShare of videoShares) { + if (videoShare.Video.isOwned() === false) continue + + console.log('Updating video share ' + videoShare.url) + + videoShare.url = getLocalVideoAnnounceActivityPubUrl(videoShare.Actor, videoShare.Video) + await videoShare.save() + } + + console.log('Updating video comments.') + const videoComments: VideoCommentModel[] = await VideoCommentModel.findAll({ + include: [ + { + model: VideoModel.unscoped() + }, + { + model: AccountModel.unscoped(), + include: [ + { + model: ActorModel.unscoped() + } + ] + } + ] }) + for (const comment of videoComments) { + if (comment.isOwned() === false) continue + + console.log('Updating comment ' + comment.url) + + comment.url = getLocalVideoCommentActivityPubUrl(comment.Video, comment) + await comment.save() + } + + console.log('Updating video and torrent files.') + + const ids = await VideoModel.listLocalIds() + for (const id of ids) { + const video = await VideoModel.loadFull(id) + + console.log('Updating video ' + video.uuid) + + video.url = getLocalVideoActivityPubUrl(video) + await video.save() + + for (const file of video.VideoFiles) { + console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid) + await updateTorrentMetadata(video, file) + + await file.save() + } + + const playlist = video.getHLSPlaylist() + for (const file of (playlist?.VideoFiles || [])) { + console.log('Updating fragmented torrent file %s of video %s.', file.resolution, video.uuid) + + await updateTorrentMetadata(playlist, file) + + await file.save() + } + } +}