Files
DiscordClone/convex/schema.ts
Bryan1029384756 b83360db35
Some checks failed
Build and Release / build-and-release (push) Has been cancelled
bump
2026-04-20 17:09:04 -05:00

275 lines
9.3 KiB
TypeScript

import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
export default defineSchema({
userProfiles: defineTable({
username: v.string(),
clientSalt: v.string(),
encryptedMasterKey: v.string(),
hashedAuthKey: v.string(),
publicIdentityKey: v.string(),
publicSigningKey: v.string(),
encryptedPrivateKeys: v.string(),
isAdmin: v.boolean(),
status: v.optional(v.string()),
displayName: v.optional(v.string()),
avatarStorageId: v.optional(v.id("_storage")),
aboutMe: v.optional(v.string()),
customStatus: v.optional(v.string()),
joinSoundStorageId: v.optional(v.id("_storage")),
accentColor: v.optional(v.string()),
bannerStorageId: v.optional(v.id("_storage")),
// Discord backup import support. `discordId` lets us dedupe
// ghost creation across re-runs and later "claim" an identity
// by attaching the snowflake to a real user. `isGhost` marks a
// placeholder profile created to attribute imported messages
// when the original Discord user hasn't been mapped to a real
// local user yet — ghosts can't log in (their auth material is
// random junk) and are filtered out of member / presence /
// mention lookups.
discordId: v.optional(v.string()),
isGhost: v.optional(v.boolean()),
// URL-based avatar fallback for ghosts — we copy Discord's CDN
// URL rather than re-hosting, since the ghost might never be
// merged and uploading 35+ avatars-that-might-expire isn't
// worth the storage churn. `null`/missing on real users, who
// use `avatarStorageId` instead.
ghostAvatarUrl: v.optional(v.string()),
}).index("by_username", ["username"])
.index("by_discord_id", ["discordId"]),
categories: defineTable({
name: v.string(),
position: v.number(),
}).index("by_position", ["position"]),
channels: defineTable({
name: v.string(),
type: v.string(), // 'text' | 'voice' | 'dm'
categoryId: v.optional(v.id("categories")),
topic: v.optional(v.string()),
position: v.optional(v.number()),
}).index("by_name", ["name"])
.index("by_category", ["categoryId"]),
messages: defineTable({
channelId: v.id("channels"),
senderId: v.id("userProfiles"),
ciphertext: v.string(),
nonce: v.string(),
signature: v.string(),
keyVersion: v.number(),
replyTo: v.optional(v.id("messages")),
editedAt: v.optional(v.number()),
pinned: v.optional(v.boolean()),
// Discord backup import fields. `importedCreatedAt` preserves
// the original Discord timestamp so imports land in the right
// place in channel history — the renderer prefers it over
// `_creationTime` when present. `discordMessageId` is the
// snowflake used for dedupe on re-runs + remapping Discord
// reply-to IDs to Convex message IDs. `isImported` is a cheap
// flag for UI badges and future filters.
importedCreatedAt: v.optional(v.number()),
discordMessageId: v.optional(v.string()),
isImported: v.optional(v.boolean()),
}).index("by_channel", ["channelId"])
.index("by_channel_pinned", ["channelId", "pinned"])
.index("by_sender", ["senderId"])
.index("by_channel_imported_at", ["channelId", "importedCreatedAt"])
.index("by_discord_message_id", ["discordMessageId"])
.index("by_channel_discord_message_id", ["channelId", "discordMessageId"]),
messageReactions: defineTable({
messageId: v.id("messages"),
userId: v.id("userProfiles"),
emoji: v.string(),
})
.index("by_message", ["messageId"])
.index("by_message_user_emoji", ["messageId", "userId", "emoji"])
.index("by_user", ["userId"]),
channelKeys: defineTable({
channelId: v.id("channels"),
userId: v.id("userProfiles"),
encryptedKeyBundle: v.string(),
keyVersion: v.number(),
})
.index("by_channel", ["channelId"])
.index("by_user", ["userId"])
.index("by_channel_and_user", ["channelId", "userId"]),
roles: defineTable({
name: v.string(),
color: v.string(),
position: v.number(),
permissions: v.any(), // JSON object of permissions
isHoist: v.boolean(),
}),
userRoles: defineTable({
userId: v.id("userProfiles"),
roleId: v.id("roles"),
})
.index("by_user", ["userId"])
.index("by_role", ["roleId"])
.index("by_user_and_role", ["userId", "roleId"]),
invites: defineTable({
code: v.string(),
encryptedPayload: v.string(),
createdBy: v.id("userProfiles"),
maxUses: v.optional(v.number()),
uses: v.number(),
expiresAt: v.optional(v.number()), // timestamp
keyVersion: v.number(),
}).index("by_code", ["code"])
.index("by_creator", ["createdBy"]),
dmParticipants: defineTable({
channelId: v.id("channels"),
userId: v.id("userProfiles"),
})
.index("by_channel", ["channelId"])
.index("by_user", ["userId"]),
typingIndicators: defineTable({
channelId: v.id("channels"),
userId: v.id("userProfiles"),
username: v.string(),
expiresAt: v.number(), // timestamp
}).index("by_channel", ["channelId"])
.index("by_user", ["userId"])
.index("by_expires_at", ["expiresAt"]),
voiceStates: defineTable({
channelId: v.id("channels"),
userId: v.id("userProfiles"),
username: v.string(),
isMuted: v.boolean(),
isDeafened: v.boolean(),
isScreenSharing: v.boolean(),
isServerMuted: v.boolean(),
// Webcam publish state — optional so existing rows still
// validate. Undefined means "unknown / not reported yet",
// which the UI treats the same as `false`.
isCameraOn: v.optional(v.boolean()),
watchingStream: v.optional(v.id("userProfiles")),
lastHeartbeat: v.optional(v.number()),
})
.index("by_channel", ["channelId"])
.index("by_user", ["userId"]),
channelReadState: defineTable({
userId: v.id("userProfiles"),
channelId: v.id("channels"),
lastReadTimestamp: v.number(),
})
.index("by_user", ["userId"])
.index("by_channel", ["channelId"])
.index("by_user_and_channel", ["userId", "channelId"]),
serverSettings: defineTable({
serverName: v.optional(v.string()),
afkChannelId: v.optional(v.id("channels")),
afkTimeout: v.number(), // seconds (default 300 = 5 min)
iconStorageId: v.optional(v.id("_storage")),
}),
customEmojis: defineTable({
name: v.string(),
storageId: v.id("_storage"),
uploadedBy: v.id("userProfiles"),
// `true` for animated (GIF / APNG) uploads so the settings UI
// can split Static vs Animated in separate sections. Optional
// so existing rows without the flag still validate — they
// surface as static in the UI by default.
animated: v.optional(v.boolean()),
createdAt: v.number(),
}).index("by_name", ["name"])
.index("by_uploader", ["uploadedBy"]),
polls: defineTable({
channelId: v.id("channels"),
createdBy: v.id("userProfiles"),
question: v.string(),
options: v.array(
v.object({
id: v.string(),
text: v.string(),
}),
),
allowMultiple: v.boolean(),
disclosed: v.boolean(),
closed: v.boolean(),
closesAt: v.optional(v.number()),
createdAt: v.number(),
})
.index("by_channel", ["channelId"])
.index("by_creator", ["createdBy"]),
pollVotes: defineTable({
pollId: v.id("polls"),
userId: v.id("userProfiles"),
optionIds: v.array(v.string()),
votedAt: v.number(),
})
.index("by_poll", ["pollId"])
.index("by_poll_and_user", ["pollId", "userId"]),
pollReactions: defineTable({
pollId: v.id("polls"),
userId: v.id("userProfiles"),
emoji: v.string(),
})
.index("by_poll", ["pollId"])
.index("by_poll_user_emoji", ["pollId", "userId", "emoji"])
.index("by_user", ["userId"]),
// Banned users — presence of a row = login + send blocked for that
// user. Unique by `userId` (enforced by the only caller path —
// `bans.ban` does an upsert, not raw insert).
bans: defineTable({
userId: v.id("userProfiles"),
bannedBy: v.id("userProfiles"),
reason: v.optional(v.string()),
createdAt: v.number(),
}).index("by_user", ["userId"]),
// Append-only log of admin-ish actions: channel / role edits, bans,
// server settings changes. Target fields are stringly-typed so
// entries can point at any table without a discriminated union.
auditLog: defineTable({
actorId: v.id("userProfiles"),
action: v.string(),
targetType: v.optional(v.string()),
targetId: v.optional(v.string()),
targetName: v.optional(v.string()),
metadata: v.optional(v.any()),
createdAt: v.number(),
})
.index("by_created_at", ["createdAt"])
.index("by_actor", ["actorId"]),
savedMedia: defineTable({
userId: v.id("userProfiles"),
// Convex storage URL — also the dedupe key for a single user.
url: v.string(),
kind: v.string(), // 'image' | 'video' | 'audio'
filename: v.string(),
mimeType: v.optional(v.string()),
width: v.optional(v.number()),
height: v.optional(v.number()),
size: v.optional(v.number()),
// Re-post path: keep the per-file AES key + iv so the same
// attachment metadata can be embedded in a future message
// without re-uploading. The file stays encrypted in storage —
// saving just bookmarks the metadata.
encryptionKey: v.string(),
encryptionIv: v.string(),
savedAt: v.number(),
})
.index("by_user", ["userId"])
.index("by_user_and_url", ["userId", "url"]),
});