feat: 修改对话 ID 对话名称 对话头像

* 仅群组
This commit is contained in:
CrescentLeaf
2025-10-24 22:00:22 +08:00
parent 4807038619
commit bc7b932c5c
6 changed files with 101 additions and 7 deletions

View File

@@ -19,6 +19,7 @@ export type CallMethod =
"Chat.getInfo" | "Chat.getInfo" |
"Chat.updateSettings" | "Chat.updateSettings" |
"Chat.setAvatar" |
"Chat.createGroup" | "Chat.createGroup" |

View File

@@ -4,6 +4,10 @@ interface GroupSettings {
new_member_join_method?: 'disabled' | 'allowed_by_admin' | 'answered_and_allowed_by_admin' new_member_join_method?: 'disabled' | 'allowed_by_admin' | 'answered_and_allowed_by_admin'
answered_and_allowed_by_admin_question?: string answered_and_allowed_by_admin_question?: string
// 下面两个比较特殊, 由服务端给予
group_title: string
group_id: string
[key: string]: unknown [key: string]: unknown
} }

View File

@@ -195,6 +195,7 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
} }
const attachFileInputRef = React.useRef<HTMLInputElement>(null) const attachFileInputRef = React.useRef<HTMLInputElement>(null)
const uploadChatAvatarRef = React.useRef<HTMLInputElement>(null)
function insertText(text: string) { function insertText(text: string) {
const input = inputRef.current!.shadowRoot!.querySelector('[part=input]') as HTMLTextAreaElement const input = inputRef.current!.shadowRoot!.querySelector('[part=input]') as HTMLTextAreaElement
@@ -224,6 +225,22 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
addFile(file.type, file.name, file) addFile(file.type, file.name, file)
} }
}) })
useEventListener(uploadChatAvatarRef, 'change', async (_e) => {
const file = uploadChatAvatarRef.current!.files?.[0] as File
if (file == null) return
const re = await Client.invoke("Chat.setAvatar", {
token: data.access_token,
target: target,
avatar: file
})
if (checkApiSuccessOrSncakbar(re, "修改失败")) return
snackbar({
message: "修改成功 (刷新页面以更新)",
placement: "top",
})
})
const groupPreferenceStore = new PreferenceStore<GroupSettings>() const groupPreferenceStore = new PreferenceStore<GroupSettings>()
groupPreferenceStore.setOnUpdate(async (value, oldvalue) => { groupPreferenceStore.setOnUpdate(async (value, oldvalue) => {
@@ -494,9 +511,35 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
flexDirection: "column", flexDirection: "column",
height: "100%", height: "100%",
}}> }}>
<div style={{
display: 'none'
}}>
<input accept="image/*" type="file" name="上传对话头像" ref={uploadChatAvatarRef}></input>
</div>
{ {
chatInfo.type == 'group' && <PreferenceLayout> chatInfo.type == 'group' && <PreferenceLayout>
<PreferenceUpdater.Provider value={groupPreferenceStore.createUpdater()}> <PreferenceUpdater.Provider value={groupPreferenceStore.createUpdater()}>
<PreferenceHeader
title="群组资料" />
<Preference
title="上传新的头像"
icon="image"
disabled={!chatInfo.is_admin}
onClick={() => {
uploadChatAvatarRef.current!.click()
}} />
<TextFieldPreference
title="设置群名称"
icon="edit"
id="group_title"
state={groupPreferenceStore.state.group_title || ''}
disabled={!chatInfo.is_admin} />
<TextFieldPreference
title="设置群 ID"
icon="edit"
id="group_id"
state={groupPreferenceStore.state.group_id || ''}
disabled={!chatInfo.is_admin} />
<PreferenceHeader <PreferenceHeader
title="群组管理" /> title="群组管理" />
<Preference <Preference

View File

@@ -19,6 +19,7 @@ export type CallMethod =
"Chat.getInfo" | "Chat.getInfo" |
"Chat.updateSettings" | "Chat.updateSettings" |
"Chat.setAvatar" |
"Chat.createGroup" | "Chat.createGroup" |

View File

@@ -437,7 +437,12 @@ export default class ChatApi extends BaseApi {
type: chat.bean.type, type: chat.bean.type,
title: chat.getTitle(), title: chat.getTitle(),
avatar_file_hash: chat.getAvatarFileHash() ? chat.getAvatarFileHash() : undefined, avatar_file_hash: chat.getAvatarFileHash() ? chat.getAvatarFileHash() : undefined,
settings: JSON.parse(chat.bean.settings), settings: {
...JSON.parse(chat.bean.settings),
// 下面两个比较特殊, 用于群设置
group_id: chat.bean.id,
group_title: chat.getTitle(),
},
is_member: UserChatLinker.checkUserIsLinkedToChat(token.author, chat!.bean.id), is_member: UserChatLinker.checkUserIsLinkedToChat(token.author, chat!.bean.id),
is_admin: chat.checkUserIsAdmin(token.author), is_admin: chat.checkUserIsAdmin(token.author),
} }
@@ -449,8 +454,44 @@ export default class ChatApi extends BaseApi {
msg: "找不到对话", msg: "找不到对话",
} }
}) })
// 更新頭像
this.registerEvent("Chat.setAvatar", (args, { deviceId }) => {
if (this.checkArgsMissing(args, ['avatar', 'token'])) return {
msg: "参数缺失",
code: 400,
}
if (!(args.avatar instanceof Buffer)) return {
msg: "参数不合法",
code: 400,
}
const token = TokenManager.decode(args.token as string)
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')
if (chat.checkUserIsAdmin(user.bean.id)) {
const avatar: Buffer = args.avatar as Buffer
if (avatar)
chat.setAvatar(avatar)
} else
return {
code: 403,
msg: "没有此权限",
}
return {
msg: "成功",
code: 200,
}
})
/** /**
* 更新设定 * 更新设定 (包括资料)
* @param token 令牌 * @param token 令牌
* @param title 名称 * @param title 名称
* @param [id] 群组 ID * @param [id] 群组 ID
@@ -475,9 +516,15 @@ export default class ChatApi extends BaseApi {
} }
if (chat.bean.type == 'group') if (chat.bean.type == 'group')
if (chat.checkUserIsAdmin(user.bean.id)) if (chat.checkUserIsAdmin(user.bean.id)) {
ChatGroup.fromChat(chat).getSettings().update(args.settings as GroupSettingsBean) ChatGroup.fromChat(chat).getSettings().update(args.settings as GroupSettingsBean)
else
const settings = args.settings as any
if (settings.group_title != null)
chat.setTitle(settings.group_title)
if (settings.group_id != null)
chat.setId(settings.group_id)
} else
return { return {
code: 403, code: 403,
msg: "没有此权限", msg: "没有此权限",

View File

@@ -200,10 +200,8 @@ export default class Chat {
return null return null
} }
getId() {
return this.bean.id
}
setId(id: string) { setId(id: string) {
if (this.bean.id == id) return
if (Chat.findAllChatBeansByCondition('id = ?', id).length > 0) if (Chat.findAllChatBeansByCondition('id = ?', id).length > 0)
throw new DataWrongError(`对话ID ${id} 已被使用`) throw new DataWrongError(`对话ID ${id} 已被使用`)
this.setAttr("id", id) this.setAttr("id", id)