Merge from upstream
[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
17 run()
18   .then(() => process.exit(0))
19   .catch(err => {
20     console.error(err)
21     process.exit(-1)
22   })
23
24 async 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
33     if (hasFollowing === true) {
34       throw new Error('Cannot update host because you follow other servers!')
35     }
36   }
37
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
49       }
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() ]
75   })
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 = getVideoAnnounceActivityPubUrl(videoShare.Actor, videoShare.Video)
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     ]
100   })
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 }