Compare commits
5 Commits
1eb8b50b78
...
0000eeead2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0000eeead2 | ||
|
|
581a6acc52 | ||
|
|
c23ce07c85 | ||
|
|
163e9d97c2 | ||
|
|
b39be9301d |
12
src/api/ApiManager.ts
Normal file
12
src/api/ApiManager.ts
Normal 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
12
src/api/BaseApi.ts
Normal 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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { DatabaseSync } from "node:sqlite"
|
import { DatabaseSync } from "node:sqlite"
|
||||||
import { Buffer } from "node:buffer"
|
// import { Buffer } from "node:buffer"
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
|
|
||||||
import config from '../config.ts'
|
import config from '../config.ts'
|
||||||
@@ -25,40 +25,58 @@ export default class Chat {
|
|||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
private static findAllByCondition(condition: string, ...args: unknown[]): UserBean[] {
|
private static findAllByCondition(condition: string, ...args: unknown[]): ChatBean[] {
|
||||||
return database.prepare(`SELECT count, id FROM ${User.table_name} WHERE ${condition}`).all(...args)
|
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
|
declare bean: ChatBean
|
||||||
constructor(bean: ChatBean) {
|
constructor(bean: ChatBean) {
|
||||||
this.bean = bean
|
this.bean = bean
|
||||||
}
|
}
|
||||||
private setAttr(key: string, value: unknown): void {
|
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
|
this.bean[key] = value
|
||||||
}
|
}
|
||||||
getUserName(): string {
|
|
||||||
return this.bean.username
|
getSettings(): Chat.Settings {
|
||||||
}
|
return new Settings(JSON.parse(this.bean.settings))
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,16 +7,17 @@ import fs from 'node:fs/promises'
|
|||||||
import { fileTypeFromBuffer } from 'file-type'
|
import { fileTypeFromBuffer } from 'file-type'
|
||||||
|
|
||||||
export default class FileManager {
|
export default class FileManager {
|
||||||
static FileBean = interface {
|
static FileBean = class {
|
||||||
count: number,
|
declare count: number
|
||||||
name: string,
|
declare name: string
|
||||||
hash: string,
|
declare hash: string
|
||||||
mime: string,
|
declare mime: string
|
||||||
|
declare chatid: string
|
||||||
}
|
}
|
||||||
|
|
||||||
static File = class {
|
static File = class {
|
||||||
declare bean: FileManager.FileBean
|
declare bean: FileManager.FileBean
|
||||||
constructor(bean: UserBean) {
|
constructor(bean: FileManager.FileBean) {
|
||||||
this.bean = bean
|
this.bean = bean
|
||||||
}
|
}
|
||||||
getMime(): string {
|
getMime(): string {
|
||||||
@@ -36,6 +37,9 @@ export default class FileManager {
|
|||||||
this.bean.hash
|
this.bean.hash
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
getChatId(): string {
|
||||||
|
return this.bean.chatid
|
||||||
|
}
|
||||||
async readAsync(): Buffer {
|
async readAsync(): Buffer {
|
||||||
return await fs.readFile(this.getFilePath())
|
return await fs.readFile(this.getFilePath())
|
||||||
}
|
}
|
||||||
@@ -50,16 +54,49 @@ export default class FileManager {
|
|||||||
/* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT,
|
/* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
/* 文件名称 */ name TEXT NOT NULL,
|
/* 文件名称 */ name TEXT NOT NULL,
|
||||||
/* 文件哈希 */ hash 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
|
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')
|
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[] {
|
private static findAllBeansByCondition(condition: string, ...args: unknown[]): FileManager.FileBean[] {
|
||||||
|
|||||||
@@ -113,14 +113,36 @@ export default class User {
|
|||||||
this.setAttr("avatar", avatar)
|
this.setAttr("avatar", avatar)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* getSettings(): Settings {
|
getSettings(): User.Settings {
|
||||||
|
return new Settings(JSON.parse(this.bean.settings))
|
||||||
}
|
}
|
||||||
|
|
||||||
static Settings = class {
|
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 {
|
|
||||||
|
|
||||||
} */
|
|
||||||
}
|
}
|
||||||
|
|||||||
4
src/types/HttpServerLike.ts
Normal file
4
src/types/HttpServerLike.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
import http from 'node:http'
|
||||||
|
import https from 'node:https'
|
||||||
|
|
||||||
|
export type HttpServerLike = http.Server | https.Server
|
||||||
1
src/types/UnknownFunction.ts
Normal file
1
src/types/UnknownFunction.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export type UnknownFunction = (...args: unknown[]) => unknown
|
||||||
Reference in New Issue
Block a user