Skip to content

Commit

Permalink
fix thread/comment creation/editing
Browse files Browse the repository at this point in the history
  • Loading branch information
timolegros committed Sep 11, 2024
1 parent 568e17e commit 08966c7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
4 changes: 2 additions & 2 deletions libs/model/src/comment/UpdateComment.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { models } from '../database';
import { isAuthorized, type AuthContext } from '../middleware';
import { mustBeAuthorized } from '../middleware/guards';
import {
decodeContent,
emitMentions,
findMentionDiff,
parseUserMentions,
quillToPlain,
sanitizeQuillText,
uniqueMentions,
} from '../utils';

Expand Down Expand Up @@ -37,7 +37,7 @@ export function UpdateComment(): Command<
});

if (currentVersion?.text !== payload.text) {
const text = sanitizeQuillText(payload.text);
const text = decodeContent(payload.text);
const plaintext = quillToPlain(text);
const mentions = findMentionDiff(
parseUserMentions(currentVersion?.text),
Expand Down
28 changes: 1 addition & 27 deletions packages/commonwealth/scripts/decode-db-content.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,11 @@
import { dispose, logger } from '@hicommonwealth/core';
import { models } from '@hicommonwealth/model';
import { deltaToMarkdown } from 'quill-delta-to-markdown';
import { decodeContent, models } from '@hicommonwealth/model';
import { Op, QueryTypes } from 'sequelize';

const log = logger(import.meta);
const BATCH_SIZE = 10;
const queryCase = 'WHEN id = ? THEN ? ';

function decodeContent(content: string) {
// decode if URI encoded
let decodedContent: string;
try {
decodedContent = decodeURIComponent(content);
} catch {
decodedContent = content;
}

// convert to Markdown if Quill Delta format
let rawMarkdown: string;
try {
const delta = JSON.parse(decodedContent);
if ('ops' in delta) {
rawMarkdown = deltaToMarkdown(delta.ops);
} else {
rawMarkdown = delta;
}
} catch (e) {
rawMarkdown = decodedContent;
}

return rawMarkdown.trim();
}

async function decodeThreads(lastId: number = 0) {
let lastThreadId = lastId;
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ThreadAttributes,
ThreadInstance,
UserInstance,
decodeContent,
emitMentions,
findMentionDiff,
parseUserMentions,
Expand Down Expand Up @@ -246,7 +247,7 @@ export async function __updateThread(
{
thread_id: threadId!,
address: address.address,
body,
body: decodeContent(body),
timestamp: new Date(),
},
{
Expand Down Expand Up @@ -487,12 +488,12 @@ async function setThreadAttributes(
if (thread.kind === 'discussion' && (!body || !body.trim())) {
throw new AppError(Errors.NoBody);
}
toUpdate.body = body;
toUpdate.body = decodeContent(body);
toUpdate.plaintext = (() => {
try {
return renderQuillDeltaToText(JSON.parse(decodeURIComponent(body)));
return renderQuillDeltaToText(JSON.parse(body));
} catch (e) {
return decodeURIComponent(body);
return body;
}
})();
}
Expand Down

0 comments on commit 08966c7

Please sign in to comment.