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