384 lines
18 KiB
JavaScript
384 lines
18 KiB
JavaScript
"use strict";
|
|
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.updateStatus = exports.getMyJoinSoundUrl = exports.updateProfile = exports.getPublicKeys = exports.createUserWithProfile = exports.verifyUser = exports.getSalt = void 0;
|
|
var server_1 = require("./_generated/server");
|
|
var values_1 = require("convex/values");
|
|
var storageUrl_1 = require("./storageUrl");
|
|
function sha256Hex(input) {
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
var buffer;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, crypto.subtle.digest("SHA-256", new TextEncoder().encode(input))];
|
|
case 1:
|
|
buffer = _a.sent();
|
|
return [2 /*return*/, Array.from(new Uint8Array(buffer))
|
|
.map(function (b) { return b.toString(16).padStart(2, "0"); })
|
|
.join("")];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
// Get salt for a username (returns fake salt for non-existent users)
|
|
exports.getSalt = (0, server_1.query)({
|
|
args: { username: values_1.v.string() },
|
|
returns: values_1.v.object({ salt: values_1.v.string() }),
|
|
handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
var user, fakeSalt;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, ctx.db
|
|
.query("userProfiles")
|
|
.withIndex("by_username", function (q) { return q.eq("username", args.username); })
|
|
.unique()];
|
|
case 1:
|
|
user = _a.sent();
|
|
if (user) {
|
|
return [2 /*return*/, { salt: user.clientSalt }];
|
|
}
|
|
return [4 /*yield*/, sha256Hex("SERVER_SECRET_KEY" + args.username)];
|
|
case 2:
|
|
fakeSalt = _a.sent();
|
|
return [2 /*return*/, { salt: fakeSalt }];
|
|
}
|
|
});
|
|
}); },
|
|
});
|
|
// Verify user credentials (DAK comparison)
|
|
exports.verifyUser = (0, server_1.mutation)({
|
|
args: {
|
|
username: values_1.v.string(),
|
|
dak: values_1.v.string(),
|
|
},
|
|
returns: values_1.v.union(values_1.v.object({
|
|
success: values_1.v.boolean(),
|
|
userId: values_1.v.string(),
|
|
encryptedMK: values_1.v.string(),
|
|
encryptedPrivateKeys: values_1.v.string(),
|
|
publicKey: values_1.v.string(),
|
|
}), values_1.v.object({ error: values_1.v.string() })),
|
|
handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
var user, hashedDAK;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, ctx.db
|
|
.query("userProfiles")
|
|
.withIndex("by_username", function (q) { return q.eq("username", args.username); })
|
|
.unique()];
|
|
case 1:
|
|
user = _a.sent();
|
|
if (!user) {
|
|
return [2 /*return*/, { error: "Invalid credentials" }];
|
|
}
|
|
return [4 /*yield*/, sha256Hex(args.dak)];
|
|
case 2:
|
|
hashedDAK = _a.sent();
|
|
if (hashedDAK === user.hashedAuthKey) {
|
|
return [2 /*return*/, {
|
|
success: true,
|
|
userId: user._id,
|
|
encryptedMK: user.encryptedMasterKey,
|
|
encryptedPrivateKeys: user.encryptedPrivateKeys,
|
|
publicKey: user.publicIdentityKey,
|
|
}];
|
|
}
|
|
return [2 /*return*/, { error: "Invalid credentials" }];
|
|
}
|
|
});
|
|
}); },
|
|
});
|
|
// Register new user with crypto keys
|
|
exports.createUserWithProfile = (0, server_1.mutation)({
|
|
args: {
|
|
username: values_1.v.string(),
|
|
salt: values_1.v.string(),
|
|
encryptedMK: values_1.v.string(),
|
|
hak: values_1.v.string(),
|
|
publicKey: values_1.v.string(),
|
|
signingKey: values_1.v.string(),
|
|
encryptedPrivateKeys: values_1.v.string(),
|
|
inviteCode: values_1.v.optional(values_1.v.string()),
|
|
},
|
|
returns: values_1.v.union(values_1.v.object({ success: values_1.v.boolean(), userId: values_1.v.string() }), values_1.v.object({ error: values_1.v.string() })),
|
|
handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
var existing, isFirstUser, invite, userId, everyoneRoleId, ownerRoleId, everyoneRole;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, ctx.db
|
|
.query("userProfiles")
|
|
.withIndex("by_username", function (q) { return q.eq("username", args.username); })
|
|
.unique()];
|
|
case 1:
|
|
existing = _a.sent();
|
|
if (existing) {
|
|
return [2 /*return*/, { error: "Username taken" }];
|
|
}
|
|
return [4 /*yield*/, ctx.db.query("userProfiles").first()];
|
|
case 2:
|
|
isFirstUser = (_a.sent()) === null;
|
|
if (!!isFirstUser) return [3 /*break*/, 5];
|
|
if (!args.inviteCode) {
|
|
return [2 /*return*/, { error: "Invite code required" }];
|
|
}
|
|
return [4 /*yield*/, ctx.db
|
|
.query("invites")
|
|
.withIndex("by_code", function (q) { return q.eq("code", args.inviteCode); })
|
|
.unique()];
|
|
case 3:
|
|
invite = _a.sent();
|
|
if (!invite) {
|
|
return [2 /*return*/, { error: "Invalid invite code" }];
|
|
}
|
|
if (invite.expiresAt && Date.now() > invite.expiresAt) {
|
|
return [2 /*return*/, { error: "Invite expired" }];
|
|
}
|
|
if (invite.maxUses !== undefined &&
|
|
invite.maxUses !== null &&
|
|
invite.uses >= invite.maxUses) {
|
|
return [2 /*return*/, { error: "Invite max uses reached" }];
|
|
}
|
|
return [4 /*yield*/, ctx.db.patch(invite._id, { uses: invite.uses + 1 })];
|
|
case 4:
|
|
_a.sent();
|
|
_a.label = 5;
|
|
case 5: return [4 /*yield*/, ctx.db.insert("userProfiles", {
|
|
username: args.username,
|
|
clientSalt: args.salt,
|
|
encryptedMasterKey: args.encryptedMK,
|
|
hashedAuthKey: args.hak,
|
|
publicIdentityKey: args.publicKey,
|
|
publicSigningKey: args.signingKey,
|
|
encryptedPrivateKeys: args.encryptedPrivateKeys,
|
|
isAdmin: isFirstUser,
|
|
})];
|
|
case 6:
|
|
userId = _a.sent();
|
|
if (!isFirstUser) return [3 /*break*/, 11];
|
|
return [4 /*yield*/, ctx.db.insert("roles", {
|
|
name: "@everyone",
|
|
color: "#99aab5",
|
|
position: 0,
|
|
permissions: {
|
|
create_invite: true,
|
|
embed_links: true,
|
|
attach_files: true,
|
|
},
|
|
isHoist: false,
|
|
})];
|
|
case 7:
|
|
everyoneRoleId = _a.sent();
|
|
return [4 /*yield*/, ctx.db.insert("roles", {
|
|
name: "Owner",
|
|
color: "#e91e63",
|
|
position: 100,
|
|
permissions: {
|
|
manage_channels: true,
|
|
manage_roles: true,
|
|
manage_messages: true,
|
|
create_invite: true,
|
|
embed_links: true,
|
|
attach_files: true,
|
|
},
|
|
isHoist: true,
|
|
})];
|
|
case 8:
|
|
ownerRoleId = _a.sent();
|
|
return [4 /*yield*/, ctx.db.insert("userRoles", { userId: userId, roleId: everyoneRoleId })];
|
|
case 9:
|
|
_a.sent();
|
|
return [4 /*yield*/, ctx.db.insert("userRoles", { userId: userId, roleId: ownerRoleId })];
|
|
case 10:
|
|
_a.sent();
|
|
return [3 /*break*/, 14];
|
|
case 11: return [4 /*yield*/, ctx.db
|
|
.query("roles")
|
|
.filter(function (q) { return q.eq(q.field("name"), "@everyone"); })
|
|
.first()];
|
|
case 12:
|
|
everyoneRole = _a.sent();
|
|
if (!everyoneRole) return [3 /*break*/, 14];
|
|
return [4 /*yield*/, ctx.db.insert("userRoles", {
|
|
userId: userId,
|
|
roleId: everyoneRole._id,
|
|
})];
|
|
case 13:
|
|
_a.sent();
|
|
_a.label = 14;
|
|
case 14: return [2 /*return*/, { success: true, userId: userId }];
|
|
}
|
|
});
|
|
}); },
|
|
});
|
|
// Get all users' public keys
|
|
exports.getPublicKeys = (0, server_1.query)({
|
|
args: {},
|
|
returns: values_1.v.array(values_1.v.object({
|
|
id: values_1.v.string(),
|
|
username: values_1.v.string(),
|
|
public_identity_key: values_1.v.string(),
|
|
status: values_1.v.optional(values_1.v.string()),
|
|
displayName: values_1.v.optional(values_1.v.string()),
|
|
avatarUrl: values_1.v.optional(values_1.v.union(values_1.v.string(), values_1.v.null())),
|
|
aboutMe: values_1.v.optional(values_1.v.string()),
|
|
customStatus: values_1.v.optional(values_1.v.string()),
|
|
joinSoundUrl: values_1.v.optional(values_1.v.union(values_1.v.string(), values_1.v.null())),
|
|
})),
|
|
handler: function (ctx) { return __awaiter(void 0, void 0, void 0, function () {
|
|
var users, results, _i, users_1, u, avatarUrl, joinSoundUrl;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, ctx.db.query("userProfiles").collect()];
|
|
case 1:
|
|
users = _a.sent();
|
|
results = [];
|
|
_i = 0, users_1 = users;
|
|
_a.label = 2;
|
|
case 2:
|
|
if (!(_i < users_1.length)) return [3 /*break*/, 8];
|
|
u = users_1[_i];
|
|
avatarUrl = null;
|
|
if (!u.avatarStorageId) return [3 /*break*/, 4];
|
|
return [4 /*yield*/, (0, storageUrl_1.getPublicStorageUrl)(ctx, u.avatarStorageId)];
|
|
case 3:
|
|
avatarUrl = _a.sent();
|
|
_a.label = 4;
|
|
case 4:
|
|
joinSoundUrl = null;
|
|
if (!u.joinSoundStorageId) return [3 /*break*/, 6];
|
|
return [4 /*yield*/, (0, storageUrl_1.getPublicStorageUrl)(ctx, u.joinSoundStorageId)];
|
|
case 5:
|
|
joinSoundUrl = _a.sent();
|
|
_a.label = 6;
|
|
case 6:
|
|
results.push({
|
|
id: u._id,
|
|
username: u.username,
|
|
public_identity_key: u.publicIdentityKey,
|
|
status: u.status || "offline",
|
|
displayName: u.displayName,
|
|
avatarUrl: avatarUrl,
|
|
aboutMe: u.aboutMe,
|
|
customStatus: u.customStatus,
|
|
joinSoundUrl: joinSoundUrl,
|
|
});
|
|
_a.label = 7;
|
|
case 7:
|
|
_i++;
|
|
return [3 /*break*/, 2];
|
|
case 8: return [2 /*return*/, results];
|
|
}
|
|
});
|
|
}); },
|
|
});
|
|
// Update user profile (aboutMe, avatar, customStatus)
|
|
exports.updateProfile = (0, server_1.mutation)({
|
|
args: {
|
|
userId: values_1.v.id("userProfiles"),
|
|
displayName: values_1.v.optional(values_1.v.string()),
|
|
aboutMe: values_1.v.optional(values_1.v.string()),
|
|
avatarStorageId: values_1.v.optional(values_1.v.id("_storage")),
|
|
customStatus: values_1.v.optional(values_1.v.string()),
|
|
joinSoundStorageId: values_1.v.optional(values_1.v.id("_storage")),
|
|
removeJoinSound: values_1.v.optional(values_1.v.boolean()),
|
|
},
|
|
returns: values_1.v.null(),
|
|
handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
var patch;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
patch = {};
|
|
if (args.displayName !== undefined)
|
|
patch.displayName = args.displayName;
|
|
if (args.aboutMe !== undefined)
|
|
patch.aboutMe = args.aboutMe;
|
|
if (args.avatarStorageId !== undefined)
|
|
patch.avatarStorageId = args.avatarStorageId;
|
|
if (args.customStatus !== undefined)
|
|
patch.customStatus = args.customStatus;
|
|
if (args.joinSoundStorageId !== undefined)
|
|
patch.joinSoundStorageId = args.joinSoundStorageId;
|
|
if (args.removeJoinSound)
|
|
patch.joinSoundStorageId = undefined;
|
|
return [4 /*yield*/, ctx.db.patch(args.userId, patch)];
|
|
case 1:
|
|
_a.sent();
|
|
return [2 /*return*/, null];
|
|
}
|
|
});
|
|
}); },
|
|
});
|
|
// Get the current user's join sound URL
|
|
exports.getMyJoinSoundUrl = (0, server_1.query)({
|
|
args: { userId: values_1.v.id("userProfiles") },
|
|
returns: values_1.v.union(values_1.v.string(), values_1.v.null()),
|
|
handler: function (ctx, args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
var user;
|
|
return __generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, ctx.db.get(args.userId)];
|
|
case 1:
|
|
user = _a.sent();
|
|
if (!(user === null || user === void 0 ? void 0 : user.joinSoundStorageId))
|
|
return [2 /*return*/, null];
|
|
return [4 /*yield*/, (0, storageUrl_1.getPublicStorageUrl)(ctx, user.joinSoundStorageId)];
|
|
case 2: return [2 /*return*/, _a.sent()];
|
|
}
|
|
});
|
|
}); },
|
|
});
|
|
// Update user status
|
|
exports.updateStatus = (0, server_1.mutation)({
|
|
args: {
|
|
userId: values_1.v.id("userProfiles"),
|
|
status: values_1.v.string(),
|
|
},
|
|
returns: 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.patch(args.userId, { status: args.status })];
|
|
case 1:
|
|
_a.sent();
|
|
return [2 /*return*/, null];
|
|
}
|
|
});
|
|
}); },
|
|
});
|