add get_event api

This commit is contained in:
super1207 2024-07-20 17:58:00 +08:00
parent f9454039a1
commit 7a33a36f44
5 changed files with 100 additions and 0 deletions

View File

@ -50,6 +50,7 @@ import { ForwardFriendSingleMsg, ForwardGroupSingleMsg } from './msg/ForwardSing
import { GetGroupEssence } from './group/GetGroupEssence' import { GetGroupEssence } from './group/GetGroupEssence'
import { GetGroupHonorInfo } from './group/GetGroupHonorInfo' import { GetGroupHonorInfo } from './group/GetGroupHonorInfo'
import { GoCQHTTHandleQuickOperation } from './go-cqhttp/QuickOperation' import { GoCQHTTHandleQuickOperation } from './go-cqhttp/QuickOperation'
import GetEvent from './llonebot/GetEvent'
export const actionHandlers = [ export const actionHandlers = [
new GetFile(), new GetFile(),
@ -59,6 +60,7 @@ export const actionHandlers = [
new GetGroupAddRequest(), new GetGroupAddRequest(),
new SetQQAvatar(), new SetQQAvatar(),
new GetFriendWithCategory(), new GetFriendWithCategory(),
new GetEvent(),
// onebot11 // onebot11
new SendLike(), new SendLike(),
new GetMsg(), new GetMsg(),

View File

@ -0,0 +1,23 @@
import BaseAction from '../BaseAction'
import { ActionName } from '../types'
import { getHttpEvent } from '../../server/event-for-http'
import { PostEventType } from '../../server/post-ob11-event'
// import { log } from "../../../common/utils";
interface Payload {
key: string
timeout: number
}
export default class GetEvent extends BaseAction<Payload, PostEventType[]> {
actionName = ActionName.GetEvent
protected async _handle(payload: Payload): Promise<PostEventType[]> {
let key = ''
if (payload.key) {
key = payload.key;
}
let timeout = parseInt(payload.timeout?.toString()) || 0;
let evts = await getHttpEvent(key,timeout);
return evts;
}
}

View File

@ -22,6 +22,7 @@ export enum ActionName {
Debug = 'llonebot_debug', Debug = 'llonebot_debug',
GetFile = 'get_file', GetFile = 'get_file',
GetFriendsWithCategory = 'get_friends_with_category', GetFriendsWithCategory = 'get_friends_with_category',
GetEvent = 'get_event',
// onebot 11 // onebot 11
SendLike = 'send_like', SendLike = 'send_like',
GetLoginInfo = 'get_login_info', GetLoginInfo = 'get_login_info',

View File

@ -0,0 +1,68 @@
import { PostEventType } from "./post-ob11-event"
interface HttpEventType {
seq: number
event: PostEventType
}
interface HttpUserType {
lastAccessTime: number
userSeq: number
}
let curentSeq:number = 0;
let eventList:HttpEventType[] = [];
let httpUser:Record<string,HttpUserType> = {};
export function postHttpEvent(event: PostEventType) {
curentSeq += 1;
eventList.push({
seq: curentSeq,
event: event
});
while(eventList.length > 100) {
eventList.shift();
}
}
export async function getHttpEvent(userKey:string,timeout = 0) {
let toRetEvent = [];
// 清除过时的user5分钟没访问过的user将被删除
let now = Date.now();
for(let key in httpUser) {
let user = httpUser[key];
if(now - user.lastAccessTime > 1000 * 60 * 5) {
delete httpUser[key];
}
}
// 增加新的user
if(!httpUser[userKey] ) {
httpUser[userKey] = {
lastAccessTime: now,
userSeq: curentSeq
}
}
let user = httpUser[userKey];
// 等待数据到来,暂时先这么写吧......
while(curentSeq == user.userSeq && Date.now() - now < timeout) {
await new Promise( resolve => setTimeout(resolve, 10) );
}
// 取数据
for(let i = 0; i < eventList.length; i++) {
let evt = eventList[i];
if(evt.seq > user.userSeq) {
toRetEvent.push(evt.event);
}
}
// 更新user数据
user.lastAccessTime = Date.now();
user.userSeq = curentSeq;
return toRetEvent;
}

View File

@ -8,6 +8,7 @@ import { log } from '@/common/utils'
import { getConfigUtil } from '@/common/config' import { getConfigUtil } from '@/common/config'
import crypto from 'crypto' import crypto from 'crypto'
import { handleQuickOperation, QuickOperationEvent } from '../action/quick-operation' import { handleQuickOperation, QuickOperationEvent } from '../action/quick-operation'
import { postHttpEvent } from './event-for-http'
export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
@ -78,4 +79,9 @@ export function postOb11Event(msg: PostEventType, reportSelf = false, postWs = t
if (postWs) { if (postWs) {
postWsEvent(msg) postWsEvent(msg)
} }
if(!(msg.post_type == 'meta_event' && (msg as OB11BaseMetaEvent).meta_event_type == 'heartbeat')) {
// 不上报心跳
postHttpEvent(msg)
}
} }