]>
Commit | Line | Data |
---|---|---|
1 | import { isRedundancyAccepted } from '@server/lib/redundancy' | |
2 | import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub' | |
3 | import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor' | |
4 | import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' | |
5 | import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file' | |
6 | import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos' | |
7 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
8 | import { logger } from '../../../helpers/logger' | |
9 | import { sequelizeTypescript } from '../../../initializers/database' | |
10 | import { ActorModel } from '../../../models/actor/actor' | |
11 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' | |
12 | import { MActorFull, MActorSignature } from '../../../types/models' | |
13 | import { APActorUpdater } from '../actors/updater' | |
14 | import { createOrUpdateCacheFile } from '../cache-file' | |
15 | import { createOrUpdateVideoPlaylist } from '../playlists' | |
16 | import { forwardVideoRelatedActivity } from '../send/utils' | |
17 | import { APVideoUpdater, getOrCreateAPVideo } from '../videos' | |
18 | ||
19 | async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) { | |
20 | const { activity, byActor } = options | |
21 | ||
22 | const objectType = activity.object.type | |
23 | ||
24 | if (objectType === 'Video') { | |
25 | return retryTransactionWrapper(processUpdateVideo, activity) | |
26 | } | |
27 | ||
28 | if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') { | |
29 | // We need more attributes | |
30 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
31 | return retryTransactionWrapper(processUpdateActor, byActorFull, activity) | |
32 | } | |
33 | ||
34 | if (objectType === 'CacheFile') { | |
35 | // We need more attributes | |
36 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
37 | return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity) | |
38 | } | |
39 | ||
40 | if (objectType === 'Playlist') { | |
41 | return retryTransactionWrapper(processUpdatePlaylist, byActor, activity) | |
42 | } | |
43 | ||
44 | return undefined | |
45 | } | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | export { | |
50 | processUpdateActivity | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | async function processUpdateVideo (activity: ActivityUpdate) { | |
56 | const videoObject = activity.object as VideoObject | |
57 | ||
58 | if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) { | |
59 | logger.debug('Video sent by update is not valid.', { videoObject }) | |
60 | return undefined | |
61 | } | |
62 | ||
63 | const { video, created } = await getOrCreateAPVideo({ | |
64 | videoObject: videoObject.id, | |
65 | allowRefresh: false, | |
66 | fetchType: 'all' | |
67 | }) | |
68 | // We did not have this video, it has been created so no need to update | |
69 | if (created) return | |
70 | ||
71 | const updater = new APVideoUpdater(videoObject, video) | |
72 | return updater.update(activity.to) | |
73 | } | |
74 | ||
75 | async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) { | |
76 | if (await isRedundancyAccepted(activity, byActor) !== true) return | |
77 | ||
78 | const cacheFileObject = activity.object as CacheFileObject | |
79 | ||
80 | if (!isCacheFileObjectValid(cacheFileObject)) { | |
81 | logger.debug('Cache file object sent by update is not valid.', { cacheFileObject }) | |
82 | return undefined | |
83 | } | |
84 | ||
85 | const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object }) | |
86 | ||
87 | await sequelizeTypescript.transaction(async t => { | |
88 | await createOrUpdateCacheFile(cacheFileObject, video, byActor, t) | |
89 | }) | |
90 | ||
91 | if (video.isOwned()) { | |
92 | // Don't resend the activity to the sender | |
93 | const exceptions = [ byActor ] | |
94 | ||
95 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | |
96 | } | |
97 | } | |
98 | ||
99 | async function processUpdateActor (actor: MActorFull, activity: ActivityUpdate) { | |
100 | const actorObject = activity.object as ActivityPubActor | |
101 | ||
102 | logger.debug('Updating remote account "%s".', actorObject.url) | |
103 | ||
104 | const updater = new APActorUpdater(actorObject, actor) | |
105 | return updater.update() | |
106 | } | |
107 | ||
108 | async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) { | |
109 | const playlistObject = activity.object as PlaylistObject | |
110 | const byAccount = byActor.Account | |
111 | ||
112 | if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url) | |
113 | ||
114 | await createOrUpdateVideoPlaylist(playlistObject, activity.to) | |
115 | } |