audience-utils.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Transaction } from 'sequelize'
  2. import { ACTIVITY_PUB } from '@server/initializers/constants'
  3. import { ActorModel } from '@server/models/actor/actor'
  4. import { VideoModel } from '@server/models/video/video'
  5. import { VideoShareModel } from '@server/models/video/video-share'
  6. import { MActorFollowersUrl, MActorUrl, MCommentOwner, MCommentOwnerVideo, MVideoId } from '@server/types/models'
  7. import { ActivityAudience } from '@shared/models'
  8. function getOriginVideoAudience (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 = await VideoShareModel.listActorIdsAndFollowerUrlsByShare(video.id, t)
  46. const videoAll = video as VideoModel
  47. const videoActor = videoAll.VideoChannel?.Account
  48. ? videoAll.VideoChannel.Account.Actor
  49. : await ActorModel.loadAccountActorFollowerUrlByVideoId(video.id, t)
  50. actors.push(videoActor)
  51. return actors
  52. }
  53. // ---------------------------------------------------------------------------
  54. export {
  55. getOriginVideoAudience,
  56. getActorsInvolvedInVideo,
  57. getAudienceFromFollowersOf,
  58. getVideoCommentAudience
  59. }