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'
18 async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) {
19 const objectType = activity.object.type
21 if (objectType === 'Video') {
22 return retryTransactionWrapper(processUpdateVideo, byActor, activity)
25 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
26 // We need more attributes
27 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
28 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
31 if (objectType === 'CacheFile') {
32 // We need more attributes
33 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
34 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
37 if (objectType === 'Playlist') {
38 return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
44 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
53 const videoObject = activity.object as VideoTorrentObject
55 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
56 logger.debug('Video sent by update is not valid.', { videoObject })
60 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false })
61 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
63 const updateOptions = {
66 account: actor.Account,
67 channel: channelActor.VideoChannel,
68 overrideTo: activity.to
70 return updateVideoFromAP(updateOptions)
73 async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
74 const cacheFileObject = activity.object as CacheFileObject
76 if (!isCacheFileObjectValid(cacheFileObject)) {
77 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
81 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
83 await sequelizeTypescript.transaction(async t => {
84 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
87 if (video.isOwned()) {
88 // Don't resend the activity to the sender
89 const exceptions = [ byActor ]
91 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
95 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
96 const actorAttributesToUpdate = activity.object as ActivityPubActor
98 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
99 let accountOrChannelInstance: AccountModel | VideoChannelModel
100 let actorFieldsSave: object
101 let accountOrChannelFieldsSave: object
104 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
107 await sequelizeTypescript.transaction(async t => {
108 actorFieldsSave = actor.toJSON()
110 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
111 else accountOrChannelInstance = actor.Account
113 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
115 await updateActorInstance(actor, actorAttributesToUpdate)
117 if (avatarName !== undefined) {
118 await updateActorAvatarInstance(actor, avatarName, t)
121 await actor.save({ transaction: t })
123 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
124 accountOrChannelInstance.description = actorAttributesToUpdate.summary
126 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
128 await accountOrChannelInstance.save({ transaction: t })
131 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
133 if (actor !== undefined && actorFieldsSave !== undefined) {
134 resetSequelizeInstance(actor, actorFieldsSave)
137 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
138 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
141 // This is just a debug because we will retry the insert
142 logger.debug('Cannot update the remote account.', { err })
147 async function processUpdatePlaylist (byActor: ActorModel, activity: ActivityUpdate) {
148 const playlistObject = activity.object as PlaylistObject
149 const byAccount = byActor.Account
151 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
153 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)