]>
Commit | Line | Data |
---|---|---|
c48e82b5 | 1 | import { ActivityUpdate, CacheFileObject, VideoTorrentObject } 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) { |
3cd0734f | 58 | const videoObject = activity.object as VideoTorrentObject |
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 | |
453e83ea | 65 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false, fetchType: 'all' }) |
f37dc0dd | 66 | const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject) |
2ccaeeb3 | 67 | |
f92e7f76 C |
68 | const account = actor.Account as MAccountIdActor |
69 | account.Actor = actor | |
70 | ||
d4defe07 C |
71 | const updateOptions = { |
72 | video, | |
73 | videoObject, | |
f92e7f76 | 74 | account, |
d4defe07 | 75 | channel: channelActor.VideoChannel, |
d4defe07 C |
76 | overrideTo: activity.to |
77 | } | |
78 | return updateVideoFromAP(updateOptions) | |
c48e82b5 C |
79 | } |
80 | ||
453e83ea | 81 | async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) { |
8c9e7875 C |
82 | if (await isRedundancyAccepted(activity, byActor) !== true) return |
83 | ||
c48e82b5 C |
84 | const cacheFileObject = activity.object as CacheFileObject |
85 | ||
e5565833 C |
86 | if (!isCacheFileObjectValid(cacheFileObject)) { |
87 | logger.debug('Cache file object sent by update is not valid.', { cacheFileObject }) | |
c48e82b5 C |
88 | return undefined |
89 | } | |
90 | ||
e5565833 C |
91 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) |
92 | ||
93 | await sequelizeTypescript.transaction(async t => { | |
b88a4596 | 94 | await createOrUpdateCacheFile(cacheFileObject, video, byActor, t) |
e5565833 | 95 | }) |
c48e82b5 | 96 | |
e5565833 C |
97 | if (video.isOwned()) { |
98 | // Don't resend the activity to the sender | |
99 | const exceptions = [ byActor ] | |
100 | ||
101 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | |
102 | } | |
0d0e8dd0 | 103 | } |
265ba139 | 104 | |
90d4bb81 | 105 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { |
2422c46b | 106 | const actorAttributesToUpdate = activity.object as ActivityPubActor |
265ba139 | 107 | |
57cfff78 | 108 | logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url) |
2422c46b | 109 | let accountOrChannelInstance: AccountModel | VideoChannelModel |
265ba139 | 110 | let actorFieldsSave: object |
2422c46b | 111 | let accountOrChannelFieldsSave: object |
265ba139 C |
112 | |
113 | // Fetch icon? | |
557b13ae | 114 | const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate) |
265ba139 C |
115 | |
116 | try { | |
117 | await sequelizeTypescript.transaction(async t => { | |
a5625b41 | 118 | actorFieldsSave = actor.toJSON() |
265ba139 | 119 | |
2422c46b C |
120 | if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel |
121 | else accountOrChannelInstance = actor.Account | |
122 | ||
123 | accountOrChannelFieldsSave = accountOrChannelInstance.toJSON() | |
124 | ||
125 | await updateActorInstance(actor, actorAttributesToUpdate) | |
265ba139 | 126 | |
557b13ae C |
127 | if (avatarInfo !== undefined) { |
128 | const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false }) | |
129 | ||
130 | await updateActorAvatarInstance(actor, avatarOptions, t) | |
265ba139 C |
131 | } |
132 | ||
133 | await actor.save({ transaction: t }) | |
134 | ||
1735c825 C |
135 | accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername |
136 | accountOrChannelInstance.description = actorAttributesToUpdate.summary | |
137 | ||
138 | if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support | |
139 | ||
2422c46b | 140 | await accountOrChannelInstance.save({ transaction: t }) |
265ba139 C |
141 | }) |
142 | ||
57cfff78 | 143 | logger.info('Remote account %s updated', actorAttributesToUpdate.url) |
265ba139 | 144 | } catch (err) { |
a5625b41 C |
145 | if (actor !== undefined && actorFieldsSave !== undefined) { |
146 | resetSequelizeInstance(actor, actorFieldsSave) | |
265ba139 C |
147 | } |
148 | ||
2422c46b C |
149 | if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) { |
150 | resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave) | |
265ba139 C |
151 | } |
152 | ||
153 | // This is just a debug because we will retry the insert | |
d5b7d911 | 154 | logger.debug('Cannot update the remote account.', { err }) |
265ba139 C |
155 | throw err |
156 | } | |
157 | } | |
418d092a | 158 | |
453e83ea | 159 | async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) { |
418d092a C |
160 | const playlistObject = activity.object as PlaylistObject |
161 | const byAccount = byActor.Account | |
162 | ||
163 | if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url) | |
164 | ||
165 | await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to) | |
166 | } |