Use typescript paths in cli scripts too
[oweals/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/activitypub/actor-follow'
6 import { VideoModel } from '../server/models/video/video'
7 import { ActorModel } from '../server/models/activitypub/actor'
8 import {
9   getAccountActivityPubUrl,
10   getVideoActivityPubUrl,
11   getVideoAnnounceActivityPubUrl,
12   getVideoChannelActivityPubUrl,
13   getVideoCommentActivityPubUrl
14 } from '../server/lib/activitypub'
15 import { VideoShareModel } from '../server/models/video/video-share'
16 import { VideoCommentModel } from '../server/models/video/video-comment'
17 import { getServerActor } from '../server/helpers/utils'
18 import { AccountModel } from '../server/models/account/account'
19 import { VideoChannelModel } from '../server/models/video/video-channel'
20 import { VideoStreamingPlaylistModel } from '../server/models/video/video-streaming-playlist'
21 import { initDatabaseModels } from '../server/initializers'
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       ? getAccountActivityPubUrl(actor.preferredUsername)
65       : getVideoChannelActivityPubUrl(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 = getVideoAnnounceActivityPubUrl(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 = getVideoCommentActivityPubUrl(comment.Video, comment)
113     await comment.save()
114   }
115
116   console.log('Updating video and torrent files.')
117
118   const videos = await VideoModel.listLocal()
119   for (const video of videos) {
120     console.log('Updating video ' + video.uuid)
121
122     video.url = getVideoActivityPubUrl(video)
123     await video.save()
124
125     for (const file of video.VideoFiles) {
126       console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid)
127       await video.createTorrentAndSetInfoHash(file)
128     }
129
130     for (const playlist of video.VideoStreamingPlaylists) {
131       playlist.playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
132       playlist.segmentsSha256Url = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid)
133
134       await playlist.save()
135     }
136   }
137 }