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