From 74e061ccac5cf736b040f48b3d2e49a026785dba Mon Sep 17 00:00:00 2001 From: an-lee Date: Thu, 22 Feb 2024 22:17:46 +0800 Subject: [PATCH] fix messages findOne (#342) --- .../src/main/db/handlers/messages-handler.ts | 4 ++- enjoy/src/preload.ts | 34 +++++++++---------- .../components/messages/assistant-message.tsx | 2 +- enjoy/src/types/enjoy-app.d.ts | 2 +- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/enjoy/src/main/db/handlers/messages-handler.ts b/enjoy/src/main/db/handlers/messages-handler.ts index 8bbce4c13..e342516d7 100644 --- a/enjoy/src/main/db/handlers/messages-handler.ts +++ b/enjoy/src/main/db/handlers/messages-handler.ts @@ -5,6 +5,7 @@ import log from "electron-log/main"; import { t } from "i18next"; import db from "@main/db"; +const logger = log.scope("messages-handler"); class MessagesHandler { private async findAll( event: IpcMainEvent, @@ -47,6 +48,7 @@ class MessagesHandler { association: "speeches", model: Speech, where: { sourceType: "Message" }, + required: false, }, ], where: { @@ -57,7 +59,7 @@ class MessagesHandler { return message?.toJSON(); }) .catch((err) => { - log.error(err); + logger.error(err); event.sender.send("on-notification", { type: "error", message: err.message, diff --git a/enjoy/src/preload.ts b/enjoy/src/preload.ts index c5f293ba5..a4941827d 100644 --- a/enjoy/src/preload.ts +++ b/enjoy/src/preload.ts @@ -185,13 +185,13 @@ contextBridge.exposeInMainWorld("__ENJOY_APP__", { }) => { return ipcRenderer.invoke("audios-find-all", params); }, - findOne: (params: object) => { + findOne: (params: any) => { return ipcRenderer.invoke("audios-find-one", params); }, - create: (uri: string, params?: object) => { + create: (uri: string, params?: any) => { return ipcRenderer.invoke("audios-create", uri, params); }, - update: (id: string, params: object) => { + update: (id: string, params: any) => { return ipcRenderer.invoke("audios-update", id, params); }, destroy: (id: string) => { @@ -208,13 +208,13 @@ contextBridge.exposeInMainWorld("__ENJOY_APP__", { }) => { return ipcRenderer.invoke("videos-find-all", params); }, - findOne: (params: object) => { + findOne: (params: any) => { return ipcRenderer.invoke("videos-find-one", params); }, - create: (uri: string, params?: object) => { + create: (uri: string, params?: any) => { return ipcRenderer.invoke("videos-create", uri, params); }, - update: (id: string, params: object) => { + update: (id: string, params: any) => { return ipcRenderer.invoke("videos-update", id, params); }, destroy: (id: string) => { @@ -233,16 +233,16 @@ contextBridge.exposeInMainWorld("__ENJOY_APP__", { }) => { return ipcRenderer.invoke("recordings-find-all", params); }, - findOne: (params: object) => { + findOne: (params: any) => { return ipcRenderer.invoke("recordings-find-one", params); }, syncAll: () => { return ipcRenderer.invoke("recordings-sync-all"); }, - create: (params: object) => { + create: (params: any) => { return ipcRenderer.invoke("recordings-create", params); }, - update: (id: string, params: object) => { + update: (id: string, params: any) => { return ipcRenderer.invoke("recordings-update", id, params); }, destroy: (id: string) => { @@ -272,16 +272,16 @@ contextBridge.exposeInMainWorld("__ENJOY_APP__", { }, }, conversations: { - findAll: (params: { where?: object; offset?: number; limit?: number }) => { + findAll: (params: { where?: any; offset?: number; limit?: number }) => { return ipcRenderer.invoke("conversations-find-all", params); }, - findOne: (params: object) => { + findOne: (params: any) => { return ipcRenderer.invoke("conversations-find-one", params); }, - create: (params: object) => { + create: (params: any) => { return ipcRenderer.invoke("conversations-create", params); }, - update: (id: string, params: object) => { + update: (id: string, params: any) => { return ipcRenderer.invoke("conversations-update", id, params); }, destroy: (id: string) => { @@ -303,11 +303,11 @@ contextBridge.exposeInMainWorld("__ENJOY_APP__", { }, }, messages: { - findAll: (params: { where?: object; offset?: number; limit?: number }) => { + findAll: (params: { where?: any; offset?: number; limit?: number }) => { return ipcRenderer.invoke("messages-find-all", params); }, - findOne: (params: object) => { - return ipcRenderer.invoke("messages-find-one", params); + findOne: (where: any) => { + return ipcRenderer.invoke("messages-find-one", where); }, createInBatch: (messages: Partial[]) => { return ipcRenderer.invoke("messages-create-in-batch", messages); @@ -417,7 +417,7 @@ contextBridge.exposeInMainWorld("__ENJOY_APP__", { get: (key: string) => { return ipcRenderer.invoke("cache-objects-get", key); }, - set: (key: string, value: string | object, ttl?: number) => { + set: (key: string, value: string | any, ttl?: number) => { return ipcRenderer.invoke("cache-objects-set", key, value, ttl); }, delete: (key: string) => { diff --git a/enjoy/src/renderer/components/messages/assistant-message.tsx b/enjoy/src/renderer/components/messages/assistant-message.tsx index d52f03d1d..86bd8ccca 100644 --- a/enjoy/src/renderer/components/messages/assistant-message.tsx +++ b/enjoy/src/renderer/components/messages/assistant-message.tsx @@ -61,7 +61,7 @@ export const AssistantMessageComponent = (props: { const findOrCreateSpeech = async () => { const msg = await EnjoyApp.messages.findOne({ id: message.id }); - if (msg.speeches.length > 0) { + if (msg && msg.speeches.length > 0) { setSpeech(msg.speeches[0]); } else { createSpeech(); diff --git a/enjoy/src/types/enjoy-app.d.ts b/enjoy/src/types/enjoy-app.d.ts index 07436d742..7665218ba 100644 --- a/enjoy/src/types/enjoy-app.d.ts +++ b/enjoy/src/types/enjoy-app.d.ts @@ -183,7 +183,7 @@ type EnjoyAppType = { }; messages: { findAll: (params: any) => Promise; - findOne: (params: any) => Promise; + findOne: (where: any) => Promise; createInBatch: (messages: Partial[]) => Promise; destroy: (id: string) => Promise; createSpeech: (id: string, configuration?: any) => Promise;