]>
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 { getAvatarInfoIfExists, 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 | import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' | |
16 | import { createOrUpdateVideoPlaylist } from '../playlist' | |
17 | import { APProcessorOptions } from '../../../typings/activitypub-processor.model' | |
18 | import { SignatureActorModel } from '../../../typings/models' | |
19 | ||
20 | async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) { | |
21 | const { activity, byActor } = options | |
22 | ||
23 | const objectType = activity.object.type | |
24 | ||
25 | if (objectType === 'Video') { | |
26 | return retryTransactionWrapper(processUpdateVideo, byActor, activity) | |
27 | } | |
28 | ||
29 | if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') { | |
30 | // We need more attributes | |
31 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
32 | return retryTransactionWrapper(processUpdateActor, byActorFull, activity) | |
33 | } | |
34 | ||
35 | if (objectType === 'CacheFile') { | |
36 | // We need more attributes | |
37 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
38 | return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity) | |
39 | } | |
40 | ||
41 | if (objectType === 'Playlist') { | |
42 | return retryTransactionWrapper(processUpdatePlaylist, byActor, activity) | |
43 | } | |
44 | ||
45 | return undefined | |
46 | } | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | export { | |
51 | processUpdateActivity | |
52 | } | |
53 | ||
54 | // --------------------------------------------------------------------------- | |
55 | ||
56 | async function processUpdateVideo (actor: SignatureActorModel, activity: ActivityUpdate) { | |
57 | const videoObject = activity.object as VideoTorrentObject | |
58 | ||
59 | if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) { | |
60 | logger.debug('Video sent by update is not valid.', { videoObject }) | |
61 | return undefined | |
62 | } | |
63 | ||
64 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false }) | |
65 | const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject) | |
66 | ||
67 | const updateOptions = { | |
68 | video, | |
69 | videoObject, | |
70 | account: actor.Account, | |
71 | channel: channelActor.VideoChannel, | |
72 | overrideTo: activity.to | |
73 | } | |
74 | return updateVideoFromAP(updateOptions) | |
75 | } | |
76 | ||
77 | async function processUpdateCacheFile (byActor: SignatureActorModel, activity: ActivityUpdate) { | |
78 | const cacheFileObject = activity.object as CacheFileObject | |
79 | ||
80 | if (!isCacheFileObjectValid(cacheFileObject)) { | |
81 | logger.debug('Cache file object sent by update is not valid.', { cacheFileObject }) | |
82 | return undefined | |
83 | } | |
84 | ||
85 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) | |
86 | ||
87 | await sequelizeTypescript.transaction(async t => { | |
88 | await createOrUpdateCacheFile(cacheFileObject, video, byActor, t) | |
89 | }) | |
90 | ||
91 | if (video.isOwned()) { | |
92 | // Don't resend the activity to the sender | |
93 | const exceptions = [ byActor ] | |
94 | ||
95 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | |
96 | } | |
97 | } | |
98 | ||
99 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { | |
100 | const actorAttributesToUpdate = activity.object as ActivityPubActor | |
101 | ||
102 | logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url) | |
103 | let accountOrChannelInstance: AccountModel | VideoChannelModel | |
104 | let actorFieldsSave: object | |
105 | let accountOrChannelFieldsSave: object | |
106 | ||
107 | // Fetch icon? | |
108 | const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate) | |
109 | ||
110 | try { | |
111 | await sequelizeTypescript.transaction(async t => { | |
112 | actorFieldsSave = actor.toJSON() | |
113 | ||
114 | if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel | |
115 | else accountOrChannelInstance = actor.Account | |
116 | ||
117 | accountOrChannelFieldsSave = accountOrChannelInstance.toJSON() | |
118 | ||
119 | await updateActorInstance(actor, actorAttributesToUpdate) | |
120 | ||
121 | if (avatarInfo !== undefined) { | |
122 | const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false }) | |
123 | ||
124 | await updateActorAvatarInstance(actor, avatarOptions, t) | |
125 | } | |
126 | ||
127 | await actor.save({ transaction: t }) | |
128 | ||
129 | accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername | |
130 | accountOrChannelInstance.description = actorAttributesToUpdate.summary | |
131 | ||
132 | if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support | |
133 | ||
134 | await accountOrChannelInstance.save({ transaction: t }) | |
135 | }) | |
136 | ||
137 | logger.info('Remote account %s updated', actorAttributesToUpdate.url) | |
138 | } catch (err) { | |
139 | if (actor !== undefined && actorFieldsSave !== undefined) { | |
140 | resetSequelizeInstance(actor, actorFieldsSave) | |
141 | } | |
142 | ||
143 | if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) { | |
144 | resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave) | |
145 | } | |
146 | ||
147 | // This is just a debug because we will retry the insert | |
148 | logger.debug('Cannot update the remote account.', { err }) | |
149 | throw err | |
150 | } | |
151 | } | |
152 | ||
153 | async function processUpdatePlaylist (byActor: SignatureActorModel, activity: ActivityUpdate) { | |
154 | const playlistObject = activity.object as PlaylistObject | |
155 | const byAccount = byActor.Account | |
156 | ||
157 | if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url) | |
158 | ||
159 | await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to) | |
160 | } |