share.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { Transaction } from 'sequelize'
  2. import { VideoShareModel } from '../../models/video/video-share'
  3. import { sendUndoAnnounce, sendVideoAnnounce } from './send'
  4. import { getLocalVideoAnnounceActivityPubUrl } from './url'
  5. import * as Bluebird from 'bluebird'
  6. import { doRequest } from '../../helpers/requests'
  7. import { getOrCreateActorAndServerAndModel } from './actor'
  8. import { logger } from '../../helpers/logger'
  9. import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
  10. import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
  11. import { MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../types/models/video'
  12. import { getServerActor } from '@server/models/application/application'
  13. async function shareVideoByServerAndChannel (video: MVideoAccountLight, t: Transaction) {
  14. if (!video.hasPrivacyForFederation()) return undefined
  15. return Promise.all([
  16. shareByServer(video, t),
  17. shareByVideoChannel(video, t)
  18. ])
  19. }
  20. async function changeVideoChannelShare (
  21. video: MVideoAccountLight,
  22. oldVideoChannel: MChannelActorLight,
  23. t: Transaction
  24. ) {
  25. logger.info('Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name)
  26. await undoShareByVideoChannel(video, oldVideoChannel, t)
  27. await shareByVideoChannel(video, t)
  28. }
  29. async function addVideoShares (shareUrls: string[], video: MVideoId) {
  30. await Bluebird.map(shareUrls, async shareUrl => {
  31. try {
  32. // Fetch url
  33. const { body } = await doRequest<any>({
  34. uri: shareUrl,
  35. json: true,
  36. activityPub: true
  37. })
  38. if (!body || !body.actor) throw new Error('Body or body actor is invalid')
  39. const actorUrl = getAPId(body.actor)
  40. if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
  41. throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
  42. }
  43. const actor = await getOrCreateActorAndServerAndModel(actorUrl)
  44. const entry = {
  45. actorId: actor.id,
  46. videoId: video.id,
  47. url: shareUrl
  48. }
  49. await VideoShareModel.upsert(entry)
  50. } catch (err) {
  51. logger.warn('Cannot add share %s.', shareUrl, { err })
  52. }
  53. }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
  54. }
  55. export {
  56. changeVideoChannelShare,
  57. addVideoShares,
  58. shareVideoByServerAndChannel
  59. }
  60. // ---------------------------------------------------------------------------
  61. async function shareByServer (video: MVideo, t: Transaction) {
  62. const serverActor = await getServerActor()
  63. const serverShareUrl = getLocalVideoAnnounceActivityPubUrl(serverActor, video)
  64. const [ serverShare ] = await VideoShareModel.findOrCreate({
  65. defaults: {
  66. actorId: serverActor.id,
  67. videoId: video.id,
  68. url: serverShareUrl
  69. },
  70. where: {
  71. url: serverShareUrl
  72. },
  73. transaction: t
  74. })
  75. return sendVideoAnnounce(serverActor, serverShare, video, t)
  76. }
  77. async function shareByVideoChannel (video: MVideoAccountLight, t: Transaction) {
  78. const videoChannelShareUrl = getLocalVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
  79. const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
  80. defaults: {
  81. actorId: video.VideoChannel.actorId,
  82. videoId: video.id,
  83. url: videoChannelShareUrl
  84. },
  85. where: {
  86. url: videoChannelShareUrl
  87. },
  88. transaction: t
  89. })
  90. return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
  91. }
  92. async function undoShareByVideoChannel (video: MVideo, oldVideoChannel: MChannelActorLight, t: Transaction) {
  93. // Load old share
  94. const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
  95. if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
  96. await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
  97. await oldShare.destroy({ transaction: t })
  98. }