]>
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 | import { forwardVideoRelatedActivity } from '../send/utils' | |
16 | ||
17 | async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) { | |
18 | const objectType = activity.object.type | |
19 | ||
20 | if (objectType === 'Video') { | |
21 | return retryTransactionWrapper(processUpdateVideo, byActor, activity) | |
22 | } | |
23 | ||
24 | if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') { | |
25 | // We need more attributes | |
26 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
27 | return retryTransactionWrapper(processUpdateActor, byActorFull, activity) | |
28 | } | |
29 | ||
30 | if (objectType === 'CacheFile') { | |
31 | // We need more attributes | |
32 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
33 | return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity) | |
34 | } | |
35 | ||
36 | return undefined | |
37 | } | |
38 | ||
39 | // --------------------------------------------------------------------------- | |
40 | ||
41 | export { | |
42 | processUpdateActivity | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) { | |
48 | const videoObject = activity.object as VideoTorrentObject | |
49 | ||
50 | if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) { | |
51 | logger.debug('Video sent by update is not valid.', { videoObject }) | |
52 | return undefined | |
53 | } | |
54 | ||
55 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id }) | |
56 | const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject) | |
57 | ||
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) | |
67 | } | |
68 | ||
69 | async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) { | |
70 | const cacheFileObject = activity.object as CacheFileObject | |
71 | ||
72 | if (!isCacheFileObjectValid(cacheFileObject)) { | |
73 | logger.debug('Cache file object sent by update is not valid.', { cacheFileObject }) | |
74 | return undefined | |
75 | } | |
76 | ||
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 | }) | |
88 | ||
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 | } | |
95 | } | |
96 | ||
97 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { | |
98 | const actorAttributesToUpdate = activity.object as ActivityPubActor | |
99 | ||
100 | logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid) | |
101 | let accountOrChannelInstance: AccountModel | VideoChannelModel | |
102 | let actorFieldsSave: object | |
103 | let accountOrChannelFieldsSave: object | |
104 | ||
105 | // Fetch icon? | |
106 | const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate) | |
107 | ||
108 | try { | |
109 | await sequelizeTypescript.transaction(async t => { | |
110 | actorFieldsSave = actor.toJSON() | |
111 | ||
112 | if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel | |
113 | else accountOrChannelInstance = actor.Account | |
114 | ||
115 | accountOrChannelFieldsSave = accountOrChannelInstance.toJSON() | |
116 | ||
117 | await updateActorInstance(actor, actorAttributesToUpdate) | |
118 | ||
119 | if (avatarName !== undefined) { | |
120 | await updateActorAvatarInstance(actor, avatarName, t) | |
121 | } | |
122 | ||
123 | await actor.save({ transaction: t }) | |
124 | ||
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 }) | |
129 | }) | |
130 | ||
131 | logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid) | |
132 | } catch (err) { | |
133 | if (actor !== undefined && actorFieldsSave !== undefined) { | |
134 | resetSequelizeInstance(actor, actorFieldsSave) | |
135 | } | |
136 | ||
137 | if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) { | |
138 | resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave) | |
139 | } | |
140 | ||
141 | // This is just a debug because we will retry the insert | |
142 | logger.debug('Cannot update the remote account.', { err }) | |
143 | throw err | |
144 | } | |
145 | } |