-
Notifications
You must be signed in to change notification settings - Fork 0
Dev #10
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
Merged
Merged
Dev #10
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a7fe26
feat: added right sidebar panel with multiple uses
BuckyMcYolo 6584764
feat(presence): add API-backed guild members sidebar with realtime
BuckyMcYolo d128561
fix(presence): harden guild presence sync (atomic redis ops, init
BuckyMcYolo cd37196
fix: fixed init promise on realtime sserver
BuckyMcYolo f7286e7
fix(realtime): prevent stale online broadcasts during init/disconnect
BuckyMcYolo 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| import { env } from "@repo/env/server" | ||
| import { createClient, type RedisClientType } from "redis" | ||
|
|
||
| const redisClient: RedisClientType = createClient({ url: env.REDIS_URL }) | ||
|
|
||
| let connectPromise: Promise<RedisClientType> | null = null | ||
|
|
||
| redisClient.on("error", (error) => { | ||
| console.error("[api] redis error:", error) | ||
| }) | ||
|
|
||
| export async function getRedisClient() { | ||
| if (redisClient.isOpen) { | ||
| return redisClient | ||
| } | ||
|
|
||
| if (!connectPromise) { | ||
| connectPromise = redisClient.connect().finally(() => { | ||
| connectPromise = null | ||
| }) | ||
| } | ||
|
|
||
| await connectPromise | ||
| return redisClient | ||
| } |
This file contains hidden or 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,77 @@ | ||
| import { db, eq, schema } from "@repo/db" | ||
| import { PRESENCE_ONLINE_USERS_SET_KEY } from "@repo/realtime-types" | ||
| import { asc } from "drizzle-orm" | ||
| import * as HttpStatusCodes from "@/lib/helpers/http/status-codes" | ||
| import { getRedisClient } from "@/lib/redis" | ||
| import type { AppRouteHandler } from "@/lib/types/app-types" | ||
| import type { ListGuildMembersRoute } from "@/routes/v1/guilds/routes" | ||
|
|
||
| const PRESENCE_MEMBERSHIP_CHUNK_SIZE = 250 | ||
|
|
||
| async function listOnlineUserIds(userIds: string[]) { | ||
| if (userIds.length === 0) return new Set<string>() | ||
|
|
||
| try { | ||
| const redis = await getRedisClient() | ||
| const membership: boolean[] = [] | ||
|
|
||
| for ( | ||
| let index = 0; | ||
| index < userIds.length; | ||
| index += PRESENCE_MEMBERSHIP_CHUNK_SIZE | ||
| ) { | ||
| const chunk = userIds.slice(index, index + PRESENCE_MEMBERSHIP_CHUNK_SIZE) | ||
| const chunkMembership = await redis.smIsMember( | ||
| PRESENCE_ONLINE_USERS_SET_KEY, | ||
| chunk | ||
| ) | ||
| membership.push(...chunkMembership) | ||
| } | ||
|
|
||
| const onlineIds = userIds.filter((_, index) => membership[index] === true) | ||
|
|
||
| return new Set(onlineIds) | ||
| } catch (error) { | ||
| console.error("[api] failed to read presence from redis:", error) | ||
| return new Set<string>() | ||
| } | ||
| } | ||
|
|
||
| export const listGuildMembers: AppRouteHandler<ListGuildMembersRoute> = async ( | ||
| c | ||
| ) => { | ||
| const guild = c.var.guild | ||
|
|
||
| const memberRows = await db | ||
| .select({ | ||
| userId: schema.guildMember.userId, | ||
| role: schema.guildMember.role, | ||
| name: schema.user.name, | ||
| image: schema.user.image, | ||
| }) | ||
| .from(schema.guildMember) | ||
| .innerJoin(schema.user, eq(schema.guildMember.userId, schema.user.id)) | ||
| .where(eq(schema.guildMember.guildId, guild.id)) | ||
| .orderBy(asc(schema.user.name)) | ||
|
|
||
| const userIds = memberRows.map((row) => row.userId) | ||
| const onlineUserIds = await listOnlineUserIds(userIds) | ||
|
|
||
| return c.json( | ||
| { | ||
| guildId: guild.id, | ||
| guildSlug: guild.slug, | ||
| guildName: guild.name, | ||
| members: memberRows.map((member) => ({ | ||
| userId: member.userId, | ||
| name: member.name, | ||
| image: member.image, | ||
| role: member.role, | ||
| status: onlineUserIds.has(member.userId) | ||
| ? ("online" as const) | ||
| : ("offline" as const), | ||
| })), | ||
| }, | ||
| HttpStatusCodes.OK | ||
| ) | ||
| } |
This file contains hidden or 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,10 @@ | ||
| import { createRouter } from "@/lib/helpers/app/create-app" | ||
| import * as handlers from "@/routes/v1/guilds/handlers" | ||
| import * as routes from "@/routes/v1/guilds/routes" | ||
|
|
||
| const guildsRouter = createRouter().openapi( | ||
| routes.listGuildMembers, | ||
| handlers.listGuildMembers | ||
| ) | ||
|
|
||
| export default guildsRouter |
This file contains hidden or 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,36 @@ | ||
| import { createRoute } from "@hono/zod-openapi" | ||
| import * as HttpStatusCodes from "@/lib/helpers/http/status-codes" | ||
| import jsonContent from "@/lib/helpers/openapi/json-content" | ||
| import { | ||
| forbiddenSchema, | ||
| internalServerErrorSchema, | ||
| notFoundSchema, | ||
| unauthorizedSchema, | ||
| } from "@/lib/helpers/openapi/schemas" | ||
| import { guildAuthMiddleware } from "@/middleware/guild-auth" | ||
| import { guildSlugParamsSchema, listGuildMembersResponseSchema } from "./schema" | ||
|
|
||
| export const listGuildMembers = createRoute({ | ||
| path: "/guilds/{guildSlug}/members", | ||
| method: "get", | ||
| summary: "List guild members with presence", | ||
| description: | ||
| "Returns all guild members and their current online/offline status.", | ||
| tags: ["Guilds"], | ||
| middleware: [guildAuthMiddleware] as const, | ||
| request: { | ||
| params: guildSlugParamsSchema, | ||
| }, | ||
| responses: { | ||
| [HttpStatusCodes.OK]: jsonContent({ | ||
| schema: listGuildMembersResponseSchema, | ||
| description: "Guild members with presence status", | ||
| }), | ||
| [HttpStatusCodes.UNAUTHORIZED]: unauthorizedSchema, | ||
| [HttpStatusCodes.FORBIDDEN]: forbiddenSchema, | ||
| [HttpStatusCodes.NOT_FOUND]: notFoundSchema, | ||
| [HttpStatusCodes.INTERNAL_SERVER_ERROR]: internalServerErrorSchema, | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }) | ||
|
|
||
| export type ListGuildMembersRoute = typeof listGuildMembers | ||
This file contains hidden or 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,19 @@ | ||
| import { z } from "@hono/zod-openapi" | ||
| import { guildSlugParamsSchema } from "@/routes/v1/channels/schema" | ||
|
|
||
| export { guildSlugParamsSchema } | ||
|
|
||
| export const guildMemberPresenceSchema = z.object({ | ||
| userId: z.string().uuid(), | ||
| name: z.string(), | ||
| image: z.string().nullable(), | ||
| role: z.string(), | ||
| status: z.enum(["online", "offline"]), | ||
| }) | ||
|
|
||
| export const listGuildMembersResponseSchema = z.object({ | ||
| guildId: z.string().uuid(), | ||
| guildSlug: z.string(), | ||
| guildName: z.string(), | ||
| members: z.array(guildMemberPresenceSchema), | ||
| }) |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.