feat: stream comment attachments
This commit is contained in:
@@ -5,25 +5,43 @@ const {
|
||||
convertBasicStringToDocument,
|
||||
isSerializedTextEditorValueSchema
|
||||
} = require('@/modules/core/services/richTextEditorService')
|
||||
const { isString } = require('lodash')
|
||||
const { isString, uniq } = require('lodash')
|
||||
const { getBlobs } = require('@/modules/blobstorage/services')
|
||||
const { InvalidAttachmentsError } = require('@/modules/comments/errors')
|
||||
|
||||
const COMMENT_SCHEMA_VERSION = '1.0.0'
|
||||
const COMMENT_SCHEMA_TYPE = 'stream_comment'
|
||||
|
||||
async function validateInputAttachments(streamId, blobIds) {
|
||||
blobIds = uniq(blobIds || [])
|
||||
if (!blobIds.length) return
|
||||
|
||||
const blobs = await getBlobs({ blobIds, streamId })
|
||||
if (!blobs || blobs.length !== blobIds.length) {
|
||||
throw new InvalidAttachmentsError('Attempting to attach invalid blobs to comment')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build comment.text value from a ProseMirror doc
|
||||
* @param {import("@tiptap/core").JSONContent} doc
|
||||
* @param {{
|
||||
* doc: import("@tiptap/core").JSONContent | undefined,
|
||||
* blobIds: string[]
|
||||
* }} param1
|
||||
* @returns {import('@/modules/core/services/richTextEditorService').SmartTextEditorValueSchema}
|
||||
*/
|
||||
function buildCommentTextFromInput(doc) {
|
||||
if (!isTextEditorDoc(doc)) {
|
||||
throw new RichTextParseError('Unexpected comment input doc!')
|
||||
function buildCommentTextFromInput({ doc = undefined, blobIds = [] }) {
|
||||
if (!isTextEditorDoc(doc) && !blobIds.length) {
|
||||
throw new RichTextParseError(
|
||||
'Attempting to build comment text without document & attachments!'
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
version: COMMENT_SCHEMA_VERSION,
|
||||
type: COMMENT_SCHEMA_TYPE,
|
||||
doc
|
||||
doc,
|
||||
blobIds
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +58,7 @@ function ensureCommentSchema(stringOrSchema) {
|
||||
|
||||
// A basic string, convert it to the schema format
|
||||
const basicTextDoc = convertBasicStringToDocument(stringOrSchema)
|
||||
return buildCommentTextFromInput(basicTextDoc)
|
||||
return buildCommentTextFromInput({ doc: basicTextDoc })
|
||||
}
|
||||
|
||||
throw new RichTextParseError('Unexpected comment schema format')
|
||||
@@ -48,5 +66,6 @@ function ensureCommentSchema(stringOrSchema) {
|
||||
|
||||
module.exports = {
|
||||
buildCommentTextFromInput,
|
||||
ensureCommentSchema
|
||||
ensureCommentSchema,
|
||||
validateInputAttachments
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user