Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | /** * Room Member History Manager * Tracks all users who have ever been in a room */ import { and, eq } from 'drizzle-orm' import { db, schema } from '@/db' /** * Record or update a user's membership in room history * This is append-only for new users, or updates the lastAction for existing users */ export async function recordRoomMemberHistory(params: { roomId: string userId: string displayName: string action: 'active' | 'left' | 'kicked' | 'banned' }): Promise<schema.RoomMemberHistory> { const now = new Date() // Check if this user already has a history entry for this room const existing = await db .select() .from(schema.roomMemberHistory) .where( and( eq(schema.roomMemberHistory.roomId, params.roomId), eq(schema.roomMemberHistory.userId, params.userId) ) ) .limit(1) if (existing.length > 0) { // Update existing record const [updated] = await db .update(schema.roomMemberHistory) .set({ lastSeenAt: now, lastAction: params.action, lastActionAt: now, displayName: params.displayName, // Update display name in case it changed }) .where(eq(schema.roomMemberHistory.id, existing[0].id)) .returning() return updated } // Create new history record const [history] = await db .insert(schema.roomMemberHistory) .values({ roomId: params.roomId, userId: params.userId, displayName: params.displayName, firstJoinedAt: now, lastSeenAt: now, lastAction: params.action, lastActionAt: now, }) .returning() console.log('[Room History] Recorded history:', { userId: params.userId, roomId: params.roomId, action: params.action, }) return history } /** * Get all historical members for a room */ export async function getRoomMemberHistory(roomId: string): Promise<schema.RoomMemberHistory[]> { return await db .select() .from(schema.roomMemberHistory) .where(eq(schema.roomMemberHistory.roomId, roomId)) .orderBy(schema.roomMemberHistory.firstJoinedAt) } /** * Get history for a specific user in a room */ export async function getUserRoomHistory( roomId: string, userId: string ): Promise<schema.RoomMemberHistory | undefined> { const results = await db .select() .from(schema.roomMemberHistory) .where( and(eq(schema.roomMemberHistory.roomId, roomId), eq(schema.roomMemberHistory.userId, userId)) ) .limit(1) return results[0] } /** * Update the last action for a user in a room */ export async function updateRoomMemberAction( roomId: string, userId: string, action: 'active' | 'left' | 'kicked' | 'banned' ): Promise<void> { const now = new Date() await db .update(schema.roomMemberHistory) .set({ lastAction: action, lastActionAt: now, lastSeenAt: now, }) .where( and(eq(schema.roomMemberHistory.roomId, roomId), eq(schema.roomMemberHistory.userId, userId)) ) console.log('[Room History] Updated action:', { userId, roomId, action }) } export interface HistoricalMemberWithStatus { userId: string displayName: string firstJoinedAt: Date lastSeenAt: Date status: 'active' | 'banned' | 'kicked' | 'left' isCurrentlyInRoom: boolean isBanned: boolean banDetails?: { reason: string bannedBy: string bannedByName: string bannedAt: Date } invitationStatus?: 'pending' | 'accepted' | 'declined' | 'expired' | null } /** * Get all historical members with their current status * Combines data from history, current members, bans, and invitations */ export async function getRoomHistoricalMembersWithStatus( roomId: string ): Promise<HistoricalMemberWithStatus[]> { // Get all historical members const history = await getRoomMemberHistory(roomId) // Get current members const currentMembers = await db .select() .from(schema.roomMembers) .where(eq(schema.roomMembers.roomId, roomId)) const currentMemberIds = new Set(currentMembers.map((m) => m.userId)) // Get all bans const bans = await db.select().from(schema.roomBans).where(eq(schema.roomBans.roomId, roomId)) const banMap = new Map(bans.map((ban) => [ban.userId, ban])) // Get all invitations const invitations = await db .select() .from(schema.roomInvitations) .where(eq(schema.roomInvitations.roomId, roomId)) const invitationMap = new Map(invitations.map((inv) => [inv.userId, inv])) // Combine into result const results: HistoricalMemberWithStatus[] = history.map((h) => { const isCurrentlyInRoom = currentMemberIds.has(h.userId) const ban = banMap.get(h.userId) const invitation = invitationMap.get(h.userId) // Determine current status let status: 'active' | 'banned' | 'kicked' | 'left' if (ban) { status = 'banned' } else if (isCurrentlyInRoom) { status = 'active' } else { status = h.lastAction // Use the recorded action from history } return { userId: h.userId, displayName: h.displayName, firstJoinedAt: h.firstJoinedAt, lastSeenAt: h.lastSeenAt, status, isCurrentlyInRoom, isBanned: !!ban, banDetails: ban ? { reason: ban.reason, bannedBy: ban.bannedBy, bannedByName: ban.bannedByName, bannedAt: ban.createdAt, } : undefined, invitationStatus: invitation?.status || null, } }) return results } |