Compare commits

..

5 Commits

Author SHA1 Message Date
CrescentLeaf
e1039703d1 修改控制台提示 2025-11-16 21:59:52 +08:00
CrescentLeaf
ace3f8c4f9 feat: 提及某个对话或用户
* 暂时不支持提醒某个在对话内的用户
2025-11-16 21:59:39 +08:00
CrescentLeaf
30c09d0613 fix: 文件文字文件消息, 但是文字(trim)为空导致的显示问题 2025-11-16 21:58:59 +08:00
CrescentLeaf
dec9068cc8 导出 openUserInfoDialog openChatInfoDialog 到 window
* 无奈之举
2025-11-16 19:31:16 +08:00
CrescentLeaf
19cfd84e7d fix: 错误的 openUserInfoDialog 参数类型判断 2025-11-16 19:30:36 +08:00
7 changed files with 137 additions and 30 deletions

View File

@@ -1,7 +1,6 @@
import 'mdui/mdui.css'
import 'mdui'
import { $ } from "mdui/jq"
import { breakpoint, Dialog } from "mdui"
import { breakpoint } from "mdui"
import * as React from 'react'
import ReactDOM from 'react-dom/client'
@@ -10,6 +9,7 @@ import './ui/custom-elements/chat-image.ts'
import './ui/custom-elements/chat-video.ts'
import './ui/custom-elements/chat-file.ts'
import './ui/custom-elements/chat-text.ts'
import './ui/custom-elements/chat-mention.ts'
import './ui/custom-elements/chat-text-container.ts'
import App from './ui/App.tsx'
@@ -17,6 +17,16 @@ import AppMobile from './ui/AppMobile.tsx'
import isMobileUI from "./ui/isMobileUI.ts"
ReactDOM.createRoot(document.getElementById('app') as HTMLElement).render(React.createElement(isMobileUI() ? AppMobile : App, null))
import User from "./api/client_data/User.ts"
import Chat from "./api/client_data/Chat.ts"
// TODO: 无奈之举 以后会找更好的办法
declare global {
interface Window {
openUserInfoDialog: (user: User | string) => Promise<void>
openChatInfoDialog: (chat: Chat) => void
}
}
const onResize = () => {
document.body.style.setProperty('--whitesilk-widget-message-maxwidth', breakpoint().down('md') ? "80%" : "70%")
// deno-lint-ignore no-window

View File

@@ -112,7 +112,7 @@ export default function App() {
}
async function openUserInfoDialog(user: User | string) {
if (user instanceof User) {
if (typeof user == 'object') {
setUserInfo(user)
} else {
setUserInfo(await DataCaches.getUserProfile(user))
@@ -120,6 +120,10 @@ export default function App() {
}
userProfileDialogRef.current!.open = true
}
// deno-lint-ignore no-window
window.openUserInfoDialog = openUserInfoDialog
// deno-lint-ignore no-window
window.openChatInfoDialog = openChatInfoDialog
if ('Notification' in window) {
Notification.requestPermission()

View File

@@ -112,7 +112,7 @@ export default function AppMobile() {
}
async function openUserInfoDialog(user: User | string) {
if (user instanceof User) {
if (typeof user == 'object') {
setUserInfo(user)
} else {
setUserInfo(await DataCaches.getUserProfile(user))
@@ -120,6 +120,10 @@ export default function AppMobile() {
}
userProfileDialogRef.current!.open = true
}
// deno-lint-ignore no-window
window.openUserInfoDialog = openUserInfoDialog
// deno-lint-ignore no-window
window.openChatInfoDialog = openChatInfoDialog
return (
<div style={{

View File

@@ -46,15 +46,17 @@ const sanitizeConfig = {
"chat-file",
'chat-text',
"chat-link",
'chat-mention',
],
ALLOWED_ATTR: [
'underline',
'em',
'src',
'alt',
'href',
'name',
'user-id',
'chat-id',
],
}
@@ -71,16 +73,23 @@ const markedInstance = new marked.Marked({
return `<chat-text>${escapeHTML(text)}</chat-text>`
},
image({ text, href }) {
const type = /^(Video|File)=.*/.exec(text)?.[1] || 'Image'
if (/tws:\/\/file\?hash=[A-Za-z0-9]+$/.test(href)) {
const type = /^(Video|File|UserMention|ChatMention)=.*/.exec(text)?.[1]
const fileType = /^(Video|File)=.*/.exec(text)?.[1] || 'Image'
if (fileType != null && /tws:\/\/file\?hash=[A-Za-z0-9]+$/.test(href)) {
const url = getUrlForFileByHash(/^tws:\/\/file\?hash=(.*)/.exec(href)?.[1])
return ({
Image: `<chat-image src="${url}" alt="${escapeHTML(text)}"></chat-image>`,
Video: `<chat-video src="${url}"></chat-video>`,
File: `<chat-file href="${url}" name="${escapeHTML(/^Video|File=(.*)/.exec(text)?.[1] || 'Unnamed file')}"></chat-file>`,
})?.[type] || ``
}
return ``
})?.[fileType] || ``
} else
switch (type) {
case "UserMention":
return `<chat-mention user-id="${escapeHTML(/^tws:\/\/user\?id=(.*)/.exec(href)?.[1] || '')}" text="${escapeHTML(/^UserMention=(.*)/.exec(text)?.[1] || '')}"></chat-mention>`
case "ChatMention":
return `<chat-mention chat-id="${escapeHTML(/^tws:\/\/chat\?id=(.*)/.exec(href)?.[1] || '')}" text="${escapeHTML(/^ChatMention=(.*)/.exec(text)?.[1] || '')}"></chat-mention>`
}
return `<chat-text em="true">(不支持的附件语法: ![${text}](${href}))</chat-text>`
},
}
})
@@ -191,26 +200,28 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
let i = 1
let i2 = 0
const sendingFilesSnackbarId = setInterval(() => {
sendingFilesSnackbar.textContent = `上传第 ${i2}/${Object.keys(cachedFiles.current).length} 文件到 [${chatInfo.title}]... (${i}s)`
const len = Object.keys(cachedFiles.current).length
sendingFilesSnackbar.textContent = i2 == len ? `发送消息到 [${chatInfo.title}]... (${i}s)` : `上传第 ${i2}/${len} 文件到 [${chatInfo.title}]... (${i}s)`
i++
}, 1000)
function endSendingSnack() {
clearTimeout(sendingFilesSnackbarId)
sendingFilesSnackbar.open = false
}
try {
let text = inputRef.current!.value
if (text.trim() == '') return
setIsMessageSending(true)
for (const fileName of Object.keys(cachedFiles.current)) {
if (text.indexOf(fileName) != -1) {
/* const re = await Client.invoke("Chat.uploadFile", {
token: data.access_token,
file_name: fileName,
target,
data: cachedFiles.current[fileName],
}, 5000) */
const re = await Client.uploadFileLikeApi(
fileName,
cachedFiles.current[fileName]
)
if (checkApiSuccessOrSncakbar(re, `文件[${fileName}] 上传失败`)) return setIsMessageSending(false)
if (checkApiSuccessOrSncakbar(re, `文件[${fileName}] 上传失败`)) {
endSendingSnack()
return setIsMessageSending(false)
}
text = text.replaceAll('(' + fileName + ')', '(tws://file?hash=' + re.data!.file_hash as string + ')')
i2++
}
@@ -221,7 +232,10 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
target,
text,
}, 5000)
if (checkApiSuccessOrSncakbar(re, "发送失败")) return setIsMessageSending(false)
if (checkApiSuccessOrSncakbar(re, "发送失败")) {
endSendingSnack()
return setIsMessageSending(false)
}
inputRef.current!.value = ''
cachedFiles.current = {}
} catch (e) {
@@ -231,8 +245,7 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
})
}
setIsMessageSending(false)
clearTimeout(sendingFilesSnackbarId)
sendingFilesSnackbar.open = false
endSendingSnack()
}
const attachFileInputRef = React.useRef<HTMLInputElement>(null)
@@ -376,6 +389,7 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
if (!chatInfo.is_member) return
const scrollTop = (e.target as HTMLDivElement).scrollTop
if (scrollTop == 0 && !showLoadingMoreMessagesTip) {
setShowNoMoreMessagesTip(false)
setShowLoadingMoreMessagesTip(true)
await loadMore()
setShowLoadingMoreMessagesTip(false)
@@ -413,13 +427,21 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
(() => {
let date = new Date(0)
return messagesList.map((msg) => {
const rendeText = DOMPurify.sanitize(markedInstance.parse(msg.text) as string, sanitizeConfig)
const lastDate = date
date = new Date(msg.time)
const msgElement = msg.user_id == null ? <SystemMessage>{msg.text}</SystemMessage> : <Element_Message
const msgElement = msg.user_id == null ? <SystemMessage><div dangerouslySetInnerHTML={{
__html: DOMPurify.sanitize(markedInstance.parse(msg.text) as string, {
ALLOWED_ATTR: [
...sanitizeConfig.ALLOWED_ATTR,
],
ALLOWED_TAGS: [
...sanitizeConfig.ALLOWED_TAGS,
],
})
}} /></SystemMessage> : <Element_Message
rawData={msg.text}
renderHTML={rendeText}
renderHTML={DOMPurify.sanitize(markedInstance.parse(msg.text) as string, sanitizeConfig)}
message={msg}
key={msg.id}
slot="trigger"

View File

@@ -27,11 +27,17 @@ function prettyFlatParsedMessage(html: string) {
let ret = ''
// 第一个元素时, 不会被聚合在一起
let lastElementType = ''
const textElementTags = [
'chat-text',
'chat-mention',
]
function checkContinuousElement(tagName: string) {
if (lastElementType != tagName) {
if (lastElementType == 'chat-text')
ret += `<chat-text-container>${ls.map((v) => v.outerHTML).join('')}</chat-text-container>`
else
console.log(lastElementType, ls.map((v) => v.innerHTML))
if (textElementTags.indexOf(lastElementType) != -1) {
if (ls.map((v) => v.innerHTML).join('').trim() != '')
ret += `<chat-text-container>${ls.map((v) => v.outerHTML).join('')}</chat-text-container>`
} else
ret += ls.map((v) => v.outerHTML).join('')
ls = []
}

View File

@@ -0,0 +1,62 @@
import { $ } from 'mdui'
import DataCaches from "../../api/DataCaches.ts"
import { snackbar } from "../snackbar.ts"
customElements.define('chat-mention', class extends HTMLElement {
declare span: HTMLSpanElement
static observedAttributes = ['user-id']
constructor() {
super()
this.attachShadow({ mode: 'open' })
}
connectedCallback() {
const shadow = this.shadowRoot as ShadowRoot
this.span = document.createElement('span')
this.span.style.whiteSpace = 'pre-wrap'
this.span.style.fontSynthesis = 'style weight'
this.span.style.color = 'rgb(var(--mdui-color-primary))'
shadow.appendChild(this.span)
this.update()
}
attributeChangedCallback(_name: string, _oldValue: unknown, _newValue: unknown) {
this.update()
}
async update() {
if (this.span == null) return
const userId = $(this).attr('user-id')
const chatId = $(this).attr('chat-id')
const text = $(this).attr('text')
this.span.style.fontStyle = ''
if (chatId) {
const chat = await DataCaches.getChatInfo(chatId)
this.span.textContent = chat?.title
this.span.onclick = () => {
// deno-lint-ignore no-window
window.openChatInfoDialog(chat)
}
} else if (userId) {
const user = await DataCaches.getUserProfile(userId)
this.span.textContent = user?.nickname
this.span.onclick = () => {
// deno-lint-ignore no-window
window.openUserInfoDialog(user)
}
}
text && (this.span.textContent = text)
if (!(userId || chatId)) {
this.span.textContent = "无效的提及"
this.span.style.fontStyle = 'italic'
this.span.onclick = () => {
snackbar({
message: "该提及没有指定用户或者对话!",
placement: 'top',
})
}
}
}
})

View File

@@ -71,10 +71,9 @@ ApiManager.initEvents()
ApiManager.initAllApis()
httpServer.listen(config.server.listen)
console.log(chalk.green("API & Web 服务已启动"))
console.log(chalk.green(`API & Web 服务已启动, 端口为 ${config.server.listen.port}`))
function help() {
console.log(chalk.yellow("===== LingChair Server ====="))
console.log(chalk.yellow("b - 重新编译前端"))
console.log(chalk.yellow("输入 b 或者执行 deno task build 以编译前端"))
}
help()