X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-create.ts;h=ef5a3100e8cd52271b82637d0a79586ced4e69d2;hb=304a84d59c3a800b7f7aef48cf55f307534c0926;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..ef5a3100e 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -1,30 +1,42 @@ -import { ActivityCreate, VideoChannelObject } from '../../../../shared' -import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' -import { logger, retryTransactionWrapper } from '../../../helpers' -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 { 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) { +import { isRedundancyAccepted } from '@server/lib/redundancy' +import { ActivityCreate, CacheFileObject, VideoObject } from '../../../../shared' +import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' +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/database' +import { APProcessorOptions } from '../../../types/activitypub-processor.model' +import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../types/models' +import { Notifier } from '../../notifier' +import { createOrUpdateCacheFile } from '../cache-file' +import { createOrUpdateVideoPlaylist } from '../playlist' +import { forwardVideoRelatedActivity } from '../send/utils' +import { resolveThread } from '../video-comments' +import { getOrCreateAPVideo } from '../videos' +import { isBlockedByServerOrAccount } from '@server/lib/blocklist' + +async function processCreateActivity (options: APProcessorOptions) { + const { activity, byActor } = options + + // Only notify if it is not from a fetcher job + const notify = options.fromFetch !== true 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, notify) + } + + if (activityType === 'Note') { + return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify) + } + + 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 +51,82 @@ export { // --------------------------------------------------------------------------- -async function processCreateDislike (byAccount: AccountModel, activity: ActivityCreate) { - const options = { - arguments: [ byAccount, activity ], - errorMessage: 'Cannot dislike the video with many retries.' - } - - return retryTransactionWrapper(createVideoDislike, options) -} +async function processCreateVideo (activity: ActivityCreate, notify: boolean) { + const videoToCreateData = activity.object as VideoObject -function createVideoDislike (byAccount: AccountModel, activity: ActivityCreate) { - const dislike = activity.object as DislikeObject + const syncParam = { likes: false, dislikes: false, shares: false, comments: false, thumbnail: true, refreshVideo: false } + const { video, created } = await getOrCreateAPVideo({ videoObject: videoToCreateData, syncParam }) - return sequelizeTypescript.transaction(async t => { - const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) - if (!video) throw new Error('Unknown video ' + dislike.object) + if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video) - 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) - } - }) + return video } -async function processCreateView (byAccount: AccountModel, activity: ActivityCreate) { - const view = activity.object as ViewObject +async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) { + if (await isRedundancyAccepted(activity, byActor) !== true) return - const video = await VideoModel.loadByUrlAndPopulateAccount(view.object) + const cacheFile = activity.object as CacheFileObject - if (!video) throw new Error('Unknown video ' + view.object) + const { video } = await getOrCreateAPVideo({ videoObject: cacheFile.object }) - const account = await AccountModel.loadByUrl(view.actor) - if (!account) throw new Error('Unknown account ' + view.actor) - - 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: MActorSignature, notify: boolean) { + 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: MVideoAccountLightBlacklistAllFiles + let created: boolean + let comment: MCommentOwnerVideo + try { + const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false }) + + video = resolveThreadResult.video + created = resolveThreadResult.commentCreated + comment = resolveThreadResult.comment + } 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) - - 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) + // Try to not forward unwanted commments on our videos + if (video.isOwned()) { + if (await isBlockedByServerOrAccount(comment.Account, video.VideoChannel.Account)) { + logger.info('Skip comment forward from blocked account or server %s.', comment.Account.Actor.url) + return + } - return videoChannel - }) -} + if (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 && notify) Notifier.Instance.notifyOnNewComment(comment) } -function addRemoteVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) { - logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) - - 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 - } - - const videoAbuseData = { - reporterAccountId: account.id, - reason: videoAbuseToCreateData.content, - videoId: video.id - } +async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) { + const playlistObject = activity.object as PlaylistObject + const byAccount = byActor.Account - await VideoAbuseModel.create(videoAbuseData) + if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url) - logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) - }) + await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to) }