audience.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, MCommentOwner, MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../../typings/models'
  8. function getRemoteVideoAudience (video: MVideoAccountLight, actorsInvolvedInVideo: MActorFollowersUrl[]): ActivityAudience {
  9. return {
  10. to: [ video.VideoChannel.Account.Actor.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. 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: MActorFollowersUrl[]): ActivityAudience {
  38. return {
  39. to: [ ACTIVITY_PUB.PUBLIC ].concat(actorsInvolvedInObject.map(a => a.followersUrl)),
  40. cc: []
  41. }
  42. }
  43. async function getActorsInvolvedInVideo (video: MVideo, t: Transaction) {
  44. const actors: MActorLight[] = await VideoShareModel.loadActorsByShare(video.id, t)
  45. const videoAll = video as VideoModel
  46. const videoActor = videoAll.VideoChannel && videoAll.VideoChannel.Account
  47. ? videoAll.VideoChannel.Account.Actor
  48. : await ActorModel.loadFromAccountByVideoId(video.id, t)
  49. actors.push(videoActor)
  50. return actors
  51. }
  52. function getAudience (actorSender: MActorFollowersUrl, isPublic = true) {
  53. return buildAudience([ actorSender.followersUrl ], isPublic)
  54. }
  55. function buildAudience (followerUrls: string[], isPublic = true) {
  56. let to: string[] = []
  57. let cc: string[] = []
  58. if (isPublic) {
  59. to = [ ACTIVITY_PUB.PUBLIC ]
  60. cc = followerUrls
  61. } else { // Unlisted
  62. to = []
  63. cc = []
  64. }
  65. return { to, cc }
  66. }
  67. function audiencify<T> (object: T, audience: ActivityAudience) {
  68. return Object.assign(object, audience)
  69. }
  70. // ---------------------------------------------------------------------------
  71. export {
  72. buildAudience,
  73. getAudience,
  74. getRemoteVideoAudience,
  75. getActorsInvolvedInVideo,
  76. getAudienceFromFollowersOf,
  77. audiencify,
  78. getVideoCommentAudience
  79. }