From 338ef82a1e2a49c1b1eb28e9510027b9b60c7ffd Mon Sep 17 00:00:00 2001 From: an-lee Date: Fri, 9 Feb 2024 18:24:36 +0800 Subject: [PATCH] Fix recording sync (#291) * delete audio/video/recording in remote * sync recordings on profile page * handle recording sync failed --- enjoy/src/api/client.ts | 14 +++++++- enjoy/src/i18n/en.json | 5 ++- enjoy/src/i18n/zh-CN.json | 5 ++- .../main/db/handlers/recordings-handler.ts | 36 ++++++++++++++++++- enjoy/src/main/db/models/audio.ts | 14 ++++++-- enjoy/src/main/db/models/recording.ts | 12 +++++-- enjoy/src/main/db/models/video.ts | 14 ++++++-- enjoy/src/preload.ts | 3 ++ enjoy/src/renderer/pages/profile.tsx | 8 ++++- enjoy/src/types/enjoy-app.d.ts | 29 +++++++-------- 10 files changed, 115 insertions(+), 25 deletions(-) diff --git a/enjoy/src/api/client.ts b/enjoy/src/api/client.ts index d1fa39f5..8f787508 100644 --- a/enjoy/src/api/client.ts +++ b/enjoy/src/api/client.ts @@ -28,7 +28,7 @@ export class Client { }); this.api.interceptors.request.use((config) => { config.headers.Authorization = `Bearer ${accessToken}`; - config.headers['Accept-Language'] = locale; + config.headers["Accept-Language"] = locale; this.logger.debug( config.method.toUpperCase(), @@ -140,10 +140,18 @@ export class Client { return this.api.post("/api/mine/audios", decamelizeKeys(audio)); } + deleteAudio(id: string) { + return this.api.delete(`/api/mine/audios/${id}`); + } + syncVideo(video: Partial) { return this.api.post("/api/mine/videos", decamelizeKeys(video)); } + deleteVideo(id: string) { + return this.api.delete(`/api/mine/videos/${id}`); + } + syncTranscription(transcription: Partial) { return this.api.post("/api/transcriptions", decamelizeKeys(transcription)); } @@ -154,6 +162,10 @@ export class Client { return this.api.post("/api/mine/recordings", decamelizeKeys(recording)); } + deleteRecording(id: string) { + return this.api.delete(`/api/mine/recordings/${id}`); + } + generateSpeechToken(): Promise<{ token: string; region: string }> { return this.api.post("/api/speech/tokens"); } diff --git a/enjoy/src/i18n/en.json b/enjoy/src/i18n/en.json index 018aab81..30313649 100644 --- a/enjoy/src/i18n/en.json +++ b/enjoy/src/i18n/en.json @@ -444,5 +444,8 @@ "generatingIpaFailed": "Generating IPA failed", "translating": "Translating", "translatedSuccessfully": "Translated successfully", - "translationFailed": "Translation failed" + "translationFailed": "Translation failed", + "allRecordingsSynced": "All recordings synced", + "syncingRecordings": "Syncing {{count}} recordings", + "failedToSyncRecordings": "Syncing recordings failed" } diff --git a/enjoy/src/i18n/zh-CN.json b/enjoy/src/i18n/zh-CN.json index 6996fdfa..75710717 100644 --- a/enjoy/src/i18n/zh-CN.json +++ b/enjoy/src/i18n/zh-CN.json @@ -443,5 +443,8 @@ "generatingIpaFailed": "音标生成失败", "translating": "正在翻译", "translatedSuccessfully": "翻译成功", - "translationFailed": "翻译失败" + "translationFailed": "翻译失败", + "allRecordingsSynced": "所有录音已同步", + "syncingRecordings": "{{count}} 条录音正在同步", + "failedToSyncRecordings": "同步录音失败" } diff --git a/enjoy/src/main/db/handlers/recordings-handler.ts b/enjoy/src/main/db/handlers/recordings-handler.ts index a4124176..75ae09ee 100644 --- a/enjoy/src/main/db/handlers/recordings-handler.ts +++ b/enjoy/src/main/db/handlers/recordings-handler.ts @@ -14,6 +14,9 @@ import { } from "sequelize"; import dayjs from "dayjs"; import { t } from "i18next"; +import log from "electron-log/main"; + +const logger = log.scope("db/handlers/recordings-handler"); class RecordingsHandler { private async findAll( @@ -64,6 +67,36 @@ class RecordingsHandler { }); } + private async syncAll(event: IpcMainEvent) { + const recordings = await Recording.findAll({ + where: { syncedAt: null }, + }); + if (recordings.length == 0) return; + + event.sender.send("on-notification", { + type: "warning", + message: t("syncingRecordings", { count: recordings.length }), + }); + + try { + recordings.forEach(async (recording) => { + await recording.sync(); + }); + + event.sender.send("on-notification", { + type: "info", + message: t("allRecordingsSynced"), + }); + } catch (err) { + logger.error("failed to sync recordings", err.message); + + event.sender.send("on-notification", { + type: "error", + message: t("failedToSyncRecordings"), + }); + } + } + private async create( event: IpcMainEvent, options: Attributes & { @@ -293,7 +326,7 @@ class RecordingsHandler { private async groupBySegment( event: IpcMainEvent, targetId: string, - targetType: string, + targetType: string ) { return Recording.findAll({ where: { @@ -328,6 +361,7 @@ class RecordingsHandler { register() { ipcMain.handle("recordings-find-all", this.findAll); ipcMain.handle("recordings-find-one", this.findOne); + ipcMain.handle("recordings-sync-all", this.syncAll); ipcMain.handle("recordings-create", this.create); ipcMain.handle("recordings-destroy", this.destroy); ipcMain.handle("recordings-upload", this.upload); diff --git a/enjoy/src/main/db/models/audio.ts b/enjoy/src/main/db/models/audio.ts index ff901df9..ce3a2a9b 100644 --- a/enjoy/src/main/db/models/audio.ts +++ b/enjoy/src/main/db/models/audio.ts @@ -16,7 +16,7 @@ import { } from "sequelize-typescript"; import { Recording, Speech, Transcription, Video } from "@main/db/models"; import settings from "@main/settings"; -import { AudioFormats, VideoFormats , WEB_API_URL } from "@/constants"; +import { AudioFormats, VideoFormats, WEB_API_URL } from "@/constants"; import { hashFile } from "@/utils"; import path from "path"; import fs from "fs-extra"; @@ -170,7 +170,7 @@ export class Audio extends Model