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 { MActorSignature, MAccountIdActor } from '../../../typings/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 VideoTorrentObject
60 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
61 logger.debug('Video sent by update is not valid.', { videoObject })
65 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false, fetchType: 'all' })
66 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
68 const account = actor.Account as MAccountIdActor
71 const updateOptions = {
75 channel: channelActor.VideoChannel,
76 overrideTo: activity.to
78 return updateVideoFromAP(updateOptions)
81 async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
82 if (await isRedundancyAccepted(activity, byActor) !== true) return
84 const cacheFileObject = activity.object as CacheFileObject
86 if (!isCacheFileObjectValid(cacheFileObject)) {
87 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
91 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
93 await sequelizeTypescript.transaction(async t => {
94 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
97 if (video.isOwned()) {
98 // Don't resend the activity to the sender
99 const exceptions = [ byActor ]
101 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
105 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
106 const actorAttributesToUpdate = activity.object as ActivityPubActor
108 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
109 let accountOrChannelInstance: AccountModel | VideoChannelModel
110 let actorFieldsSave: object
111 let accountOrChannelFieldsSave: object
114 const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate)
117 await sequelizeTypescript.transaction(async t => {
118 actorFieldsSave = actor.toJSON()
120 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
121 else accountOrChannelInstance = actor.Account
123 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
125 await updateActorInstance(actor, actorAttributesToUpdate)
127 if (avatarInfo !== undefined) {
128 const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false })
130 await updateActorAvatarInstance(actor, avatarOptions, t)
133 await actor.save({ transaction: t })
135 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
136 accountOrChannelInstance.description = actorAttributesToUpdate.summary
138 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
140 await accountOrChannelInstance.save({ transaction: t })
143 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
145 if (actor !== undefined && actorFieldsSave !== undefined) {
146 resetSequelizeInstance(actor, actorFieldsSave)
149 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
150 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
153 // This is just a debug because we will retry the insert
154 logger.debug('Cannot update the remote account.', { err })
159 async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
160 const playlistObject = activity.object as PlaylistObject
161 const byAccount = byActor.Account
163 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
165 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)