"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.remove = exports.reorderChannels = exports.moveChannel = exports.rename = exports.updateTopic = exports.create = exports.get = exports.list = void 0; var server_1 = require("./_generated/server"); var values_1 = require("convex/values"); var api_1 = require("./_generated/api"); function deleteByChannel(ctx, table, channelId) { return __awaiter(this, void 0, void 0, function () { var docs, _i, docs_1, doc; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ctx.db.query(table) .withIndex("by_channel", function (q) { return q.eq("channelId", channelId); }) .collect()]; case 1: docs = _a.sent(); _i = 0, docs_1 = docs; _a.label = 2; case 2: if (!(_i < docs_1.length)) return [3 /*break*/, 5]; doc = docs_1[_i]; return [4 /*yield*/, ctx.db.delete(doc._id)]; case 3: _a.sent(); _a.label = 4; case 4: _i++; return [3 /*break*/, 2]; case 5: return [2 /*return*/]; } }); }); } // List all non-DM channels exports.list = (0, server_1.query)({ args: {}, returns: values_1.v.array(values_1.v.object({ _id: values_1.v.id("channels"), _creationTime: values_1.v.number(), name: values_1.v.string(), type: values_1.v.string(), categoryId: values_1.v.optional(values_1.v.id("categories")), topic: values_1.v.optional(values_1.v.string()), position: values_1.v.optional(values_1.v.number()), })), handler: function (ctx) { return __awaiter(void 0, void 0, void 0, function () { var channels; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ctx.db.query("channels").collect()]; case 1: channels = _a.sent(); return [2 /*return*/, channels .filter(function (c) { return c.type !== "dm"; }) .sort(function (a, b) { var _a, _b; return ((_a = a.position) !== null && _a !== void 0 ? _a : 0) - ((_b = b.position) !== null && _b !== void 0 ? _b : 0) || a.name.localeCompare(b.name); })]; } }); }); }, }); // Get single channel by ID exports.get = (0, server_1.query)({ args: { id: values_1.v.id("channels") }, returns: values_1.v.union(values_1.v.object({ _id: values_1.v.id("channels"), _creationTime: values_1.v.number(), name: values_1.v.string(), type: values_1.v.string(), categoryId: values_1.v.optional(values_1.v.id("categories")), topic: values_1.v.optional(values_1.v.string()), position: values_1.v.optional(values_1.v.number()), }), values_1.v.null()), handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ctx.db.get(args.id)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }, }); // Create new channel exports.create = (0, server_1.mutation)({ args: { name: values_1.v.string(), type: values_1.v.optional(values_1.v.string()), categoryId: values_1.v.optional(values_1.v.id("categories")), topic: values_1.v.optional(values_1.v.string()), position: values_1.v.optional(values_1.v.number()), }, returns: values_1.v.object({ id: values_1.v.id("channels") }), handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () { var existing, position, allChannels, sameCategory, maxPos, id; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!args.name.trim()) { throw new Error("Channel name required"); } return [4 /*yield*/, ctx.db .query("channels") .withIndex("by_name", function (q) { return q.eq("name", args.name); }) .unique()]; case 1: existing = _a.sent(); if (existing) { throw new Error("Channel already exists"); } position = args.position; if (!(position === undefined)) return [3 /*break*/, 3]; return [4 /*yield*/, ctx.db.query("channels").collect()]; case 2: allChannels = _a.sent(); sameCategory = allChannels.filter(function (c) { return c.categoryId === args.categoryId && c.type !== "dm"; }); maxPos = sameCategory.reduce(function (max, c) { var _a; return Math.max(max, (_a = c.position) !== null && _a !== void 0 ? _a : 0); }, -1000); position = maxPos + 1000; _a.label = 3; case 3: return [4 /*yield*/, ctx.db.insert("channels", { name: args.name, type: args.type || "text", categoryId: args.categoryId, topic: args.topic, position: position, })]; case 4: id = _a.sent(); return [2 /*return*/, { id: id }]; } }); }); }, }); // Update channel topic exports.updateTopic = (0, server_1.mutation)({ args: { id: values_1.v.id("channels"), topic: values_1.v.string(), }, returns: values_1.v.null(), handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () { var channel; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ctx.db.get(args.id)]; case 1: channel = _a.sent(); if (!channel) throw new Error("Channel not found"); return [4 /*yield*/, ctx.db.patch(args.id, { topic: args.topic })]; case 2: _a.sent(); return [2 /*return*/, null]; } }); }); }, }); // Rename channel exports.rename = (0, server_1.mutation)({ args: { id: values_1.v.id("channels"), name: values_1.v.string(), }, returns: values_1.v.object({ _id: values_1.v.id("channels"), _creationTime: values_1.v.number(), name: values_1.v.string(), type: values_1.v.string(), categoryId: values_1.v.optional(values_1.v.id("categories")), topic: values_1.v.optional(values_1.v.string()), position: values_1.v.optional(values_1.v.number()), }), handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () { var channel; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!args.name.trim()) { throw new Error("Name required"); } return [4 /*yield*/, ctx.db.get(args.id)]; case 1: channel = _a.sent(); if (!channel) { throw new Error("Channel not found"); } return [4 /*yield*/, ctx.db.patch(args.id, { name: args.name })]; case 2: _a.sent(); return [2 /*return*/, __assign(__assign({}, channel), { name: args.name })]; } }); }); }, }); // Move a channel to a different category with a new position exports.moveChannel = (0, server_1.mutation)({ args: { id: values_1.v.id("channels"), categoryId: values_1.v.optional(values_1.v.id("categories")), position: values_1.v.number(), }, returns: values_1.v.null(), handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () { var channel; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ctx.db.get(args.id)]; case 1: channel = _a.sent(); if (!channel) throw new Error("Channel not found"); return [4 /*yield*/, ctx.db.patch(args.id, { categoryId: args.categoryId, position: args.position, })]; case 2: _a.sent(); return [2 /*return*/, null]; } }); }); }, }); // Batch reorder channels exports.reorderChannels = (0, server_1.mutation)({ args: { updates: values_1.v.array(values_1.v.object({ id: values_1.v.id("channels"), categoryId: values_1.v.optional(values_1.v.id("categories")), position: values_1.v.number(), })), }, returns: values_1.v.null(), handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () { var _i, _a, u; return __generator(this, function (_b) { switch (_b.label) { case 0: _i = 0, _a = args.updates; _b.label = 1; case 1: if (!(_i < _a.length)) return [3 /*break*/, 4]; u = _a[_i]; return [4 /*yield*/, ctx.db.patch(u.id, { categoryId: u.categoryId, position: u.position, })]; case 2: _b.sent(); _b.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/, null]; } }); }); }, }); // Delete channel + cascade messages and keys exports.remove = (0, server_1.mutation)({ args: { id: values_1.v.id("channels") }, returns: values_1.v.object({ success: values_1.v.boolean() }), handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () { var channel, messages, _loop_1, _i, messages_1, msg; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ctx.db.get(args.id)]; case 1: channel = _a.sent(); if (!channel) { throw new Error("Channel not found"); } return [4 /*yield*/, ctx.db .query("messages") .withIndex("by_channel", function (q) { return q.eq("channelId", args.id); }) .collect()]; case 2: messages = _a.sent(); _loop_1 = function (msg) { var reactions, _b, reactions_1, r; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, ctx.db .query("messageReactions") .withIndex("by_message", function (q) { return q.eq("messageId", msg._id); }) .collect()]; case 1: reactions = _c.sent(); _b = 0, reactions_1 = reactions; _c.label = 2; case 2: if (!(_b < reactions_1.length)) return [3 /*break*/, 5]; r = reactions_1[_b]; return [4 /*yield*/, ctx.db.delete(r._id)]; case 3: _c.sent(); _c.label = 4; case 4: _b++; return [3 /*break*/, 2]; case 5: return [4 /*yield*/, ctx.db.delete(msg._id)]; case 6: _c.sent(); return [2 /*return*/]; } }); }; _i = 0, messages_1 = messages; _a.label = 3; case 3: if (!(_i < messages_1.length)) return [3 /*break*/, 6]; msg = messages_1[_i]; return [5 /*yield**/, _loop_1(msg)]; case 4: _a.sent(); _a.label = 5; case 5: _i++; return [3 /*break*/, 3]; case 6: return [4 /*yield*/, deleteByChannel(ctx, "channelKeys", args.id)]; case 7: _a.sent(); return [4 /*yield*/, deleteByChannel(ctx, "dmParticipants", args.id)]; case 8: _a.sent(); return [4 /*yield*/, deleteByChannel(ctx, "typingIndicators", args.id)]; case 9: _a.sent(); return [4 /*yield*/, deleteByChannel(ctx, "voiceStates", args.id)]; case 10: _a.sent(); return [4 /*yield*/, deleteByChannel(ctx, "channelReadState", args.id)]; case 11: _a.sent(); // Clear AFK setting if this channel was the AFK channel return [4 /*yield*/, ctx.runMutation(api_1.internal.serverSettings.clearAfkChannel, { channelId: args.id })]; case 12: // Clear AFK setting if this channel was the AFK channel _a.sent(); return [4 /*yield*/, ctx.db.delete(args.id)]; case 13: _a.sent(); return [2 /*return*/, { success: true }]; } }); }); }, });