-
Notifications
You must be signed in to change notification settings - Fork 44
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
kurtisassad
wants to merge
29
commits into
master
Choose a base branch
from
ka.externalGetThreads
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 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 052ee32
Fixed external API routes
kurtisassad cd20621
Merge branch 'master' into ka.externalGetThreads
kurtisassad 4cc1470
Fixed recursive reference
kurtisassad 54f71b9
Fixed type issue
kurtisassad 46711b9
Fixed type issue
kurtisassad 078e946
Fixed linter error
kurtisassad 538ebd2
Fixed PR comment
kurtisassad 25fc97b
Merge branch 'master' into ka.externalGetThreads
kurtisassad 3a34659
Resolved merge conflicts
kurtisassad 8b17eaa
Fixed PR comment
kurtisassad d06f592
Fixed PR comments
kurtisassad 909c188
Merge branch 'master' into ka.externalGetThreads
kurtisassad 8ab54d6
Resolved merge conflicts
kurtisassad 0499bd9
Merge branch 'master' into ka.externalGetThreads
kurtisassad e5f3253
Merge branch 'master' into ka.externalGetThreads
kurtisassad 1811800
Migrated client usage for topics
kurtisassad 8fe3c69
Fixed lint errors
kurtisassad abfcb10
Fixed lint errors
kurtisassad 4ce424a
Fixed type errors
kurtisassad 9f5ef6b
Fixed type errors
kurtisassad e9d103b
Merge branch 'master' into ka.externalGetThreads
kurtisassad 8c0c07d
Resolved merge conflicts
kurtisassad f9254ce
Fixed merge conflicts
kurtisassad 03a0258
Resolved merge conflicts
kurtisassad a9aa03d
Resolved merge conflicts
kurtisassad b6b3244
Fixed lint errors
kurtisassad cbfa76a
Merge branch 'master' into ka.externalGetThreads
kurtisassad 5353e53
Fixed type errors
kurtisassad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); | ||
|
||
return schemas.buildPaginatedResponse(threads, count as number, payload); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './GetTopics.query'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
|
@@ -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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 coercesorder_by
to one of the attributes instead ofstring
.There was a problem hiding this comment.
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