feat: 自动检验用户名和对话 ID 是否已经存在
This commit is contained in:
@@ -9,6 +9,7 @@ import chalk from "chalk"
|
|||||||
import User from "./User.ts"
|
import User from "./User.ts"
|
||||||
import ChatType from "./ChatType.ts"
|
import ChatType from "./ChatType.ts"
|
||||||
import UserChatLinker from "./UserChatLinker.ts"
|
import UserChatLinker from "./UserChatLinker.ts"
|
||||||
|
import DataWrongError from '../api/DataWrongError.ts'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Chat.ts - Wrapper and manager
|
* Chat.ts - Wrapper and manager
|
||||||
@@ -48,6 +49,8 @@ export default class Chat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static create(chatId: string, type: ChatType) {
|
static create(chatId: string, type: ChatType) {
|
||||||
|
if (this.findAllBeansByCondition('id = ?', chatId).length > 0)
|
||||||
|
throw new DataWrongError(`对话ID ${chatId} 已被使用`)
|
||||||
const chat = new Chat(
|
const chat = new Chat(
|
||||||
Chat.findAllBeansByCondition(
|
Chat.findAllBeansByCondition(
|
||||||
'count = ?',
|
'count = ?',
|
||||||
@@ -101,6 +104,11 @@ export default class Chat {
|
|||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
setTitle(title: string) {
|
||||||
|
if (this.bean.type == 'private')
|
||||||
|
throw new Error('不允许对私聊进行命名')
|
||||||
|
this.setAttr('title', title)
|
||||||
|
}
|
||||||
getTitle(userMySelf?: User) {
|
getTitle(userMySelf?: User) {
|
||||||
if (this.bean.type == 'group') return this.bean.title
|
if (this.bean.type == 'group') return this.bean.title
|
||||||
if (this.bean.type == 'private') return this.getAnotherUserForPrivate(userMySelf as User)?.getNickName()
|
if (this.bean.type == 'private') return this.getAnotherUserForPrivate(userMySelf as User)?.getNickName()
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import UserBean from './UserBean.ts'
|
|||||||
|
|
||||||
import FileManager from './FileManager.ts'
|
import FileManager from './FileManager.ts'
|
||||||
import { SQLInputValue } from "node:sqlite"
|
import { SQLInputValue } from "node:sqlite"
|
||||||
import DataWrongError from "../api/DataWrongError.ts"
|
|
||||||
import ChatPrivate from "./ChatPrivate.ts"
|
import ChatPrivate from "./ChatPrivate.ts"
|
||||||
import Chat from "./Chat.ts"
|
import Chat from "./Chat.ts"
|
||||||
import ChatBean from "./ChatBean.ts"
|
import ChatBean from "./ChatBean.ts"
|
||||||
import MapJson from "../MapJson.ts";
|
import MapJson from "../MapJson.ts"
|
||||||
|
import DataWrongError from '../api/DataWrongError.ts'
|
||||||
|
|
||||||
type UserBeanKey = keyof UserBean
|
type UserBeanKey = keyof UserBean
|
||||||
|
|
||||||
@@ -45,18 +45,9 @@ export default class User {
|
|||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
static createWithUserNameChecked(userName: string | null, password: string, nickName: string, avatar: Buffer | null) {
|
static create(userName: string | null, password: string, nickName: string, avatar: Buffer | null) {
|
||||||
if (userName && User.findAllBeansByCondition('username = ?', userName).length > 0)
|
if (userName && User.findAllBeansByCondition('username = ?', userName).length > 0)
|
||||||
throw new DataWrongError(`用户名 ${userName} 已存在`)
|
throw new DataWrongError(`用户名 ${userName} 已存在`)
|
||||||
return User.create(
|
|
||||||
userName,
|
|
||||||
password,
|
|
||||||
nickName,
|
|
||||||
avatar
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
static create(userName: string | null, password: string, nickName: string, avatar: Buffer | null) {
|
|
||||||
const user = new User(
|
const user = new User(
|
||||||
User.findAllBeansByCondition(
|
User.findAllBeansByCondition(
|
||||||
'count = ?',
|
'count = ?',
|
||||||
@@ -124,6 +115,8 @@ export default class User {
|
|||||||
return this.bean.username
|
return this.bean.username
|
||||||
}
|
}
|
||||||
setUserName(userName: string) {
|
setUserName(userName: string) {
|
||||||
|
if (User.findAllBeansByCondition('username = ?', userName).length > 0)
|
||||||
|
throw new DataWrongError(`用户名 ${userName} 已存在`)
|
||||||
this.setAttr("username", userName)
|
this.setAttr("username", userName)
|
||||||
}
|
}
|
||||||
updateRecentChat(chatId: string, content: string) {
|
updateRecentChat(chatId: string, content: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user