chore: fuck lint and make it happy
This commit is contained in:
@@ -4,6 +4,8 @@ import path from 'node:path'
|
||||
|
||||
import config from '../config.ts'
|
||||
import ChatBean from './ChatBean.ts'
|
||||
import { SQLInputValue } from "node:sqlite"
|
||||
import chalk from "chalk"
|
||||
|
||||
/**
|
||||
* Chat.ts - Wrapper and manager
|
||||
@@ -25,12 +27,12 @@ export default class Chat {
|
||||
return db
|
||||
}
|
||||
|
||||
private static findAllByCondition(condition: string, ...args: unknown[]): ChatBean[] {
|
||||
return database.prepare(`SELECT * FROM ${Chat.table_name} WHERE ${condition}`).all(...args)
|
||||
private static findAllBeansByCondition(condition: string, ...args: SQLInputValue[]): ChatBean[] {
|
||||
return this.database.prepare(`SELECT * FROM ${Chat.table_name} WHERE ${condition}`).all(...args) as unknown as ChatBean[]
|
||||
}
|
||||
|
||||
static findById(id: string): Chat {
|
||||
const beans = Chat.findAllBeansByCondition('id = ?', id)
|
||||
const beans = this.findAllBeansByCondition('id = ?', id)
|
||||
if (beans.length == 0)
|
||||
throw new Error(`找不到 id 为 ${id} 的 Chat`)
|
||||
else if (beans.length > 1)
|
||||
@@ -42,41 +44,8 @@ export default class Chat {
|
||||
constructor(bean: ChatBean) {
|
||||
this.bean = bean
|
||||
}
|
||||
private setAttr(key: string, value: unknown): void {
|
||||
User.database.prepare(`UPDATE ${Chat.table_name} SET ${key} = ? WHERE id = ?`).run(value, this.bean.id)
|
||||
private setAttr(key: string, value: SQLInputValue): void {
|
||||
Chat.database.prepare(`UPDATE ${Chat.table_name} SET ${key} = ? WHERE id = ?`).run(value, this.bean.id)
|
||||
this.bean[key] = value
|
||||
}
|
||||
|
||||
getSettings(): Chat.Settings {
|
||||
return new Chat.Settings(this, JSON.parse(this.bean.settings))
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
export default class ChatBean {
|
||||
declare id: string
|
||||
declare settings: string
|
||||
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import FileManager from './FileManager.ts'
|
||||
import { SQLInputValue } from "node:sqlite";
|
||||
import DataWrongError from "../api/DataWrongError.ts";
|
||||
|
||||
type UserBeanKey = keyof UserBean
|
||||
|
||||
/**
|
||||
* User.ts - Wrapper and manager
|
||||
* Wrap with UserBean to directly update database
|
||||
@@ -100,7 +102,7 @@ export default class User {
|
||||
this.bean = bean
|
||||
}
|
||||
/* 一切的基础都是 count ID */
|
||||
private setAttr(key: string, value: unknown) {
|
||||
private setAttr(key: string, value: SQLInputValue) {
|
||||
User.database.prepare(`UPDATE ${User.table_name} SET ${key} = ? WHERE count = ?`).run(value, this.bean.count)
|
||||
this.bean[key] = value
|
||||
}
|
||||
@@ -123,42 +125,9 @@ export default class User {
|
||||
this.setAttr("password", password)
|
||||
}
|
||||
getAvatar(): Buffer | null {
|
||||
return FileManager.findByHash(this.bean.avatar_file_hash)?.readSync()
|
||||
return this.bean.avatar_file_hash != null ? FileManager.findByHash(this.bean.avatar_file_hash)?.readSync() : null
|
||||
}
|
||||
setAvatar(avatar: Buffer) {
|
||||
this.setAttr("avatar_file_hash", FileManager.uploadFile(`avatar_user_${this.bean.count}`, avatar).getHash())
|
||||
}
|
||||
|
||||
getSettings() {
|
||||
return new User.Settings(this, 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 {
|
||||
|
||||
async setAvatar(avatar: Buffer) {
|
||||
this.setAttr("avatar_file_hash", (await FileManager.uploadFile(`avatar_user_${this.bean.count}`, avatar)).getHash())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,6 @@ export default class UserBean {
|
||||
declare nickname: string
|
||||
declare avatar_file_hash: string | null
|
||||
declare settings: string
|
||||
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user