FEAT(灵车 WIP): CHAT SETTINGS
This commit is contained in:
10
client/api/client_data/GroupSettings.ts
Normal file
10
client/api/client_data/GroupSettings.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
interface GroupSettings {
|
||||
allow_new_member_join?: boolean
|
||||
allow_new_member_from_invitation?: boolean
|
||||
new_member_join_method?: 'disabled' | 'allowed_by_admin' | 'answered_and_allowed_by_admin'
|
||||
answered_and_allowed_by_admin_question?: string
|
||||
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export default GroupSettings
|
||||
@@ -9,6 +9,7 @@ import BaseApi from "./BaseApi.ts"
|
||||
import TokenManager from "./TokenManager.ts"
|
||||
import ChatPrivate from "../data/ChatPrivate.ts"
|
||||
import ChatGroup from "../data/ChatGroup.ts"
|
||||
import GroupSettingsBean from "../data/GroupSettingsBean.ts"
|
||||
|
||||
export default class ChatApi extends BaseApi {
|
||||
override getName(): string {
|
||||
@@ -53,7 +54,8 @@ export default class ChatApi extends BaseApi {
|
||||
id: args.target as string,
|
||||
type: chat.bean.type,
|
||||
title: chat.getTitle(mine),
|
||||
avatar: chat.getAvatarFileHash(mine) ? "uploaded_files/" + chat.getAvatarFileHash(mine) : undefined
|
||||
avatar: chat.getAvatarFileHash(mine) ? "uploaded_files/" + chat.getAvatarFileHash(mine) : undefined,
|
||||
settings: JSON.parse(chat.bean.settings),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,7 +67,8 @@ export default class ChatApi extends BaseApi {
|
||||
id: args.target as string,
|
||||
type: chat.bean.type,
|
||||
title: chat.getTitle(),
|
||||
avatar: chat.getAvatarFileHash() ? "uploaded_files/" + chat.getAvatarFileHash() : undefined
|
||||
avatar: chat.getAvatarFileHash() ? "uploaded_files/" + chat.getAvatarFileHash() : undefined,
|
||||
settings: JSON.parse(chat.bean.settings),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,6 +290,39 @@ export default class ChatApi extends BaseApi {
|
||||
}
|
||||
}
|
||||
})
|
||||
/**
|
||||
* 更新设定
|
||||
* @param token 令牌
|
||||
* @param title 名称
|
||||
* @param [id] 群组 ID
|
||||
*/
|
||||
this.registerEvent("Chat.updateSettings", (args, { deviceId }) => {
|
||||
if (this.checkArgsMissing(args, ['token', 'target', 'settings'])) return {
|
||||
msg: "参数缺失",
|
||||
code: 400,
|
||||
}
|
||||
|
||||
const token = TokenManager.decode(args.token as string)
|
||||
if (!this.checkToken(token, deviceId)) return {
|
||||
code: 401,
|
||||
msg: "令牌无效",
|
||||
}
|
||||
const user = User.findById(token.author) as User
|
||||
|
||||
const chat = Chat.findById(args.target as string)
|
||||
if (chat == null) return {
|
||||
code: 404,
|
||||
msg: "对话不存在",
|
||||
}
|
||||
|
||||
if (chat.bean.type == 'group')
|
||||
ChatGroup.fromChat(chat).getSettings().update(args.settings as GroupSettingsBean)
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
msg: '成功',
|
||||
}
|
||||
})
|
||||
/**
|
||||
* 从私聊获取对方的 UserId
|
||||
* @param token 令牌
|
||||
|
||||
@@ -1,8 +1,41 @@
|
||||
import chalk from "chalk"
|
||||
import Chat from "./Chat.ts"
|
||||
import User from "./User.ts"
|
||||
import GroupSettingsBean from "./GroupSettingsBean.ts"
|
||||
|
||||
class GroupSettings {
|
||||
declare chat: ChatGroup
|
||||
declare settings: GroupSettingsBean
|
||||
constructor(chat: ChatGroup) {
|
||||
this.chat = chat
|
||||
this.settings = JSON.parse(chat.bean.settings)
|
||||
}
|
||||
|
||||
update(bean: GroupSettingsBean) {
|
||||
const updateValue = (key: string) => {
|
||||
if (key in bean)
|
||||
this.settings[key] = bean[key]
|
||||
}
|
||||
for (const k of [
|
||||
'allow_new_member_join',
|
||||
'allow_new_member_from_invitation',
|
||||
'new_member_join_method',
|
||||
'answered_and_allowed_by_admin_question',
|
||||
])
|
||||
updateValue(k)
|
||||
|
||||
this.apply()
|
||||
}
|
||||
apply() {
|
||||
this.chat.setAttr('settings', JSON.stringify(this.settings))
|
||||
}
|
||||
}
|
||||
|
||||
export default class ChatGroup extends Chat {
|
||||
getSettings() {
|
||||
return new GroupSettings(this)
|
||||
}
|
||||
|
||||
static fromChat(chat: Chat) {
|
||||
return new ChatGroup(chat.bean)
|
||||
}
|
||||
|
||||
10
server/data/GroupSettingsBean.ts
Normal file
10
server/data/GroupSettingsBean.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
interface GroupSettingsBean {
|
||||
allow_new_member_join?: boolean
|
||||
allow_new_member_from_invitation?: boolean
|
||||
new_member_join_method?: 'disabled' | 'allowed_by_admin' | 'answered_and_allowed_by_admin'
|
||||
answered_and_allowed_by_admin_question?: string
|
||||
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export default GroupSettingsBean
|
||||
Reference in New Issue
Block a user