update-host.ts 3.6 KB

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