]>
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' |
3fd3ab2d | 5 | import { sequelizeTypescript } from '../../../initializers' |
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' |
e587e0ec C |
9 | import { fetchAvatarIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor' |
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' |
0d0e8dd0 | 15 | |
e587e0ec | 16 | async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) { |
2422c46b | 17 | const objectType = activity.object.type |
e4f97bab | 18 | |
2422c46b | 19 | if (objectType === 'Video') { |
e587e0ec | 20 | return retryTransactionWrapper(processUpdateVideo, byActor, activity) |
c48e82b5 C |
21 | } |
22 | ||
23 | if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') { | |
e587e0ec C |
24 | // We need more attributes |
25 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
26 | return retryTransactionWrapper(processUpdateActor, byActorFull, activity) | |
e4f97bab | 27 | } |
0d0e8dd0 | 28 | |
c48e82b5 | 29 | if (objectType === 'CacheFile') { |
e587e0ec C |
30 | // We need more attributes |
31 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
32 | return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity) | |
c48e82b5 C |
33 | } |
34 | ||
3cd0734f | 35 | return undefined |
e4f97bab C |
36 | } |
37 | ||
38 | // --------------------------------------------------------------------------- | |
39 | ||
40 | export { | |
41 | processUpdateActivity | |
42 | } | |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | ||
90d4bb81 | 46 | async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) { |
3cd0734f | 47 | const videoObject = activity.object as VideoTorrentObject |
50d6de9c | 48 | |
3cd0734f C |
49 | if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) { |
50 | logger.debug('Video sent by update is not valid.', { videoObject }) | |
51 | return undefined | |
52 | } | |
2186386c | 53 | |
74577825 | 54 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false }) |
f37dc0dd | 55 | const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject) |
2ccaeeb3 | 56 | |
d4defe07 C |
57 | const updateOptions = { |
58 | video, | |
59 | videoObject, | |
60 | account: actor.Account, | |
61 | channel: channelActor.VideoChannel, | |
d4defe07 C |
62 | overrideTo: activity.to |
63 | } | |
64 | return updateVideoFromAP(updateOptions) | |
c48e82b5 C |
65 | } |
66 | ||
67 | async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) { | |
68 | const cacheFileObject = activity.object as CacheFileObject | |
69 | ||
e5565833 C |
70 | if (!isCacheFileObjectValid(cacheFileObject)) { |
71 | logger.debug('Cache file object sent by update is not valid.', { cacheFileObject }) | |
c48e82b5 C |
72 | return undefined |
73 | } | |
74 | ||
e5565833 C |
75 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) |
76 | ||
77 | await sequelizeTypescript.transaction(async t => { | |
b88a4596 | 78 | await createOrUpdateCacheFile(cacheFileObject, video, byActor, t) |
e5565833 | 79 | }) |
c48e82b5 | 80 | |
e5565833 C |
81 | if (video.isOwned()) { |
82 | // Don't resend the activity to the sender | |
83 | const exceptions = [ byActor ] | |
84 | ||
85 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | |
86 | } | |
0d0e8dd0 | 87 | } |
265ba139 | 88 | |
90d4bb81 | 89 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { |
2422c46b | 90 | const actorAttributesToUpdate = activity.object as ActivityPubActor |
265ba139 | 91 | |
2422c46b C |
92 | logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid) |
93 | let accountOrChannelInstance: AccountModel | VideoChannelModel | |
265ba139 | 94 | let actorFieldsSave: object |
2422c46b | 95 | let accountOrChannelFieldsSave: object |
265ba139 C |
96 | |
97 | // Fetch icon? | |
2422c46b | 98 | const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate) |
265ba139 C |
99 | |
100 | try { | |
101 | await sequelizeTypescript.transaction(async t => { | |
a5625b41 | 102 | actorFieldsSave = actor.toJSON() |
265ba139 | 103 | |
2422c46b C |
104 | if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel |
105 | else accountOrChannelInstance = actor.Account | |
106 | ||
107 | accountOrChannelFieldsSave = accountOrChannelInstance.toJSON() | |
108 | ||
109 | await updateActorInstance(actor, actorAttributesToUpdate) | |
265ba139 | 110 | |
a5625b41 C |
111 | if (avatarName !== undefined) { |
112 | await updateActorAvatarInstance(actor, avatarName, t) | |
265ba139 C |
113 | } |
114 | ||
115 | await actor.save({ transaction: t }) | |
116 | ||
2422c46b C |
117 | accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername) |
118 | accountOrChannelInstance.set('description', actorAttributesToUpdate.summary) | |
119 | accountOrChannelInstance.set('support', actorAttributesToUpdate.support) | |
120 | await accountOrChannelInstance.save({ transaction: t }) | |
265ba139 C |
121 | }) |
122 | ||
2422c46b | 123 | logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid) |
265ba139 | 124 | } catch (err) { |
a5625b41 C |
125 | if (actor !== undefined && actorFieldsSave !== undefined) { |
126 | resetSequelizeInstance(actor, actorFieldsSave) | |
265ba139 C |
127 | } |
128 | ||
2422c46b C |
129 | if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) { |
130 | resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave) | |
265ba139 C |
131 | } |
132 | ||
133 | // This is just a debug because we will retry the insert | |
d5b7d911 | 134 | logger.debug('Cannot update the remote account.', { err }) |
265ba139 C |
135 | throw err |
136 | } | |
137 | } |