]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/audience.ts
Don't refresh videos when processing views
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / audience.ts
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 {
8 MActorFollowersUrl,
9 MActorLight,
10 MCommentOwner,
11 MCommentOwnerVideo,
12 MVideo,
13 MVideoAccountLight,
14 MVideoId
15 } from '../../typings/models'
16
17 function getRemoteVideoAudience (video: MVideoAccountLight, actorsInvolvedInVideo: MActorFollowersUrl[]): ActivityAudience {
18 return {
19 to: [ video.VideoChannel.Account.Actor.url ],
20 cc: actorsInvolvedInVideo.map(a => a.followersUrl)
21 }
22 }
23
24 function getVideoCommentAudience (
25 videoComment: MCommentOwnerVideo,
26 threadParentComments: MCommentOwner[],
27 actorsInvolvedInVideo: MActorFollowersUrl[],
28 isOrigin = false
29 ): ActivityAudience {
30 const to = [ ACTIVITY_PUB.PUBLIC ]
31 const cc: string[] = []
32
33 // Owner of the video we comment
34 if (isOrigin === false) {
35 cc.push(videoComment.Video.VideoChannel.Account.Actor.url)
36 }
37
38 // Followers of the poster
39 cc.push(videoComment.Account.Actor.followersUrl)
40
41 // Send to actors we reply to
42 for (const parentComment of threadParentComments) {
43 cc.push(parentComment.Account.Actor.url)
44 }
45
46 return {
47 to,
48 cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
49 }
50 }
51
52 function getAudienceFromFollowersOf (actorsInvolvedInObject: MActorFollowersUrl[]): ActivityAudience {
53 return {
54 to: [ ACTIVITY_PUB.PUBLIC ].concat(actorsInvolvedInObject.map(a => a.followersUrl)),
55 cc: []
56 }
57 }
58
59 async function getActorsInvolvedInVideo (video: MVideoId, t: Transaction) {
60 const actors: MActorLight[] = await VideoShareModel.loadActorsByShare(video.id, t)
61
62 const videoAll = video as VideoModel
63
64 const videoActor = videoAll.VideoChannel && videoAll.VideoChannel.Account
65 ? videoAll.VideoChannel.Account.Actor
66 : await ActorModel.loadFromAccountByVideoId(video.id, t)
67
68 actors.push(videoActor)
69
70 return actors
71 }
72
73 function getAudience (actorSender: MActorFollowersUrl, isPublic = true) {
74 return buildAudience([ actorSender.followersUrl ], isPublic)
75 }
76
77 function buildAudience (followerUrls: string[], isPublic = true) {
78 let to: string[] = []
79 let cc: string[] = []
80
81 if (isPublic) {
82 to = [ ACTIVITY_PUB.PUBLIC ]
83 cc = followerUrls
84 } else { // Unlisted
85 to = []
86 cc = []
87 }
88
89 return { to, cc }
90 }
91
92 function audiencify<T> (object: T, audience: ActivityAudience) {
93 return Object.assign(object, audience)
94 }
95
96 // ---------------------------------------------------------------------------
97
98 export {
99 buildAudience,
100 getAudience,
101 getRemoteVideoAudience,
102 getActorsInvolvedInVideo,
103 getAudienceFromFollowersOf,
104 audiencify,
105 getVideoCommentAudience
106 }