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 { fetchAvatarIfExists, 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'
19 async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
20 const { activity, byActor } = options
22 const objectType = activity.object.type
24 if (objectType === 'Video') {
25 return retryTransactionWrapper(processUpdateVideo, byActor, activity)
28 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
29 // We need more attributes
30 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
31 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
34 if (objectType === 'CacheFile') {
35 // We need more attributes
36 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
37 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
40 if (objectType === 'Playlist') {
41 return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
47 // ---------------------------------------------------------------------------
53 // ---------------------------------------------------------------------------
55 async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
56 const videoObject = activity.object as VideoTorrentObject
58 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
59 logger.debug('Video sent by update is not valid.', { videoObject })
63 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false })
64 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
66 const updateOptions = {
69 account: actor.Account,
70 channel: channelActor.VideoChannel,
71 overrideTo: activity.to
73 return updateVideoFromAP(updateOptions)
76 async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
77 const cacheFileObject = activity.object as CacheFileObject
79 if (!isCacheFileObjectValid(cacheFileObject)) {
80 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
84 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
86 await sequelizeTypescript.transaction(async t => {
87 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
90 if (video.isOwned()) {
91 // Don't resend the activity to the sender
92 const exceptions = [ byActor ]
94 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
98 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
99 const actorAttributesToUpdate = activity.object as ActivityPubActor
101 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
102 let accountOrChannelInstance: AccountModel | VideoChannelModel
103 let actorFieldsSave: object
104 let accountOrChannelFieldsSave: object
107 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
110 await sequelizeTypescript.transaction(async t => {
111 actorFieldsSave = actor.toJSON()
113 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
114 else accountOrChannelInstance = actor.Account
116 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
118 await updateActorInstance(actor, actorAttributesToUpdate)
120 if (avatarName !== undefined) {
121 await updateActorAvatarInstance(actor, avatarName, t)
124 await actor.save({ transaction: t })
126 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
127 accountOrChannelInstance.description = actorAttributesToUpdate.summary
129 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
131 await accountOrChannelInstance.save({ transaction: t })
134 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
136 if (actor !== undefined && actorFieldsSave !== undefined) {
137 resetSequelizeInstance(actor, actorFieldsSave)
140 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
141 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
144 // This is just a debug because we will retry the insert
145 logger.debug('Cannot update the remote account.', { err })
150 async function processUpdatePlaylist (byActor: ActorModel, activity: ActivityUpdate) {
151 const playlistObject = activity.object as PlaylistObject
152 const byAccount = byActor.Account
154 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
156 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)