audience.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { Transaction } from 'sequelize'
  2. import { ActivityAudience } from '../../../shared/models/activitypub'
  3. import { ACTIVITY_PUB } from '../../initializers/constants'
  4. import { ActorModel } from '../../models/activitypub/actor'
  5. import { VideoModel } from '../../models/video/video'
  6. import { VideoShareModel } from '../../models/video/video-share'
  7. import { MActorFollowersUrl, MActorLight, MActorUrl, MCommentOwner, MCommentOwnerVideo, MVideoId } from '../../types/models'
  8. function getRemoteVideoAudience (accountActor: MActorUrl, actorsInvolvedInVideo: MActorFollowersUrl[]): ActivityAudience {
  9. return {
  10. to: [ accountActor.url ],
  11. cc: actorsInvolvedInVideo.map(a => a.followersUrl)
  12. }
  13. }
  14. function getVideoCommentAudience (
  15. videoComment: MCommentOwnerVideo,
  16. threadParentComments: MCommentOwner[],
  17. actorsInvolvedInVideo: MActorFollowersUrl[],
  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. if (parentComment.isDeleted()) continue
  31. cc.push(parentComment.Account.Actor.url)
  32. }
  33. return {
  34. to,
  35. cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
  36. }
  37. }
  38. function getAudienceFromFollowersOf (actorsInvolvedInObject: MActorFollowersUrl[]): ActivityAudience {
  39. return {
  40. to: [ ACTIVITY_PUB.PUBLIC ].concat(actorsInvolvedInObject.map(a => a.followersUrl)),
  41. cc: []
  42. }
  43. }
  44. async function getActorsInvolvedInVideo (video: MVideoId, t: Transaction) {
  45. const actors: MActorLight[] = await VideoShareModel.loadActorsByShare(video.id, t)
  46. const videoAll = video as VideoModel
  47. const videoActor = videoAll.VideoChannel?.Account
  48. ? videoAll.VideoChannel.Account.Actor
  49. : await ActorModel.loadFromAccountByVideoId(video.id, t)
  50. actors.push(videoActor)
  51. return actors
  52. }
  53. function getAudience (actorSender: MActorFollowersUrl, isPublic = true) {
  54. return buildAudience([ actorSender.followersUrl ], isPublic)
  55. }
  56. function buildAudience (followerUrls: string[], isPublic = true) {
  57. let to: string[] = []
  58. let cc: string[] = []
  59. if (isPublic) {
  60. to = [ ACTIVITY_PUB.PUBLIC ]
  61. cc = followerUrls
  62. } else { // Unlisted
  63. to = []
  64. cc = []
  65. }
  66. return { to, cc }
  67. }
  68. function audiencify<T> (object: T, audience: ActivityAudience) {
  69. return Object.assign(object, audience)
  70. }
  71. // ---------------------------------------------------------------------------
  72. export {
  73. buildAudience,
  74. getAudience,
  75. getRemoteVideoAudience,
  76. getActorsInvolvedInVideo,
  77. getAudienceFromFollowersOf,
  78. audiencify,
  79. getVideoCommentAudience
  80. }