From 05f0985f7f75c0508617ae1b6a44d255cd8e14a0 Mon Sep 17 00:00:00 2001
From: linyuchen <lin.yu.chen@hotmail.com>
Date: Thu, 25 Apr 2024 23:27:14 +0800
Subject: [PATCH] feat: upload private file

---
 .../{UploadGroupFile.ts => UploadFile.ts}     | 32 +++++++++++++------
 1 file changed, 23 insertions(+), 9 deletions(-)
 rename src/onebot11/action/go-cqhttp/{UploadGroupFile.ts => UploadFile.ts} (52%)

diff --git a/src/onebot11/action/go-cqhttp/UploadGroupFile.ts b/src/onebot11/action/go-cqhttp/UploadFile.ts
similarity index 52%
rename from src/onebot11/action/go-cqhttp/UploadGroupFile.ts
rename to src/onebot11/action/go-cqhttp/UploadFile.ts
index d010090..2dc49c9 100644
--- a/src/onebot11/action/go-cqhttp/UploadGroupFile.ts
+++ b/src/onebot11/action/go-cqhttp/UploadFile.ts
@@ -1,27 +1,32 @@
 import BaseAction from "../BaseAction";
-import {getGroup} from "../../../common/data";
+import {getGroup, getUidByUin} from "../../../common/data";
 import {ActionName} from "../types";
 import {SendMsgElementConstructor} from "../../../ntqqapi/constructor";
 import {ChatType, SendFileElement} from "../../../ntqqapi/types";
 import fs from "fs";
-import {NTQQMsgApi} from "../../../ntqqapi/api/msg";
+import {NTQQMsgApi, Peer} from "../../../ntqqapi/api/msg";
 import {uri2local} from "../../../common/utils";
 
 interface Payload{
-    group_id: number
+    user_id: number
+    group_id?: number
     file: string
     name: string
     folder: string
 }
 
-export default class GoCQHTTPUploadGroupFile extends BaseAction<Payload, null> {
+class GoCQHTTPUploadFileBase extends BaseAction<Payload, null> {
     actionName = ActionName.GoCQHTTP_UploadGroupFile
 
-    protected async _handle(payload: Payload): Promise<null> {
-        const group = await getGroup(payload.group_id.toString());
-        if (!group){
-            throw new Error(`群组${payload.group_id}不存在`)
+
+    getPeer(payload: Payload): Peer {
+        if (payload.user_id){
+            return {chatType: ChatType.friend, peerUid: getUidByUin(payload.user_id.toString())}
         }
+        return {chatType: ChatType.group, peerUid: payload.group_id.toString()}
+    }
+
+    protected async _handle(payload: Payload): Promise<null> {
         let file = payload.file;
         if (fs.existsSync(file)){
             file = `file://${file}`
@@ -31,7 +36,16 @@ export default class GoCQHTTPUploadGroupFile extends BaseAction<Payload, null> {
             throw new Error(downloadResult.errMsg)
         }
         let sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name);
-        await NTQQMsgApi.sendMsg({chatType: ChatType.group, peerUid: group.groupCode}, [sendFileEle]);
+        await NTQQMsgApi.sendMsg(this.getPeer(payload), [sendFileEle]);
         return null
     }
+}
+
+
+export class GoCQHTTPUploadGroupFile extends GoCQHTTPUploadFileBase {
+    actionName = ActionName.GoCQHTTP_UploadGroupFile
+}
+
+export class GoCQHTTPUploadPrivateFile extends GoCQHTTPUploadFileBase {
+    actionName = ActionName.GoCQHTTP_UploadPrivateFile
 }
\ No newline at end of file