X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fvideo-comments.ts;h=c8c17f4c41cfb88ae056d8b58b39d9ddf572f68d;hb=8578e3b5e630f0cfe1dfbf13a7be1e6b89554077;hp=17c86a38144aaa371d17228b091e974fe9c30cf3;hpb=2ccaeeb341ffe8c2609039bf4c6d8835b4650316;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/video-comments.ts b/server/lib/activitypub/video-comments.ts index 17c86a381..c8c17f4c4 100644 --- a/server/lib/activitypub/video-comments.ts +++ b/server/lib/activitypub/video-comments.ts @@ -1,13 +1,14 @@ import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' -import { isVideoCommentObjectValid } from '../../helpers/custom-validators/activitypub/video-comments' +import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments' import { logger } from '../../helpers/logger' import { doRequest } from '../../helpers/requests' -import { ACTIVITY_PUB } from '../../initializers' +import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers' import { ActorModel } from '../../models/activitypub/actor' import { VideoModel } from '../../models/video/video' import { VideoCommentModel } from '../../models/video/video-comment' import { getOrCreateActorAndServerAndModel } from './actor' -import { getOrCreateAccountAndVideoAndChannel } from './videos' +import { getOrCreateVideoAndAccountAndChannel } from './videos' +import * as Bluebird from 'bluebird' async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) { let originCommentId: number = null @@ -15,7 +16,7 @@ async function videoCommentActivityObjectToDBAttributes (video: VideoModel, acto // If this is not a reply to the video (thread), create or get the parent comment if (video.url !== comment.inReplyTo) { - const [ parent ] = await addVideoComment(video, comment.inReplyTo) + const { comment: parent } = await addVideoComment(video, comment.inReplyTo) if (!parent) { logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id) return undefined @@ -26,7 +27,7 @@ async function videoCommentActivityObjectToDBAttributes (video: VideoModel, acto } return { - url: comment.url, + url: comment.id, text: comment.content, videoId: video.id, accountId: actor.Account.id, @@ -37,10 +38,10 @@ async function videoCommentActivityObjectToDBAttributes (video: VideoModel, acto } } -async function addVideoComments (instance: VideoModel, commentUrls: string[]) { - for (const commentUrl of commentUrls) { - await addVideoComment(instance, commentUrl) - } +async function addVideoComments (commentUrls: string[], instance: VideoModel) { + return Bluebird.map(commentUrls, commentUrl => { + return addVideoComment(instance, commentUrl) + }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) } async function addVideoComment (videoInstance: VideoModel, commentUrl: string) { @@ -52,24 +53,26 @@ async function addVideoComment (videoInstance: VideoModel, commentUrl: string) { activityPub: true }) - if (isVideoCommentObjectValid(body) === false) { + if (sanitizeAndCheckVideoCommentObject(body) === false) { logger.debug('Remote video comment JSON is not valid.', { body }) - return undefined + return { created: false } } const actorUrl = body.attributedTo - if (!actorUrl) return [] + if (!actorUrl) return { created: false } const actor = await getOrCreateActorAndServerAndModel(actorUrl) const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body) - if (!entry) return [] + if (!entry) return { created: false } - return VideoCommentModel.findOrCreate({ + const [ comment, created ] = await VideoCommentModel.findOrCreate({ where: { url: body.id }, defaults: entry }) + + return { comment, created } } async function resolveThread (url: string, comments: VideoCommentModel[] = []) { @@ -81,7 +84,6 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) { // Speed up things and resolve directly the thread if (commentFromDatabase.InReplyToVideoComment) { const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC') - console.log(data) parentComments = parentComments.concat(data) } @@ -91,7 +93,8 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) { try { // Maybe it's a reply to a video? - const { video } = await getOrCreateAccountAndVideoAndChannel(url) + // If yes, it's done: we resolved all the thread + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url }) if (comments.length !== 0) { const firstReply = comments[ comments.length - 1 ] @@ -112,7 +115,7 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) { return { video, parents: comments } } catch (err) { - logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, err) + logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err }) if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) { throw new Error('Recursion limit reached when resolving a thread') @@ -124,7 +127,7 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) { activityPub: true }) - if (isVideoCommentObjectValid(body) === false) { + if (sanitizeAndCheckVideoCommentObject(body) === false) { throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body)) } @@ -133,7 +136,7 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) { const actor = await getOrCreateActorAndServerAndModel(actorUrl) const comment = new VideoCommentModel({ - url: body.url, + url: body.id, text: body.content, videoId: null, accountId: actor.Account.id,