X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Fupdate-host.ts;h=5d81a8d74d5d039694b1e141349a882fc0e495dc;hb=960b29ef6f8e014d32a3de1ff9d62959b5f0a9ca;hp=0f6af094280eba035ef948d8d910f00abe4b82c6;hpb=75d612ce3ca9d6f69fe8e4e83dc3070d9ab56615;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/update-host.ts b/scripts/update-host.ts index 0f6af0942..5d81a8d74 100755 --- a/scripts/update-host.ts +++ b/scripts/update-host.ts @@ -1,37 +1,143 @@ -import { readFileSync, writeFileSync } from 'fs' -import * as parseTorrent from 'parse-torrent' +import { registerTSPaths } from '../server/helpers/register-ts-paths' +registerTSPaths() -import { CONFIG, STATIC_PATHS } from '../server/initializers/constants' -import { database as db } from '../server/initializers/database' -import { hasFriends } from '../server/lib/friends' +import { WEBSERVER } from '../server/initializers/constants' +import { ActorFollowModel } from '../server/models/actor/actor-follow' +import { VideoModel } from '../server/models/video/video' +import { ActorModel } from '../server/models/actor/actor' +import { + getLocalAccountActivityPubUrl, + getLocalVideoActivityPubUrl, + getLocalVideoAnnounceActivityPubUrl, + getLocalVideoChannelActivityPubUrl, + getLocalVideoCommentActivityPubUrl +} from '../server/lib/activitypub/url' +import { VideoShareModel } from '../server/models/video/video-share' +import { VideoCommentModel } from '../server/models/video/video-comment' +import { AccountModel } from '../server/models/account/account' +import { VideoChannelModel } from '../server/models/video/video-channel' +import { initDatabaseModels } from '../server/initializers/database' +import { updateTorrentUrls } from '@server/helpers/webtorrent' +import { getServerActor } from '@server/models/application/application' -db.init(true, function () { - hasFriends(function (err, itHasFriends) { - if (err) throw err +run() + .then(() => process.exit(0)) + .catch(err => { + console.error(err) + process.exit(-1) + }) + +async function run () { + await initDatabaseModels(true) + + const serverAccount = await getServerActor() + + { + const res = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined) + const hasFollowing = res.total > 0 - if (itHasFriends === true) { - console.log('Cannot update host because you have friends!') - process.exit(-1) + if (hasFollowing === true) { + throw new Error('Cannot update host because you follow other servers!') } + } + + console.log('Updating actors.') + + const actors: ActorModel[] = await ActorModel.unscoped().findAll({ + include: [ + { + model: VideoChannelModel.unscoped(), + required: false + }, + { + model: AccountModel.unscoped(), + required: false + } + ] + }) + for (const actor of actors) { + if (actor.isOwned() === false) continue - console.log('Updating torrent files.') - db.Video.list(function (err, videos) { - if (err) throw err + console.log('Updating actor ' + actor.url) - videos.forEach(function (video) { - const torrentName = video.id + '.torrent' - const torrentPath = CONFIG.STORAGE.TORRENTS_DIR + torrentName - const filename = video.id + video.extname + const newUrl = actor.Account + ? getLocalAccountActivityPubUrl(actor.preferredUsername) + : getLocalVideoChannelActivityPubUrl(actor.preferredUsername) - const parsed = parseTorrent(readFileSync(torrentPath)) - parsed.announce = [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOST + '/tracker/socket' ] - parsed.urlList = [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + filename ] + actor.url = newUrl + actor.inboxUrl = newUrl + '/inbox' + actor.outboxUrl = newUrl + '/outbox' + actor.sharedInboxUrl = WEBSERVER.URL + '/inbox' + actor.followersUrl = newUrl + '/followers' + actor.followingUrl = newUrl + '/following' - const buf = parseTorrent.toTorrentFile(parsed) - writeFileSync(torrentPath, buf) - }) + await actor.save() + } - process.exit(0) - }) + console.log('Updating video shares.') + + const videoShares: VideoShareModel[] = await VideoShareModel.findAll({ + include: [ VideoModel.unscoped(), ActorModel.unscoped() ] + }) + 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 localVideos = await VideoModel.listLocal() + for (const localVideo of localVideos) { + const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(localVideo.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 updateTorrentUrls(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 updateTorrentUrls(video, file) + + await file.save() + } + } +}