]>
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' |
557b13ae | 9 | import { getAvatarInfoIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor' |
e587e0ec | 10 | import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos' |
3cd0734f | 11 | import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos' |
c48e82b5 | 12 | import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file' |
b88a4596 | 13 | import { createOrUpdateCacheFile } from '../cache-file' |
e5565833 | 14 | import { forwardVideoRelatedActivity } from '../send/utils' |
418d092a C |
15 | import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' |
16 | import { createOrUpdateVideoPlaylist } from '../playlist' | |
1198edf4 | 17 | import { APProcessorOptions } from '../../../typings/activitypub-processor.model' |
453e83ea | 18 | import { MActorSignature } from '../../../typings/models' |
1198edf4 C |
19 | |
20 | async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) { | |
21 | const { activity, byActor } = options | |
0d0e8dd0 | 22 | |
2422c46b | 23 | const objectType = activity.object.type |
e4f97bab | 24 | |
2422c46b | 25 | if (objectType === 'Video') { |
e587e0ec | 26 | return retryTransactionWrapper(processUpdateVideo, byActor, activity) |
c48e82b5 C |
27 | } |
28 | ||
29 | if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') { | |
e587e0ec C |
30 | // We need more attributes |
31 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
32 | return retryTransactionWrapper(processUpdateActor, byActorFull, activity) | |
e4f97bab | 33 | } |
0d0e8dd0 | 34 | |
c48e82b5 | 35 | if (objectType === 'CacheFile') { |
e587e0ec C |
36 | // We need more attributes |
37 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
38 | return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity) | |
c48e82b5 C |
39 | } |
40 | ||
418d092a C |
41 | if (objectType === 'Playlist') { |
42 | return retryTransactionWrapper(processUpdatePlaylist, byActor, activity) | |
43 | } | |
44 | ||
3cd0734f | 45 | return undefined |
e4f97bab C |
46 | } |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | export { | |
51 | processUpdateActivity | |
52 | } | |
53 | ||
54 | // --------------------------------------------------------------------------- | |
55 | ||
453e83ea | 56 | async function processUpdateVideo (actor: MActorSignature, activity: ActivityUpdate) { |
3cd0734f | 57 | const videoObject = activity.object as VideoTorrentObject |
50d6de9c | 58 | |
3cd0734f C |
59 | if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) { |
60 | logger.debug('Video sent by update is not valid.', { videoObject }) | |
61 | return undefined | |
62 | } | |
2186386c | 63 | |
453e83ea | 64 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false, fetchType: 'all' }) |
f37dc0dd | 65 | const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject) |
2ccaeeb3 | 66 | |
d4defe07 C |
67 | const updateOptions = { |
68 | video, | |
69 | videoObject, | |
453e83ea | 70 | account: channelActor.VideoChannel.Account, |
d4defe07 | 71 | channel: channelActor.VideoChannel, |
d4defe07 C |
72 | overrideTo: activity.to |
73 | } | |
74 | return updateVideoFromAP(updateOptions) | |
c48e82b5 C |
75 | } |
76 | ||
453e83ea | 77 | async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) { |
c48e82b5 C |
78 | const cacheFileObject = activity.object as CacheFileObject |
79 | ||
e5565833 C |
80 | if (!isCacheFileObjectValid(cacheFileObject)) { |
81 | logger.debug('Cache file object sent by update is not valid.', { cacheFileObject }) | |
c48e82b5 C |
82 | return undefined |
83 | } | |
84 | ||
e5565833 C |
85 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) |
86 | ||
87 | await sequelizeTypescript.transaction(async t => { | |
b88a4596 | 88 | await createOrUpdateCacheFile(cacheFileObject, video, byActor, t) |
e5565833 | 89 | }) |
c48e82b5 | 90 | |
e5565833 C |
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 | } | |
0d0e8dd0 | 97 | } |
265ba139 | 98 | |
90d4bb81 | 99 | async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { |
2422c46b | 100 | const actorAttributesToUpdate = activity.object as ActivityPubActor |
265ba139 | 101 | |
57cfff78 | 102 | logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url) |
2422c46b | 103 | let accountOrChannelInstance: AccountModel | VideoChannelModel |
265ba139 | 104 | let actorFieldsSave: object |
2422c46b | 105 | let accountOrChannelFieldsSave: object |
265ba139 C |
106 | |
107 | // Fetch icon? | |
557b13ae | 108 | const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate) |
265ba139 C |
109 | |
110 | try { | |
111 | await sequelizeTypescript.transaction(async t => { | |
a5625b41 | 112 | actorFieldsSave = actor.toJSON() |
265ba139 | 113 | |
2422c46b C |
114 | if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel |
115 | else accountOrChannelInstance = actor.Account | |
116 | ||
117 | accountOrChannelFieldsSave = accountOrChannelInstance.toJSON() | |
118 | ||
119 | await updateActorInstance(actor, actorAttributesToUpdate) | |
265ba139 | 120 | |
557b13ae C |
121 | if (avatarInfo !== undefined) { |
122 | const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false }) | |
123 | ||
124 | await updateActorAvatarInstance(actor, avatarOptions, t) | |
265ba139 C |
125 | } |
126 | ||
127 | await actor.save({ transaction: t }) | |
128 | ||
1735c825 C |
129 | accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername |
130 | accountOrChannelInstance.description = actorAttributesToUpdate.summary | |
131 | ||
132 | if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support | |
133 | ||
2422c46b | 134 | await accountOrChannelInstance.save({ transaction: t }) |
265ba139 C |
135 | }) |
136 | ||
57cfff78 | 137 | logger.info('Remote account %s updated', actorAttributesToUpdate.url) |
265ba139 | 138 | } catch (err) { |
a5625b41 C |
139 | if (actor !== undefined && actorFieldsSave !== undefined) { |
140 | resetSequelizeInstance(actor, actorFieldsSave) | |
265ba139 C |
141 | } |
142 | ||
2422c46b C |
143 | if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) { |
144 | resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave) | |
265ba139 C |
145 | } |
146 | ||
147 | // This is just a debug because we will retry the insert | |
d5b7d911 | 148 | logger.debug('Cannot update the remote account.', { err }) |
265ba139 C |
149 | throw err |
150 | } | |
151 | } | |
418d092a | 152 | |
453e83ea | 153 | async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) { |
418d092a C |
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 | } |