Skip to content

Commit

Permalink
add conversation clean feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 24, 2023
1 parent 3b240ae commit aaf32d1
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 4 deletions.
47 changes: 45 additions & 2 deletions app/src/components/home/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import {
useEffectAsync,
} from "../../utils.ts";
import {
deleteAllConversations,
deleteConversation,
toggleConversation,
updateConversationList,
} from "../../conversation/history.ts";
import { Button } from "../ui/button.tsx";
import { setMenu } from "../../store/menu.ts";
import { Copy, LogIn, Plus, RotateCw } from "lucide-react";
import {Copy, Eraser, LogIn, Plus, RotateCw} from "lucide-react";
import ConversationSegment from "./ConversationSegment.tsx";
import {
AlertDialog,
Expand All @@ -31,7 +32,7 @@ import {
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTitle, AlertDialogTrigger,
} from "../ui/alert-dialog.tsx";
import {
getSharedLink,
Expand All @@ -54,6 +55,7 @@ function SideBar() {
const history: ConversationInstance[] = useSelector(selectHistory);
const refresh = useRef(null);
const [shared, setShared] = useState<string>("");
const [removeAll, setRemoveAll] = useState<boolean>(false);
useEffectAsync(async () => {
await updateConversationList(dispatch);
}, []);
Expand All @@ -75,6 +77,47 @@ function SideBar() {
<Plus className={`h-4 w-4`} />
</Button>
<div className={`grow`} />
<AlertDialog open={removeAll} onOpenChange={setRemoveAll}>
<AlertDialogTrigger asChild>
<Button variant={`ghost`} size={`icon`}>
<Eraser className={`h-4 w-4`} />
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t("conversation.remove-all-title")}</AlertDialogTitle>
<AlertDialogDescription>
{t("conversation.remove-all-description")}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>
{t("conversation.cancel")}
</AlertDialogCancel>
<AlertDialogAction
onClick={async (e) => {
e.preventDefault();
e.stopPropagation();

if (await deleteAllConversations(dispatch))
toast({
title: t("conversation.delete-success"),
description: t("conversation.delete-success-prompt"),
});
else
toast({
title: t("conversation.delete-failed"),
description: t("conversation.delete-failed-prompt"),
});
setOperateConversation({ target: null, type: "" });
setRemoveAll(false);
}}
>
{t("conversation.delete")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<Button
className={`refresh-action`}
variant={`ghost`}
Expand Down
2 changes: 1 addition & 1 deletion app/src/conf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { Model } from "./conversation/types.ts";

export const version = "3.5.2";
export const version = "3.5.3";
export const dev: boolean = window.location.hostname === "localhost";
export const deploy: boolean = true;
export let rest_api: string = "http://localhost:8094";
Expand Down
9 changes: 9 additions & 0 deletions app/src/conversation/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export async function deleteConversation(
return true;
}

export async function deleteAllConversations(
dispatch: AppDispatch,
): Promise<boolean> {
const resp = await axios.get("/conversation/clean");
if (!resp.data.status) return false;
await manager.deleteAll(dispatch);
return true;
}

export async function toggleConversation(
dispatch: AppDispatch,
id: number,
Expand Down
7 changes: 7 additions & 0 deletions app/src/conversation/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export class Manager {
if (this.conversations[id]) delete this.conversations[id];
}

public async deleteAll(dispatch: AppDispatch): Promise<void> {
const ids = Object.keys(this.conversations).map((v) => parseInt(v));
for (const id of ids) await this.delete(dispatch, id);

await this.toggle(dispatch, -1);
}

public async send(t: any, auth: boolean, props: ChatProps): Promise<boolean> {
const id = this.getCurrent();
if (!this.conversations[id]) return false;
Expand Down
2 changes: 1 addition & 1 deletion app/src/dialogs/Subscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function calc_prize(month: number): number {
return base * 0.9;
}

return 8 * month;
return base;
}

type UpgradeProps = {
Expand Down
8 changes: 8 additions & 0 deletions app/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const resources = {
"remove-title": "Are you absolutely sure?",
"remove-description":
"This action cannot be undone. This will permanently delete the conversation ",
"remove-all-title": "Clear History",
"remove-all-description":
"This action cannot be undone. This will permanently delete all conversations, continue?",
cancel: "Cancel",
delete: "Delete",
"delete-conversation": "Delete Conversation",
Expand Down Expand Up @@ -225,6 +228,8 @@ const resources = {
"refresh-failed-prompt": "请求出错,请重试。",
"remove-title": "是否确定?",
"remove-description": "此操作无法撤消。这将永久删除对话 ",
"remove-all-title": "清除历史",
"remove-all-description": "此操作无法撤消。这将永久删除所有对话,是否继续?",
cancel: "取消",
delete: "删除",
"delete-conversation": "删除对话",
Expand Down Expand Up @@ -411,6 +416,9 @@ const resources = {
"remove-title": "Вы уверены?",
"remove-description":
"Это действие нельзя отменить. Это навсегда удалит разговор ",
"remove-all-title": "Очистить историю",
"remove-all-description":
"Это действие нельзя отменить. Это навсегда удалит все разговоры, продолжить?",
cancel: "Отмена",
delete: "Удалить",
"delete-conversation": "Удалить разговор",
Expand Down
25 changes: 25 additions & 0 deletions manager/conversation/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ func DeleteAPI(c *gin.Context) {
})
}

func CleanAPI(c *gin.Context) {
user := auth.GetUser(c)
if user == nil {
c.JSON(http.StatusOK, gin.H{
"status": false,
"message": "user not found",
})
return
}

db := utils.GetDBFromContext(c)
if err := DeleteAllConversations(db, *user); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": false,
"message": err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{
"status": true,
"message": "",
})
}

func ShareAPI(c *gin.Context) {
user := auth.GetUser(c)
if user == nil {
Expand Down
1 change: 1 addition & 0 deletions manager/conversation/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ func Register(app *gin.Engine) {
router.GET("/list", ListAPI)
router.GET("/load", LoadAPI)
router.GET("/delete", DeleteAPI)
router.GET("/clean", CleanAPI)

// share
router.POST("/share", ShareAPI)
Expand Down
6 changes: 6 additions & 0 deletions manager/conversation/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package conversation

import (
"chat/auth"
"chat/globals"
"chat/utils"
"database/sql"
Expand Down Expand Up @@ -97,3 +98,8 @@ func (c *Conversation) DeleteConversation(db *sql.DB) bool {
}
return true
}

func DeleteAllConversations(db *sql.DB, user auth.User) error {
_, err := db.Exec("DELETE FROM conversation WHERE user_id = ?", user.GetID(db))
return err
}

0 comments on commit aaf32d1

Please sign in to comment.