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