]>
Commit | Line | Data |
---|---|---|
e251f170 C |
1 | import { Transaction } from 'sequelize' |
2 | import { ActivityAudience } from '../../../shared/models/activitypub' | |
74dc3bca | 3 | import { ACTIVITY_PUB } from '../../initializers/constants' |
e251f170 C |
4 | import { ActorModel } from '../../models/activitypub/actor' |
5 | import { VideoModel } from '../../models/video/video' | |
e251f170 | 6 | import { VideoShareModel } from '../../models/video/video-share' |
453e83ea | 7 | import { MActorFollowersUrl, MActorLight, MCommentOwner, MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../../typings/models' |
e251f170 | 8 | |
453e83ea | 9 | function getRemoteVideoAudience (video: MVideoAccountLight, actorsInvolvedInVideo: MActorFollowersUrl[]): ActivityAudience { |
e251f170 C |
10 | return { |
11 | to: [ video.VideoChannel.Account.Actor.url ], | |
12 | cc: actorsInvolvedInVideo.map(a => a.followersUrl) | |
13 | } | |
14 | } | |
15 | ||
16 | function 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) { | |
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 | ||
453e83ea | 44 | function getAudienceFromFollowersOf (actorsInvolvedInObject: MActorFollowersUrl[]): ActivityAudience { |
e251f170 C |
45 | return { |
46 | to: [ ACTIVITY_PUB.PUBLIC ].concat(actorsInvolvedInObject.map(a => a.followersUrl)), | |
47 | cc: [] | |
48 | } | |
49 | } | |
50 | ||
453e83ea C |
51 | async function getActorsInvolvedInVideo (video: MVideo, t: Transaction) { |
52 | const actors: MActorLight[] = await VideoShareModel.loadActorsByShare(video.id, t) | |
e5565833 | 53 | |
453e83ea C |
54 | const videoAll = video as VideoModel |
55 | ||
56 | const videoActor = videoAll.VideoChannel && videoAll.VideoChannel.Account | |
57 | ? videoAll.VideoChannel.Account.Actor | |
58 | : await ActorModel.loadFromAccountByVideoId(video.id, t) | |
e5565833 C |
59 | |
60 | actors.push(videoActor) | |
e251f170 C |
61 | |
62 | return actors | |
63 | } | |
64 | ||
453e83ea | 65 | function getAudience (actorSender: MActorFollowersUrl, isPublic = true) { |
e251f170 C |
66 | return buildAudience([ actorSender.followersUrl ], isPublic) |
67 | } | |
68 | ||
e3d5ea4f | 69 | function buildAudience (followerUrls: string[], isPublic = true) { |
c1e791ba RK |
70 | let to: string[] = [] |
71 | let cc: string[] = [] | |
e251f170 C |
72 | |
73 | if (isPublic) { | |
74 | to = [ ACTIVITY_PUB.PUBLIC ] | |
e3d5ea4f | 75 | cc = followerUrls |
e251f170 | 76 | } else { // Unlisted |
2186386c C |
77 | to = [] |
78 | cc = [] | |
e251f170 C |
79 | } |
80 | ||
81 | return { to, cc } | |
82 | } | |
83 | ||
2186386c | 84 | function audiencify<T> (object: T, audience: ActivityAudience) { |
e251f170 C |
85 | return Object.assign(object, audience) |
86 | } | |
87 | ||
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | export { | |
91 | buildAudience, | |
92 | getAudience, | |
a2377d15 | 93 | getRemoteVideoAudience, |
e251f170 | 94 | getActorsInvolvedInVideo, |
a2377d15 | 95 | getAudienceFromFollowersOf, |
e251f170 C |
96 | audiencify, |
97 | getVideoCommentAudience | |
98 | } |