share.ts 3.9 KB

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