update-host.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { updateTorrentMetadata } from '@server/helpers/webtorrent.js'
  2. import { getServerActor } from '@server/models/application/application.js'
  3. import { WEBSERVER } from '@server/initializers/constants.js'
  4. import { initDatabaseModels } from '@server/initializers/database.js'
  5. import {
  6. getLocalAccountActivityPubUrl,
  7. getLocalVideoActivityPubUrl,
  8. getLocalVideoAnnounceActivityPubUrl,
  9. getLocalVideoChannelActivityPubUrl,
  10. getLocalVideoCommentActivityPubUrl,
  11. getLocalVideoPlaylistActivityPubUrl,
  12. getLocalVideoPlaylistElementActivityPubUrl
  13. } from '@server/lib/activitypub/url.js'
  14. import { AccountModel } from '@server/models/account/account.js'
  15. import { ActorFollowModel } from '@server/models/actor/actor-follow.js'
  16. import { ActorModel } from '@server/models/actor/actor.js'
  17. import { VideoChannelModel } from '@server/models/video/video-channel.js'
  18. import { VideoCommentModel } from '@server/models/video/video-comment.js'
  19. import { VideoShareModel } from '@server/models/video/video-share.js'
  20. import { VideoModel } from '@server/models/video/video.js'
  21. import { MActorAccount } from '@server/types/models/index.js'
  22. import { VideoPlaylistModel } from '@server/models/video/video-playlist.js'
  23. import { VideoPlaylistElementModel } from '@server/models/video/video-playlist-element.js'
  24. run()
  25. .then(() => process.exit(0))
  26. .catch(err => {
  27. console.error(err)
  28. process.exit(-1)
  29. })
  30. async function run () {
  31. await initDatabaseModels(true)
  32. const serverAccount = await getServerActor()
  33. {
  34. const res = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined, 0, 1)
  35. const hasFollowing = res.total > 0
  36. if (hasFollowing === true) {
  37. throw new Error('Cannot update host because you follow other servers!')
  38. }
  39. }
  40. console.log('Updating actors.')
  41. const actors: MActorAccount[] = await ActorModel.unscoped().findAll({
  42. where: {
  43. serverId: null
  44. },
  45. include: [
  46. {
  47. model: VideoChannelModel.unscoped(),
  48. required: false
  49. },
  50. {
  51. model: AccountModel.unscoped(),
  52. required: false
  53. }
  54. ]
  55. })
  56. for (const actor of actors) {
  57. console.log('Updating actor ' + actor.url)
  58. const newUrl = actor.Account
  59. ? getLocalAccountActivityPubUrl(actor.preferredUsername)
  60. : getLocalVideoChannelActivityPubUrl(actor.preferredUsername)
  61. actor.url = newUrl
  62. actor.inboxUrl = newUrl + '/inbox'
  63. actor.outboxUrl = newUrl + '/outbox'
  64. actor.sharedInboxUrl = WEBSERVER.URL + '/inbox'
  65. actor.followersUrl = newUrl + '/followers'
  66. actor.followingUrl = newUrl + '/following'
  67. await actor.save()
  68. }
  69. console.log('Updating video shares.')
  70. const videoShares: VideoShareModel[] = await VideoShareModel.findAll({
  71. include: [
  72. {
  73. model: VideoModel.unscoped(),
  74. where: {
  75. remote: false
  76. },
  77. required: true
  78. },
  79. ActorModel.unscoped()
  80. ]
  81. })
  82. for (const videoShare of videoShares) {
  83. console.log('Updating video share ' + videoShare.url)
  84. videoShare.url = getLocalVideoAnnounceActivityPubUrl(videoShare.Actor, videoShare.Video)
  85. await videoShare.save()
  86. }
  87. console.log('Updating video comments.')
  88. const videoComments: VideoCommentModel[] = await VideoCommentModel.findAll({
  89. include: [
  90. {
  91. model: VideoModel.unscoped()
  92. },
  93. {
  94. model: AccountModel.unscoped(),
  95. required: true,
  96. include: [
  97. {
  98. model: ActorModel.unscoped(),
  99. where: {
  100. serverId: null
  101. },
  102. required: true
  103. }
  104. ]
  105. }
  106. ]
  107. })
  108. for (const comment of videoComments) {
  109. console.log('Updating comment ' + comment.url)
  110. comment.url = getLocalVideoCommentActivityPubUrl(comment.Video, comment)
  111. await comment.save()
  112. }
  113. console.log('Updating video playlists.')
  114. const videoPlaylists: VideoPlaylistModel[] = await VideoPlaylistModel.findAll({
  115. include: [
  116. {
  117. model: AccountModel.unscoped(),
  118. required: true,
  119. include: [
  120. {
  121. model: ActorModel.unscoped(),
  122. where: {
  123. serverId: null
  124. },
  125. required: true
  126. }
  127. ]
  128. }
  129. ]
  130. })
  131. for (const playlist of videoPlaylists) {
  132. console.log('Updating video playlist ' + playlist.url)
  133. playlist.url = getLocalVideoPlaylistActivityPubUrl(playlist)
  134. await playlist.save()
  135. const elements: VideoPlaylistElementModel[] = await VideoPlaylistElementModel.findAll({
  136. where: {
  137. videoPlaylistId: playlist.id
  138. }
  139. })
  140. for (const element of elements) {
  141. console.log('Updating video playlist element ' + element.url)
  142. element.url = getLocalVideoPlaylistElementActivityPubUrl(playlist, element)
  143. await element.save()
  144. }
  145. }
  146. console.log('Updating video and torrent files.')
  147. const ids = await VideoModel.listLocalIds()
  148. for (const id of ids) {
  149. const video = await VideoModel.loadFull(id)
  150. console.log('Updating video ' + video.uuid)
  151. video.url = getLocalVideoActivityPubUrl(video)
  152. await video.save()
  153. for (const file of video.VideoFiles) {
  154. console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid)
  155. await updateTorrentMetadata(video, file)
  156. await file.save()
  157. }
  158. const playlist = video.getHLSPlaylist()
  159. for (const file of (playlist?.VideoFiles || [])) {
  160. console.log('Updating fragmented torrent file %s of video %s.', file.resolution, video.uuid)
  161. await updateTorrentMetadata(playlist, file)
  162. await file.save()
  163. }
  164. }
  165. }