X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Fupdate-host.ts;h=1dc19664d66ae6900929bfc968eb8f81cf5fce52;hb=a8bf1d826e379dec03d25840c7c49c1f30168380;hp=23e8d5ef35ca7234ba723b044885be4cfa551e13;hpb=6fcd19ba737f1f5614a56c6925adb882dea43b8d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/update-host.ts b/scripts/update-host.ts index 23e8d5ef3..1dc19664d 100755 --- a/scripts/update-host.ts +++ b/scripts/update-host.ts @@ -1,36 +1,126 @@ -import { readFileSync, writeFileSync } from 'fs' -import * as parseTorrent from 'parse-torrent' +import { CONFIG, initDatabaseModels } from '../server/initializers' +import { ActorFollowModel } from '../server/models/activitypub/actor-follow' +import { VideoModel } from '../server/models/video/video' +import { ActorModel } from '../server/models/activitypub/actor' +import { + getAccountActivityPubUrl, + getAnnounceActivityPubUrl, + getVideoActivityPubUrl, 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 { CONFIG, STATIC_PATHS } from '../server/initializers/constants' -import { database as db } from '../server/initializers/database' -import { hasFriends } from '../server/lib/friends' - -db.init(true) - .then(() => { - return hasFriends() +run() + .then(() => process.exit(0)) + .catch(err => { + console.error(err) + process.exit(-1) }) - .then(itHasFriends => { - if (itHasFriends === true) { - console.log('Cannot update host because you have friends!') - 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 (hasFollowing === true) { + 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 => { - videos.forEach(function (video) { - const torrentName = video.id + '.torrent' - const torrentPath = CONFIG.STORAGE.TORRENTS_DIR + torrentName - const filename = video.id + video.extname + for (const actor of actors) { + if (actor.isOwned() === false) continue - const parsed = parseTorrent(readFileSync(torrentPath)) - parsed.announce = [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOST + '/tracker/socket' ] - parsed.urlList = [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + filename ] + console.log('Updating actor ' + actor.url) - const buf = parseTorrent.toTorrentFile(parsed) - writeFileSync(torrentPath, buf) - }) + const newUrl = actor.Account + ? getAccountActivityPubUrl(actor.preferredUsername) + : getVideoChannelActivityPubUrl(actor.preferredUsername) - process.exit(0) + actor.url = newUrl + actor.inboxUrl = newUrl + '/inbox' + actor.outboxUrl = newUrl + '/outbox' + actor.sharedInboxUrl = CONFIG.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() ] }) + for (const videoShare of videoShares) { + if (videoShare.Video.isOwned() === false) continue + + console.log('Updating video share ' + videoShare.url) + + videoShare.url = getAnnounceActivityPubUrl(videoShare.Video.url, videoShare.Actor) + 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.list() + for (const video of videos) { + if (video.isOwned() === false) continue + + console.log('Updated 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) + } + } +}