Compare commits

...

5 Commits

Author SHA1 Message Date
CrescentLeaf
0000eeead2 初步编写网络接口相关类 2025-08-14 12:07:28 +08:00
CrescentLeaf
581a6acc52 添加 http(s).Server 和任意 Function 的类型定义 2025-08-14 12:06:15 +08:00
CrescentLeaf
c23ce07c85 实现了 User 类中的 Settings 2025-08-14 12:05:06 +08:00
CrescentLeaf
163e9d97c2 修正 Chat.ts 中对 table_name 的引用, 实现 Settings 类 2025-08-14 12:04:22 +08:00
CrescentLeaf
b39be9301d 修正 FileManager.uploadFile, 添加更多元数据, 修正一个无法被 lint 的错误
其中, 修正 lint 是把 FileBean 从 interface 换成了 class 定义
2025-08-14 12:01:08 +08:00
7 changed files with 144 additions and 38 deletions

12
src/api/ApiManager.ts Normal file
View File

@@ -0,0 +1,12 @@
import BaseApi from './BaseApi.ts'
import HttpServerLike from '../types/HttpServerLike.ts'
export default class ApiManager {
declare httpServer: HttpServerLike
static init(httpServer: HttpServerLike) {
this.httpServer = httpServer
}
static getHttpServer() {
return this.httpServer
}
}

12
src/api/BaseApi.ts Normal file
View File

@@ -0,0 +1,12 @@
import UnknownFunction from '../types/UnknownFunction.ts'
export default abstract class BaseApi {
abstract getName(): string
constructor() {
this.init()
}
abstract init(): void
registerEvent(name: string, func: UnknownFunction) {
}
}

View File

@@ -1,5 +1,5 @@
import { DatabaseSync } from "node:sqlite"
import { Buffer } from "node:buffer"
// import { Buffer } from "node:buffer"
import path from 'node:path'
import config from '../config.ts'
@@ -25,40 +25,58 @@ export default class Chat {
return db
}
private static findAllByCondition(condition: string, ...args: unknown[]): UserBean[] {
return database.prepare(`SELECT count, id FROM ${User.table_name} WHERE ${condition}`).all(...args)
private static findAllByCondition(condition: string, ...args: unknown[]): ChatBean[] {
return database.prepare(`SELECT * FROM ${Chat.table_name} WHERE ${condition}`).all(...args)
}
static findById(id: string): Chat {
const beans = Chat.findAllBeansByCondition('id = ?', id)
if (beans.length == 0)
throw new Error(`找不到 id 为 ${id} 的 Chat`)
else if (beans.length > 1)
console.error(chalk.red(`警告: 查询 id = ${id} 时, 查询到多个相同 ID 的 Chat`))
return new Chat(beans[0])
}
declare bean: ChatBean
constructor(bean: ChatBean) {
this.bean = bean
}
private setAttr(key: string, value: unknown): void {
User.database.prepare(`UPDATE ${User.table_name} SET ${key} = ? WHERE id = ?`).run(value, this.bean.id)
User.database.prepare(`UPDATE ${Chat.table_name} SET ${key} = ? WHERE id = ?`).run(value, this.bean.id)
this.bean[key] = value
}
getUserName(): string {
return this.bean.username
}
setUserName(userName: string): void {
this.setAttr("username", userName)
}
getNickName(): string {
return this.bean.nickname
}
setNickName(nickName: string): void {
this.setAttr("nickname", nickName)
}
getAvatar(): Uint8Array {
return this.bean.avatar
}
setAvatar(avatar: Buffer): void {
this.setAttr("avatar", avatar)
getSettings(): Chat.Settings {
return new Settings(JSON.parse(this.bean.settings))
}
getContacts() {
static Settings = class {
declare bean: Chat.SettingsBean
declare chat: Chat
constructor(chat: Chat, bean: Chat.SettingsBean) {
this.bean = bean
this.chat = chat
for (const i of [
]) {
this["set" + i.substring(0, 1).toUpperCase() + i.substring(1)] = (v: unknown) => {
this.set(i, v)
}
}
}
set(key: string, value: unknown) {
this.bean[key] = value
}
get(key: string) {
return this.bean[key]
}
apply() {
this.chat.setAttr("settings", JSON.stringify(this.bean))
}
}
static SettingsBean = class {
}
}

View File

@@ -7,16 +7,17 @@ import fs from 'node:fs/promises'
import { fileTypeFromBuffer } from 'file-type'
export default class FileManager {
static FileBean = interface {
count: number,
name: string,
hash: string,
mime: string,
static FileBean = class {
declare count: number
declare name: string
declare hash: string
declare mime: string
declare chatid: string
}
static File = class {
declare bean: FileManager.FileBean
constructor(bean: UserBean) {
constructor(bean: FileManager.FileBean) {
this.bean = bean
}
getMime(): string {
@@ -36,6 +37,9 @@ export default class FileManager {
this.bean.hash
)
}
getChatId(): string {
return this.bean.chatid
}
async readAsync(): Buffer {
return await fs.readFile(this.getFilePath())
}
@@ -50,16 +54,49 @@ export default class FileManager {
/* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT,
/* 文件名称 */ name TEXT NOT NULL,
/* 文件哈希 */ hash TEXT NOT NULL,
/* MIME 类型 */ mime TEXT NOT NULL
/* MIME 类型 */ mime TEXT NOT NULL,
/* 来源 Chat */ chatid TEXT NOT NULL,
/* 上传时间 */ upload_time INT8 NOT NULL,
/* 最后使用时间 */ last_used_time INT8 NOT NULL
);
`)
return db
}
static uploadFile(fileName: string, data: Buffer) {
static async uploadFile(fileName: string, data: Buffer, chatId: string) {
const hash = crypto.createHash('sha256').update(data).digest('hex')
mime = fileTypeFromBuffer(data)
const mime = fileTypeFromBuffer(data)
await fs.writeFile(
path.join(
config.data_path,
"files",
hash.substring(0, 1),
hash.substring(2, 3),
hash.substring(3, 4),
hash
),
data
)
return new FileManager.File(
FileManager.findAllBeansByCondition(
'count = ?',
FileManager.database.prepare(`INSERT INTO ${FileManager.table_name} (
name,
hash,
mime,
chatid,
upload_time,
last_used_time
) VALUES (?, ?, ?, ?, ?, ?);`).run(
fileName,
hash,
mime,
chatId,
Date.now(),
-1
).lastInsertRowid
)[0]
)
}
private static findAllBeansByCondition(condition: string, ...args: unknown[]): FileManager.FileBean[] {

View File

@@ -113,14 +113,36 @@ export default class User {
this.setAttr("avatar", avatar)
}
/* getSettings(): Settings {
getSettings(): User.Settings {
return new Settings(JSON.parse(this.bean.settings))
}
static Settings = class {
declare bean: User.SettingsBean
declare user: User
constructor(user: User, bean: User.SettingsBean) {
this.bean = bean
this.user = user
for (const i of [
]) {
this["set" + i.substring(0, 1).toUpperCase() + i.substring(1)] = (v: unknown) => {
this.set(i, v)
}
}
}
set(key: string, value: unknown) {
this.bean[key] = value
}
get(key: string) {
return this.bean[key]
}
apply() {
this.user.setAttr("settings", JSON.stringify(this.bean))
}
}
static SettingsBean = class {
}
static SettingsBean = interface {
} */
}

View File

@@ -0,0 +1,4 @@
import http from 'node:http'
import https from 'node:https'
export type HttpServerLike = http.Server | https.Server

View File

@@ -0,0 +1 @@
export type UnknownFunction = (...args: unknown[]) => unknown