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