update-host.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { registerTSPaths } from '../server/helpers/register-ts-paths'
  2. registerTSPaths()
  3. import { WEBSERVER } from '../server/initializers/constants'
  4. import { ActorFollowModel } from '../server/models/actor/actor-follow'
  5. import { VideoModel } from '../server/models/video/video'
  6. import { ActorModel } from '../server/models/actor/actor'
  7. import {
  8. getLocalAccountActivityPubUrl,
  9. getLocalVideoActivityPubUrl,
  10. getLocalVideoAnnounceActivityPubUrl,
  11. getLocalVideoChannelActivityPubUrl,
  12. getLocalVideoCommentActivityPubUrl
  13. } from '../server/lib/activitypub/url'
  14. import { VideoShareModel } from '../server/models/video/video-share'
  15. import { VideoCommentModel } from '../server/models/video/video-comment'
  16. import { AccountModel } from '../server/models/account/account'
  17. import { VideoChannelModel } from '../server/models/video/video-channel'
  18. import { initDatabaseModels } from '../server/initializers/database'
  19. import { updateTorrentUrls } from '@server/helpers/webtorrent'
  20. import { getServerActor } from '@server/models/application/application'
  21. run()
  22. .then(() => process.exit(0))
  23. .catch(err => {
  24. console.error(err)
  25. process.exit(-1)
  26. })
  27. async function run () {
  28. await initDatabaseModels(true)
  29. const serverAccount = await getServerActor()
  30. {
  31. const res = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined)
  32. const hasFollowing = res.total > 0
  33. if (hasFollowing === true) {
  34. throw new Error('Cannot update host because you follow other servers!')
  35. }
  36. }
  37. console.log('Updating actors.')
  38. const actors: ActorModel[] = await ActorModel.unscoped().findAll({
  39. include: [
  40. {
  41. model: VideoChannelModel.unscoped(),
  42. required: false
  43. },
  44. {
  45. model: AccountModel.unscoped(),
  46. required: false
  47. }
  48. ]
  49. })
  50. for (const actor of actors) {
  51. if (actor.isOwned() === false) continue
  52. console.log('Updating actor ' + actor.url)
  53. const newUrl = actor.Account
  54. ? getLocalAccountActivityPubUrl(actor.preferredUsername)
  55. : getLocalVideoChannelActivityPubUrl(actor.preferredUsername)
  56. actor.url = newUrl
  57. actor.inboxUrl = newUrl + '/inbox'
  58. actor.outboxUrl = newUrl + '/outbox'
  59. actor.sharedInboxUrl = WEBSERVER.URL + '/inbox'
  60. actor.followersUrl = newUrl + '/followers'
  61. actor.followingUrl = newUrl + '/following'
  62. await actor.save()
  63. }
  64. console.log('Updating video shares.')
  65. const videoShares: VideoShareModel[] = await VideoShareModel.findAll({
  66. include: [ VideoModel.unscoped(), ActorModel.unscoped() ]
  67. })
  68. for (const videoShare of videoShares) {
  69. if (videoShare.Video.isOwned() === false) continue
  70. console.log('Updating video share ' + videoShare.url)
  71. videoShare.url = getLocalVideoAnnounceActivityPubUrl(videoShare.Actor, videoShare.Video)
  72. await videoShare.save()
  73. }
  74. console.log('Updating video comments.')
  75. const videoComments: VideoCommentModel[] = await VideoCommentModel.findAll({
  76. include: [
  77. {
  78. model: VideoModel.unscoped()
  79. },
  80. {
  81. model: AccountModel.unscoped(),
  82. include: [
  83. {
  84. model: ActorModel.unscoped()
  85. }
  86. ]
  87. }
  88. ]
  89. })
  90. for (const comment of videoComments) {
  91. if (comment.isOwned() === false) continue
  92. console.log('Updating comment ' + comment.url)
  93. comment.url = getLocalVideoCommentActivityPubUrl(comment.Video, comment)
  94. await comment.save()
  95. }
  96. console.log('Updating video and torrent files.')
  97. const localVideos = await VideoModel.listLocal()
  98. for (const localVideo of localVideos) {
  99. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(localVideo.id)
  100. console.log('Updating video ' + video.uuid)
  101. video.url = getLocalVideoActivityPubUrl(video)
  102. await video.save()
  103. for (const file of video.VideoFiles) {
  104. console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid)
  105. await updateTorrentUrls(video, file)
  106. await file.save()
  107. }
  108. const playlist = video.getHLSPlaylist()
  109. for (const file of (playlist?.VideoFiles || [])) {
  110. console.log('Updating fragmented torrent file %s of video %s.', file.resolution, video.uuid)
  111. await updateTorrentUrls(video, file)
  112. await file.save()
  113. }
  114. }
  115. }