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 } from '../../../typings/models'
20 async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
21 const { activity, byActor } = options
23 const objectType = activity.object.type
25 if (objectType === 'Video') {
26 return retryTransactionWrapper(processUpdateVideo, byActor, activity)
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)
35 if (objectType === 'CacheFile') {
36 // We need more attributes
37 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
38 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
41 if (objectType === 'Playlist') {
42 return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
48 // ---------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------
56 async function processUpdateVideo (actor: MActorSignature, activity: ActivityUpdate) {
57 const videoObject = activity.object as VideoTorrentObject
59 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
60 logger.debug('Video sent by update is not valid.', { videoObject })
64 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false, fetchType: 'all' })
65 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
67 const updateOptions = {
70 account: channelActor.VideoChannel.Account,
71 channel: channelActor.VideoChannel,
72 overrideTo: activity.to
74 return updateVideoFromAP(updateOptions)
77 async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
78 const cacheFileObject = activity.object as CacheFileObject
80 if (!isCacheFileObjectValid(cacheFileObject)) {
81 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
85 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
87 await sequelizeTypescript.transaction(async t => {
88 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
91 if (video.isOwned()) {
92 // Don't resend the activity to the sender
93 const exceptions = [ byActor ]
95 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
99 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
100 const actorAttributesToUpdate = activity.object as ActivityPubActor
102 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
103 let accountOrChannelInstance: AccountModel | VideoChannelModel
104 let actorFieldsSave: object
105 let accountOrChannelFieldsSave: object
108 const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate)
111 await sequelizeTypescript.transaction(async t => {
112 actorFieldsSave = actor.toJSON()
114 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
115 else accountOrChannelInstance = actor.Account
117 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
119 await updateActorInstance(actor, actorAttributesToUpdate)
121 if (avatarInfo !== undefined) {
122 const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false })
124 await updateActorAvatarInstance(actor, avatarOptions, t)
127 await actor.save({ transaction: t })
129 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
130 accountOrChannelInstance.description = actorAttributesToUpdate.summary
132 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
134 await accountOrChannelInstance.save({ transaction: t })
137 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
139 if (actor !== undefined && actorFieldsSave !== undefined) {
140 resetSequelizeInstance(actor, actorFieldsSave)
143 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
144 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
147 // This is just a debug because we will retry the insert
148 logger.debug('Cannot update the remote account.', { err })
153 async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
154 const playlistObject = activity.object as PlaylistObject
155 const byAccount = byActor.Account
157 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
159 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)