import { query, mutation } from "./_generated/server"; import { v } from "convex/values"; import { GenericMutationCtx } from "convex/server"; import { DataModel, Id } from "./_generated/dataModel"; type TableWithChannelIndex = | "channelKeys" | "dmParticipants" | "typingIndicators" | "voiceStates"; async function deleteByChannel( ctx: GenericMutationCtx, table: TableWithChannelIndex, channelId: Id<"channels"> ) { const docs = await (ctx.db.query(table) as any) .withIndex("by_channel", (q: any) => q.eq("channelId", channelId)) .collect(); for (const doc of docs) { await ctx.db.delete(doc._id); } } // List all non-DM channels export const list = query({ args: {}, returns: v.array( v.object({ _id: v.id("channels"), _creationTime: v.number(), name: v.string(), type: v.string(), category: v.optional(v.string()), topic: v.optional(v.string()), position: v.optional(v.number()), }) ), handler: async (ctx) => { const channels = await ctx.db.query("channels").collect(); return channels .filter((c) => c.type !== "dm") .sort((a, b) => (a.position ?? 0) - (b.position ?? 0) || a.name.localeCompare(b.name)); }, }); // Get single channel by ID export const get = query({ args: { id: v.id("channels") }, returns: v.union( v.object({ _id: v.id("channels"), _creationTime: v.number(), name: v.string(), type: v.string(), category: v.optional(v.string()), topic: v.optional(v.string()), position: v.optional(v.number()), }), v.null() ), handler: async (ctx, args) => { return await ctx.db.get(args.id); }, }); // Create new channel export const create = mutation({ args: { name: v.string(), type: v.optional(v.string()), category: v.optional(v.string()), topic: v.optional(v.string()), position: v.optional(v.number()), }, returns: v.object({ id: v.id("channels") }), handler: async (ctx, args) => { if (!args.name.trim()) { throw new Error("Channel name required"); } const existing = await ctx.db .query("channels") .withIndex("by_name", (q) => q.eq("name", args.name)) .unique(); if (existing) { throw new Error("Channel already exists"); } const id = await ctx.db.insert("channels", { name: args.name, type: args.type || "text", category: args.category, topic: args.topic, position: args.position, }); return { id }; }, }); // Update channel topic export const updateTopic = mutation({ args: { id: v.id("channels"), topic: v.string(), }, returns: v.null(), handler: async (ctx, args) => { const channel = await ctx.db.get(args.id); if (!channel) throw new Error("Channel not found"); await ctx.db.patch(args.id, { topic: args.topic }); return null; }, }); // Rename channel export const rename = mutation({ args: { id: v.id("channels"), name: v.string(), }, returns: v.object({ _id: v.id("channels"), _creationTime: v.number(), name: v.string(), type: v.string(), category: v.optional(v.string()), topic: v.optional(v.string()), position: v.optional(v.number()), }), handler: async (ctx, args) => { if (!args.name.trim()) { throw new Error("Name required"); } const channel = await ctx.db.get(args.id); if (!channel) { throw new Error("Channel not found"); } await ctx.db.patch(args.id, { name: args.name }); return { ...channel, name: args.name }; }, }); // Delete channel + cascade messages and keys export const remove = mutation({ args: { id: v.id("channels") }, returns: v.object({ success: v.boolean() }), handler: async (ctx, args) => { const channel = await ctx.db.get(args.id); if (!channel) { throw new Error("Channel not found"); } // Delete reactions for all messages in this channel const messages = await ctx.db .query("messages") .withIndex("by_channel", (q) => q.eq("channelId", args.id)) .collect(); for (const msg of messages) { const reactions = await ctx.db .query("messageReactions") .withIndex("by_message", (q) => q.eq("messageId", msg._id)) .collect(); for (const r of reactions) { await ctx.db.delete(r._id); } await ctx.db.delete(msg._id); } await deleteByChannel(ctx, "channelKeys", args.id); await deleteByChannel(ctx, "dmParticipants", args.id); await deleteByChannel(ctx, "typingIndicators", args.id); await deleteByChannel(ctx, "voiceStates", args.id); await ctx.db.delete(args.id); return { success: true }; }, });