Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External API GET threads and topics #9102

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
727faeb
Added getThreads and getTopics to the external api
kurtisassad Aug 29, 2024
052ee32
Fixed external API routes
kurtisassad Sep 3, 2024
cd20621
Merge branch 'master' into ka.externalGetThreads
kurtisassad Sep 3, 2024
4cc1470
Fixed recursive reference
kurtisassad Sep 4, 2024
54f71b9
Fixed type issue
kurtisassad Sep 4, 2024
46711b9
Fixed type issue
kurtisassad Sep 4, 2024
078e946
Fixed linter error
kurtisassad Sep 4, 2024
538ebd2
Fixed PR comment
kurtisassad Sep 4, 2024
25fc97b
Merge branch 'master' into ka.externalGetThreads
kurtisassad Sep 4, 2024
3a34659
Resolved merge conflicts
kurtisassad Sep 4, 2024
8b17eaa
Fixed PR comment
kurtisassad Sep 5, 2024
d06f592
Fixed PR comments
kurtisassad Sep 9, 2024
909c188
Merge branch 'master' into ka.externalGetThreads
kurtisassad Sep 9, 2024
8ab54d6
Resolved merge conflicts
kurtisassad Sep 9, 2024
0499bd9
Merge branch 'master' into ka.externalGetThreads
kurtisassad Sep 12, 2024
e5f3253
Merge branch 'master' into ka.externalGetThreads
kurtisassad Sep 23, 2024
1811800
Migrated client usage for topics
kurtisassad Sep 23, 2024
8fe3c69
Fixed lint errors
kurtisassad Sep 23, 2024
abfcb10
Fixed lint errors
kurtisassad Sep 23, 2024
4ce424a
Fixed type errors
kurtisassad Sep 23, 2024
9f5ef6b
Fixed type errors
kurtisassad Sep 24, 2024
e9d103b
Merge branch 'master' into ka.externalGetThreads
kurtisassad Oct 2, 2024
8c0c07d
Resolved merge conflicts
kurtisassad Oct 2, 2024
f9254ce
Fixed merge conflicts
kurtisassad Oct 2, 2024
03a0258
Resolved merge conflicts
kurtisassad Oct 2, 2024
a9aa03d
Resolved merge conflicts
kurtisassad Oct 2, 2024
b6b3244
Fixed lint errors
kurtisassad Oct 2, 2024
cbfa76a
Merge branch 'master' into ka.externalGetThreads
kurtisassad Oct 8, 2024
5353e53
Fixed type errors
kurtisassad Oct 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/adapters/src/trpc/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export enum Tag {
User = 'User',
Community = 'Community',
Thread = 'Thread',
Topic = 'Topic',
Comment = 'Comment',
Reaction = 'Reaction',
Integration = 'Integration',
Expand Down
1 change: 1 addition & 0 deletions libs/model/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * as LoadTest from './load-testing';
export * as Reaction from './reaction';
export * as Subscription from './subscription';
export * as Thread from './thread';
export * as Topic from './topic';
export * as User from './user';
export * as Wallet from './wallet';
export * as Webhook from './webhook';
Expand Down
28 changes: 0 additions & 28 deletions libs/model/src/thread/GetThread.query.ts

This file was deleted.

94 changes: 94 additions & 0 deletions libs/model/src/thread/GetThreads.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { type Query } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { Comment } from '@hicommonwealth/schemas';
import { FindAndCountOptions } from 'sequelize';
import { models } from '../database';
import { ThreadAttributes } from '../models';
import { removeUndefined } from '../utils';
import { formatSequelizePagination } from '../utils/paginationUtils';

export function GetThreads(): Query<typeof schemas.GetThreads> {
return {
input: schemas.GetThreads.input,
output: schemas.GetThreads.output.extend({ Comment: Comment.nullish() }),
auth: [],
secure: false,
body: async ({ payload }) => {
const {
community_id,
topic_id,
thread_id,
include_comments,
include_user,
include_reactions,
} = payload;

const includeArray = [];
if (include_comments) {
includeArray.push({
model: models.Comment,
as: 'Comments',
include: [
{
model: models.Address,
as: 'Address',
attributes: ['id', 'address', 'last_active'],
include: [
{
model: models.User,
as: 'User',
required: true,
attributes: ['id', 'profile'],
},
],
},
],
});
}
if (include_user) {
includeArray.push({
model: models.Address,
as: 'Address',
include: [
{
model: models.User,
as: 'User',
required: true,
attributes: ['id', 'profile'],
},
],
});
}

if (include_reactions) {
includeArray.push({
model: models.Reaction,
as: 'reactions',
include: [
{
model: models.Address,
as: 'Address',
required: true,
attributes: ['id', 'address', 'last_active'],
include: [
{
model: models.User,
attributes: ['id', 'profile'],
},
],
},
],
});
}

const { count, rows: threads } = await models.Thread.findAndCountAll({
where: removeUndefined({ community_id, topic_id, id: thread_id }),
include: includeArray,
...formatSequelizePagination(payload),
paranoid: false,
} as unknown as FindAndCountOptions<ThreadAttributes>);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this type coercion from all future usage of formatSequelizePagination if we add a generic to the function which takes the attributes of the model and coerces order_by to one of the attributes instead of string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like the order_by is just typed as Order in sequelize, it does not depend on the key of the model. So I removed this as unknown typing, but just replaced it with the Order type without any generics


return schemas.buildPaginatedResponse(threads, count as number, payload);
},
};
}
2 changes: 1 addition & 1 deletion libs/model/src/thread/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './CreateThread.command';
export * from './GetBulkThread.query';
export * from './GetThread.query';
export * from './GetThreads.query';
40 changes: 40 additions & 0 deletions libs/model/src/topic/GetTopics.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { type Query } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { FindAndCountOptions } from 'sequelize';
import { models } from '../database';
import { TopicAttributes } from '../models/index';
import { removeUndefined } from '../utils';
import { formatSequelizePagination } from '../utils/paginationUtils';

export function GetTopics(): Query<typeof schemas.GetTopics> {
return {
...schemas.GetTopics,
auth: [],
secure: false,
body: async ({ payload }) => {
const { community_id, topic_id, include_threads } = payload;

const includeArray = [];
if (include_threads) {
includeArray.push({
model: models.Thread,
as: 'threads',
});
}

const { count, rows: topics } = await models.Topic.findAndCountAll({
where: removeUndefined({ community_id, id: topic_id }),
include: includeArray,
...formatSequelizePagination(payload),
paranoid: false,
} as unknown as FindAndCountOptions<TopicAttributes>);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
timolegros marked this conversation as resolved.
Show resolved Hide resolved
return schemas.buildPaginatedResponse(
topics,
count as number,
payload,
) as any;
timolegros marked this conversation as resolved.
Show resolved Hide resolved
},
};
}
1 change: 1 addition & 0 deletions libs/model/src/topic/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './GetTopics.query';
2 changes: 2 additions & 0 deletions libs/schemas/src/entities/thread.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from 'zod';
import { DiscordMetaSchema, linksSchema, PG_INT } from '../utils';
import { Reaction } from './reaction.schemas';
import { Topic } from './topic.schemas';
import { Address } from './user.schemas';

Expand Down Expand Up @@ -51,6 +52,7 @@ export const Thread = z.object({

// associations
Address: Address.nullish(),
Reaction: Reaction.nullish(),
topic: Topic.nullish(),
});

Expand Down
1 change: 1 addition & 0 deletions libs/schemas/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './feed.schemas';
export * from './pagination';
export * from './subscription.schemas';
export * from './thread.schemas';
export * from './topic.schemas';
export * from './user.schemas';
41 changes: 41 additions & 0 deletions libs/schemas/src/queries/thread.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { z } from 'zod';
import { Thread } from '../entities';
import {
DiscordMetaSchema,
linksSchema,
paginationSchema,
PG_INT,
} from '../utils';
import { PaginatedResultSchema, PaginationParamsSchema } from './pagination';

export const OrderByQueriesKeys = z.enum([
'createdAt:asc',
Expand Down Expand Up @@ -91,3 +93,42 @@ export const GetBulkThreads = {
threads: z.array(BulkThread),
}),
};

export const GetThreads = {
input: PaginationParamsSchema.extend({
community_id: z.string(),
topic_id: PG_INT.optional(),
thread_id: PG_INT.optional(),
include_comments: z.coerce.boolean(),
include_user: z.coerce.boolean(),
include_reactions: z.coerce.boolean(),
}),
output: PaginatedResultSchema.extend({
results: Thread.array(),
}),
};

export const DEPRECATED_GetThreads = z.object({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These schemas conflict with our naming convention but are used for the old API. I have prefixed them with DEPRECATED for now since I am unsure how to handle it.

community_id: z.string(),
bulk: z.coerce.boolean().default(false),
thread_ids: z.coerce.number().int().array().optional(),
active: z.string().optional(),
search: z.string().optional(),
count: z.coerce.boolean().optional().default(false),
include_count: z.coerce.boolean().default(false),
});

export const DEPRECATED_GetBulkThreads = z.object({
topic_id: z.coerce.number().int().optional(),
includePinnedThreads: z.coerce.boolean().optional(),
limit: z.coerce.number().int().optional(),
page: z.coerce.number().int().optional(),
archived: z.coerce.boolean().optional(),
stage: z.string().optional(),
orderBy: z.string().optional(),
from_date: z.string().optional(),
to_date: z.string().optional(),
contestAddress: z.string().optional(),
status: z.string().optional(),
withXRecentComments: z.coerce.number().optional(),
});
15 changes: 15 additions & 0 deletions libs/schemas/src/queries/topic.schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod';
import { Topic } from '../entities';
import { PG_INT } from '../utils';
import { PaginatedResultSchema, PaginationParamsSchema } from './pagination';

export const GetTopics = {
input: PaginationParamsSchema.extend({
community_id: z.string(),
topic_id: PG_INT.optional(),
include_threads: z.coerce.boolean(),
}),
output: PaginatedResultSchema.extend({
results: Topic.array(),
}),
};
7 changes: 6 additions & 1 deletion packages/commonwealth/server/api/external-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import passport from 'passport';
import { config } from '../config';
import * as comment from './comment';
import * as community from './community';
import * as thread from './threads';
import * as thread from './thread';
import * as topic from './topic';

const { createCommunity, getCommunities, getCommunity, getMembers } =
community.trpcRouter;
const { createThread } = thread.trpcRouter;
const { getComments } = comment.trpcRouter;
const { getThreads } = thread.trpcRouter;
const { getTopics } = topic.trpcRouter;
//const { getBulkThreads } = thread.trpcRouter;

const api = {
Expand All @@ -20,6 +23,8 @@ const api = {
getMembers,
getComments,
createThread,
getThreads,
getTopics,
//getBulkThreads,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/commonwealth/server/api/internal-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as feed from './feed';
import * as integrations from './integrations';
import * as loadTest from './load-test';
import * as subscription from './subscription';
import * as thread from './threads';
import * as thread from './thread';
import * as user from './user';
import * as wallet from './wallet';
import * as webhook from './webhook';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export const trpcRouter = trpc.router({
),
),
getBulkThreads: trpc.query(Thread.GetBulkThreads, trpc.Tag.Thread),
getThreads: trpc.query(Thread.GetThreads, trpc.Tag.Thread),
});
6 changes: 6 additions & 0 deletions packages/commonwealth/server/api/topic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { trpc } from '@hicommonwealth/adapters';
import { Topic } from '@hicommonwealth/model';

export const trpcRouter = trpc.router({
getTopics: trpc.query(Topic.GetTopics, trpc.Tag.Topic),
});
7 changes: 4 additions & 3 deletions packages/commonwealth/server/routes/feed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AppError } from '@hicommonwealth/core';
import { Thread, ThreadAttributes, type DB } from '@hicommonwealth/model';
import { ThreadAttributes, type DB } from '@hicommonwealth/model';
import * as schemas from '@hicommonwealth/schemas';
import { slugify } from '@hicommonwealth/shared';
import { Feed } from 'feed';
import { GetBulkThreadsResult } from '../controllers/server_threads_methods/get_bulk_threads';
Expand Down Expand Up @@ -47,7 +48,7 @@ export const getFeedHandler = async (
>,
res: TypedResponse<GetThreadsResponse>,
) => {
const queryValidationResult = Thread.GetThreadsParamsSchema.safeParse(
const queryValidationResult = schemas.DEPRECATED_GetBulkThreads.safeParse(
req.query,
);

Expand All @@ -65,7 +66,7 @@ export const getFeedHandler = async (
// get bulk threads
if (bulk) {
const bulkQueryValidationResult =
Thread.GetBulkThreadsParamsSchema.safeParse(req.query);
schemas.DEPRECATED_GetBulkThreads.safeParse(req.query);

if (bulkQueryValidationResult.success === false) {
throw new AppError(formatErrorPretty(bulkQueryValidationResult));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppError } from '@hicommonwealth/core';
import { Thread } from '@hicommonwealth/model';
import * as schemas from '@hicommonwealth/schemas';
import { ALL_COMMUNITIES } from '../../middleware/databaseValidationService';
import { ServerControllers } from '../../routing/router';
import {
Expand Down Expand Up @@ -66,7 +66,7 @@ export const getThreadsHandler = async (
>,
res: TypedResponse<GetThreadsResponse>,
) => {
const queryValidationResult = Thread.GetThreadsParamsSchema.safeParse(
const queryValidationResult = schemas.DEPRECATED_GetThreads.safeParse(
req.query,
);

Expand Down Expand Up @@ -95,7 +95,7 @@ export const getThreadsHandler = async (
// get bulk threads
if (bulk) {
const bulkQueryValidationResult =
Thread.GetBulkThreadsParamsSchema.safeParse(req.query);
schemas.DEPRECATED_GetBulkThreads.safeParse(req.query);

if (bulkQueryValidationResult.success === false) {
throw new AppError(formatErrorPretty(bulkQueryValidationResult));
Expand Down
Loading