PeerTube/scripts/update-host.ts

141 lines
4.1 KiB
TypeScript
Raw Normal View History

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 {
2020-11-20 04:21:08 -06:00
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'
run()
.then(() => process.exit(0))
.catch(err => {
console.error(err)
process.exit(-1)
2017-11-17 09:24:08 -06:00
})
async function run () {
await initDatabaseModels(true)
const serverAccount = await getServerActor()
{
const res = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined)
const hasFollowing = res.total > 0
2017-11-17 09:24:08 -06:00
if (hasFollowing === true) {
throw new Error('Cannot update host because you follow other servers!')
2017-06-12 14:06:32 -05:00
}
}
2017-06-12 14:06:32 -05:00
console.log('Updating actors.')
const actors: ActorModel[] = await ActorModel.unscoped().findAll({
include: [
{
model: VideoChannelModel.unscoped(),
required: false
},
{
model: AccountModel.unscoped(),
required: false
2018-03-20 02:54:24 -05:00
}
]
})
for (const actor of actors) {
if (actor.isOwned() === false) continue
console.log('Updating actor ' + actor.url)
const newUrl = actor.Account
2020-11-20 04:21:08 -06:00
? getLocalAccountActivityPubUrl(actor.preferredUsername)
: getLocalVideoChannelActivityPubUrl(actor.preferredUsername)
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() ]
2017-09-07 08:27:35 -05:00
})
for (const videoShare of videoShares) {
if (videoShare.Video.isOwned() === false) continue
console.log('Updating video share ' + videoShare.url)
2020-11-20 04:21:08 -06:00
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()
}
]
}
]
2017-06-12 14:06:32 -05:00
})
for (const comment of videoComments) {
if (comment.isOwned() === false) continue
console.log('Updating comment ' + comment.url)
2020-11-20 04:21:08 -06:00
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) {
2022-06-28 07:57:51 -05:00
const video = await VideoModel.loadFull(id)
2019-01-29 01:37:25 -06:00
console.log('Updating video ' + video.uuid)
2020-11-20 04:21:08 -06:00
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)
2021-07-23 04:20:00 -05:00
await file.save()
}
2019-01-29 01:37:25 -06:00
2021-07-23 04:20:00 -05:00
const playlist = video.getHLSPlaylist()
for (const file of (playlist?.VideoFiles || [])) {
console.log('Updating fragmented torrent file %s of video %s.', file.resolution, video.uuid)
2021-12-09 04:23:02 -06:00
await updateTorrentMetadata(playlist, file)
2019-01-29 01:37:25 -06:00
2021-07-23 04:20:00 -05:00
await file.save()
2019-01-29 01:37:25 -06:00
}
}
}