]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize' | |
2 | import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub' | |
3 | import { VideoPrivacy } from '../../../../shared/models/videos' | |
4 | import { getServerActor } from '../../../helpers' | |
5 | import { ActorModel } from '../../../models/activitypub/actor' | |
6 | import { VideoModel } from '../../../models/video/video' | |
7 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | |
8 | import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url' | |
9 | import { | |
10 | broadcastToFollowers, | |
11 | getActorsInvolvedInVideo, | |
12 | getAudience, | |
13 | getObjectFollowersAudience, | |
14 | getOriginVideoAudience, | |
15 | unicastTo | |
16 | } from './misc' | |
17 | ||
18 | async function sendCreateVideo (video: VideoModel, t: Transaction) { | |
19 | const byActor = video.VideoChannel.Account.Actor | |
20 | ||
21 | const videoObject = video.toActivityPubObject() | |
22 | const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC) | |
23 | const data = await createActivityData(video.url, byActor, videoObject, t, audience) | |
24 | ||
25 | return broadcastToFollowers(data, byActor, [ byActor ], t) | |
26 | } | |
27 | ||
28 | async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) { | |
29 | const url = getVideoAbuseActivityPubUrl(videoAbuse) | |
30 | ||
31 | const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] } | |
32 | const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience) | |
33 | ||
34 | return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t) | |
35 | } | |
36 | ||
37 | async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
38 | const url = getVideoViewActivityPubUrl(byActor, video) | |
39 | const viewActivity = createViewActivityData(byActor, video) | |
40 | ||
41 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) | |
42 | const audience = getOriginVideoAudience(video, actorsInvolvedInVideo) | |
43 | const data = await createActivityData(url, byActor, viewActivity, t, audience) | |
44 | ||
45 | return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t) | |
46 | } | |
47 | ||
48 | async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
49 | const url = getVideoViewActivityPubUrl(byActor, video) | |
50 | const viewActivity = createViewActivityData(byActor, video) | |
51 | ||
52 | const actorsToForwardView = await getActorsInvolvedInVideo(video, t) | |
53 | const audience = getObjectFollowersAudience(actorsToForwardView) | |
54 | const data = await createActivityData(url, byActor, viewActivity, t, audience) | |
55 | ||
56 | // Use the server actor to send the view | |
57 | const serverActor = await getServerActor() | |
58 | const followersException = [ byActor ] | |
59 | return broadcastToFollowers(data, serverActor, actorsToForwardView, t, followersException) | |
60 | } | |
61 | ||
62 | async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
63 | const url = getVideoDislikeActivityPubUrl(byActor, video) | |
64 | const dislikeActivity = createDislikeActivityData(byActor, video) | |
65 | ||
66 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) | |
67 | const audience = getOriginVideoAudience(video, actorsInvolvedInVideo) | |
68 | const data = await createActivityData(url, byActor, dislikeActivity, t, audience) | |
69 | ||
70 | return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t) | |
71 | } | |
72 | ||
73 | async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
74 | const url = getVideoDislikeActivityPubUrl(byActor, video) | |
75 | const dislikeActivity = createDislikeActivityData(byActor, video) | |
76 | ||
77 | const actorsToForwardView = await getActorsInvolvedInVideo(video, t) | |
78 | const audience = getObjectFollowersAudience(actorsToForwardView) | |
79 | const data = await createActivityData(url, byActor, dislikeActivity, t, audience) | |
80 | ||
81 | const followersException = [ byActor ] | |
82 | return broadcastToFollowers(data, byActor, actorsToForwardView, t, followersException) | |
83 | } | |
84 | ||
85 | async function createActivityData ( | |
86 | url: string, | |
87 | byActor: ActorModel, | |
88 | object: any, | |
89 | t: Transaction, | |
90 | audience?: ActivityAudience | |
91 | ): Promise<ActivityCreate> { | |
92 | if (!audience) { | |
93 | audience = await getAudience(byActor, t) | |
94 | } | |
95 | ||
96 | return { | |
97 | type: 'Create', | |
98 | id: url, | |
99 | actor: byActor.url, | |
100 | to: audience.to, | |
101 | cc: audience.cc, | |
102 | object | |
103 | } | |
104 | } | |
105 | ||
106 | function createDislikeActivityData (byActor: ActorModel, video: VideoModel) { | |
107 | return { | |
108 | type: 'Dislike', | |
109 | actor: byActor.url, | |
110 | object: video.url | |
111 | } | |
112 | } | |
113 | ||
114 | // --------------------------------------------------------------------------- | |
115 | ||
116 | export { | |
117 | sendCreateVideo, | |
118 | sendVideoAbuse, | |
119 | createActivityData, | |
120 | sendCreateViewToOrigin, | |
121 | sendCreateViewToVideoFollowers, | |
122 | sendCreateDislikeToOrigin, | |
123 | sendCreateDislikeToVideoFollowers, | |
124 | createDislikeActivityData | |
125 | } | |
126 | ||
127 | // --------------------------------------------------------------------------- | |
128 | ||
129 | function createViewActivityData (byActor: ActorModel, video: VideoModel) { | |
130 | return { | |
131 | type: 'View', | |
132 | actor: byActor.url, | |
133 | object: video.url | |
134 | } | |
135 | } |