All files / web/src/lib/arcade room-moderation.ts

0% Statements 0/197
0% Branches 0/1
0% Functions 0/1
0% Lines 0/197

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                                                                                                                                                                                                                                                                                                                                                                                                           
/**
 * Room Moderation Library
 * Handles reports, bans, and kicks for arcade rooms
 */

import { and, desc, eq } from 'drizzle-orm'
import { db } from '@/db'
import { roomBans, roomMembers, roomReports } from '@/db/schema'
import { recordRoomMemberHistory } from './room-member-history'

/**
 * Check if a user is banned from a room
 */
export async function isUserBanned(roomId: string, userId: string): Promise<boolean> {
  const ban = await db
    .select()
    .from(roomBans)
    .where(and(eq(roomBans.roomId, roomId), eq(roomBans.userId, userId)))
    .limit(1)

  return ban.length > 0
}

/**
 * Get all bans for a room
 */
export async function getRoomBans(roomId: string) {
  return db
    .select()
    .from(roomBans)
    .where(eq(roomBans.roomId, roomId))
    .orderBy(desc(roomBans.createdAt))
}

/**
 * Ban a user from a room
 */
export async function banUserFromRoom(params: {
  roomId: string
  userId: string
  userName: string
  bannedBy: string
  bannedByName: string
  reason: 'harassment' | 'cheating' | 'inappropriate-name' | 'spam' | 'afk' | 'other'
  notes?: string
}) {
  // Insert ban record (upsert in case they were already banned)
  const [ban] = await db
    .insert(roomBans)
    .values({
      roomId: params.roomId,
      userId: params.userId,
      userName: params.userName,
      bannedBy: params.bannedBy,
      bannedByName: params.bannedByName,
      reason: params.reason,
      notes: params.notes,
    })
    .onConflictDoUpdate({
      target: [roomBans.userId, roomBans.roomId],
      set: {
        bannedBy: params.bannedBy,
        bannedByName: params.bannedByName,
        reason: params.reason,
        notes: params.notes,
        createdAt: new Date(),
      },
    })
    .returning()

  // Remove user from room members
  await db
    .delete(roomMembers)
    .where(and(eq(roomMembers.roomId, params.roomId), eq(roomMembers.userId, params.userId)))

  // Record in history
  await recordRoomMemberHistory({
    roomId: params.roomId,
    userId: params.userId,
    displayName: params.userName,
    action: 'banned',
  })

  return ban
}

/**
 * Unban a user from a room
 */
export async function unbanUserFromRoom(roomId: string, userId: string) {
  await db.delete(roomBans).where(and(eq(roomBans.roomId, roomId), eq(roomBans.userId, userId)))
}

/**
 * Kick a user from a room (remove without banning)
 */
export async function kickUserFromRoom(roomId: string, userId: string) {
  // Get member info before deleting
  const member = await db
    .select()
    .from(roomMembers)
    .where(and(eq(roomMembers.roomId, roomId), eq(roomMembers.userId, userId)))
    .limit(1)

  await db
    .delete(roomMembers)
    .where(and(eq(roomMembers.roomId, roomId), eq(roomMembers.userId, userId)))

  // Record in history
  if (member.length > 0) {
    await recordRoomMemberHistory({
      roomId,
      userId,
      displayName: member[0].displayName,
      action: 'kicked',
    })
  }
}

/**
 * Submit a report
 */
export async function createReport(params: {
  roomId: string
  reporterId: string
  reporterName: string
  reportedUserId: string
  reportedUserName: string
  reason: 'harassment' | 'cheating' | 'inappropriate-name' | 'spam' | 'afk' | 'other'
  details?: string
}) {
  const [report] = await db
    .insert(roomReports)
    .values({
      roomId: params.roomId,
      reporterId: params.reporterId,
      reporterName: params.reporterName,
      reportedUserId: params.reportedUserId,
      reportedUserName: params.reportedUserName,
      reason: params.reason,
      details: params.details,
      status: 'pending',
    })
    .returning()

  return report
}

/**
 * Get pending reports for a room
 */
export async function getPendingReports(roomId: string) {
  return db
    .select()
    .from(roomReports)
    .where(and(eq(roomReports.roomId, roomId), eq(roomReports.status, 'pending')))
    .orderBy(desc(roomReports.createdAt))
}

/**
 * Get all reports for a room
 */
export async function getAllReports(roomId: string) {
  return db
    .select()
    .from(roomReports)
    .where(eq(roomReports.roomId, roomId))
    .orderBy(desc(roomReports.createdAt))
}

/**
 * Mark report as reviewed
 */
export async function markReportReviewed(reportId: string, reviewedBy: string) {
  await db
    .update(roomReports)
    .set({
      status: 'reviewed',
      reviewedAt: new Date(),
      reviewedBy,
    })
    .where(eq(roomReports.id, reportId))
}

/**
 * Dismiss a report
 */
export async function dismissReport(reportId: string, reviewedBy: string) {
  await db
    .update(roomReports)
    .set({
      status: 'dismissed',
      reviewedAt: new Date(),
      reviewedBy,
    })
    .where(eq(roomReports.id, reportId))
}