update-host.ts 4.0 KB

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