]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/update-host.ts
66c0137d926c357f23876b4ff00acbc8a7c614d7
[github/Chocobozzz/PeerTube.git] / scripts / update-host.ts
1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
2 registerTSPaths()
3
4 import { WEBSERVER } from '../server/initializers/constants'
5 import { ActorFollowModel } from '../server/models/actor/actor-follow'
6 import { VideoModel } from '../server/models/video/video'
7 import { ActorModel } from '../server/models/actor/actor'
8 import {
9 getLocalAccountActivityPubUrl,
10 getLocalVideoActivityPubUrl,
11 getLocalVideoAnnounceActivityPubUrl,
12 getLocalVideoChannelActivityPubUrl,
13 getLocalVideoCommentActivityPubUrl
14 } from '../server/lib/activitypub/url'
15 import { VideoShareModel } from '../server/models/video/video-share'
16 import { VideoCommentModel } from '../server/models/video/video-comment'
17 import { AccountModel } from '../server/models/account/account'
18 import { VideoChannelModel } from '../server/models/video/video-channel'
19 import { initDatabaseModels } from '../server/initializers/database'
20 import { updateTorrentMetadata } from '@server/helpers/webtorrent'
21 import { getServerActor } from '@server/models/application/application'
22
23 run()
24 .then(() => process.exit(0))
25 .catch(err => {
26 console.error(err)
27 process.exit(-1)
28 })
29
30 async function run () {
31 await initDatabaseModels(true)
32
33 const serverAccount = await getServerActor()
34
35 {
36 const res = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined)
37 const hasFollowing = res.total > 0
38
39 if (hasFollowing === true) {
40 throw new Error('Cannot update host because you follow other servers!')
41 }
42 }
43
44 console.log('Updating actors.')
45
46 const actors: ActorModel[] = await ActorModel.unscoped().findAll({
47 include: [
48 {
49 model: VideoChannelModel.unscoped(),
50 required: false
51 },
52 {
53 model: AccountModel.unscoped(),
54 required: false
55 }
56 ]
57 })
58 for (const actor of actors) {
59 if (actor.isOwned() === false) continue
60
61 console.log('Updating actor ' + actor.url)
62
63 const newUrl = actor.Account
64 ? getLocalAccountActivityPubUrl(actor.preferredUsername)
65 : getLocalVideoChannelActivityPubUrl(actor.preferredUsername)
66
67 actor.url = newUrl
68 actor.inboxUrl = newUrl + '/inbox'
69 actor.outboxUrl = newUrl + '/outbox'
70 actor.sharedInboxUrl = WEBSERVER.URL + '/inbox'
71 actor.followersUrl = newUrl + '/followers'
72 actor.followingUrl = newUrl + '/following'
73
74 await actor.save()
75 }
76
77 console.log('Updating video shares.')
78
79 const videoShares: VideoShareModel[] = await VideoShareModel.findAll({
80 include: [ VideoModel.unscoped(), ActorModel.unscoped() ]
81 })
82 for (const videoShare of videoShares) {
83 if (videoShare.Video.isOwned() === false) continue
84
85 console.log('Updating video share ' + videoShare.url)
86
87 videoShare.url = getLocalVideoAnnounceActivityPubUrl(videoShare.Actor, videoShare.Video)
88 await videoShare.save()
89 }
90
91 console.log('Updating video comments.')
92 const videoComments: VideoCommentModel[] = await VideoCommentModel.findAll({
93 include: [
94 {
95 model: VideoModel.unscoped()
96 },
97 {
98 model: AccountModel.unscoped(),
99 include: [
100 {
101 model: ActorModel.unscoped()
102 }
103 ]
104 }
105 ]
106 })
107 for (const comment of videoComments) {
108 if (comment.isOwned() === false) continue
109
110 console.log('Updating comment ' + comment.url)
111
112 comment.url = getLocalVideoCommentActivityPubUrl(comment.Video, comment)
113 await comment.save()
114 }
115
116 console.log('Updating video and torrent files.')
117
118 const ids = await VideoModel.listLocalIds()
119 for (const id of ids) {
120 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id)
121
122 console.log('Updating video ' + video.uuid)
123
124 video.url = getLocalVideoActivityPubUrl(video)
125 await video.save()
126
127 for (const file of video.VideoFiles) {
128 console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid)
129 await updateTorrentMetadata(video, file)
130
131 await file.save()
132 }
133
134 const playlist = video.getHLSPlaylist()
135 for (const file of (playlist?.VideoFiles || [])) {
136 console.log('Updating fragmented torrent file %s of video %s.', file.resolution, video.uuid)
137
138 await updateTorrentMetadata(playlist, file)
139
140 await file.save()
141 }
142 }
143 }