feat: Kinematic Instant Messaging #801

Merged
Sainan merged 2 commits from pom2 into main 2025-01-18 16:57:25 -08:00
2 changed files with 25 additions and 0 deletions
Showing only changes of commit d8201594b4 - Show all commits

View File

@ -0,0 +1,23 @@
import { getInventory } from "@/src/services/inventoryService";
import { getAccountIdForRequest } from "@/src/services/loginService";
import { RequestHandler } from "express";
export const clearDialogueHistoryController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId);
const request = JSON.parse(String(req.body)) as IClearDialogueRequest;
coderabbitai[bot] commented 2025-01-17 21:00:19 -08:00 (Migrated from github.com)
Review

⚠️ Potential issue

Add error handling for JSON parsing.

The JSON parsing operation could throw an exception if the request body is malformed.

Apply this diff to add error handling:

-    const request = JSON.parse(String(req.body)) as IClearDialogueRequest;
+    let request: IClearDialogueRequest;
+    try {
+        request = JSON.parse(String(req.body)) as IClearDialogueRequest;
+    } catch (error) {
+        res.status(400).json({ error: 'Invalid request body' });
+        return;
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    let request: IClearDialogueRequest;
    try {
        request = JSON.parse(String(req.body)) as IClearDialogueRequest;
    } catch (error) {
        res.status(400).json({ error: 'Invalid request body' });
        return;
    }
_:warning: Potential issue_ **Add error handling for JSON parsing.** The JSON parsing operation could throw an exception if the request body is malformed. Apply this diff to add error handling: ```diff - const request = JSON.parse(String(req.body)) as IClearDialogueRequest; + let request: IClearDialogueRequest; + try { + request = JSON.parse(String(req.body)) as IClearDialogueRequest; + } catch (error) { + res.status(400).json({ error: 'Invalid request body' }); + return; + } ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. `````suggestion let request: IClearDialogueRequest; try { request = JSON.parse(String(req.body)) as IClearDialogueRequest; } catch (error) { res.status(400).json({ error: 'Invalid request body' }); return; } ````` </details> <!-- suggestion_end --> <!-- This is an auto-generated comment by CodeRabbit -->
if (inventory.DialogueHistory && inventory.DialogueHistory.Dialogues) {
for (const dialogueName of request.Dialogues) {
const index = inventory.DialogueHistory.Dialogues.findIndex(x => x.DialogueName == dialogueName);
if (index != -1) {
inventory.DialogueHistory.Dialogues.splice(index, 1);
}
}
}
coderabbitai[bot] commented 2025-01-17 21:00:20 -08:00 (Migrated from github.com)
Review

⚠️ Potential issue

Add request validation and error handling.

The function should validate the request structure and handle potential errors during inventory operations.

Apply this diff to add validation and error handling:

+    if (!request.Dialogues || !Array.isArray(request.Dialogues)) {
+        res.status(400).json({ error: 'Invalid request: Dialogues array is required' });
+        return;
+    }
+
     if (inventory.DialogueHistory && inventory.DialogueHistory.Dialogues) {
         for (const dialogueName of request.Dialogues) {
+            if (typeof dialogueName !== 'string') {
+                res.status(400).json({ error: 'Invalid request: Dialogue names must be strings' });
+                return;
+            }
+
             const index = inventory.DialogueHistory.Dialogues.findIndex(x => x.DialogueName == dialogueName);
             if (index != -1) {
                 inventory.DialogueHistory.Dialogues.splice(index, 1);
             }
         }
     }
-    await inventory.save();
-    res.end();
+    try {
+        await inventory.save();
+        res.status(200).json({
+            success: true,
+            message: 'Dialogue history cleared successfully'
+        });
+    } catch (error) {
+        res.status(500).json({ error: 'Failed to save inventory' });
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    if (!request.Dialogues || !Array.isArray(request.Dialogues)) {
        res.status(400).json({ error: 'Invalid request: Dialogues array is required' });
        return;
    }

    if (inventory.DialogueHistory && inventory.DialogueHistory.Dialogues) {
        for (const dialogueName of request.Dialogues) {
            if (typeof dialogueName !== 'string') {
                res.status(400).json({ error: 'Invalid request: Dialogue names must be strings' });
                return;
            }

            const index = inventory.DialogueHistory.Dialogues.findIndex(x => x.DialogueName == dialogueName);
            if (index != -1) {
                inventory.DialogueHistory.Dialogues.splice(index, 1);
            }
        }
    }
    try {
        await inventory.save();
        res.status(200).json({
            success: true,
            message: 'Dialogue history cleared successfully'
        });
    } catch (error) {
        res.status(500).json({ error: 'Failed to save inventory' });
    }
🧰 Tools
🪛 Biome (1.9.4)

[error] 9-9: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

_:warning: Potential issue_ **Add request validation and error handling.** The function should validate the request structure and handle potential errors during inventory operations. Apply this diff to add validation and error handling: ```diff + if (!request.Dialogues || !Array.isArray(request.Dialogues)) { + res.status(400).json({ error: 'Invalid request: Dialogues array is required' }); + return; + } + if (inventory.DialogueHistory && inventory.DialogueHistory.Dialogues) { for (const dialogueName of request.Dialogues) { + if (typeof dialogueName !== 'string') { + res.status(400).json({ error: 'Invalid request: Dialogue names must be strings' }); + return; + } + const index = inventory.DialogueHistory.Dialogues.findIndex(x => x.DialogueName == dialogueName); if (index != -1) { inventory.DialogueHistory.Dialogues.splice(index, 1); } } } - await inventory.save(); - res.end(); + try { + await inventory.save(); + res.status(200).json({ + success: true, + message: 'Dialogue history cleared successfully' + }); + } catch (error) { + res.status(500).json({ error: 'Failed to save inventory' }); + } ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. `````suggestion if (!request.Dialogues || !Array.isArray(request.Dialogues)) { res.status(400).json({ error: 'Invalid request: Dialogues array is required' }); return; } if (inventory.DialogueHistory && inventory.DialogueHistory.Dialogues) { for (const dialogueName of request.Dialogues) { if (typeof dialogueName !== 'string') { res.status(400).json({ error: 'Invalid request: Dialogue names must be strings' }); return; } const index = inventory.DialogueHistory.Dialogues.findIndex(x => x.DialogueName == dialogueName); if (index != -1) { inventory.DialogueHistory.Dialogues.splice(index, 1); } } } try { await inventory.save(); res.status(200).json({ success: true, message: 'Dialogue history cleared successfully' }); } catch (error) { res.status(500).json({ error: 'Failed to save inventory' }); } ````` </details> <!-- suggestion_end --> <details> <summary>🧰 Tools</summary> <details> <summary>🪛 Biome (1.9.4)</summary> [error] 9-9: Change to an optional chain. Unsafe fix: Change to an optional chain. (lint/complexity/useOptionalChain) </details> </details> <!-- This is an auto-generated comment by CodeRabbit -->
await inventory.save();
res.end();
};
interface IClearDialogueRequest {
Dialogues: string[];
}

View File

@ -6,6 +6,7 @@ import { archonFusionController } from "@/src/controllers/api/archonFusionContro
import { artifactsController } from "../controllers/api/artifactsController";
import { checkDailyMissionBonusController } from "@/src/controllers/api/checkDailyMissionBonusController";
import { claimCompletedRecipeController } from "@/src/controllers/api/claimCompletedRecipeController";
import { clearDialogueHistoryController } from "@/src/controllers/api/clearDialogueHistoryController";
import { createGuildController } from "@/src/controllers/api/createGuildController";
import { creditsController } from "@/src/controllers/api/creditsController";
import { deleteSessionController } from "@/src/controllers/api/deleteSessionController";
@ -119,6 +120,7 @@ apiRouter.post("/arcaneCommon.php", arcaneCommonController);
apiRouter.post("/archonFusion.php", archonFusionController);
apiRouter.post("/artifacts.php", artifactsController);
apiRouter.post("/claimCompletedRecipe.php", claimCompletedRecipeController);
apiRouter.post("/clearDialogueHistory.php", clearDialogueHistoryController);
apiRouter.post("/createGuild.php", createGuildController);
apiRouter.post("/endlessXp.php", endlessXpController);
apiRouter.post("/evolveWeapon.php", evolveWeaponController);