]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-create.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
3 import { VideoPrivacy } from '../../../../shared/models/videos'
4 import { VideoCommentModel } from '../../../models/video/video-comment'
5 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
6 import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
7 import { logger } from '../../../helpers/logger'
8 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
9 import { getServerActor } from '../../../helpers/utils'
10 import {
11 MActorLight,
12 MCommentOwnerVideo,
13 MVideoAccountLight,
14 MVideoAP,
15 MVideoPlaylistFull,
16 MVideoRedundancyFileVideo,
17 MVideoRedundancyStreamingPlaylistVideo
18 } from '../../../typings/models'
19 import { ContextType } from '@server/helpers/activitypub'
20
21 async function sendCreateVideo (video: MVideoAP, t: Transaction) {
22 if (!video.hasPrivacyForFederation()) return undefined
23
24 logger.info('Creating job to send video creation of %s.', video.url)
25
26 const byActor = video.VideoChannel.Account.Actor
27 const videoObject = video.toActivityPubObject()
28
29 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
30 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
31
32 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
33 }
34
35 async function sendCreateCacheFile (
36 byActor: MActorLight,
37 video: MVideoAccountLight,
38 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
39 ) {
40 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
41
42 return sendVideoRelatedCreateActivity({
43 byActor,
44 video,
45 url: fileRedundancy.url,
46 object: fileRedundancy.toActivityPubObject(),
47 contextType: 'CacheFile'
48 })
49 }
50
51 async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transaction) {
52 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
53
54 logger.info('Creating job to send create video playlist of %s.', playlist.url)
55
56 const byActor = playlist.OwnerAccount.Actor
57 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
58
59 const object = await playlist.toActivityPubObject(null, t)
60 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
61
62 const serverActor = await getServerActor()
63 const toFollowersOf = [ byActor, serverActor ]
64
65 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
66
67 return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
68 }
69
70 async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transaction) {
71 logger.info('Creating job to send comment %s.', comment.url)
72
73 const isOrigin = comment.Video.isOwned()
74
75 const byActor = comment.Account.Actor
76 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
77 const commentObject = comment.toActivityPubObject(threadParentComments)
78
79 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
80 // Add the actor that commented too
81 actorsInvolvedInComment.push(byActor)
82
83 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
84
85 let audience: ActivityAudience
86 if (isOrigin) {
87 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
88 } else {
89 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
90 }
91
92 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
93
94 // This was a reply, send it to the parent actors
95 const actorsException = [ byActor ]
96 await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException)
97
98 // Broadcast to our followers
99 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
100
101 // Send to actors involved in the comment
102 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
103
104 // Send to origin
105 t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.getSharedInbox()))
106 }
107
108 function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
109 if (!audience) audience = getAudience(byActor)
110
111 return audiencify(
112 {
113 type: 'Create' as 'Create',
114 id: url + '/activity',
115 actor: byActor.url,
116 object: audiencify(object, audience)
117 },
118 audience
119 )
120 }
121
122 // ---------------------------------------------------------------------------
123
124 export {
125 sendCreateVideo,
126 buildCreateActivity,
127 sendCreateVideoComment,
128 sendCreateVideoPlaylist,
129 sendCreateCacheFile
130 }
131
132 // ---------------------------------------------------------------------------
133
134 async function sendVideoRelatedCreateActivity (options: {
135 byActor: MActorLight
136 video: MVideoAccountLight
137 url: string
138 object: any
139 transaction?: Transaction
140 contextType?: ContextType
141 }) {
142 const activityBuilder = (audience: ActivityAudience) => {
143 return buildCreateActivity(options.url, options.byActor, options.object, audience)
144 }
145
146 return sendVideoRelatedActivity(activityBuilder, options)
147 }