update-host.ts 4.3 KB

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