2018-09-11 10:27:07 -04:00
|
|
|
import { ActivityUpdate, CacheFileObject, VideoTorrentObject } from '../../../../shared/models/activitypub'
|
2018-01-03 10:38:50 -05:00
|
|
|
import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
|
2018-08-14 09:28:30 -04:00
|
|
|
import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
2018-01-03 10:38:50 -05:00
|
|
|
import { AccountModel } from '../../../models/account/account'
|
2017-12-14 11:38:41 -05:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
2018-02-15 08:46:26 -05:00
|
|
|
import { VideoChannelModel } from '../../../models/video/video-channel'
|
2018-09-19 08:44:20 -04:00
|
|
|
import { fetchAvatarIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor'
|
|
|
|
import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
|
2018-06-13 04:06:50 -04:00
|
|
|
import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
|
2018-09-11 10:27:07 -04:00
|
|
|
import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
|
2018-10-02 08:39:35 -04:00
|
|
|
import { createOrUpdateCacheFile } from '../cache-file'
|
2018-09-24 07:07:33 -04:00
|
|
|
import { forwardVideoRelatedActivity } from '../send/utils'
|
2019-02-26 04:55:40 -05:00
|
|
|
import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
|
|
|
|
import { createOrUpdateVideoPlaylist } from '../playlist'
|
2017-11-10 08:34:45 -05:00
|
|
|
|
2018-09-19 08:44:20 -04:00
|
|
|
async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) {
|
2018-02-15 08:46:26 -05:00
|
|
|
const objectType = activity.object.type
|
2017-11-09 11:51:58 -05:00
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
if (objectType === 'Video') {
|
2018-09-19 08:44:20 -04:00
|
|
|
return retryTransactionWrapper(processUpdateVideo, byActor, activity)
|
2018-09-11 10:27:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
|
2018-09-19 08:44:20 -04:00
|
|
|
// We need more attributes
|
|
|
|
const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
|
|
|
|
return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
2017-11-10 08:34:45 -05:00
|
|
|
|
2018-09-11 10:27:07 -04:00
|
|
|
if (objectType === 'CacheFile') {
|
2018-09-19 08:44:20 -04:00
|
|
|
// We need more attributes
|
|
|
|
const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
|
|
|
|
return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
|
2018-09-11 10:27:07 -04:00
|
|
|
}
|
|
|
|
|
2019-02-26 04:55:40 -05:00
|
|
|
if (objectType === 'Playlist') {
|
|
|
|
return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
|
|
|
|
}
|
|
|
|
|
2018-06-13 04:06:50 -04:00
|
|
|
return undefined
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processUpdateActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-06-13 08:27:40 -04:00
|
|
|
async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
|
2018-06-13 04:06:50 -04:00
|
|
|
const videoObject = activity.object as VideoTorrentObject
|
2017-12-14 11:38:41 -05:00
|
|
|
|
2018-06-13 04:06:50 -04:00
|
|
|
if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
|
|
|
|
logger.debug('Video sent by update is not valid.', { videoObject })
|
|
|
|
return undefined
|
|
|
|
}
|
2018-06-12 14:04:58 -04:00
|
|
|
|
2018-12-04 09:12:54 -05:00
|
|
|
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false })
|
2018-08-23 11:58:39 -04:00
|
|
|
const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2018-09-19 05:41:21 -04:00
|
|
|
const updateOptions = {
|
|
|
|
video,
|
|
|
|
videoObject,
|
|
|
|
account: actor.Account,
|
|
|
|
channel: channelActor.VideoChannel,
|
|
|
|
overrideTo: activity.to
|
|
|
|
}
|
|
|
|
return updateVideoFromAP(updateOptions)
|
2018-09-11 10:27:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
|
|
|
|
const cacheFileObject = activity.object as CacheFileObject
|
|
|
|
|
2018-09-24 07:07:33 -04:00
|
|
|
if (!isCacheFileObjectValid(cacheFileObject)) {
|
|
|
|
logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
|
2018-09-11 10:27:07 -04:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2018-09-24 07:07:33 -04:00
|
|
|
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
|
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2018-10-02 08:39:35 -04:00
|
|
|
await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
|
2018-09-24 07:07:33 -04:00
|
|
|
})
|
2018-09-11 10:27:07 -04:00
|
|
|
|
2018-09-24 07:07:33 -04:00
|
|
|
if (video.isOwned()) {
|
|
|
|
// Don't resend the activity to the sender
|
|
|
|
const exceptions = [ byActor ]
|
|
|
|
|
|
|
|
await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
|
|
|
|
}
|
2017-11-10 08:34:45 -05:00
|
|
|
}
|
2018-01-03 10:38:50 -05:00
|
|
|
|
2018-06-13 08:27:40 -04:00
|
|
|
async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
|
2018-02-15 08:46:26 -05:00
|
|
|
const actorAttributesToUpdate = activity.object as ActivityPubActor
|
2018-01-03 10:38:50 -05:00
|
|
|
|
2019-05-31 08:02:26 -04:00
|
|
|
logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
|
2018-02-15 08:46:26 -05:00
|
|
|
let accountOrChannelInstance: AccountModel | VideoChannelModel
|
2018-01-03 10:38:50 -05:00
|
|
|
let actorFieldsSave: object
|
2018-02-15 08:46:26 -05:00
|
|
|
let accountOrChannelFieldsSave: object
|
2018-01-03 10:38:50 -05:00
|
|
|
|
|
|
|
// Fetch icon?
|
2018-02-15 08:46:26 -05:00
|
|
|
const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
|
2018-01-03 10:38:50 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2018-01-04 08:04:02 -05:00
|
|
|
actorFieldsSave = actor.toJSON()
|
2018-01-03 10:38:50 -05:00
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
|
|
|
|
else accountOrChannelInstance = actor.Account
|
|
|
|
|
|
|
|
accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
|
|
|
|
|
|
|
|
await updateActorInstance(actor, actorAttributesToUpdate)
|
2018-01-03 10:38:50 -05:00
|
|
|
|
2018-01-04 08:04:02 -05:00
|
|
|
if (avatarName !== undefined) {
|
|
|
|
await updateActorAvatarInstance(actor, avatarName, t)
|
2018-01-03 10:38:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
await actor.save({ transaction: t })
|
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
|
|
|
|
accountOrChannelInstance.description = actorAttributesToUpdate.summary
|
|
|
|
|
|
|
|
if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
|
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
await accountOrChannelInstance.save({ transaction: t })
|
2018-01-03 10:38:50 -05:00
|
|
|
})
|
|
|
|
|
2019-05-31 08:02:26 -04:00
|
|
|
logger.info('Remote account %s updated', actorAttributesToUpdate.url)
|
2018-01-03 10:38:50 -05:00
|
|
|
} catch (err) {
|
2018-01-04 08:04:02 -05:00
|
|
|
if (actor !== undefined && actorFieldsSave !== undefined) {
|
|
|
|
resetSequelizeInstance(actor, actorFieldsSave)
|
2018-01-03 10:38:50 -05:00
|
|
|
}
|
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
|
|
|
|
resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
|
2018-01-03 10:38:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is just a debug because we will retry the insert
|
2018-03-26 09:54:13 -04:00
|
|
|
logger.debug('Cannot update the remote account.', { err })
|
2018-01-03 10:38:50 -05:00
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2019-02-26 04:55:40 -05:00
|
|
|
|
|
|
|
async function processUpdatePlaylist (byActor: ActorModel, activity: ActivityUpdate) {
|
|
|
|
const playlistObject = activity.object as PlaylistObject
|
|
|
|
const byAccount = byActor.Account
|
|
|
|
|
|
|
|
if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
|
|
|
|
|
|
|
|
await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
|
|
|
|
}
|