aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-undo.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/lib/activitypub/send/send-undo.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/lib/activitypub/send/send-undo.ts')
-rw-r--r--server/lib/activitypub/send/send-undo.ts172
1 files changed, 0 insertions, 172 deletions
diff --git a/server/lib/activitypub/send/send-undo.ts b/server/lib/activitypub/send/send-undo.ts
deleted file mode 100644
index b0b48c9c4..000000000
--- a/server/lib/activitypub/send/send-undo.ts
+++ /dev/null
@@ -1,172 +0,0 @@
1import { Transaction } from 'sequelize'
2import { ActivityAudience, ActivityDislike, ActivityLike, ActivityUndo, ActivityUndoObject, ContextType } from '@shared/models'
3import { logger } from '../../../helpers/logger'
4import { VideoModel } from '../../../models/video/video'
5import {
6 MActor,
7 MActorAudience,
8 MActorFollowActors,
9 MActorLight,
10 MVideo,
11 MVideoAccountLight,
12 MVideoRedundancyVideo,
13 MVideoShare
14} from '../../../types/models'
15import { audiencify, getAudience } from '../audience'
16import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url'
17import { buildAnnounceWithVideoAudience } from './send-announce'
18import { buildCreateActivity } from './send-create'
19import { buildDislikeActivity } from './send-dislike'
20import { buildFollowActivity } from './send-follow'
21import { buildLikeActivity } from './send-like'
22import { broadcastToFollowers, sendVideoActivityToOrigin, sendVideoRelatedActivity, unicastTo } from './shared/send-utils'
23
24function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
25 const me = actorFollow.ActorFollower
26 const following = actorFollow.ActorFollowing
27
28 // Same server as ours
29 if (!following.serverId) return
30
31 logger.info('Creating job to send an unfollow request to %s.', following.url)
32
33 const undoUrl = getUndoActivityPubUrl(actorFollow.url)
34
35 const followActivity = buildFollowActivity(actorFollow.url, me, following)
36 const undoActivity = undoActivityData(undoUrl, me, followActivity)
37
38 t.afterCommit(() => {
39 return unicastTo({
40 data: undoActivity,
41 byActor: me,
42 toActorUrl: following.inboxUrl,
43 contextType: 'Follow'
44 })
45 })
46}
47
48// ---------------------------------------------------------------------------
49
50async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, transaction: Transaction) {
51 logger.info('Creating job to undo announce %s.', videoShare.url)
52
53 const undoUrl = getUndoActivityPubUrl(videoShare.url)
54
55 const { activity: announce, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, transaction)
56 const undoActivity = undoActivityData(undoUrl, byActor, announce)
57
58 return broadcastToFollowers({
59 data: undoActivity,
60 byActor,
61 toFollowersOf: actorsInvolvedInVideo,
62 transaction,
63 actorsException: [ byActor ],
64 contextType: 'Announce'
65 })
66}
67
68async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, transaction: Transaction) {
69 logger.info('Creating job to undo cache file %s.', redundancyModel.url)
70
71 const associatedVideo = redundancyModel.getVideo()
72 if (!associatedVideo) {
73 logger.warn('Cannot send undo activity for redundancy %s: no video files associated.', redundancyModel.url)
74 return
75 }
76
77 const video = await VideoModel.loadFull(associatedVideo.id)
78 const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
79
80 return sendUndoVideoRelatedActivity({
81 byActor,
82 video,
83 url: redundancyModel.url,
84 activity: createActivity,
85 contextType: 'CacheFile',
86 transaction
87 })
88}
89
90// ---------------------------------------------------------------------------
91
92async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
93 logger.info('Creating job to undo a like of video %s.', video.url)
94
95 const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video)
96 const likeActivity = buildLikeActivity(likeUrl, byActor, video)
97
98 return sendUndoVideoRateToOriginActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
99}
100
101async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
102 logger.info('Creating job to undo a dislike of video %s.', video.url)
103
104 const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video)
105 const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
106
107 return sendUndoVideoRateToOriginActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
108}
109
110// ---------------------------------------------------------------------------
111
112export {
113 sendUndoFollow,
114 sendUndoLike,
115 sendUndoDislike,
116 sendUndoAnnounce,
117 sendUndoCacheFile
118}
119
120// ---------------------------------------------------------------------------
121
122function undoActivityData <T extends ActivityUndoObject> (
123 url: string,
124 byActor: MActorAudience,
125 object: T,
126 audience?: ActivityAudience
127): ActivityUndo<T> {
128 if (!audience) audience = getAudience(byActor)
129
130 return audiencify(
131 {
132 type: 'Undo' as 'Undo',
133 id: url,
134 actor: byActor.url,
135 object
136 },
137 audience
138 )
139}
140
141async function sendUndoVideoRelatedActivity (options: {
142 byActor: MActor
143 video: MVideoAccountLight
144 url: string
145 activity: ActivityUndoObject
146 contextType: ContextType
147 transaction: Transaction
148}) {
149 const activityBuilder = (audience: ActivityAudience) => {
150 const undoUrl = getUndoActivityPubUrl(options.url)
151
152 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
153 }
154
155 return sendVideoRelatedActivity(activityBuilder, options)
156}
157
158async function sendUndoVideoRateToOriginActivity (options: {
159 byActor: MActor
160 video: MVideoAccountLight
161 url: string
162 activity: ActivityLike | ActivityDislike
163 transaction: Transaction
164}) {
165 const activityBuilder = (audience: ActivityAudience) => {
166 const undoUrl = getUndoActivityPubUrl(options.url)
167
168 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
169 }
170
171 return sendVideoActivityToOrigin(activityBuilder, { ...options, contextType: 'Rate' })
172}