Skip to content

Commit

Permalink
getCommentSearchVector
Browse files Browse the repository at this point in the history
  • Loading branch information
timolegros committed Sep 10, 2024
1 parent 7c60409 commit af7166c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions libs/core/src/integration/events.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import {
} from './chain-event.schemas';
import { EventMetadata } from './util.schemas';

export const ThreadCreated = Thread.extend({
export const ThreadCreated = Thread.omit({ search: true }).extend({
contestManagers: z.array(z.object({ contest_address: z.string() })).nullish(),
});
export const ThreadUpvoted = Reaction.omit({ comment_id: true }).extend({
thread_id: PG_INT,
community_id: z.string(),
contestManagers: z.array(z.object({ contest_address: z.string() })).nullish(),
});
export const CommentCreated = Comment.extend({
export const CommentCreated = Comment.omit({ search: true }).extend({
community_id: z.string(),
users_mentioned: z
.array(PG_INT)
Expand Down
2 changes: 2 additions & 0 deletions libs/model/src/comment/CreateComment.command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventNames, InvalidState, type Command } from '@hicommonwealth/core';
import { getCommentSearchVector } from '@hicommonwealth/model';
import * as schemas from '@hicommonwealth/schemas';
import { models } from '../database';
import { isAuthorized, type AuthContext } from '../middleware';
Expand Down Expand Up @@ -70,6 +71,7 @@ export function CreateComment(): Command<
reaction_count: 0,
reaction_weights_sum: 0,
created_by: '',
search: getCommentSearchVector(text),
},
{
transaction,
Expand Down
17 changes: 17 additions & 0 deletions libs/model/src/models/comment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { stats } from '@hicommonwealth/core';
import { Comment } from '@hicommonwealth/schemas';
import pg from 'pg';
import Sequelize from 'sequelize';
import { z } from 'zod';
import type {
Expand Down Expand Up @@ -60,6 +61,10 @@ export default (
allowNull: false,
defaultValue: 0,
},
search: {
type: Sequelize.TSVECTOR,
allowNull: false,
},
},
{
hooks: {
Expand Down Expand Up @@ -114,3 +119,15 @@ export default (
],
},
);

export function getCommentSearchVector(body: string) {
let decodedBody = body;

try {
decodedBody = decodeURIComponent(body);
} catch (e) {}

Check failure on line 128 in libs/model/src/models/comment.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Empty block statement

return Sequelize.literal(
`to_tsvector('english', ${pg.escapeLiteral(decodedBody)})`,
);
}
4 changes: 2 additions & 2 deletions libs/model/src/models/thread.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventNames } from '@hicommonwealth/core';
import { Thread } from '@hicommonwealth/schemas';
import pg from 'pg';
import Sequelize from 'sequelize';
import { z } from 'zod';
import { emitEvent, getThreadContestManagers } from '../utils';
Expand Down Expand Up @@ -179,8 +180,7 @@ export function getThreadSearchVector(title: string, body: string) {
try {
decodedBody = decodeURIComponent(body);
} catch (e) {}

Check failure on line 182 in libs/model/src/models/thread.ts

View workflow job for this annotation

GitHub Actions / Code Quality Recommendations (20)

Empty block statement

return Sequelize.literal(
`to_tsvector('english', '${decodedTitle}' || ' ' || '${decodedBody}')`,
`to_tsvector('english', ${pg.escapeLiteral(decodedTitle)} || ' ' || ${pg.escapeLiteral(decodedBody)})`,
);
}
2 changes: 2 additions & 0 deletions libs/model/test/util-tests/getCommentDepth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from 'chai';
import { afterAll, beforeAll, describe, test } from 'vitest';
import {
CommentInstance,
getCommentSearchVector,
getThreadSearchVector,
models,
tester,
Expand Down Expand Up @@ -41,6 +42,7 @@ describe('getCommentDepth', () => {
// @ts-expect-error StrictNullChecks
address_id: address.id,
text: String(i),
search: getCommentSearchVector(String(i)),
});
comments.push(result);
comment = result;
Expand Down
2 changes: 2 additions & 0 deletions libs/schemas/src/entities/comment.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const Comment = z.object({
reaction_count: PG_INT,
reaction_weights_sum: PG_INT.optional(),

search: z.union([z.string(), z.record(z.any())]),

Address: Address.nullish(),
Thread: Thread.nullish(),
Reaction: Reaction.nullish(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { dispose } from '@hicommonwealth/core';
import { getThreadSearchVector, tester, type DB } from '@hicommonwealth/model';
import {
getCommentSearchVector,
getThreadSearchVector,
tester,
type DB,
} from '@hicommonwealth/model';
import chai from 'chai';
import chaiHttp from 'chai-http';
import { Sequelize } from 'sequelize';
Expand Down Expand Up @@ -194,6 +199,7 @@ describe('DatabaseCleaner Tests', async () => {
reaction_count: 0,
reaction_weights_sum: 0,
plaintext: 'Testing',
search: getCommentSearchVector('Testing'),
});

await models.ThreadSubscription.create({
Expand Down

0 comments on commit af7166c

Please sign in to comment.