Compare commits
3 Commits
f3850a6e2f
...
ca565e3c3e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca565e3c3e | ||
|
|
12861b80a1 | ||
|
|
02b1d28a6b |
@@ -1 +1,11 @@
|
||||
export * from 'lingchair-internal-shared'
|
||||
|
||||
import { ClientEvent } from "lingchair-internal-shared"
|
||||
|
||||
import Message from "./Message.ts"
|
||||
|
||||
export type ClientEventData<T extends ClientEvent> =
|
||||
T extends "Client.onMessage" ? { message: Message } :
|
||||
never
|
||||
|
||||
export type ClientEventCallback<T extends ClientEvent> = (data: ClientEventData<T>) => void
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
// deno-lint-ignore-file no-explicit-any
|
||||
import { io, ManagerOptions, Socket, SocketOptions } from 'socket.io-client'
|
||||
import crypto from 'node:crypto'
|
||||
import { CallMethod, ClientEvent } from './ApiDeclare.ts'
|
||||
import { CallMethod, ClientEvent, ClientEventCallback } from './ApiDeclare.ts'
|
||||
import ApiCallbackMessage from './ApiCallbackMessage.ts'
|
||||
import User from "./User.ts"
|
||||
import UserMySelf from "./UserMySelf.ts"
|
||||
import CallbackError from "./CallbackError.ts"
|
||||
import Chat from "./Chat.ts"
|
||||
import { CallableMethodBeforeAuth } from "lingchair-internal-shared"
|
||||
import Message from "./Message.ts";
|
||||
|
||||
export {
|
||||
User,
|
||||
@@ -46,15 +47,34 @@ export default class LingChairClient {
|
||||
session_id: crypto.randomUUID(),
|
||||
},
|
||||
})
|
||||
this.client.on("The_White_Silk", (name: string, data: unknown, _callback: (ret: unknown) => void) => {
|
||||
this.client.on("The_White_Silk", (name: ClientEvent, data: any, _callback: (ret: unknown) => void) => {
|
||||
try {
|
||||
if (name == null || data == null) return
|
||||
this.events[name]?.forEach((v) => v(data))
|
||||
for (const v of (this.events[name] || []))
|
||||
v(({
|
||||
"Client.onMessage": {
|
||||
message: new Message(this, data)
|
||||
}
|
||||
})[name])
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
events: { [K in ClientEvent]?: ClientEventCallback<K>[] } = {}
|
||||
on<K extends ClientEvent>(eventName: K, func: ClientEventCallback<K>) {
|
||||
if (this.events[eventName] == null)
|
||||
this.events[eventName] = []
|
||||
if (this.events[eventName].indexOf(func) == -1)
|
||||
this.events[eventName].push(func)
|
||||
}
|
||||
off<K extends ClientEvent>(eventName: K, func: ClientEventCallback<K>) {
|
||||
if (this.events[eventName] == null)
|
||||
this.events[eventName] = []
|
||||
const index = this.events[eventName].indexOf(func)
|
||||
if (index != -1)
|
||||
this.events[eventName].splice(index, 1)
|
||||
}
|
||||
connect() {
|
||||
this.client.connect()
|
||||
}
|
||||
@@ -86,20 +106,6 @@ export default class LingChairClient {
|
||||
})
|
||||
})
|
||||
}
|
||||
events: { [key: string]: ((data: any) => void)[] } = {}
|
||||
on(eventName: ClientEvent, func: (data: any) => void) {
|
||||
if (this.events[eventName] == null)
|
||||
this.events[eventName] = []
|
||||
if (this.events[eventName].indexOf(func) == -1)
|
||||
this.events[eventName].push(func)
|
||||
}
|
||||
off(eventName: ClientEvent, func: (data: any) => void) {
|
||||
if (this.events[eventName] == null)
|
||||
this.events[eventName] = []
|
||||
const index = this.events[eventName].indexOf(func)
|
||||
if (index != -1)
|
||||
this.events[eventName].splice(index, 1)
|
||||
}
|
||||
async auth(args: {
|
||||
refresh_token?: string,
|
||||
access_token?: string,
|
||||
@@ -168,6 +174,18 @@ export default class LingChairClient {
|
||||
return false
|
||||
}
|
||||
}
|
||||
getBaseHttpUrl() {
|
||||
const url = new URL(this.server_url)
|
||||
return (({
|
||||
'ws:': 'http:',
|
||||
'wss:': 'https:',
|
||||
'http:': 'http:',
|
||||
'https:': 'https:',
|
||||
})[url.protocol] || 'http:') + '//' + url.host
|
||||
}
|
||||
getUrlForFileByHash(file_hash?: string, defaultUrl?: string) {
|
||||
return file_hash ? (this.getBaseHttpUrl() + '/uploaded_files/' + file_hash) : defaultUrl
|
||||
}
|
||||
async registerOrThrow({
|
||||
nickname,
|
||||
username,
|
||||
@@ -201,13 +219,8 @@ export default class LingChairClient {
|
||||
)
|
||||
form.append('file_name', fileName)
|
||||
chatId && form.append('chat_id', chatId)
|
||||
const url = new URL(this.server_url)
|
||||
const re = await fetch((({
|
||||
'ws:': 'http:',
|
||||
'wss:': 'https:',
|
||||
'http:': 'http:',
|
||||
'https:': 'https:',
|
||||
})[url.protocol] || 'http:') + '//' + url.host + '/upload_file', {
|
||||
|
||||
const re = await fetch(this.getBaseHttpUrl() + '/upload_file', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Token": this.access_token,
|
||||
|
||||
@@ -3,6 +3,63 @@ import MessageBean from "./bean/MessageBean.ts"
|
||||
import LingChairClient from "./LingChairClient.ts"
|
||||
import Chat from "./Chat.ts"
|
||||
import User from "./User.ts"
|
||||
import CallbackError from "./CallbackError.ts"
|
||||
import ApiCallbackMessage from "./ApiCallbackMessage.ts"
|
||||
|
||||
import marked from 'marked'
|
||||
|
||||
class ChatMention extends BaseClientObject {
|
||||
declare chat_id?: string
|
||||
declare user_id?: string
|
||||
constructor(client: LingChairClient, {
|
||||
user_id,
|
||||
chat_id,
|
||||
}: {
|
||||
user_id?: string,
|
||||
chat_id?: string,
|
||||
}) {
|
||||
super(client)
|
||||
this.user_id = user_id
|
||||
this.chat_id = chat_id
|
||||
}
|
||||
async getChat() {
|
||||
return await Chat.getById(this.client, this.chat_id as string)
|
||||
}
|
||||
async getUser() {
|
||||
return await User.getById(this.client, this.user_id as string)
|
||||
}
|
||||
}
|
||||
|
||||
type FileType = 'Video' | 'Image' | 'File'
|
||||
type MentionType = 'ChatMention' | 'UserMention'
|
||||
|
||||
class ChatAttachment extends BaseClientObject {
|
||||
declare file_hash: string
|
||||
constructor(client: LingChairClient, file_hash: string) {
|
||||
super(client)
|
||||
this.file_hash = file_hash
|
||||
}
|
||||
async blob() {
|
||||
try {
|
||||
return await this.blobOrThrow()
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
async blobOrThrow() {
|
||||
const url = this.client.getUrlForFileByHash(this.file_hash)
|
||||
const re = await fetch(url!)
|
||||
const blob = await re.blob()
|
||||
if (!re.ok) throw new CallbackError({
|
||||
msg: await blob.text(),
|
||||
code: re.status,
|
||||
} as ApiCallbackMessage)
|
||||
return blob
|
||||
}
|
||||
getFileHash() {
|
||||
return this.file_hash
|
||||
}
|
||||
}
|
||||
|
||||
export default class Message extends BaseClientObject {
|
||||
declare bean: MessageBean
|
||||
@@ -27,6 +84,63 @@ export default class Message extends BaseClientObject {
|
||||
getText() {
|
||||
return this.bean.text
|
||||
}
|
||||
parseWithTransformers({
|
||||
attachment,
|
||||
mention,
|
||||
}: {
|
||||
attachment?: ({ text, fileType, attachment }: { text: string, fileType: FileType, attachment: ChatAttachment }) => string,
|
||||
mention?: ({ text, mentionType, mention }: { text: string, mentionType: MentionType, mention: ChatMention }) => string,
|
||||
}) {
|
||||
new marked.Marked({
|
||||
extensions: [
|
||||
{
|
||||
name: 'image',
|
||||
renderer: ({ text, href }) => {
|
||||
const mentionType = /^(UserMention|ChatMention)=.*/.exec(text)?.[1] as MentionType
|
||||
const fileType = (/^(Video|File|Image)=.*/.exec(text)?.[1] || 'Image') as FileType
|
||||
|
||||
if (fileType != null && /tws:\/\/file\?hash=[A-Za-z0-9]+$/.test(href)) {
|
||||
const file_hash = /^tws:\/\/file\?hash=(.*)/.exec(href)?.[1]!
|
||||
return attachment ? attachment({ text: text, attachment: new ChatAttachment(this.client, file_hash), fileType: fileType, }) : text
|
||||
}
|
||||
if (mentionType != null && /^tws:\/\/chat\?id=[A-Za-z0-9]+/.test(href)) {
|
||||
const id = /^tws:\/\/chat\?id=(.*)/.exec(href)?.[1]!
|
||||
return mention ? mention({
|
||||
text: text,
|
||||
mention: new ChatMention(this.client, {
|
||||
[({
|
||||
ChatMention: 'chat_id',
|
||||
UserMention: 'user_id',
|
||||
})[mentionType]]: id
|
||||
}),
|
||||
mentionType: mentionType,
|
||||
}) : text
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
}).parse(this.getText())
|
||||
}
|
||||
getAttachments() {
|
||||
const attachments: ChatAttachment[] = []
|
||||
this.parseWithTransformers({
|
||||
attachment({ attachment }) {
|
||||
attachments.push(attachment)
|
||||
return ''
|
||||
}
|
||||
})
|
||||
return attachments
|
||||
}
|
||||
getMentions() {
|
||||
const mentions: ChatMention[] = []
|
||||
this.parseWithTransformers({
|
||||
mention({ mention }) {
|
||||
mentions.push(mention)
|
||||
return ''
|
||||
}
|
||||
})
|
||||
return mentions
|
||||
}
|
||||
getUserId() {
|
||||
return this.bean.user_id
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"imports": {
|
||||
"socket.io-client": "npm:socket.io-client@4.8.1",
|
||||
"lingchair-internal-shared": "../internal-shared/mod.ts"
|
||||
"lingchair-internal-shared": "../internal-shared/mod.ts",
|
||||
"marked": "npm:marked@16.3.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user