X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-create.ts;h=791148919f2958c2e1a6e9528543cbb478d2962d;hb=22a16e36f6526887ed8f5e5d3c9f9e5da0b4a8cd;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..791148919 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -1,30 +1,33 @@ -import { ActivityCreate, VideoChannelObject } from '../../../../shared' +import { ActivityCreate, VideoAbuseState, VideoTorrentObject } from '../../../../shared' import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' -import { logger, retryTransactionWrapper } from '../../../helpers' +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 { VideoModel } from '../../../models/video/video' +import { ActorModel } from '../../../models/activitypub/actor' 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' +import { VideoCommentModel } from '../../../models/video/video-comment' +import { getOrCreateActorAndServerAndModel } from '../actor' +import { resolveThread } from '../video-comments' +import { getOrCreateAccountAndVideoAndChannel } from '../videos' +import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils' async function processCreateActivity (activity: ActivityCreate) { const activityObject = activity.object const activityType = activityObject.type - const account = await getOrCreateAccountAndServer(activity.actor) + const actor = await getOrCreateActorAndServerAndModel(activity.actor) if (activityType === 'View') { - return processCreateView(account, activity) + return processCreateView(actor, activity) } else if (activityType === 'Dislike') { - return processCreateDislike(account, activity) - } else if (activityType === 'VideoChannel') { - return processCreateVideoChannel(account, activityObject as VideoChannelObject) + return retryTransactionWrapper(processCreateDislike, actor, activity) + } else if (activityType === 'Video') { + return processCreateVideo(actor, activity) } else if (activityType === 'Flag') { - return processCreateVideoAbuse(account, activityObject as VideoAbuseObject) + return retryTransactionWrapper(processCreateVideoAbuse, actor, activityObject as VideoAbuseObject) + } else if (activityType === 'Note') { + return retryTransactionWrapper(processCreateVideoComment, actor, activity) } logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) @@ -39,22 +42,26 @@ export { // --------------------------------------------------------------------------- -async function processCreateDislike (byAccount: AccountModel, activity: ActivityCreate) { - const options = { - arguments: [ byAccount, activity ], - errorMessage: 'Cannot dislike the video with many retries.' - } +async function processCreateVideo ( + actor: ActorModel, + activity: ActivityCreate +) { + const videoToCreateData = activity.object as VideoTorrentObject + + const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor) - return retryTransactionWrapper(createVideoDislike, options) + return video } -function createVideoDislike (byAccount: AccountModel, activity: ActivityCreate) { +async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { const dislike = activity.object as DislikeObject + const byAccount = byActor.Account - return sequelizeTypescript.transaction(async t => { - const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) - if (!video) throw new Error('Unknown video ' + dislike.object) + if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) + const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) + + return sequelizeTypescript.transaction(async t => { const rate = { type: 'dislike' as 'dislike', videoId: video.id, @@ -69,91 +76,95 @@ function createVideoDislike (byAccount: AccountModel, activity: ActivityCreate) if (video.isOwned() && created === true) { // Don't resend the activity to the sender - const exceptions = [ byAccount ] - await forwardActivity(activity, t, exceptions) + const exceptions = [ byActor ] + + await forwardVideoRelatedActivity(activity, t, exceptions, video) } }) } -async function processCreateView (byAccount: AccountModel, activity: ActivityCreate) { +async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { const view = activity.object as ViewObject - const video = await VideoModel.loadByUrlAndPopulateAccount(view.object) - - if (!video) throw new Error('Unknown video ' + view.object) + const { video } = await getOrCreateAccountAndVideoAndChannel(view.object) - const account = await AccountModel.loadByUrl(view.actor) - if (!account) throw new Error('Unknown account ' + view.actor) + const actor = await ActorModel.loadByUrl(view.actor) + if (!actor) throw new Error('Unknown actor ' + view.actor) await video.increment('views') if (video.isOwned()) { // Don't resend the activity to the sender - const exceptions = [ byAccount ] + const exceptions = [ byActor ] await forwardActivity(activity, undefined, exceptions) } } -async function processCreateVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) { - const options = { - arguments: [ account, videoChannelToCreateData ], - errorMessage: 'Cannot insert the remote video channel with many retries.' - } - - const videoChannel = await retryTransactionWrapper(addRemoteVideoChannel, options) - - if (videoChannelToCreateData.shares && Array.isArray(videoChannelToCreateData.shares.orderedItems)) { - await addVideoChannelShares(videoChannel, videoChannelToCreateData.shares.orderedItems) - } +async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { + logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) - return videoChannel -} + const account = actor.Account + if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url) -function addRemoteVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) { - logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) + const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object) 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) + const videoAbuseData = { + reporterAccountId: account.id, + reason: videoAbuseToCreateData.content, + videoId: video.id, + state: VideoAbuseState.PENDING + } - videoChannel = await videoChannel.save({ transaction: t }) - logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid) + await VideoAbuseModel.create(videoAbuseData) - return videoChannel + logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) }) } -function processCreateVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) { - const options = { - arguments: [ account, videoAbuseToCreateData ], - errorMessage: 'Cannot insert the remote video abuse with many retries.' - } +async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { + const comment = activity.object as VideoCommentObject + const byAccount = byActor.Account - return retryTransactionWrapper(addRemoteVideoAbuse, options) -} + if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) -function addRemoteVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) { - logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) + const { video, parents } = await resolveThread(comment.inReplyTo) 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 + let originCommentId = null + let inReplyToCommentId = null + + if (parents.length !== 0) { + const parent = parents[0] + + originCommentId = parent.getThreadId() + inReplyToCommentId = parent.id } - const videoAbuseData = { - reporterAccountId: account.id, - reason: videoAbuseToCreateData.content, - videoId: video.id + // This is a new thread + const objectToCreate = { + url: comment.id, + text: comment.content, + originCommentId, + inReplyToCommentId, + videoId: video.id, + accountId: byAccount.id } - await VideoAbuseModel.create(videoAbuseData) + const options = { + where: { + url: objectToCreate.url + }, + defaults: objectToCreate, + transaction: t + } + const [ ,created ] = await VideoCommentModel.findOrCreate(options) - logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) + if (video.isOwned() && created === true) { + // Don't resend the activity to the sender + const exceptions = [ byActor ] + + await forwardVideoRelatedActivity(activity, t, exceptions, video) + } }) }