feat(wip): 對話
This commit is contained in:
@@ -1,19 +1,68 @@
|
||||
import Chat from "../data/Chat.ts";
|
||||
import ChatPrivate from "../data/ChatPrivate.ts";
|
||||
import User from "../data/User.ts"
|
||||
import BaseApi from "./BaseApi.ts"
|
||||
import TokenManager from "./TokenManager.ts"
|
||||
|
||||
export default class UserApi extends BaseApi {
|
||||
export default class ChatApi extends BaseApi {
|
||||
override getName(): string {
|
||||
return "Chat"
|
||||
}
|
||||
override onInit(): void {
|
||||
this.registerEvent("Chat.getInfo", (args) => {
|
||||
|
||||
if (this.checkArgsMissing(args, ['token', 'target'])) return {
|
||||
msg: "參數缺失",
|
||||
code: 400,
|
||||
}
|
||||
|
||||
const token = TokenManager.decode(args.token as string)
|
||||
if (!this.checkToken(token)) return {
|
||||
code: 401,
|
||||
msg: "令牌無效",
|
||||
}
|
||||
|
||||
const chat = Chat.findById(args.target as string)
|
||||
if (chat == null) return {
|
||||
code: 404,
|
||||
msg: "對話不存在",
|
||||
}
|
||||
|
||||
// 私聊
|
||||
if (chat!.bean.type == 'private') {
|
||||
const targetId = args.target as string
|
||||
const target = User.findById(targetId)
|
||||
const mine = User.findById(token.author) as User
|
||||
if (target == null) return {
|
||||
code: 404,
|
||||
msg: "找不到用户",
|
||||
}
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
msg: "成功",
|
||||
data: {
|
||||
type: chat.bean.type,
|
||||
title: ChatPrivate.fromChat(chat).getTitleForPrivate(mine, target)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
code: 501,
|
||||
msg: "未實現",
|
||||
msg: "not implmented",
|
||||
}
|
||||
})
|
||||
this.registerEvent("Chat.sendMessage", (args) => {
|
||||
if (this.checkArgsMissing(args, ['token', 'target'])) return {
|
||||
msg: "參數缺失",
|
||||
code: 400,
|
||||
}
|
||||
|
||||
const token = TokenManager.decode(args.token as string)
|
||||
if (!this.checkToken(token)) return {
|
||||
code: 401,
|
||||
msg: "令牌無效",
|
||||
}
|
||||
|
||||
return {
|
||||
code: 501,
|
||||
@@ -21,6 +70,16 @@ export default class UserApi extends BaseApi {
|
||||
}
|
||||
})
|
||||
this.registerEvent("Chat.getMessageHistory", (args) => {
|
||||
if (this.checkArgsMissing(args, ['token', 'target'])) return {
|
||||
msg: "參數缺失",
|
||||
code: 400,
|
||||
}
|
||||
|
||||
const token = TokenManager.decode(args.token as string)
|
||||
if (!this.checkToken(token)) return {
|
||||
code: 401,
|
||||
msg: "令牌無效",
|
||||
}
|
||||
|
||||
return {
|
||||
code: 501,
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import chalk from "chalk"
|
||||
import Chat from "./Chat.ts"
|
||||
import User from "./User.ts";
|
||||
import User from "./User.ts"
|
||||
|
||||
export default class ChatPrivate extends Chat {
|
||||
static fromChat(chat: Chat) {
|
||||
return new ChatPrivate(chat.bean)
|
||||
}
|
||||
|
||||
static getChatIdByUsersId(userIdA: string, userIdB: string) {
|
||||
return [userIdA, userIdB].sort().join('-')
|
||||
}
|
||||
@@ -9,8 +14,16 @@ export default class ChatPrivate extends Chat {
|
||||
static createForPrivate(userA: User, userB: User) {
|
||||
return this.create(this.getChatIdByUsersId(userA.bean.id, userB.bean.id), 'private')
|
||||
}
|
||||
static findForPrivate() {
|
||||
|
||||
static findForPrivate(userA: User, userB: User) {
|
||||
return this.fromChat(this.findById(this.getChatIdByUsersId(userA.bean.id, userB.bean.id)) as Chat)
|
||||
}
|
||||
static findOrCreateForPrivate(userA: User, userB: User) {
|
||||
let a = this.findForPrivate(userA, userB)
|
||||
if (a == null) {
|
||||
this.createForPrivate(userA, userB)
|
||||
a = this.findForPrivate(userA, userB) as ChatPrivate
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
getTitleForPrivate(user: User, targetUser: User) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
export default class Message {
|
||||
export default class MessageBean {
|
||||
declare id: number
|
||||
declare text: string
|
||||
declare user_id: string
|
||||
declare user_id?: string
|
||||
|
||||
[key: string]: unknown
|
||||
}
|
||||
@@ -5,22 +5,59 @@ import chalk from "chalk"
|
||||
|
||||
import config from "../config.ts"
|
||||
import Chat from "./Chat.ts"
|
||||
import MessageBean from "./MessageBean.ts"
|
||||
|
||||
export default class MessagesManager {
|
||||
static table_name: string = "Messages"
|
||||
static database: DatabaseSync = this.init()
|
||||
|
||||
|
||||
private static init(): DatabaseSync {
|
||||
const db: DatabaseSync = new DatabaseSync(path.join(config.data_path, this.table_name + '.db'))
|
||||
const db: DatabaseSync = new DatabaseSync(path.join(config.data_path, 'Messages.db'))
|
||||
return db
|
||||
}
|
||||
|
||||
static getInstance(chat: Chat) {
|
||||
static getInstanceForChat(chat: Chat) {
|
||||
return new MessagesManager(chat)
|
||||
}
|
||||
|
||||
|
||||
declare chat: Chat
|
||||
constructor(chat: Chat) {
|
||||
this.chat = chat
|
||||
|
||||
MessagesManager.database.exec(`
|
||||
CREATE TABLE IF NOT EXISTS ${this.getTableName()} (
|
||||
/* 序号, MessageId */ id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
/* 消息文本 */ text TEXT NOT NULL,
|
||||
/* 发送者 */ user_id TEXT NOT NULL,
|
||||
);
|
||||
`)
|
||||
}
|
||||
protected getTableName() {
|
||||
return `messages_${this.chat.bean.id}`
|
||||
}
|
||||
addMessage({
|
||||
text,
|
||||
user_id
|
||||
}: {
|
||||
text: string,
|
||||
user_id?: string
|
||||
}) {
|
||||
MessagesManager.database.prepare(`INSERT INTO ${this.getTableName()} (
|
||||
text,
|
||||
user_id
|
||||
) VALUES (?, ?);`).run(
|
||||
text,
|
||||
user_id || null
|
||||
)
|
||||
}
|
||||
addSystemMessage(text: string) {
|
||||
this.addMessage({
|
||||
text
|
||||
})
|
||||
}
|
||||
getMessages(limit: number = 15, offset: number = 0) {
|
||||
return MessagesManager.database.prepare(`SELECT * FROM ${this.getTableName()} ORDER BY id DESC LIMIT ? OFFSET ?;`).all(limit, offset) as unknown as MessageBean[]
|
||||
}
|
||||
getMessagesWithPage(limit: number = 15, page: number = 0) {
|
||||
return this.getMessages(limit, limit * page)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user