Skip to content

Commit

Permalink
Comment model semantics (#6199)
Browse files Browse the repository at this point in the history
* init migration + model index fixes

* update Sequelize model + fix type errors on server

* more fixes

* raw queries

* raw queries

* fix unit test + skip legacy TBC unit test

* fix stats

* update getCommentDepth to use recursive query rather than recursive function with multiple queries

* remove todo from migration

* down migration

* fix unit tests

* getcommentDepth integration tests

* remove .only

* remove .skip since TBC removal was merged
  • Loading branch information
timolegros authored Jan 5, 2024
1 parent dd7a963 commit 5e4fd9a
Show file tree
Hide file tree
Showing 42 changed files with 321 additions and 202 deletions.
2 changes: 1 addition & 1 deletion knowledge_base/Over-Fetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ SELECT
"Thread->Address"."wallet_id" AS "Thread.Address.wallet_id",
"Thread->Address"."user_id" AS "Thread.Address.UserId",
"Comment"."id" AS "Comment.id",
"Comment"."chain" AS "Comment.chain",
"Comment"."community_id" AS "Comment.community_id",
"Comment"."thread_id" AS "Comment.thread_id",
"Comment"."parent_id" AS "Comment.parent_id",
"Comment"."address_id" AS "Comment.address_id",
Expand Down
13 changes: 5 additions & 8 deletions packages/commonwealth/client/scripts/models/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@ import type { IUniqueId } from './interfaces';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export class Comment<T extends IUniqueId> {
[x: string]: any;

public readonly chain: string;
public readonly communityId: string;
public readonly author: string;
public readonly Address: AddressInfo;
public readonly text: string;
public readonly plaintext: string;
public readonly reactions: Reaction[];
public readonly id: number;
public readonly created_at: momentType.Moment;
public readonly createdAt: momentType.Moment;
public readonly authorChain?: string;
public readonly parentComment: number;
public readonly threadId: number;
public readonly version_history: VersionHistory[];
public readonly versionHistory: VersionHistory[];
public readonly lastEdited: string;
public markedAsSpamAt: momentType.Moment;
public readonly deleted: boolean;
public readonly rootThread: string;
public readonly parentId: number;
public readonly deletedAt: any;

public readonly canvasAction: string;
public readonly canvasSession: string;
Expand All @@ -36,7 +33,7 @@ export class Comment<T extends IUniqueId> {
constructor({
id,
text,
chain,
community_id,
author,
Address,
thread_id,
Expand Down Expand Up @@ -74,7 +71,7 @@ export class Comment<T extends IUniqueId> {
})
: [];

this.chain = chain;
this.communityId = community_id;
this.author = Address?.address || author;
this.text = deleted_at?.length > 0 ? '[deleted]' : decodeURIComponent(text);
this.plaintext = deleted_at?.length > 0 ? '[deleted]' : plaintext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import moment from 'moment';

import type { SubscriptionInstance } from 'server/models/subscription';
import type ChainInfo from './ChainInfo';
import { default as CommentT } from './Comment';
import { Comment as CommentT } from './Comment';
import { Thread as ThreadT } from './Thread';
import type { IUniqueId } from './interfaces';

Expand Down Expand Up @@ -95,7 +94,7 @@ class NotificationSubscription {
}
}

export const modelFromServer = (subscription: SubscriptionInstance) => {
export const modelFromServer = (subscription) => {
const {
id,
category_id,
Expand Down Expand Up @@ -125,7 +124,7 @@ export const modelFromServer = (subscription: SubscriptionInstance) => {

if (Comment) {
try {
modeledComment = new CommentT({ ...Comment } as any);
modeledComment = new CommentT({ ...Comment });
} catch (e) {
console.log('error', e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ type ProfileActivityRowProps = {

const ProfileActivityRow = ({ activity }: ProfileActivityRowProps) => {
const navigate = useCommonNavigate();
const { createdAt, author, title, id, body } = activity;
let communityId: string;
const { communityId, createdAt, author, id } = activity;
let title: string, body: string;
if (activity instanceof Thread) {
communityId = activity.communityId;
} else {
communityId = activity.chain;
title = activity.title;
body = activity.body;
}
const isThread = !!(activity as Thread).kind;
const comment = activity as CommentWithAssociatedThread;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export const CommentTree = ({
try {
await deleteComment({
commentId: comment.id,
canvasHash: comment.canvas_hash,
canvasHash: comment.canvasHash,
communityId: app.activeChainId(),
address: app.user.activeAccount.address,
existingNumberOfComments: thread.numberOfComments,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { commentsByDate } from 'helpers/dates';
import type { IUniqueId } from 'models/interfaces';
import { CommentsFeaturedFilterTypes } from 'models/types';
import app from 'state';
import type { Comment as CommentType } from '../../../../models/Comment';
import { CommentsFeaturedFilterTypes } from 'models/types';

const MAX_THREAD_LEVEL = 8;

Expand All @@ -10,7 +11,7 @@ type ExtendedComment = {
isCommentAuthor: boolean;
maxReplyLimitReached: boolean;
replyBtnVisible: boolean;
children: CommentType<any> & ExtendedComment;
children: Array<CommentType<IUniqueId> & ExtendedComment>;
};

interface UsePrepareCommentsListProps {
Expand Down Expand Up @@ -39,7 +40,7 @@ const usePrepareCommentsList = ({
}: UsePrepareCommentsListProps) => {
const isLivingCommentTree = (
comment: CommentType<any>,
children: Array<CommentType<any>>
children: Array<CommentType<any>>,
) => {
if (!comment.deleted) {
return true;
Expand All @@ -57,7 +58,7 @@ const usePrepareCommentsList = ({
}

const grandchildren = allComments.filter(
(c) => c.threadId === threadId && c.parentComment === comment.id
(c) => c.threadId === threadId && c.parentComment === comment.id,
);

for (let j = 0; j < grandchildren.length; j++) {
Expand All @@ -79,7 +80,7 @@ const usePrepareCommentsList = ({
// This functions creates recursively a tree of zero level comments and their nested children (replies)
const recursivelyGatherComments = (
_levelZeroComments: Array<CommentType<any>>,
threadLevel: number
threadLevel: number,
): Array<CommentType<any> & ExtendedComment> => {
const canContinueThreading = threadLevel <= MAX_THREAD_LEVEL;

Expand All @@ -94,7 +95,7 @@ const usePrepareCommentsList = ({
const children = allComments
// take only comments that are direct children of current comment
.filter(
(c) => c.threadId === threadId && c.parentComment === comment.id
(c) => c.threadId === threadId && c.parentComment === comment.id,
)
// sorts comments and nested comments according to user selection from dropdown
.sort((a, b) => commentsByDate(a, b, commentSortType));
Expand Down Expand Up @@ -134,11 +135,12 @@ const usePrepareCommentsList = ({
// This functions transforms nested list of comments created by "recursivelyGatherComments"
// into flat list which is exactly in the order how user would see this in the UI
const getFlattenComments = (
_comments: Array<CommentType<any> & ExtendedComment>
_comments: Array<CommentType<IUniqueId> & ExtendedComment>,
) => {
const flattenedComments: Array<CommentType<any> & ExtendedComment> = [];
const flattenedComments: Array<CommentType<IUniqueId> & ExtendedComment> =
[];

const flatten = (comment: CommentType<any> & ExtendedComment) => {
const flatten = (comment: CommentType<IUniqueId> & ExtendedComment) => {
flattenedComments.push(comment);

if (comment.children && comment.children.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const getTextRows = (
<User
shouldHideAvatar
userAddress={subscription.Comment.author}
userCommunityId={subscription.Comment.chain}
userCommunityId={subscription.Comment.communityId}
/>
&apos;
</CWText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,11 @@ const NotificationSettingsPage = () => {
userCommunityId={sub.Thread.communityId}
/>
);
} else if (sub.Comment?.chain) {
} else if (sub.Comment?.communityId) {
return (
<User
userAddress={sub.Comment.author}
userCommunityId={sub.Comment.chain}
userCommunityId={sub.Comment.communityId}
/>
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function __getStats(
{ type: QueryTypes.SELECT },
),
this.models.Comment.count({
where: whereChain,
where: whereCommunityId,
}),
this.models.Thread.count({
where: whereCommunityId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function __searchComments(
}

const communityWhere = bind.community
? '"Comments".chain = $community AND'
? '"Comments".community_id = $community AND'
: '';

const sqlBaseQuery = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function __updateComment(
root_type: ProposalType.Thread,
comment_id: +finalComment.id,
comment_text: finalComment.text,
chain_id: finalComment.chain,
chain_id: finalComment.community_id,
author_address: finalComment.Address.address,
author_chain: finalComment.Address.community_id,
},
Expand Down Expand Up @@ -191,7 +191,7 @@ export async function __updateComment(
root_type: ProposalType.Thread,
comment_id: +finalComment.id,
comment_text: finalComment.text,
chain_id: finalComment.chain,
chain_id: finalComment.community_id,
author_address: finalComment.Address.address,
author_chain: finalComment.Address.community_id,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ export async function __deleteCommunity(
await sequelize.query(
`UPDATE "Comments" SET
created_by = (SELECT address FROM "Addresses" WHERE "Comments".address_id = "Addresses".id)
WHERE chain = '${community.id}'`,
WHERE community_id = '${community.id}'`,
{ transaction: t },
);

await this.models.Comment.destroy({
where: { chain: community.id },
where: { community_id: community.id },
transaction: t,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function __createThreadComment(
parentComment = await this.models.Comment.findOne({
where: {
id: parentId,
chain: community.id,
community_id: community.id,
},
include: [this.models.Address],
});
Expand Down Expand Up @@ -164,18 +164,19 @@ export async function __createThreadComment(
body: decodeURIComponent(text),
};
const version_history: string[] = [JSON.stringify(firstVersion)];
const commentContent = {
const commentContent: CommentAttributes = {
thread_id: `${threadId}`,
text,
plaintext,
version_history,
address_id: address.id,
chain: community.id,
community_id: community.id,
parent_id: null,
canvas_action: canvasAction,
canvas_session: canvasSession,
canvas_hash: canvasHash,
discord_meta: discordMeta,
reaction_count: 0,
};
if (parentId) {
Object.assign(commentContent, { parent_id: parentId });
Expand All @@ -196,7 +197,7 @@ export async function __createThreadComment(
{
subscriber_id: user.id,
category_id: NotificationCategories.NewReaction,
chain_id: finalComment.chain || null,
chain_id: finalComment.community_id || null,
comment_id: finalComment.id,
is_active: true,
},
Expand All @@ -206,7 +207,7 @@ export async function __createThreadComment(
{
subscriber_id: user.id,
category_id: NotificationCategories.NewComment,
chain_id: finalComment.chain || null,
chain_id: finalComment.community_id || null,
comment_id: finalComment.id,
is_active: true,
},
Expand Down Expand Up @@ -267,7 +268,7 @@ export async function __createThreadComment(
root_type: ProposalType.Thread,
comment_id: +finalComment.id,
comment_text: finalComment.text,
chain_id: finalComment.chain,
chain_id: finalComment.community_id,
author_address: finalComment.Address.address,
author_chain: finalComment.Address.community_id,
},
Expand All @@ -289,7 +290,7 @@ export async function __createThreadComment(
comment_text: finalComment.text,
parent_comment_id: +parentId,
parent_comment_text: parentComment.text,
chain_id: finalComment.chain,
chain_id: finalComment.community_id,
author_address: finalComment.Address.address,
author_chain: finalComment.Address.community_id,
},
Expand All @@ -316,7 +317,7 @@ export async function __createThreadComment(
root_type: ProposalType.Thread,
comment_id: +finalComment.id,
comment_text: finalComment.text,
chain_id: finalComment.chain,
chain_id: finalComment.community_id,
author_address: finalComment.Address.address,
author_chain: finalComment.Address.community_id,
},
Expand Down
Loading

0 comments on commit 5e4fd9a

Please sign in to comment.