Skip to content

Commit

Permalink
feat(api): add active chat for buddy
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Apr 7, 2024
1 parent 0d08ce5 commit fa44222
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 38 deletions.
5 changes: 5 additions & 0 deletions packages/engine-chronocat-api/src/definitions/msgService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,8 @@ export const getMsgsIncludeSelfAndAddActiveChat = define<
},
]
>('ns-ntApi-2', 'nodeIKernelMsgService/getMsgsIncludeSelfAndAddActiveChat')

export const deleteActiveChatByUid = define<Record<string, never>, [string]>(
'ns-ntApi-2',
'nodeIKernelMsgService/deleteActiveChatByUid',
)
64 changes: 55 additions & 9 deletions packages/engine-chronocat-api/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,69 @@ export const buildHandler = (ctx: ChronocatContext) => (data: IpcManData) => {
if (!d.length) return
const e = d[0]
if (!e || !('cmdName' in e)) return
void dispatcher(ctx, e.cmdName, e.payload)
void responseDispatcher(ctx, e.cmdName, e.payload)
return
}

case 'wrapped-request': {
requestMethodMap[data.id] = (data.args[1] as RedIpcDataRequest)[0]
const req = data.args[1] as RedIpcDataRequest
const method = req[0]
requestMethodMap[data.id] = method

void requestDispatcher(ctx, method, req.slice(1))

return
}

case 'wrapped-response': {
const res = requestCallbackMap[data.id]
if (res) res(data.args[1] /* RedIpcDataResponse */)
const cb = requestCallbackMap[data.id]
if (cb) cb(data.args[1] /* RedIpcDataResponse */)

const method = requestMethodMap[data.id]
if (method) {
delete requestMethodMap[data.id]
void dispatcher(ctx, method, data.args[1] /* RedIpcDataResponse */)
void responseDispatcher(
ctx,
method,
data.args[1] /* RedIpcDataResponse */,
)
}

return
}
}
}

const dispatcher = async (
const requestDispatcher = async (
ctx: ChronocatContext,
method: string,
payload: unknown,
) => {
switch (method) {
case 'nodeIKernelMsgService/deleteActiveChatByUid': {
const [peerUid] = payload as [string]

if (!peerUid) return

setTimeout(() => {
ctx.chronocatEngineChronocatApi.msgBoxActiv.activate(
{
chatType: ctx.chronocat.uix.isUid(peerUid)
? ChatType.Private
: ChatType.Group,
peerUid,
guildId: '',
},
true,
)
}, 100)

return
}
}
}

const responseDispatcher = async (
ctx: ChronocatContext,
method: string,
payload: unknown,
Expand Down Expand Up @@ -151,6 +189,12 @@ const dispatcher = async (
for (const buddy of category.buddyList) {
ctx.chronocat.uix.add(buddy.uid, buddy.uin)

ctx.chronocatEngineChronocatApi.msgBoxActiv.activate({
chatType: ChatType.Private,
peerUid: buddy.uid,
guildId: '',
})

// buddy.category = category.categoryName
friendMap[buddy.uin] = buddy
}
Expand Down Expand Up @@ -226,9 +270,11 @@ const dispatcher = async (
case ContactListType.MsgBox: {
for (const contact of changedList)
if (contact.chatType === ChatType.Group)
ctx.chronocatEngineChronocatApi.msgBoxActiv.activate(
contact.peerUid,
)
ctx.chronocatEngineChronocatApi.msgBoxActiv.activate({
chatType: contact.chatType,
peerUid: contact.peerUid,
guildId: '',
})

break
}
Expand Down
59 changes: 32 additions & 27 deletions packages/engine-chronocat-api/src/services/msgBoxActiv.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { ChatType, ContactListType } from '@chronocat/red'
import type { Peer } from '@chronocat/red'
import { ContactListType } from '@chronocat/red'
import type { ChronocatContext } from '@chronocat/shell'
import { getAioFirstViewLatestMsgsAndAddActiveChat } from '../definitions/msgService'
import type { F } from 'ts-toolbelt'
import { getMsgsIncludeSelfAndAddActiveChat } from '../definitions/msgService'
import { getBuddyList } from '../definitions/nodeStore'
import { fetchAndSubscribeABatchOfRecentContact } from '../definitions/recentContactService'

export interface MsgBoxActiv {
activate: (peerUid: string) => void
activate: <T>(peer: F.Exact<T, Peer>, force?: boolean) => void
}

export const msgBoxActiv = (ctx: ChronocatContext): MsgBoxActiv => {
const activated: string[] = []

let activateIntl: (peerUid: string) => void = () => {}
let activateIntl: (peer: Peer, force?: boolean) => void = () => {}

const activate = (peerUid: string) => activateIntl(peerUid)
const activate = (peer: Peer, force?: boolean) => activateIntl(peer, force)

void ctx.chronocat
.whenReady()
Expand All @@ -21,7 +24,29 @@ export const msgBoxActiv = (ctx: ChronocatContext): MsgBoxActiv => {
.then((config) => {
if (!config.receive_msgbox) return

let task = fetchAndSubscribeABatchOfRecentContact({
let task = Promise.resolve()

activateIntl = (peer: Peer, force?: boolean) =>
void (task = task.then(async () => {
await ctx.chronocat.sleep(500)

if (!peer || !peer.peerUid) return

// 使用 isUid 即可判断群聊/私聊,无需携带 peer.chatType
if (!force && activated.includes(peer.peerUid)) return
activated.push(peer.peerUid)

void getMsgsIncludeSelfAndAddActiveChat({
peer,
msgId: '0',
cnt: 1,
queryOrder: true,
})
}))

void getBuddyList()

void fetchAndSubscribeABatchOfRecentContact({
fetchParam: {
anchorPointContact: {
contactId: '',
Expand All @@ -33,27 +58,7 @@ export const msgBoxActiv = (ctx: ChronocatContext): MsgBoxActiv => {
count: 200,
fetchOld: true,
},
}) as unknown as Promise<void>

activateIntl = (peerUid: string) =>
void (task = task.then(async () => {
await ctx.chronocat.sleep(500)

if (!peerUid) return

if (activated.includes(peerUid)) return

activated.push(peerUid)

void getAioFirstViewLatestMsgsAndAddActiveChat({
peer: {
chatType: ChatType.Group,
peerUid,
guildId: '',
},
cnt: 20,
})
}))
})
})

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/shell/src/services/config/configEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export interface ChronocatBaseConfig {
/**
* @title 接收群助手消息
*
* @description 是否接收群助手内群的消息。启用时,Chronocat
* @description 是否接收私聊消息和群助手内群的消息。启用时,Chronocat
* 会在应用启动后主动向 QQ
* 申请推送群助手内所有群的消息。默认启用,关闭则默认不推送群助手内群的新消息
* 申请推送所有私聊消息群助手内所有群的消息。默认启用,关闭则默认不推送私聊消息和群助手内群的新消息
*
* @default true
*/
Expand Down

0 comments on commit fa44222

Please sign in to comment.