Removed run moves in redis
This commit is contained in:
@@ -1,35 +0,0 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import { createClient } from "@/lib/supabase/server";
|
|
||||||
import * as Core from "@/modules/live-run/core";
|
|
||||||
import { getSession } from "@/modules/account/core";
|
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
|
||||||
const { runId, moves } = await req.json();
|
|
||||||
|
|
||||||
if (!runId || !moves)
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "body requires runId and moves" },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
|
|
||||||
const supabase = await createClient();
|
|
||||||
|
|
||||||
const session = await getSession(supabase);
|
|
||||||
|
|
||||||
if (!session)
|
|
||||||
return NextResponse.json({ error: "Invalid session" }, { status: 401 });
|
|
||||||
|
|
||||||
const liveRun = await Core.getLiveRun(runId);
|
|
||||||
|
|
||||||
if (!liveRun)
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Run is not live or does not exist" },
|
|
||||||
{ status: 401 },
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(moves);
|
|
||||||
|
|
||||||
const run = await Core.addLiveRunMoves(liveRun.id, moves);
|
|
||||||
|
|
||||||
return NextResponse.json({ ok: true, run });
|
|
||||||
}
|
|
||||||
@@ -9,8 +9,6 @@ import { json } from "@codemirror/lang-json";
|
|||||||
import { python } from "@codemirror/lang-python";
|
import { python } from "@codemirror/lang-python";
|
||||||
import { java } from "@codemirror/lang-java";
|
import { java } from "@codemirror/lang-java";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import useAction from "@/hook/use-action";
|
|
||||||
import { getLiveRunMoves } from "@/modules/live-run/actions";
|
|
||||||
import { LiveRun, LiveRunMove } from "@/modules/live-run/types";
|
import { LiveRun, LiveRunMove } from "@/modules/live-run/types";
|
||||||
|
|
||||||
type Language =
|
type Language =
|
||||||
|
|||||||
@@ -10,7 +10,3 @@ export async function getLiveRuns(): Promise<LiveRun[]> {
|
|||||||
export async function getLiveRun(id: string): Promise<LiveRun | null> {
|
export async function getLiveRun(id: string): Promise<LiveRun | null> {
|
||||||
return Core.getLiveRun(id);
|
return Core.getLiveRun(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getLiveRunMoves(id: string) {
|
|
||||||
return Core.getLiveRunMoves(id);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -43,35 +43,6 @@ export async function removeLiveRun(id: string) {
|
|||||||
await redis.del(`liveRun:${id}`, `liveRunMoves:${id}`);
|
await redis.del(`liveRun:${id}`, `liveRunMoves:${id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** -------------------- Live Run Moves -------------------- */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add moves to a live run (ephemeral)
|
|
||||||
*/
|
|
||||||
export async function addLiveRunMoves(id: string, moves: LiveRunMove[]) {
|
|
||||||
const runExists = await redis.exists(`liveRun:${id}`);
|
|
||||||
if (!runExists) throw new Error("Invalid run");
|
|
||||||
|
|
||||||
const pipeline = redis.pipeline();
|
|
||||||
moves.forEach((move) => {
|
|
||||||
pipeline.rpush(`liveRunMoves:${id}`, JSON.stringify(move));
|
|
||||||
});
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
redis.del(`liveRunMoves:${id}`);
|
|
||||||
}, 3000);
|
|
||||||
|
|
||||||
await pipeline.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get live moves for a run
|
|
||||||
*/
|
|
||||||
export async function getLiveRunMoves(id: string): Promise<LiveRunMove[]> {
|
|
||||||
const moves = await redis.lrange(`liveRunMoves:${id}`, 0, -1);
|
|
||||||
return moves.map((m) => JSON.parse(m)) as LiveRunMove[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** -------------------- Submit Run -------------------- */
|
/** -------------------- Submit Run -------------------- */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user