X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Fupdate-host.ts;h=a946d2e4229523a4f5278d6999ef8e8500588a98;hb=a42294c5edbdea3075c8defc03ba2cb73e0d86fc;hp=4410e1ff7e67110fad1830c6a7526bb7f320c1a8;hpb=eb8b27c93e61a896a08923dc1ca3c87ba8cf4948;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/update-host.ts b/scripts/update-host.ts index 4410e1ff7..a946d2e42 100755 --- a/scripts/update-host.ts +++ b/scripts/update-host.ts @@ -1,38 +1,137 @@ -import { database as db } from '../server/initializers/database' -import { getServerAccount } from '../server/helpers/utils' +import { registerTSPaths } from '../server/helpers/register-ts-paths' +registerTSPaths() -db.init(true) - .then(() => { - return getServerAccount() - }) - .then(serverAccount => { - return db.AccountFollow.listAcceptedFollowingUrlsForApi([ serverAccount.id ]) - }) - .then(res => { - return res.total > 0 +import { WEBSERVER } from '../server/initializers/constants' +import { ActorFollowModel } from '../server/models/activitypub/actor-follow' +import { VideoModel } from '../server/models/video/video' +import { ActorModel } from '../server/models/activitypub/actor' +import { + getAccountActivityPubUrl, + getVideoActivityPubUrl, + getVideoAnnounceActivityPubUrl, + getVideoChannelActivityPubUrl, + getVideoCommentActivityPubUrl +} from '../server/lib/activitypub' +import { VideoShareModel } from '../server/models/video/video-share' +import { VideoCommentModel } from '../server/models/video/video-comment' +import { getServerActor } from '../server/helpers/utils' +import { AccountModel } from '../server/models/account/account' +import { VideoChannelModel } from '../server/models/video/video-channel' +import { VideoStreamingPlaylistModel } from '../server/models/video/video-streaming-playlist' +import { initDatabaseModels } from '../server/initializers' + +run() + .then(() => process.exit(0)) + .catch(err => { + console.error(err) + process.exit(-1) }) - .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 db.Video.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 + + console.log('Updating actor ' + actor.url) - videos.forEach(video => { - console.log('Updating video ' + video.uuid) + const newUrl = actor.Account + ? getAccountActivityPubUrl(actor.preferredUsername) + : getVideoChannelActivityPubUrl(actor.preferredUsername) - video.VideoFiles.forEach(file => { - tasks.push(video.createTorrentAndSetInfoHash(file)) - }) - }) + actor.url = newUrl + actor.inboxUrl = newUrl + '/inbox' + actor.outboxUrl = newUrl + '/outbox' + actor.sharedInboxUrl = WEBSERVER.URL + '/inbox' + actor.followersUrl = newUrl + '/followers' + actor.followingUrl = newUrl + '/following' - return Promise.all(tasks) + 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 = getVideoAnnounceActivityPubUrl(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 = getVideoCommentActivityPubUrl(comment.Video, comment) + await comment.save() + } + + console.log('Updating video and torrent files.') + + const videos = await VideoModel.listLocal() + for (const video of videos) { + console.log('Updating video ' + video.uuid) + + video.url = getVideoActivityPubUrl(video) + await video.save() + + for (const file of video.VideoFiles) { + console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid) + await video.createTorrentAndSetInfoHash(file) + } + + for (const playlist of video.VideoStreamingPlaylists) { + playlist.playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) + playlist.segmentsSha256Url = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid) + + await playlist.save() + } + } +}