feat(WIP): 对话管理员
This commit is contained in:
6
server/data/AdminPermissions.ts
Normal file
6
server/data/AdminPermissions.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export default class AdminPermissions {
|
||||||
|
static readonly OWNER = "OWNER"
|
||||||
|
static readonly MANAGE_ADMIN = "MANAGE_ADMIN"
|
||||||
|
static readonly MUTE_MEMBERS = "MUTE_MEMBERS"
|
||||||
|
static readonly REMOVE_MEMBERS = "REMOVE_MEMBERS"
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ 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
|
||||||
@@ -80,6 +81,10 @@ export default class Chat {
|
|||||||
this.bean[key] = value
|
this.bean[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addAdmin(userId: string, permission: string[] | string) {
|
||||||
|
ChatAdminLinker.linkAdminAndChat(userId, this.bean.id)
|
||||||
|
ChatAdminLinker.updatePermissions(userId, this.bean.id, permission instanceof Array ? JSON.stringify(permission) : permission)
|
||||||
|
}
|
||||||
getMembersList() {
|
getMembersList() {
|
||||||
return UserChatLinker.getChatMembers(this.bean.id)
|
return UserChatLinker.getChatMembers(this.bean.id)
|
||||||
}
|
}
|
||||||
|
|||||||
49
server/data/ChatAdminLinker.ts
Normal file
49
server/data/ChatAdminLinker.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
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