aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/update-host.ts
blob: c6eb9d5337f33da44b14954ffd5c899213cd0783 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { registerTSPaths } from '../server/helpers/register-ts-paths'
registerTSPaths()

import { WEBSERVER } from '../server/initializers/constants'
import { ActorFollowModel } from '../server/models/actor/actor-follow'
import { VideoModel } from '../server/models/video/video'
import { ActorModel } from '../server/models/actor/actor'
import {
  getLocalAccountActivityPubUrl,
  getLocalVideoActivityPubUrl,
  getLocalVideoAnnounceActivityPubUrl,
  getLocalVideoChannelActivityPubUrl,
  getLocalVideoCommentActivityPubUrl
} from '../server/lib/activitypub/url'
import { VideoShareModel } from '../server/models/video/video-share'
import { VideoCommentModel } from '../server/models/video/video-comment'
import { AccountModel } from '../server/models/account/account'
import { VideoChannelModel } from '../server/models/video/video-channel'
import { initDatabaseModels } from '../server/initializers/database'
import { updateTorrentUrls } from '@server/helpers/webtorrent'
import { getServerActor } from '@server/models/application/application'

run()
  .then(() => process.exit(0))
  .catch(err => {
    console.error(err)
    process.exit(-1)
  })

async function run () {
  await initDatabaseModels(true)

  const serverAccount = await getServerActor()

  {
    const res = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined)
    const hasFollowing = res.total > 0

    if (hasFollowing === true) {
      throw new Error('Cannot update host because you follow other servers!')
    }
  }

  console.log('Updating actors.')

  const actors: ActorModel[] = await ActorModel.unscoped().findAll({
    include: [
      {
        model: VideoChannelModel.unscoped(),
        required: false
      },
      {
        model: AccountModel.unscoped(),
        required: false
      }
    ]
  })
  for (const actor of actors) {
    if (actor.isOwned() === false) continue

    console.log('Updating actor ' + actor.url)

    const newUrl = actor.Account
      ? getLocalAccountActivityPubUrl(actor.preferredUsername)
      : getLocalVideoChannelActivityPubUrl(actor.preferredUsername)

    actor.url = newUrl
    actor.inboxUrl = newUrl + '/inbox'
    actor.outboxUrl = newUrl + '/outbox'
    actor.sharedInboxUrl = WEBSERVER.URL + '/inbox'
    actor.followersUrl = newUrl + '/followers'
    actor.followingUrl = newUrl + '/following'

    await actor.save()
  }

  console.log('Updating video shares.')

  const videoShares: VideoShareModel[] = await VideoShareModel.findAll({
    include: [ VideoModel.unscoped(), ActorModel.unscoped() ]
  })
  for (const videoShare of videoShares) {
    if (videoShare.Video.isOwned() === false) continue

    console.log('Updating video share ' + videoShare.url)

    videoShare.url = getLocalVideoAnnounceActivityPubUrl(videoShare.Actor, videoShare.Video)
    await videoShare.save()
  }

  console.log('Updating video comments.')
  const videoComments: VideoCommentModel[] = await VideoCommentModel.findAll({
    include: [
      {
        model: VideoModel.unscoped()
      },
      {
        model: AccountModel.unscoped(),
        include: [
          {
            model: ActorModel.unscoped()
          }
        ]
      }
    ]
  })
  for (const comment of videoComments) {
    if (comment.isOwned() === false) continue

    console.log('Updating comment ' + comment.url)

    comment.url = getLocalVideoCommentActivityPubUrl(comment.Video, comment)
    await comment.save()
  }

  console.log('Updating video and torrent files.')

  const ids = await VideoModel.listLocalIds()
  for (const id of ids) {
    const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id)

    console.log('Updating video ' + video.uuid)

    video.url = getLocalVideoActivityPubUrl(video)
    await video.save()

    for (const file of video.VideoFiles) {
      console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid)
      await updateTorrentUrls(video, file)

      await file.save()
    }

    const playlist = video.getHLSPlaylist()
    for (const file of (playlist?.VideoFiles || [])) {
      console.log('Updating fragmented torrent file %s of video %s.', file.resolution, video.uuid)

      await updateTorrentUrls(video, file)

      await file.save()
    }
  }
}