]>
Commit | Line | Data |
---|---|---|
de6310b2 | 1 | import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub' |
265ba139 | 2 | import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor' |
06215f15 | 3 | import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils' |
da854ddd | 4 | import { logger } from '../../../helpers/logger' |
80fdaf06 | 5 | import { sequelizeTypescript } from '../../../initializers/database' |
265ba139 | 6 | import { AccountModel } from '../../../models/account/account' |
50d6de9c | 7 | import { ActorModel } from '../../../models/activitypub/actor' |
2422c46b | 8 | import { VideoChannelModel } from '../../../models/video/video-channel' |
557b13ae | 9 | import { getAvatarInfoIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor' |
e587e0ec | 10 | import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos' |
3cd0734f | 11 | import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos' |
c48e82b5 | 12 | import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file' |
b88a4596 | 13 | import { createOrUpdateCacheFile } from '../cache-file' |
e5565833 | 14 | import { forwardVideoRelatedActivity } from '../send/utils' |
418d092a C |
15 | import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' |
16 | import { createOrUpdateVideoPlaylist } from '../playlist' | |
26d6bf65 C |
17 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
18 | import { MActorSignature, MAccountIdActor } from '../../../types/models' | |
8c9e7875 | 19 | import { isRedundancyAccepted } from '@server/lib/redundancy' |
1198edf4 C |
20 | |
21 | async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) { | |
22 | const { activity, byActor } = options | |
0d0e8dd0 | 23 | |
2422c46b | 24 | const objectType = activity.object.type |
e4f97bab | 25 | |
2422c46b | 26 | if (objectType === 'Video') { |
e587e0ec | 27 | return retryTransactionWrapper(processUpdateVideo, byActor, activity) |
c48e82b5 C |
28 | } |
29 | ||
30 | if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') { | |
e587e0ec C |
31 | // We need more attributes |
32 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
33 | return retryTransactionWrapper(processUpdateActor, byActorFull, activity) | |
e4f97bab | 34 | } |
0d0e8dd0 | 35 | |
c48e82b5 | 36 | if (objectType === 'CacheFile') { |
e587e0ec C |
37 | // We need more attributes |
38 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
39 | return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity) | |
c48e82b5 C |
40 | } |
41 | ||
418d092a C |
42 | if (objectType === 'Playlist') { |
43 | return retryTransactionWrapper(processUpdatePlaylist, byActor, activity) | |
44 | } | |
45 | ||
3cd0734f | 46 | return undefined |
e4f97bab C |
47 | } |
48 | ||
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | export { | |
52 | processUpdateActivity | |
53 | } | |
54 | ||
55 | // --------------------------------------------------------------------------- | |
56 | ||
453e83ea | 57 | async function processUpdateVideo (actor: MActorSignature, activity: ActivityUpdate) { |
de6310b2 | 58 | const videoObject = activity.object as VideoObject |
50d6de9c | 59 | |
3cd0734f C |
60 | if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) { |
61 | logger.debug('Video sent by update is not valid.', { videoObject }) | |
62 | return undefined | |
63 | } | |
2186386c | 64 | |
92315d97 C |
65 | const { video, created } = await getOrCreateVideoAndAccountAndChannel({ |
66 | videoObject: videoObject.id, | |
67 | allowRefresh: false, | |
68 | fetchType: 'all' | |
69 | }) | |
70 | // We did not have this video, it has been created so no need to update | |
71 | if (created) return | |
72 | ||
73 | // Load new channel | |
f37dc0dd | 74 | const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject) |
2ccaeeb3 | 75 | |
f92e7f76 C |
76 | const account = actor.Account as MAccountIdActor |
77 | account.Actor = actor | |
78 | ||
d4defe07 C |
79 | const updateOptions = { |
80 | video, | |
81 | videoObject, | |
f92e7f76 | 82 | account, |
d4defe07 | 83 | channel: channelActor.VideoChannel, |
d4defe07 C |
84 | overrideTo: activity.to |
85 | } | |
86 | return updateVideoFromAP(updateOptions) | |
c48e82b5 C |
87 | } |
88 | ||
453e83ea | 89 | async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) { |
8c9e7875 C |
90 | if (await isRedundancyAccepted(activity, byActor) !== true) return |
91 | ||
c48e82b5 C |
92 | const cacheFileObject = activity.object as CacheFileObject |
93 | ||
e5565833 C |
94 | if (!isCacheFileObjectValid(cacheFileObject)) { |
95 | logger.debug('Cache file object sent by update is not valid.', { cacheFileObject }) | |
c48e82b5 C |
96 | return undefined |
97 | } | |
98 | ||
e5565833 C |
99 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) |
100 | ||
101 | await sequelizeTypescript.transaction(async t => { | |
b88a4596 | 102 | await createOrUpdateCacheFile(cacheFileObject, video, byActor, t) |
e5565833 | 103 | }) |
c48e82b5 | 104 | |
e5565833 C |
105 | if (video.isOwned()) { |
106 | // Don't resend the activity to the sender | |
107 | const exceptions = [ byActor ] | |
108 | ||
109 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | |
110 | } | |
0d0e8dd0 | 111 | } |
265ba139 | 112 | |
90d4bb81 | 113 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { |
2422c46b | 114 | const actorAttributesToUpdate = activity.object as ActivityPubActor |
265ba139 | 115 | |
57cfff78 | 116 | logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url) |
2422c46b | 117 | let accountOrChannelInstance: AccountModel | VideoChannelModel |
265ba139 | 118 | let actorFieldsSave: object |
2422c46b | 119 | let accountOrChannelFieldsSave: object |
265ba139 C |
120 | |
121 | // Fetch icon? | |
557b13ae | 122 | const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate) |
265ba139 C |
123 | |
124 | try { | |
125 | await sequelizeTypescript.transaction(async t => { | |
a5625b41 | 126 | actorFieldsSave = actor.toJSON() |
265ba139 | 127 | |
2422c46b C |
128 | if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel |
129 | else accountOrChannelInstance = actor.Account | |
130 | ||
131 | accountOrChannelFieldsSave = accountOrChannelInstance.toJSON() | |
132 | ||
133 | await updateActorInstance(actor, actorAttributesToUpdate) | |
265ba139 | 134 | |
557b13ae C |
135 | if (avatarInfo !== undefined) { |
136 | const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false }) | |
137 | ||
138 | await updateActorAvatarInstance(actor, avatarOptions, t) | |
265ba139 C |
139 | } |
140 | ||
141 | await actor.save({ transaction: t }) | |
142 | ||
1735c825 C |
143 | accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername |
144 | accountOrChannelInstance.description = actorAttributesToUpdate.summary | |
145 | ||
146 | if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support | |
147 | ||
2422c46b | 148 | await accountOrChannelInstance.save({ transaction: t }) |
265ba139 C |
149 | }) |
150 | ||
57cfff78 | 151 | logger.info('Remote account %s updated', actorAttributesToUpdate.url) |
265ba139 | 152 | } catch (err) { |
a5625b41 C |
153 | if (actor !== undefined && actorFieldsSave !== undefined) { |
154 | resetSequelizeInstance(actor, actorFieldsSave) | |
265ba139 C |
155 | } |
156 | ||
2422c46b C |
157 | if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) { |
158 | resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave) | |
265ba139 C |
159 | } |
160 | ||
161 | // This is just a debug because we will retry the insert | |
d5b7d911 | 162 | logger.debug('Cannot update the remote account.', { err }) |
265ba139 C |
163 | throw err |
164 | } | |
165 | } | |
418d092a | 166 | |
453e83ea | 167 | async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) { |
418d092a C |
168 | const playlistObject = activity.object as PlaylistObject |
169 | const byAccount = byActor.Account | |
170 | ||
171 | if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url) | |
172 | ||
173 | await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to) | |
174 | } |