sipher/convex/betterAuth/nests/locals.ts
Nixyan e7dd6c961d feat: enhance user status management and introduce nests functionality
- Updated user status handling to include optional user-set status, improving user experience during reconnections.
- Added new queries and mutations for managing nests, including fetching non-offline user IDs and forcing users offline.
- Introduced new database schema for nests, roles, and channels, enhancing the application's organizational structure.
- Updated dependencies in package.json and bun.lock for improved stability and compatibility.
- Refactored related components and API to support the new nests functionality.
2026-02-20 10:01:07 -03:00

60 lines
No EOL
1.9 KiB
TypeScript

import { UserIdentity } from "convex/server";
import { Doc, Id } from "../_generated/dataModel";
import { MutationCtx, query, QueryCtx } from "../_generated/server";
// Overload signatures
async function userValidation(ctx: MutationCtx | QueryCtx, options: { required: false }): Promise<{ userId: Id<"user">; user: any } | null>;
async function userValidation(ctx: MutationCtx | QueryCtx, options?: { required?: true }): Promise<{ userId: Id<"user">; user: UserIdentity }>;
// Implementation
async function userValidation(ctx: MutationCtx | QueryCtx, options?: { required?: boolean }) {
const required = options?.required ?? true;
const user = await ctx.auth.getUserIdentity();
if (!user) {
if (required) throw new Error("User not found");
return null;
}
const userId = ctx.db.normalizeId("user", user.subject as string) as Id<"user">;
if (!userId) {
if (required) throw new Error("User not found");
return null;
}
return { userId, user };
}
export const getUserNests = query({
handler: async (ctx) => {
const { userId } = await userValidation(ctx, { required: true });
if (!userId) throw new Error("User not found");
const getUser = await ctx.db.get<"user">(userId);
if (!getUser) throw new Error("User not found");
else if (!getUser.nests || getUser.nests.length === 0) return [];
// Get the nests the user is a member of
const nests: Doc<"nests">[] = [];
for (const nestId of getUser.nests) {
const nest = await ctx.db.get<"nests">(nestId);
if (!nest) continue;
nests.push(nest);
}
return nests;
}
});
export const getRecommendedNests = query({
handler: async (ctx) => {
const { userId } = await userValidation(ctx, { required: true });
if (!userId) throw new Error("User not found");
const getUser = await ctx.db.get<"user">(userId);
if (!getUser) throw new Error("User not found");
const nests = await ctx.db.query<"nests">("nests").withIndex("onDiscover", q => q.eq("onDiscover", true)).collect();
return nests;
}
});