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