This commit is contained in:
CrescentLeaf
2025-09-20 20:32:26 +08:00
parent 1acc73c7b4
commit eaf0f98058
8 changed files with 114 additions and 40 deletions

View File

@@ -0,0 +1,55 @@
import React from 'react'
import Chat from "../../api/client_data/Chat.ts"
import useAsyncEffect from "../useAsyncEffect.ts"
import Client from "../../api/Client.ts"
import data from "../../Data.ts"
import { Dialog } from "mdui"
interface Args extends React.HTMLAttributes<HTMLElement> {
chat: Chat
chatInfoDialogRef: React.MutableRefObject<Dialog>
}
export default function ChatInfoDialog({ chat, chatInfoDialogRef }: Args) {
const [isMySelf, setIsMySelf] = React.useState(false)
useAsyncEffect(async () => {
const re = await Client.invoke("Chat.getInfo", {
token: data.access_token,
target: chat.id,
})
})
return (
<mdui-dialog close-on-overlay-click close-on-esc ref={chatInfoDialogRef}>
<div style={{
display: 'flex',
alignItems: 'center',
}}>
<Avatar src={chat?.avatar} text={chat?.nickname} style={{
width: '50px',
height: '50px',
}} />
<span style={{
marginLeft: "15px",
fontSize: '16.5px',
}}>{user?.nickname}</span>
</div>
<mdui-divider style={{
marginTop: "10px",
marginBottom: "10px",
}}></mdui-divider>
<mdui-list>
{!isMySelf && <mdui-list-item icon="edit" rounded></mdui-list-item>}
{
isMySelf && <>
<mdui-list-item icon="edit" rounded></mdui-list-item>
<mdui-list-item icon="settings" rounded></mdui-list-item>
<mdui-list-item icon="lock" rounded></mdui-list-item>
</>
}
</mdui-list>
</mdui-dialog>
)
}

View File

@@ -3,17 +3,16 @@ import User from "../../api/client_data/User.ts"
import ContactsListItem from "./ContactsListItem.tsx" import ContactsListItem from "./ContactsListItem.tsx"
import useEventListener from "../useEventListener.ts" import useEventListener from "../useEventListener.ts"
import { ListItem, TextField } from "mdui" import { ListItem, TextField } from "mdui"
import useAsyncEffect from "../useAsyncEffect.ts"
import Client from "../../api/Client.ts"
import data from "../../Data.ts"
interface Args extends React.HTMLAttributes<HTMLElement> { interface Args extends React.HTMLAttributes<HTMLElement> {
contactsList: User[]
setContactsList: React.Dispatch<React.SetStateAction<User[]>>
display: boolean display: boolean
openChatFragment: (id: string) => void openChatFragment: (id: string) => void
} }
export default function ContactsList({ export default function ContactsList({
contactsList,
setContactsList,
display, display,
openChatFragment, openChatFragment,
...props ...props
@@ -21,11 +20,27 @@ export default function ContactsList({
const searchRef = React.useRef<HTMLElement>(null) const searchRef = React.useRef<HTMLElement>(null)
const [isMultiSelecting, setIsMultiSelecting] = React.useState(false) const [isMultiSelecting, setIsMultiSelecting] = React.useState(false)
const [searchText, setSearchText] = React.useState('') const [searchText, setSearchText] = React.useState('')
const [contactsList, setContactsList] = React.useState([
{
id: '1',
avatar: "https://www.court-records.net/mugshot/aa6-004-maya.png",
nickname: "麻油衣酱",
},
{
id: '0',
avatar: "https://www.court-records.net/mugshot/aa6-004-maya.png",
nickname: "Maya Fey",
},
] as User[])
useEventListener(searchRef, 'input', (e) => { useEventListener(searchRef, 'input', (e) => {
setSearchText((e.target as unknown as TextField).value) setSearchText((e.target as unknown as TextField).value)
}) })
useAsyncEffect(async () => {
})
return <mdui-list style={{ return <mdui-list style={{
overflowY: 'auto', overflowY: 'auto',
paddingLeft: '10px', paddingLeft: '10px',
@@ -49,7 +64,7 @@ export default function ContactsList({
{ {
contactsList.filter((user) => contactsList.filter((user) =>
searchText == '' || searchText == '' ||
user.nickname.includes(searchText) || user.nickname.includes(searchText) ||
user.id.includes(searchText) || user.id.includes(searchText) ||
user.username?.includes(searchText) user.username?.includes(searchText)

View File

@@ -8,8 +8,9 @@ export type CallMethod =
"User.setAvatar" | "User.setAvatar" |
"User.getMyInfo" | "User.getMyInfo" |
"User.getMyContactGroups" | "User.getMyContacts" |
"User.setMyContactGroups" | "User.addContact" |
"User.removeContacts" |
"Chat.getInfo" | "Chat.getInfo" |
"Chat.sendMessage" | "Chat.sendMessage" |

View File

@@ -29,20 +29,15 @@ export default class ChatApi extends BaseApi {
// 私聊 // 私聊
if (chat!.bean.type == 'private') { if (chat!.bean.type == 'private') {
const targetId = args.target as string
const target = User.findById(targetId)
const mine = User.findById(token.author) as User const mine = User.findById(token.author) as User
if (target == null) return {
code: 404,
msg: "找不到用户",
}
return { return {
code: 200, code: 200,
msg: "成功", msg: "成功",
data: { data: {
type: chat.bean.type, type: chat.bean.type,
title: ChatPrivate.fromChat(chat).getTitleForPrivate(mine, target) title: chat.getTitleForPrivate(mine),
avatar: chat.bean.avatar_file_hash ? "uploaded_files/" + chat.bean.avatar_file_hash : chat.bean.avatar_file_hash
} }
} }
} }

View File

@@ -2,6 +2,8 @@ import { Buffer } from "node:buffer";
import User from "../data/User.ts"; import User from "../data/User.ts";
import BaseApi from "./BaseApi.ts" import BaseApi from "./BaseApi.ts"
import TokenManager from "./TokenManager.ts"; import TokenManager from "./TokenManager.ts";
import ChatPrivate from "../data/ChatPrivate.ts";
import Chat from "../data/Chat.ts";
export default class UserApi extends BaseApi { export default class UserApi extends BaseApi {
override getName(): string { override getName(): string {
@@ -196,7 +198,7 @@ export default class UserApi extends BaseApi {
} }
}) })
// 獲取聯絡人列表 // 獲取聯絡人列表
this.registerEvent("User.getMyContactGroups", (args) => { this.registerEvent("User.getMyContacts", (args) => {
if (this.checkArgsMissing(args, ['token'])) return { if (this.checkArgsMissing(args, ['token'])) return {
msg: "參數缺失", msg: "參數缺失",
code: 400, code: 400,
@@ -214,13 +216,15 @@ export default class UserApi extends BaseApi {
msg: "成功", msg: "成功",
code: 200, code: 200,
data: { data: {
contact_groups: user!.getContactGroups() contacts: user!.getContactsList().map((id) => {
title: Chat.findById(id)?.bean.title
})
} }
} }
}) })
// 更新聯絡人列表 // 添加聯絡人
this.registerEvent("User.setMyContactGroups", (args) => { this.registerEvent("User.addContact", (args) => {
if (this.checkArgsMissing(args, ['token', 'contact_groups'])) return { if (this.checkArgsMissing(args, ['token', 'contact_chat_id'])) return {
msg: "參數缺失", msg: "參數缺失",
code: 400, code: 400,
} }
@@ -232,7 +236,7 @@ export default class UserApi extends BaseApi {
} }
const user = User.findById(token.author) const user = User.findById(token.author)
user!.setContactGroups(args.contact_groups as { [key: string]: string[] }) user!.addContact(args.contact_chat_id as string)
return { return {
msg: "成功", msg: "成功",

View File

@@ -30,13 +30,13 @@ export default class Chat {
/* 设置 */ settings TEXT NOT NULL /* 设置 */ settings TEXT NOT NULL
); );
`) `)
return db return db
} }
protected static findAllBeansByCondition(condition: string, ...args: SQLInputValue[]): ChatBean[] { protected static findAllBeansByCondition(condition: string, ...args: SQLInputValue[]): ChatBean[] {
return this.database.prepare(`SELECT * FROM ${Chat.table_name} WHERE ${condition}`).all(...args) as unknown as ChatBean[] return this.database.prepare(`SELECT * FROM ${Chat.table_name} WHERE ${condition}`).all(...args) as unknown as ChatBean[]
} }
static findById(id: string) { static findById(id: string) {
const beans = this.findAllBeansByCondition('id = ?', id) const beans = this.findAllBeansByCondition('id = ?', id)
if (beans.length == 0) if (beans.length == 0)
@@ -49,7 +49,7 @@ export default class Chat {
static create(chatId: string, type: 'private' | 'group') { static create(chatId: string, type: 'private' | 'group') {
const chat = new Chat( const chat = new Chat(
Chat.findAllBeansByCondition( Chat.findAllBeansByCondition(
'count = ?', 'count = ?',
Chat.database.prepare(`INSERT INTO ${Chat.table_name} ( Chat.database.prepare(`INSERT INTO ${Chat.table_name} (
type, type,
id, id,
@@ -80,4 +80,13 @@ export default class Chat {
Chat.database.prepare(`UPDATE ${Chat.table_name} SET ${key} = ? WHERE id = ?`).run(value, this.bean.id) Chat.database.prepare(`UPDATE ${Chat.table_name} SET ${key} = ? WHERE id = ?`).run(value, this.bean.id)
this.bean[key] = value this.bean[key] = value
} }
getTitleForPrivate(userMySelf: User) {
if (this.bean.user_a_id == userMySelf.bean.id)
return User.findById(this.bean?.user_b_id as string)?.getNickName() || "未知對話"
if (this.bean.user_b_id == userMySelf.bean.id)
return userMySelf.getNickName()
return "未知對話"
}
} }

View File

@@ -25,15 +25,4 @@ export default class ChatPrivate extends Chat {
} }
return a return a
} }
getTitleForPrivate(user: User, targetUser: User) {
const chat = Chat.findById(ChatPrivate.getChatIdByUsersId(user.bean.id, targetUser.bean.id))
if (chat?.bean.user_a_id == user.bean.id)
return targetUser.getNickName()
if (chat?.bean.user_b_id == user.bean.id)
return user.getNickName()
return "未知對話"
}
} }

View File

@@ -9,8 +9,11 @@ import config from '../config.ts'
import UserBean from './UserBean.ts' 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 DataWrongError from "../api/DataWrongError.ts"
import ChatPrivate from "./ChatPrivate.ts"
import Chat from "./Chat.ts"
import ChatBean from "./ChatBean.ts";
type UserBeanKey = keyof UserBean type UserBeanKey = keyof UserBean
@@ -77,6 +80,7 @@ export default class User {
)[0] )[0]
) )
avatar && user.setAvatar(avatar) avatar && user.setAvatar(avatar)
user.addContact(ChatPrivate.getChatIdByUsersId(user.bean.id, user.bean.id))
return user return user
} }
@@ -115,12 +119,14 @@ export default class User {
setUserName(userName: string) { setUserName(userName: string) {
this.setAttr("username", userName) this.setAttr("username", userName)
} }
addContact(userId) { addContact(chatId: string) {
const ls = this.getContactsList()
ls.push(chatId)
this.setAttr("contacts_list", JSON.stringify(ls))
} }
getContactsList() { getContactsList() {
try { try {
return JSON.parse(this.bean.contacts_list) return JSON.parse(this.bean.contacts_list) as string[]
} catch (e) { } catch (e) {
console.log(chalk.yellow(`警告: 聯絡人組解析失敗: ${(e as Error).message}`)) console.log(chalk.yellow(`警告: 聯絡人組解析失敗: ${(e as Error).message}`))
return [ return [