X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-create.ts;h=b9f584aa572572f36b81606aa21789ef5cc7f705;hb=6691c52280363fc42f7865230ebb3741f02fff23;hp=c1eb2a8ab2dbdf4240c124ba32d5eeb1e198ed1b;hpb=3fd3ab2d34d512b160a5e6084d7609be7b4f4452;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index c1eb2a8ab..b9f584aa5 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -1,30 +1,36 @@ -import { ActivityCreate, VideoChannelObject } from '../../../../shared' -import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' -import { logger, retryTransactionWrapper } from '../../../helpers' +import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared' +import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' +import { retryTransactionWrapper } from '../../../helpers/database-utils' +import { logger } from '../../../helpers/logger' import { sequelizeTypescript } from '../../../initializers' -import { AccountModel } from '../../../models/account/account' -import { AccountVideoRateModel } from '../../../models/account/account-video-rate' +import { ActorModel } from '../../../models/activitypub/actor' +import { addVideoComment, resolveThread } from '../video-comments' +import { getOrCreateVideoAndAccountAndChannel } from '../videos' +import { forwardVideoRelatedActivity } from '../send/utils' +import { createOrUpdateCacheFile } from '../cache-file' +import { Notifier } from '../../notifier' +import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' +import { createOrUpdateVideoPlaylist } from '../playlist' import { VideoModel } from '../../../models/video/video' -import { VideoAbuseModel } from '../../../models/video/video-abuse' -import { VideoChannelModel } from '../../../models/video/video-channel' -import { getOrCreateAccountAndServer } from '../account' -import { forwardActivity } from '../send/misc' -import { getVideoChannelActivityPubUrl } from '../url' -import { addVideoChannelShares, videoChannelActivityObjectToDBAttributes } from './misc' - -async function processCreateActivity (activity: ActivityCreate) { + +async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { const activityObject = activity.object const activityType = activityObject.type - const account = await getOrCreateAccountAndServer(activity.actor) - - if (activityType === 'View') { - return processCreateView(account, activity) - } else if (activityType === 'Dislike') { - return processCreateDislike(account, activity) - } else if (activityType === 'VideoChannel') { - return processCreateVideoChannel(account, activityObject as VideoChannelObject) - } else if (activityType === 'Flag') { - return processCreateVideoAbuse(account, activityObject as VideoAbuseObject) + + if (activityType === 'Video') { + return processCreateVideo(activity) + } + + if (activityType === 'Note') { + return retryTransactionWrapper(processCreateVideoComment, activity, byActor) + } + + if (activityType === 'CacheFile') { + return retryTransactionWrapper(processCreateCacheFile, activity, byActor) + } + + if (activityType === 'Playlist') { + return retryTransactionWrapper(processCreatePlaylist, activity, byActor) } logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) @@ -39,121 +45,68 @@ export { // --------------------------------------------------------------------------- -async function processCreateDislike (byAccount: AccountModel, activity: ActivityCreate) { - const options = { - arguments: [ byAccount, activity ], - errorMessage: 'Cannot dislike the video with many retries.' - } +async function processCreateVideo (activity: ActivityCreate) { + const videoToCreateData = activity.object as VideoTorrentObject - return retryTransactionWrapper(createVideoDislike, options) -} + const { video, created, autoBlacklisted } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) -function createVideoDislike (byAccount: AccountModel, activity: ActivityCreate) { - const dislike = activity.object as DislikeObject - - return sequelizeTypescript.transaction(async t => { - const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) - if (!video) throw new Error('Unknown video ' + dislike.object) - - const rate = { - type: 'dislike' as 'dislike', - videoId: video.id, - accountId: byAccount.id - } - const [ , created ] = await AccountVideoRateModel.findOrCreate({ - where: rate, - defaults: rate, - transaction: t - }) - if (created === true) await video.increment('dislikes', { transaction: t }) - - if (video.isOwned() && created === true) { - // Don't resend the activity to the sender - const exceptions = [ byAccount ] - await forwardActivity(activity, t, exceptions) - } - }) -} + if (created && !autoBlacklisted) Notifier.Instance.notifyOnNewVideo(video) -async function processCreateView (byAccount: AccountModel, activity: ActivityCreate) { - const view = activity.object as ViewObject - - const video = await VideoModel.loadByUrlAndPopulateAccount(view.object) + return video +} - if (!video) throw new Error('Unknown video ' + view.object) +async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) { + const cacheFile = activity.object as CacheFileObject - const account = await AccountModel.loadByUrl(view.actor) - if (!account) throw new Error('Unknown account ' + view.actor) + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) - await video.increment('views') + await sequelizeTypescript.transaction(async t => { + return createOrUpdateCacheFile(cacheFile, video, byActor, t) + }) if (video.isOwned()) { // Don't resend the activity to the sender - const exceptions = [ byAccount ] - await forwardActivity(activity, undefined, exceptions) + const exceptions = [ byActor ] + await forwardVideoRelatedActivity(activity, undefined, exceptions, video) } } -async function processCreateVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) { - const options = { - arguments: [ account, videoChannelToCreateData ], - errorMessage: 'Cannot insert the remote video channel with many retries.' +async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) { + const commentObject = activity.object as VideoCommentObject + const byAccount = byActor.Account + + if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) + + let video: VideoModel + try { + const resolveThreadResult = await resolveThread(commentObject.inReplyTo) + video = resolveThreadResult.video + } catch (err) { + logger.debug( + 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.', + commentObject.inReplyTo, + { err } + ) + return } - const videoChannel = await retryTransactionWrapper(addRemoteVideoChannel, options) + const { comment, created } = await addVideoComment(video, commentObject.id) - if (videoChannelToCreateData.shares && Array.isArray(videoChannelToCreateData.shares.orderedItems)) { - await addVideoChannelShares(videoChannel, videoChannelToCreateData.shares.orderedItems) - } - - return videoChannel -} - -function addRemoteVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) { - logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) - - return sequelizeTypescript.transaction(async t => { - let videoChannel = await VideoChannelModel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) - if (videoChannel) return videoChannel - - const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) - videoChannel = new VideoChannelModel(videoChannelData) - videoChannel.url = getVideoChannelActivityPubUrl(videoChannel) - - videoChannel = await videoChannel.save({ transaction: t }) - logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid) - - return videoChannel - }) -} + if (video.isOwned() && created === true) { + // Don't resend the activity to the sender + const exceptions = [ byActor ] -function processCreateVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) { - const options = { - arguments: [ account, videoAbuseToCreateData ], - errorMessage: 'Cannot insert the remote video abuse with many retries.' + await forwardVideoRelatedActivity(activity, undefined, exceptions, video) } - return retryTransactionWrapper(addRemoteVideoAbuse, options) + if (created === true) Notifier.Instance.notifyOnNewComment(comment) } -function addRemoteVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) { - logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) +async function processCreatePlaylist (activity: ActivityCreate, byActor: ActorModel) { + const playlistObject = activity.object as PlaylistObject + const byAccount = byActor.Account - return sequelizeTypescript.transaction(async t => { - const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) - if (!video) { - logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) - return undefined - } + if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url) - const videoAbuseData = { - reporterAccountId: account.id, - reason: videoAbuseToCreateData.content, - videoId: video.id - } - - await VideoAbuseModel.create(videoAbuseData) - - logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) - }) + await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to) }