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