1 import { ActivityUpdate, CacheFileObject, VideoObject } 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/database'
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 '../../../types/activitypub-processor.model'
18 import { MActorSignature, MAccountIdActor } from '../../../types/models'
19 import { isRedundancyAccepted } from '@server/lib/redundancy'
21 async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
22 const { activity, byActor } = options
24 const objectType = activity.object.type
26 if (objectType === 'Video') {
27 return retryTransactionWrapper(processUpdateVideo, byActor, activity)
30 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
31 // We need more attributes
32 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
33 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
36 if (objectType === 'CacheFile') {
37 // We need more attributes
38 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
39 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
42 if (objectType === 'Playlist') {
43 return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
49 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 async function processUpdateVideo (actor: MActorSignature, activity: ActivityUpdate) {
58 const videoObject = activity.object as VideoObject
60 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
61 logger.debug('Video sent by update is not valid.', { videoObject })
65 const { video, created } = await getOrCreateVideoAndAccountAndChannel({
66 videoObject: videoObject.id,
70 // We did not have this video, it has been created so no need to update
74 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
76 const account = actor.Account as MAccountIdActor
79 const updateOptions = {
83 channel: channelActor.VideoChannel,
84 overrideTo: activity.to
86 return updateVideoFromAP(updateOptions)
89 async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
90 if (await isRedundancyAccepted(activity, byActor) !== true) return
92 const cacheFileObject = activity.object as CacheFileObject
94 if (!isCacheFileObjectValid(cacheFileObject)) {
95 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
99 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
101 await sequelizeTypescript.transaction(async t => {
102 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
105 if (video.isOwned()) {
106 // Don't resend the activity to the sender
107 const exceptions = [ byActor ]
109 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
113 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
114 const actorAttributesToUpdate = activity.object as ActivityPubActor
116 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
117 let accountOrChannelInstance: AccountModel | VideoChannelModel
118 let actorFieldsSave: object
119 let accountOrChannelFieldsSave: object
122 const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate)
125 await sequelizeTypescript.transaction(async t => {
126 actorFieldsSave = actor.toJSON()
128 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
129 else accountOrChannelInstance = actor.Account
131 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
133 await updateActorInstance(actor, actorAttributesToUpdate)
135 if (avatarInfo !== undefined) {
136 const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false })
138 await updateActorAvatarInstance(actor, avatarOptions, t)
141 await actor.save({ transaction: t })
143 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
144 accountOrChannelInstance.description = actorAttributesToUpdate.summary
146 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
148 await accountOrChannelInstance.save({ transaction: t })
151 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
153 if (actor !== undefined && actorFieldsSave !== undefined) {
154 resetSequelizeInstance(actor, actorFieldsSave)
157 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
158 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
161 // This is just a debug because we will retry the insert
162 logger.debug('Cannot update the remote account.', { err })
167 async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
168 const playlistObject = activity.object as PlaylistObject
169 const byAccount = byActor.Account
171 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
173 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)