Files
speckle-server/packages/server/modules/comments/services/commentTextService.ts
T
Kristaps Fabians Geikins bde148f286 chore(server): migrating fully to ESM (#5042)
* wip

* some extra fixes

* stuff kinda works?

* need to figure out mocks

* need to figure out mocks

* fix db listener

* gqlgen fix

* minor gqlgen watch adjustment

* lint fixes

* delete old codegen file

* converting migrations to ESM

* getModuleDIrectory

* vitest sort of works

* added back ts-vitest

* resolve gql double load

* fixing test timeout configs

* TSC lint fix

* fix automate tests

* moar debugging

* debugging

* more debugging

* codegen update

* server works

* yargs migrated

* chore(server): getting rid of global mocks for Server ESM (#5046)

* got rid of email mock

* got rid of comment mocks

* got rid of multi region mocks

* got rid of stripe mock

* admin override mock updated

* removed final mock

* fixing import.meta.resolve calls

* another import.meta.resolve fix

* added requested test

* nyc ESM fix

* removed unneeded deps + linting

* yarn lock forgot to commit

* tryna fix flakyness

* email capture util fix

* sendEmail fix

* fix TSX check

* sender transporter fix + CR comments

* merge main fix

* test fixx

* circleci fix

* gqlgen bigint fix

* error formatter fix

* more error formatting improvements

* esmloader added to Dockerfile

* more dockerfile fixes

* bg jobs fix
2025-07-14 10:26:19 +03:00

89 lines
2.6 KiB
TypeScript

import { RichTextParseError } from '@/modules/shared/errors'
import {
isTextEditorValueSchema,
isTextEditorDoc,
convertBasicStringToDocument,
isSerializedTextEditorValueSchema,
SmartTextEditorValueSchema,
isDocEmpty,
documentToBasicString
} from '@/modules/core/services/richTextEditorService'
import { isString, uniq } from 'lodash-es'
import { InvalidAttachmentsError } from '@/modules/comments/errors'
import { JSONContent } from '@tiptap/core'
import { ValidateInputAttachments } from '@/modules/comments/domain/operations'
import { GetBlobs } from '@/modules/blobstorage/domain/operations'
import { Nullable } from '@speckle/shared'
const COMMENT_SCHEMA_VERSION = '1.0.0'
const COMMENT_SCHEMA_TYPE = 'stream_comment'
export const validateInputAttachmentsFactory =
(deps: { getBlobs: GetBlobs }): ValidateInputAttachments =>
async (streamId: string, blobIds: string[]) => {
blobIds = uniq(blobIds || [])
if (!blobIds.length) return
const blobs = await deps.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
*/
export function buildCommentTextFromInput({
doc = undefined,
blobIds = []
}: Partial<{
doc: JSONContent | null
blobIds: string[]
}>) {
if ((!isTextEditorDoc(doc) || isDocEmpty(doc)) && !blobIds.length) {
throw new RichTextParseError(
'Attempting to build comment text without document & attachments!',
{
info: {
doc,
blobIds
}
}
)
}
return <SmartTextEditorValueSchema>{
version: COMMENT_SCHEMA_VERSION,
type: COMMENT_SCHEMA_TYPE,
doc,
blobIds
}
}
/**
* Ensure a comment value pulled from db (string or schema JSON) is formatted to be a text editor schema
*/
export function ensureCommentSchema(
stringOrSchema: SmartTextEditorValueSchema | string
) {
if (isTextEditorValueSchema(stringOrSchema)) return stringOrSchema
if (isString(stringOrSchema)) {
const deserializedSchema = isSerializedTextEditorValueSchema(stringOrSchema)
if (deserializedSchema) return deserializedSchema
// A basic string, convert it to the schema format
const basicTextDoc = convertBasicStringToDocument(stringOrSchema)
return buildCommentTextFromInput({ doc: basicTextDoc })
}
throw new RichTextParseError('Unexpected comment schema format')
}
export const commentTextToRawString = (
text: Nullable<SmartTextEditorValueSchema | string>
) => {
if (!text) return null
const schema = ensureCommentSchema(text)
return documentToBasicString(schema.doc)
}