Skip to content

Commit

Permalink
feat(shell): uix: implement getUix2()
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Aug 23, 2024
1 parent ab26d18 commit c3b9a4b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/shell/src/services/uix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventEmitter } from 'node:events'
import { api } from './api'

const uinRegex = /\d+/

Expand All @@ -9,11 +10,14 @@ const isUin = (uin: unknown) =>
const isUid = (uid: unknown) =>
typeof uid === 'string' && uid.length === 24 && uid.startsWith('u_')

const isGroup = isUin

export class Uix extends EventEmitter {
map: Record<string, string> = {}

isUin = isUin
isUid = isUid
isGroup = isGroup

add = (uid: string, uin: string) => {
if (!isUid(uid) || !isUin(uin)) return
Expand All @@ -23,19 +27,57 @@ export class Uix extends EventEmitter {
this.emit(uid, uin)
}

/**
* @deprecated
*/
getUin = (uid: string) => {
if (!isUid(uid)) return undefined
const uin = this.map[uid]
if (!isUin(uin)) return undefined
return uin
}

getUin2 = async (uid: string, group: string | number | undefined) => {
if (!isUid(uid)) return undefined
let uin: string | undefined
if (group) {
if (!isGroup(group)) return undefined
// const groupString = `${group}`
try {
uin = await api['chronocat.internal.uix.uin.get'](uid)
// uin = await api['chronocat.internal.uix.uin.get.group'](uid, groupString)
} catch (e) {
// TODO
}
} else {
try {
uin = await api['chronocat.internal.uix.uin.get'](uid)
} catch (e) {
// TODO
}
}
if (uin) this.add(uid, uin)
return this.getUin(uid)
}

/**
* @deprecated
*/
getUid = (uin: string) => {
if (!isUin(uin)) return undefined
const uid = this.map[uin]
if (!isUid(uid)) return undefined
return uid
}

getUid2 = async (uin: string, _group: string | number | undefined) => {
if (!isUin(uin)) return undefined
// if (group) {
// if (!isGroup(group)) return undefined
// const groupString = `${group}`
// }
return this.getUid(uin)
}
}

export const uix = new Uix()
3 changes: 3 additions & 0 deletions packages/shell/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ export interface CCInternalMethods {

'chronocat.internal.qface.get': [[string], QFace | undefined]
'chronocat.internal.qface.list': [[], QFace[] | undefined]

'chronocat.internal.uix.uin.get': [[string], string | undefined]
'chronocat.internal.uix.uin.get.group': [[string, string], string | undefined]
}

export type Methods = SatoriMethods & CCInternalMethods

0 comments on commit c3b9a4b

Please sign in to comment.