audience.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Transaction } from 'sequelize'
  2. import { ActivityAudience } from '../../../shared/models/activitypub'
  3. import { ACTIVITY_PUB } from '../../initializers'
  4. import { ActorModel } from '../../models/activitypub/actor'
  5. import { VideoModel } from '../../models/video/video'
  6. import { VideoCommentModel } from '../../models/video/video-comment'
  7. import { VideoShareModel } from '../../models/video/video-share'
  8. function getRemoteVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]): ActivityAudience {
  9. return {
  10. to: [ video.VideoChannel.Account.Actor.url ],
  11. cc: actorsInvolvedInVideo.map(a => a.followersUrl)
  12. }
  13. }
  14. function getVideoCommentAudience (
  15. videoComment: VideoCommentModel,
  16. threadParentComments: VideoCommentModel[],
  17. actorsInvolvedInVideo: ActorModel[],
  18. isOrigin = false
  19. ): ActivityAudience {
  20. const to = [ ACTIVITY_PUB.PUBLIC ]
  21. const cc: string[] = []
  22. // Owner of the video we comment
  23. if (isOrigin === false) {
  24. cc.push(videoComment.Video.VideoChannel.Account.Actor.url)
  25. }
  26. // Followers of the poster
  27. cc.push(videoComment.Account.Actor.followersUrl)
  28. // Send to actors we reply to
  29. for (const parentComment of threadParentComments) {
  30. cc.push(parentComment.Account.Actor.url)
  31. }
  32. return {
  33. to,
  34. cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
  35. }
  36. }
  37. function getAudienceFromFollowersOf (actorsInvolvedInObject: ActorModel[]): ActivityAudience {
  38. return {
  39. to: [ ACTIVITY_PUB.PUBLIC ].concat(actorsInvolvedInObject.map(a => a.followersUrl)),
  40. cc: []
  41. }
  42. }
  43. async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
  44. const actors = await VideoShareModel.loadActorsByShare(video.id, t)
  45. const videoActor = video.VideoChannel && video.VideoChannel.Account
  46. ? video.VideoChannel.Account.Actor
  47. : await ActorModel.loadAccountActorByVideoId(video.id, t)
  48. actors.push(videoActor)
  49. return actors
  50. }
  51. function getAudience (actorSender: ActorModel, isPublic = true) {
  52. return buildAudience([ actorSender.followersUrl ], isPublic)
  53. }
  54. function buildAudience (followerUrls: string[], isPublic = true) {
  55. let to: string[] = []
  56. let cc: string[] = []
  57. if (isPublic) {
  58. to = [ ACTIVITY_PUB.PUBLIC ]
  59. cc = followerUrls
  60. } else { // Unlisted
  61. to = []
  62. cc = []
  63. }
  64. return { to, cc }
  65. }
  66. function audiencify<T> (object: T, audience: ActivityAudience) {
  67. return Object.assign(object, audience)
  68. }
  69. // ---------------------------------------------------------------------------
  70. export {
  71. buildAudience,
  72. getAudience,
  73. getRemoteVideoAudience,
  74. getActorsInvolvedInVideo,
  75. getAudienceFromFollowersOf,
  76. audiencify,
  77. getVideoCommentAudience
  78. }