]>
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 { createOrUpdateCacheFile } from '../cache-file' | |
14 | import { forwardVideoRelatedActivity } from '../send/utils' | |
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 | overrideTo: activity.to | |
63 | } | |
64 | return updateVideoFromAP(updateOptions) | |
65 | } | |
66 | ||
67 | async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) { | |
68 | const cacheFileObject = activity.object as CacheFileObject | |
69 | ||
70 | if (!isCacheFileObjectValid(cacheFileObject)) { | |
71 | logger.debug('Cache file object sent by update is not valid.', { cacheFileObject }) | |
72 | return undefined | |
73 | } | |
74 | ||
75 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) | |
76 | ||
77 | await sequelizeTypescript.transaction(async t => { | |
78 | await createOrUpdateCacheFile(cacheFileObject, video, byActor, t) | |
79 | }) | |
80 | ||
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 | } | |
87 | } | |
88 | ||
89 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { | |
90 | const actorAttributesToUpdate = activity.object as ActivityPubActor | |
91 | ||
92 | logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid) | |
93 | let accountOrChannelInstance: AccountModel | VideoChannelModel | |
94 | let actorFieldsSave: object | |
95 | let accountOrChannelFieldsSave: object | |
96 | ||
97 | // Fetch icon? | |
98 | const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate) | |
99 | ||
100 | try { | |
101 | await sequelizeTypescript.transaction(async t => { | |
102 | actorFieldsSave = actor.toJSON() | |
103 | ||
104 | if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel | |
105 | else accountOrChannelInstance = actor.Account | |
106 | ||
107 | accountOrChannelFieldsSave = accountOrChannelInstance.toJSON() | |
108 | ||
109 | await updateActorInstance(actor, actorAttributesToUpdate) | |
110 | ||
111 | if (avatarName !== undefined) { | |
112 | await updateActorAvatarInstance(actor, avatarName, t) | |
113 | } | |
114 | ||
115 | await actor.save({ transaction: t }) | |
116 | ||
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 }) | |
121 | }) | |
122 | ||
123 | logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid) | |
124 | } catch (err) { | |
125 | if (actor !== undefined && actorFieldsSave !== undefined) { | |
126 | resetSequelizeInstance(actor, actorFieldsSave) | |
127 | } | |
128 | ||
129 | if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) { | |
130 | resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave) | |
131 | } | |
132 | ||
133 | // This is just a debug because we will retry the insert | |
134 | logger.debug('Cannot update the remote account.', { err }) | |
135 | throw err | |
136 | } | |
137 | } |