Skip to content

Commit

Permalink
Merge pull request #9186 from hicommonwealth/tim/decode-db-contents-s…
Browse files Browse the repository at this point in the history
…cripts
  • Loading branch information
timolegros authored Sep 16, 2024
2 parents 954e5bd + a5653e7 commit 889a63d
Show file tree
Hide file tree
Showing 17 changed files with 405 additions and 32 deletions.
1 change: 1 addition & 0 deletions libs/model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"node-fetch": "2",
"node-object-hash": "^3.0.0",
"pg": "^8.11.3",
"quill-delta-to-markdown": "^0.6.0",
"sequelize": "^6.32.1",
"umzug": "^3.7.0",
"uuid": "^9.0.1",
Expand Down
5 changes: 2 additions & 3 deletions libs/model/src/comment/CreateComment.command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventNames, InvalidState, type Command } from '@hicommonwealth/core';
import { getCommentSearchVector } from '@hicommonwealth/model';
import { decodeContent, getCommentSearchVector } from '@hicommonwealth/model';
import * as schemas from '@hicommonwealth/schemas';
import { models } from '../database';
import { isAuthorized, type AuthContext } from '../middleware';
Expand All @@ -10,7 +10,6 @@ import {
emitMentions,
parseUserMentions,
quillToPlain,
sanitizeQuillText,
uniqueMentions,
} from '../utils';
import { getCommentDepth } from '../utils/getCommentDepth';
Expand Down Expand Up @@ -53,7 +52,7 @@ export function CreateComment(): Command<
throw new InvalidState(CreateCommentErrors.NestingTooDeep);
}

const text = sanitizeQuillText(payload.text);
const text = decodeContent(payload.text);
const plaintext = quillToPlain(text);
const mentions = uniqueMentions(parseUserMentions(text));

Expand Down
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 @@ -5,11 +5,11 @@ import { isAuthorized, type AuthContext } from '../middleware';
import { mustBeAuthorized } from '../middleware/guards';
import { getCommentSearchVector } from '../models';
import {
decodeContent,
emitMentions,
findMentionDiff,
parseUserMentions,
quillToPlain,
sanitizeQuillText,
uniqueMentions,
} from '../utils';

Expand Down Expand Up @@ -38,7 +38,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
4 changes: 2 additions & 2 deletions libs/model/src/thread/CreateThread.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import { mustBeAuthorized } from '../middleware/guards';
import { getThreadSearchVector } from '../models/thread';
import { tokenBalanceCache } from '../services';
import {
decodeContent,
emitMentions,
parseUserMentions,
quillToPlain,
sanitizeQuillText,
uniqueMentions,
} from '../utils';

Expand Down Expand Up @@ -112,7 +112,7 @@ export function CreateThread(): Command<
checkContestLimits(activeContestManagers, actor.address!);
}

const body = sanitizeQuillText(payload.body);
const body = decodeContent(payload.body);
const plaintext = kind === 'discussion' ? quillToPlain(body) : body;
const mentions = uniqueMentions(parseUserMentions(body));

Expand Down
27 changes: 27 additions & 0 deletions libs/model/src/utils/decodeContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @ts-expect-error quill-delta-to-markdown doesn't have types
import { deltaToMarkdown } from 'quill-delta-to-markdown';

export 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();
}
1 change: 1 addition & 0 deletions libs/model/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './decodeContent';
export * from './denormalizedCountUtils';
export * from './getDelta';
export * from './parseUserMentions';
Expand Down
8 changes: 8 additions & 0 deletions libs/shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,11 @@ export function getWebhookDestination(webhookUrl = ''): string {

return destination;
}

export function getDecodedString(str: string) {
try {
return decodeURIComponent(str);
} catch (err) {
return str;
}
}
3 changes: 2 additions & 1 deletion packages/commonwealth/client/scripts/models/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDecodedString } from '@hicommonwealth/shared';
import type momentType from 'moment';
import moment, { Moment } from 'moment';
import AddressInfo from './AddressInfo';
Expand Down Expand Up @@ -64,7 +65,7 @@ export class Comment<T extends IUniqueId> {
const versionHistory = CommentVersionHistories;
this.communityId = community_id;
this.author = Address?.address || author;
this.text = deleted_at?.length > 0 ? '[deleted]' : decodeURIComponent(text);
this.text = deleted_at?.length > 0 ? '[deleted]' : getDecodedString(text);
this.plaintext = deleted_at?.length > 0 ? '[deleted]' : plaintext;
this.versionHistory = versionHistory;
this.threadId = thread_id;
Expand Down
11 changes: 1 addition & 10 deletions packages/commonwealth/client/scripts/models/Thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ContestManager,
ContestScore,
} from '@hicommonwealth/schemas';
import { ProposalType } from '@hicommonwealth/shared';
import { ProposalType, getDecodedString } from '@hicommonwealth/shared';
import { UserProfile, addressToUserProfile } from 'models/MinimumProfile';
import moment, { Moment } from 'moment';
import { z } from 'zod';
Expand All @@ -14,15 +14,6 @@ import Topic from './Topic';
import type { IUniqueId } from './interfaces';
import type { ThreadKind, ThreadStage } from './types';

function getDecodedString(str: string) {
try {
return decodeURIComponent(str);
} catch (err) {
console.error(`Could not decode str: "${str}"`);
return str;
}
}

function processAssociatedContests(
associatedContests?: AssociatedContest[] | null,
contestActions?: ContestActionT[] | null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const buildCreateCommentInput = async ({
return {
thread_id: threadId,
parent_id: parentCommentId ?? undefined,
text: encodeURIComponent(unescapedText),
text: unescapedText,
...toCanvasSignedDataApiArgs(canvasSignedData),
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const buildUpdateCommentInput = async ({
author_community_id: communityId,
comment_id: commentId,
community_id: communityId,
text: encodeURIComponent(updatedBody),
text: updatedBody,
jwt: userStore.getState().jwt,
...toCanvasSignedDataApiArgs(canvasSignedData),
};
Expand All @@ -65,8 +65,6 @@ const useEditCommentMutation = ({
onSuccess: async (updatedComment) => {
// @ts-expect-error StrictNullChecks
const comment = new Comment(updatedComment);
console.log({ comment, updatedComment });

// update fetch comments query state with updated comment
const key = [ApiEndpoints.FETCH_COMMENTS, communityId, threadId];
queryClient.cancelQueries({ queryKey: key });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const buildCreateThreadInput = async ({
return {
community_id: communityId,
topic_id: topic.id,
title: encodeURIComponent(title),
body: encodeURIComponent(body ?? ''),
title: title,
body: body ?? '',
kind,
stage,
url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export const buildUpdateThreadInput = async ({
jwt: userStore.getState().jwt,
// for edit profile
...(url && { url }),
...(newBody && { body: encodeURIComponent(newBody) }),
...(newTitle && { title: encodeURIComponent(newTitle) }),
...(newBody && { body: newBody }),
...(newTitle && { title: newTitle }),
...(authorProfile && { author: JSON.stringify(authorProfile) }),
// for editing thread locked status
...(readOnly !== undefined && { locked: readOnly }),
Expand Down
4 changes: 2 additions & 2 deletions packages/commonwealth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
"buffer": "^6.0.3",
"cheerio": "1.0.0-rc.3",
"clsx": "^1.2.1",
"commonwealth-mdxeditor": "^0.0.3",
"compression": "^1.7.4",
"connect-session-sequelize": "^7.1.1",
"cookie-parser": "^1.4.4",
Expand Down Expand Up @@ -261,8 +262,7 @@
"web3-validator": "2.0.5",
"yargs": "^17.7.2",
"zod": "^3.22.4",
"zustand": "^4.3.8",
"commonwealth-mdxeditor": "^0.0.3"
"zustand": "^4.3.8"
},
"devDependencies": {
"@ethersproject/keccak256": "5.7.0",
Expand Down
Loading

0 comments on commit 889a63d

Please sign in to comment.