X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-create.ts;h=ef5a3100e8cd52271b82637d0a79586ced4e69d2;hb=304a84d59c3a800b7f7aef48cf55f307534c0926;hp=c88082bbf537cd0d174d681e048b32a26b451f43;hpb=4e50b6a1c9a3eb261e04ede73241648e6edf21d6;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index c88082bbf..ef5a3100e 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -1,28 +1,42 @@ -import { ActivityCreate, VideoChannelObject } from '../../../../shared' -import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' -import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object' -import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object' -import { logger, retryTransactionWrapper } from '../../../helpers' -import { database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' -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 }) @@ -37,121 +51,82 @@ export { // --------------------------------------------------------------------------- -async function processCreateDislike (byAccount: AccountInstance, 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: AccountInstance, 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 db.sequelize.transaction(async t => { - const video = await db.Video.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 db.AccountVideoRate.findOrCreate({ - where: rate, - defaults: rate, - transaction: t - }) - 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: AccountInstance, 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 db.Video.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 db.Account.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: AccountInstance, 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: AccountInstance, videoChannelToCreateData: VideoChannelObject) { - logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) - - return db.sequelize.transaction(async t => { - let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) - if (videoChannel) return videoChannel - - const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) - videoChannel = db.VideoChannel.build(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: AccountInstance, 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: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { - logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) - - return db.sequelize.transaction(async t => { - const video = await db.Video.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 db.VideoAbuse.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) }