refactor: ChatAdmins stored in Chat.db
This commit is contained in:
@@ -10,7 +10,6 @@ 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'
|
import DataWrongError from '../api/DataWrongError.ts'
|
||||||
import ChatAdminLinker from "./ChatAdminLinker.ts"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Chat.ts - Wrapper and manager
|
* Chat.ts - Wrapper and manager
|
||||||
@@ -31,6 +30,14 @@ export default class Chat {
|
|||||||
/* 头像 */ avatar BLOB,
|
/* 头像 */ avatar BLOB,
|
||||||
/* 设置 */ settings TEXT NOT NULL
|
/* 设置 */ settings TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
`)
|
||||||
|
db.exec(`
|
||||||
|
CREATE TABLE IF NOT EXISTS ChatAdmin (
|
||||||
|
/* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
/* 用戶 ID */ user_id TEXT NOT NULL,
|
||||||
|
/* Chat ID */ chat_id TEXT NOT NULL,
|
||||||
|
/* 管理权限 */ permissions TEXT NOT NULL
|
||||||
|
);
|
||||||
`)
|
`)
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
@@ -82,14 +89,38 @@ export default class Chat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addAdmin(userId: string, permission: string[] | string) {
|
addAdmin(userId: string, permission: string[] | string) {
|
||||||
ChatAdminLinker.linkAdminAndChat(userId, this.bean.id)
|
if (!this.checkUserIsAdmin(userId))
|
||||||
|
Chat.database.prepare(`INSERT INTO ChatAdmin (
|
||||||
|
user_id,
|
||||||
|
chat_id,
|
||||||
|
permissions
|
||||||
|
) VALUES (?, ?, ?);`).run(
|
||||||
|
userId,
|
||||||
|
this.bean.id,
|
||||||
|
'[]'
|
||||||
|
)
|
||||||
this.setAdminPermissions(userId, permission)
|
this.setAdminPermissions(userId, permission)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkUserIsAdmin(userId: string) {
|
||||||
|
return Chat.findAllAdminsByCondition('user_id = ? AND chat_id = ?', userId, this.bean.id).length != 0
|
||||||
|
}
|
||||||
|
getAdmins() {
|
||||||
|
return Chat.findAllAdminsByCondition('chat_id = ?', this.bean.id).map((v) => v.user_id) as string[]
|
||||||
|
}
|
||||||
|
protected static findAllAdminsByCondition(condition: string, ...args: SQLInputValue[]) {
|
||||||
|
return this.database.prepare(`SELECT * FROM ChatAdmin WHERE ${condition}`).all(...args)
|
||||||
|
}
|
||||||
|
|
||||||
setAdminPermissions(userId: string, permission: string[] | string) {
|
setAdminPermissions(userId: string, permission: string[] | string) {
|
||||||
ChatAdminLinker.updatePermissions(userId, this.bean.id, permission instanceof Array ? JSON.stringify(permission) : permission)
|
Chat.database.prepare(`UPDATE ChatAdmin SET permissions = ? WHERE user_id = ? AND chat_id = ?`).run(
|
||||||
|
userId,
|
||||||
|
this.bean.id,
|
||||||
|
permission instanceof Array ? JSON.stringify(permission) : permission
|
||||||
|
)
|
||||||
}
|
}
|
||||||
removeAdmins(userIds: string[]) {
|
removeAdmins(userIds: string[]) {
|
||||||
userIds.forEach((v) => ChatAdminLinker.unlinkAdminAndChat(v, this.bean.id))
|
userIds.forEach((v) => Chat.database.prepare(`DELETE FROM ChatAdmin WHERE user_id = ? AND chat_id = ?`).run(v, this.bean.id))
|
||||||
}
|
}
|
||||||
getMembersList() {
|
getMembersList() {
|
||||||
return UserChatLinker.getChatMembers(this.bean.id)
|
return UserChatLinker.getChatMembers(this.bean.id)
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
import { DatabaseSync } from "node:sqlite"
|
|
||||||
import path from 'node:path'
|
|
||||||
|
|
||||||
import config from "../config.ts"
|
|
||||||
import { SQLInputValue } from "node:sqlite"
|
|
||||||
export default class ChatAdminLinker {
|
|
||||||
static database: DatabaseSync = this.init()
|
|
||||||
|
|
||||||
private static init(): DatabaseSync {
|
|
||||||
const db: DatabaseSync = new DatabaseSync(path.join(config.data_path, 'ChatAdminLinker.db'))
|
|
||||||
db.exec(`
|
|
||||||
CREATE TABLE IF NOT EXISTS ChatAdminLinker (
|
|
||||||
/* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
/* 用戶 ID */ user_id TEXT NOT NULL,
|
|
||||||
/* Chat ID */ chat_id TEXT NOT NULL,
|
|
||||||
/* 管理权限 */ permissions TEXT NOT NULL
|
|
||||||
);
|
|
||||||
`)
|
|
||||||
return db
|
|
||||||
}
|
|
||||||
|
|
||||||
static linkAdminAndChat(userId: string, chatId: string) {
|
|
||||||
if (!this.checkAdminIsLinkedToChat(userId, chatId))
|
|
||||||
this.database.prepare(`INSERT INTO ChatAdminLinker (
|
|
||||||
user_id,
|
|
||||||
chat_id,
|
|
||||||
permissions
|
|
||||||
) VALUES (?, ?, ?);`).run(
|
|
||||||
userId,
|
|
||||||
chatId,
|
|
||||||
'[]'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
static updatePermissions(userId: string, chatId: string, permissions: string) {
|
|
||||||
this.database.prepare(`UPDATE ChatAdminLinker SET permissions = ? WHERE user_id = ? AND chat_id = ?`).run(permissions, userId, chatId)
|
|
||||||
}
|
|
||||||
static unlinkAdminAndChat(userId: string, chatId: string) {
|
|
||||||
this.database.prepare(`DELETE FROM ChatAdminLinker WHERE user_id = ? AND chat_id = ?`).run(userId, chatId)
|
|
||||||
}
|
|
||||||
static checkAdminIsLinkedToChat(userId: string, chatId: string) {
|
|
||||||
return this.findAllByCondition('user_id = ? AND chat_id = ?', userId, chatId).length != 0
|
|
||||||
}
|
|
||||||
static getChatAdmins(chatId: string) {
|
|
||||||
return this.findAllByCondition('chat_id = ?', chatId).map((v) => v.user_id) as string[]
|
|
||||||
}
|
|
||||||
protected static findAllByCondition(condition: string, ...args: SQLInputValue[]) {
|
|
||||||
return this.database.prepare(`SELECT * FROM ChatAdminLinker WHERE ${condition}`).all(...args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user