]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/audience.ts
Merge branch 'release/2.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / audience.ts
CommitLineData
e251f170
C
1import { Transaction } from 'sequelize'
2import { ActivityAudience } from '../../../shared/models/activitypub'
74dc3bca 3import { ACTIVITY_PUB } from '../../initializers/constants'
e251f170
C
4import { ActorModel } from '../../models/activitypub/actor'
5import { VideoModel } from '../../models/video/video'
e251f170 6import { VideoShareModel } from '../../models/video/video-share'
2c8776fc 7import { MActorFollowersUrl, MActorLight, MActorUrl, MCommentOwner, MCommentOwnerVideo, MVideoId } from '../../typings/models'
e251f170 8
2c8776fc 9function getRemoteVideoAudience (accountActor: MActorUrl, actorsInvolvedInVideo: MActorFollowersUrl[]): ActivityAudience {
e251f170 10 return {
2c8776fc 11 to: [ accountActor.url ],
e251f170
C
12 cc: actorsInvolvedInVideo.map(a => a.followersUrl)
13 }
14}
15
16function getVideoCommentAudience (
453e83ea
C
17 videoComment: MCommentOwnerVideo,
18 threadParentComments: MCommentOwner[],
19 actorsInvolvedInVideo: MActorFollowersUrl[],
e251f170 20 isOrigin = false
a2377d15 21): ActivityAudience {
e251f170 22 const to = [ ACTIVITY_PUB.PUBLIC ]
c1e791ba 23 const cc: string[] = []
e251f170
C
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) {
6cb55644
C
35 if (parentComment.isDeleted()) continue
36
e251f170
C
37 cc.push(parentComment.Account.Actor.url)
38 }
39
40 return {
41 to,
42 cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
43 }
44}
45
453e83ea 46function getAudienceFromFollowersOf (actorsInvolvedInObject: MActorFollowersUrl[]): ActivityAudience {
e251f170
C
47 return {
48 to: [ ACTIVITY_PUB.PUBLIC ].concat(actorsInvolvedInObject.map(a => a.followersUrl)),
49 cc: []
50 }
51}
52
943e5193 53async function getActorsInvolvedInVideo (video: MVideoId, t: Transaction) {
453e83ea 54 const actors: MActorLight[] = await VideoShareModel.loadActorsByShare(video.id, t)
e5565833 55
453e83ea
C
56 const videoAll = video as VideoModel
57
58 const videoActor = videoAll.VideoChannel && videoAll.VideoChannel.Account
59 ? videoAll.VideoChannel.Account.Actor
60 : await ActorModel.loadFromAccountByVideoId(video.id, t)
e5565833
C
61
62 actors.push(videoActor)
e251f170
C
63
64 return actors
65}
66
453e83ea 67function getAudience (actorSender: MActorFollowersUrl, isPublic = true) {
e251f170
C
68 return buildAudience([ actorSender.followersUrl ], isPublic)
69}
70
e3d5ea4f 71function buildAudience (followerUrls: string[], isPublic = true) {
c1e791ba
RK
72 let to: string[] = []
73 let cc: string[] = []
e251f170
C
74
75 if (isPublic) {
76 to = [ ACTIVITY_PUB.PUBLIC ]
e3d5ea4f 77 cc = followerUrls
e251f170 78 } else { // Unlisted
2186386c
C
79 to = []
80 cc = []
e251f170
C
81 }
82
83 return { to, cc }
84}
85
2186386c 86function audiencify<T> (object: T, audience: ActivityAudience) {
e251f170
C
87 return Object.assign(object, audience)
88}
89
90// ---------------------------------------------------------------------------
91
92export {
93 buildAudience,
94 getAudience,
a2377d15 95 getRemoteVideoAudience,
e251f170 96 getActorsInvolvedInVideo,
a2377d15 97 getAudienceFromFollowersOf,
e251f170
C
98 audiencify,
99 getVideoCommentAudience
100}