refactor: get friends, get groups, get group members, get group member

This commit is contained in:
linyuchen
2024-04-28 18:14:21 +08:00
parent e97f323d9a
commit f8abb73c92
36 changed files with 61 additions and 149 deletions

View File

@@ -1,90 +0,0 @@
import {
type Friend,
type FriendRequest,
type Group,
type GroupMember, GroupNotify,
type SelfInfo
} from '../core/src/entities';
import { isNumeric } from './utils/helper';
import { log } from '@/common/utils/log';
export const selfInfo: SelfInfo = {
uid: '',
uin: '',
nick: '',
online: true
};
// groupCode -> Group
export const groups: Map<string, Group> = new Map<string, Group>();
export function deleteGroup(groupQQ: string) {
groups.delete(groupQQ);
groupMembers.delete(groupQQ);
}
// 群号 -> 群成员map(uid=>GroupMember)
export const groupMembers: Map<string, Map<string, GroupMember>> = new Map<string, Map<string, GroupMember>>();
// uid -> Friend
export const friends: Map<string, Friend> = new Map<string, Friend>();
export const friendRequests: Record<string, FriendRequest> = {}; // flag->FriendRequest
export const groupNotifies: Record<string, GroupNotify> = {}; // flag->GroupNotify
export const napCatError = {
ffmpegError: '',
httpServerError: '',
wsServerError: '',
otherError: 'NapCat未能正常启动请检查日志查看错误'
};
export async function getFriend(uinOrUid: string): Promise<Friend | undefined> {
uinOrUid = uinOrUid.toString();
if (isNumeric(uinOrUid)) {
const friendList = Array.from(friends.values());
return friendList.find(friend => friend.uin === uinOrUid);
} else {
return friends.get(uinOrUid);
}
}
export async function getGroup(qq: string | number): Promise<Group | undefined> {
const group = groups.get(qq.toString());
return group;
}
export async function getGroupMember(groupQQ: string | number, memberUinOrUid: string | number) {
groupQQ = groupQQ.toString();
memberUinOrUid = memberUinOrUid.toString();
const members = groupMembers.get(groupQQ);
if (!members) {
return null;
}
// log('getGroupMember', members);
if (isNumeric(memberUinOrUid)) {
return Array.from(members.values()).find(member => member.uin === memberUinOrUid);
} else {
return members.get(memberUinOrUid);
}
}
export async function refreshGroupMembers(groupQQ: string) {
// const group = groups.find(group => group.groupCode === groupQQ)
// if (group) {
// group.members = await NTQQGroupApi.getGroupMembers(groupQQ)
// }
}
export const uid2UinMap: Record<string, string> = {}; // 一串加密的字符串(uid) -> qq号
export function getUidByUin(uin: string) {
for (const uid in uid2UinMap) {
if (uid2UinMap[uid] === uin) {
return uid;
}
}
}
export const tempGroupCodeMap: Record<string, string> = {}; // peerUid => 群号

View File

@@ -1,7 +1,7 @@
import { GroupNotify, GroupNotifyStatus } from '@/core/entities';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { uid2UinMap } from '@/common/data';
import { uid2UinMap } from '@/core/data';
import { NTQQUserApi } from '@/core/apis/user';
import { NTQQGroupApi } from '@/core/apis/group';

View File

@@ -1,14 +1,14 @@
import { OB11User } from '../../types';
import { OB11Constructor } from '../../constructor';
import { friends } from '../../../common/data';
import { friends } from '@/core/data';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { NTQQUserApi } from '@/core/apis';
export class GetRobotUinRange extends BaseAction<void, Array<any>> {
actionName = ActionName.GetRobotUinRange;
actionName = ActionName.GetRobotUinRange;
protected async _handle(payload: void) {
// console.log(await NTQQUserApi.getRobotUinRange());
return await NTQQUserApi.getRobotUinRange();
}
protected async _handle(payload: void) {
// console.log(await NTQQUserApi.getRobotUinRange());
return await NTQQUserApi.getRobotUinRange();
}
}

View File

@@ -1,6 +1,6 @@
import { OB11User } from '../../types';
import { OB11Constructor } from '../../constructor';
import { friends } from '../../../common/data';
import { friends } from '@/core/data';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { NTQQUserApi } from '@/core/apis';
@@ -11,19 +11,19 @@ interface Payload {
batteryStatus: number;
}
export class SetOnlineStatus extends BaseAction<Payload, null> {
actionName = ActionName.SetOnlineStatus;
actionName = ActionName.SetOnlineStatus;
protected async _handle(payload: Payload) {
// 可设置状态
// { status: 10, extStatus: 1027, batteryStatus: 0 }
// { status: 30, extStatus: 0, batteryStatus: 0 }
// { status: 50, extStatus: 0, batteryStatus: 0 }
// { status: 60, extStatus: 0, batteryStatus: 0 }
// { status: 70, extStatus: 0, batteryStatus: 0 }
let ret = await NTQQUserApi.setSelfOnlineStatus(payload.status, payload.extStatus, payload.batteryStatus);
if (ret.result !== 0) {
throw new Error("设置在线状态失败");
}
return null;
protected async _handle(payload: Payload) {
// 可设置状态
// { status: 10, extStatus: 1027, batteryStatus: 0 }
// { status: 30, extStatus: 0, batteryStatus: 0 }
// { status: 50, extStatus: 0, batteryStatus: 0 }
// { status: 60, extStatus: 0, batteryStatus: 0 }
// { status: 70, extStatus: 0, batteryStatus: 0 }
const ret = await NTQQUserApi.setSelfOnlineStatus(payload.status, payload.extStatus, payload.batteryStatus);
if (ret.result !== 0) {
throw new Error('设置在线状态失败');
}
return null;
}
}

View File

@@ -1,6 +1,6 @@
import BaseAction from '../BaseAction';
import { OB11Message, OB11User } from '../../types';
import { getFriend, friends, uid2UinMap, getUidByUin } from '@/common/data';
import { getFriend, friends, uid2UinMap, getUidByUin } from '@/core/data';
import { ActionName } from '../types';
import { ChatType } from '@/core/entities';
import { dbUtil } from '@/common/utils/db';

View File

@@ -1,6 +1,6 @@
import BaseAction from '../BaseAction';
import { OB11Message, OB11User } from '../../types';
import { getGroup, groups } from '@/common/data';
import { getGroup, groups } from '@/core/data';
import { ActionName } from '../types';
import { ChatType } from '@/core/entities';
import { dbUtil } from '@/common/utils/db';

View File

@@ -1,6 +1,6 @@
import BaseAction from '../BaseAction';
import { OB11User } from '../../types';
import { getUidByUin, uid2UinMap } from '@/common/data';
import { getUidByUin, uid2UinMap } from '@/core/data';
import { OB11Constructor } from '../../constructor';
import { ActionName } from '../types';
import { NTQQUserApi } from '@/core/apis/user';

View File

@@ -1,5 +1,5 @@
import BaseAction from '../BaseAction';
import { getGroup } from '@/common/data';
import { getGroup } from '@/core/data';
import { ActionName } from '../types';
import { SendMsgElementConstructor } from '@/core/entities/constructor';
import { ChatType, SendFileElement } from '@/core/entities';

View File

@@ -1,4 +1,4 @@
import { getGroup } from '@/common/data';
import { getGroup } from '@/core/data';
import { OB11Group } from '../../types';
import { OB11Constructor } from '../../constructor';
import BaseAction from '../BaseAction';

View File

@@ -2,7 +2,7 @@ import { OB11Group } from '../../types';
import { OB11Constructor } from '../../constructor';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { groups } from '@/common/data';
import { groups } from '@/core/data';
import { NTQQGroupApi } from '@/core/apis';
import { Group } from '@/core/entities';
import { log } from '@/common/utils/log';

View File

@@ -1,5 +1,5 @@
import { OB11GroupMember } from '../../types';
import { getGroupMember } from '@/common/data';
import { getGroupMember } from '@/core/data';
import { OB11Constructor } from '../../constructor';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';

View File

@@ -1,10 +1,11 @@
import { getGroup } from '@/common/data';
import { getGroup } from '@/core/data';
import { OB11GroupMember } from '../../types';
import { OB11Constructor } from '../../constructor';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { napCatCore } from '@/core';
import { napCatCore, NTQQGroupApi } from '@/core';
import { WebApi } from '@/core/apis/webapi';
import { logDebug } from '@/common/utils/log';
export interface PayloadType {
group_id: number
@@ -15,7 +16,8 @@ class GetGroupMemberList extends BaseAction<PayloadType, OB11GroupMember[]> {
actionName = ActionName.GetGroupMemberList;
protected async _handle(payload: PayloadType) {
console.log(await WebApi.getGroupMember(payload.group_id.toString()));
// logDebug(await WebApi.getGroupMembers(payload.group_id.toString()));
// await NTQQGroupApi.getGroupMembers(payload.group_id.toString());
const group = await getGroup(payload.group_id.toString());
if (group) {
return OB11Constructor.groupMembers(group);

View File

@@ -2,7 +2,7 @@ import BaseAction from '../BaseAction';
import { GroupRequestOperateTypes } from '@/core/entities';
import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';
import { groupNotifies } from '@/common/data';
import { groupNotifies } from '@/core/data';
interface Payload {
flag: string,

View File

@@ -1,5 +1,5 @@
import BaseAction from '../BaseAction';
import { getGroupMember } from '@/common/data';
import { getGroupMember } from '@/core/data';
import { GroupMemberRole } from '@/core/entities';
import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';

View File

@@ -1,5 +1,5 @@
import BaseAction from '../BaseAction';
import { getGroupMember } from '@/common/data';
import { getGroupMember } from '@/core/data';
import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';

View File

@@ -1,5 +1,5 @@
import BaseAction from '../BaseAction';
import { getGroupMember } from '@/common/data';
import { getGroupMember } from '@/core/data';
import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';

View File

@@ -1,5 +1,5 @@
import BaseAction from '../BaseAction';
import { getGroupMember } from '@/common/data';
import { getGroupMember } from '@/core/data';
import { ActionName } from '../types';
import { NTQQGroupApi } from '@/core/apis/group';

View File

@@ -2,7 +2,7 @@ import { ChatType, Peer } from '@/core/entities';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { NTQQMsgApi } from '@/core/apis';
import { getFriend, getUidByUin } from '@/common/data';
import { getFriend, getUidByUin } from '@/core/data';
interface Payload {
user_id: number;

View File

@@ -26,7 +26,7 @@ import { dbUtil } from '@/common/utils/db';
import { log, logDebug, logError } from '@/common/utils/log';
import { sleep } from '@/common/utils/helper';
import { uri2local } from '@/common/utils/file';
import { getFriend, getGroup, getGroupMember, getUidByUin, selfInfo } from '@/common/data';
import { getFriend, getGroup, getGroupMember, getUidByUin, selfInfo } from '@/core/data';
import { NTQQMsgApi } from '../../../core/src/apis';
import { NTQQFileApi } from '../../../core/src/apis';
import { ob11Config } from '@/onebot11/config';

View File

@@ -1,4 +1,4 @@
import { selfInfo } from '@/common/data';
import { selfInfo } from '@/core/data';
import { OB11User } from '../../types';
import { OB11Constructor } from '../../constructor';
import BaseAction from '../BaseAction';

View File

@@ -1,7 +1,7 @@
import BaseAction from '../BaseAction';
import { OB11Status } from '../../types';
import { ActionName } from '../types';
import { selfInfo } from '@/common/data';
import { selfInfo } from '@/core/data';
export default class GetStatus extends BaseAction<any, OB11Status> {

View File

@@ -1,6 +1,6 @@
import { OB11User } from '../../types';
import { OB11Constructor } from '../../constructor';
import { friends } from '../../../common/data';
import { friends } from '@/core/data';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { NTQQUserApi } from '@/core/apis';
@@ -15,7 +15,7 @@ export class GetCookies extends BaseAction<Payload, Response> {
actionName = ActionName.GetCookies;
protected async _handle(payload: Payload) {
let _Skey = await NTQQUserApi.getSkey();
const _Skey = await NTQQUserApi.getSkey();
// 取Skey
// 先NodeIKernelTicketService.forceFetchClientKey('')
// 返回值
@@ -29,7 +29,7 @@ export class GetCookies extends BaseAction<Payload, Response> {
// }
// request https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=1627126029&clientkey=key
// &u1=https%3A%2F%2Fh5.qzone.qq.com%2Fqqnt%2Fqzoneinpcqq%2Ffriend%3Frefresh%3D0%26clientuin%3D0%26darkMode%3D0&keyindex=keyIndex
let _PSkey = await NTQQUserApi.getPSkey([payload.domain]);
const _PSkey = await NTQQUserApi.getPSkey([payload.domain]);
// 取Pskey
// NodeIKernelTipOffService.getPskey([ 'qun.qq.com' ], true )
// {
@@ -40,7 +40,7 @@ export class GetCookies extends BaseAction<Payload, Response> {
// }
// }
if (!_PSkey || !_Skey) {
throw new Error("获取Cookies失败");
throw new Error('获取Cookies失败');
}
return { Pskey: _PSkey, Skey: _Skey };
}

View File

@@ -1,6 +1,6 @@
import { OB11User } from '../../types';
import { OB11Constructor } from '../../constructor';
import { friends } from '../../../common/data';
import { friends } from '@/core/data';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';

View File

@@ -1,6 +1,6 @@
import { NTQQUserApi } from '@/core/apis';
import BaseAction from '../BaseAction';
import { getFriend, getUidByUin, uid2UinMap } from '@/common/data';
import { getFriend, getUidByUin, uid2UinMap } from '@/core/data';
import { ActionName } from '../types';
import { log, logDebug } from '@/common/utils/log';

View File

@@ -1,7 +1,7 @@
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { NTQQFriendApi } from '@/core/apis/friend';
import { friendRequests } from '@/common/data';
import { friendRequests } from '@/core/data';
interface Payload {
flag: string,

View File

@@ -1,6 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import { selfInfo } from '@/common/data';
import { selfInfo } from '@/core/data';
import { logDebug, logError } from '@/common/utils/log';
import { ConfigBase } from '@/common/utils/ConfigBase';

View File

@@ -40,7 +40,7 @@ import { OB11GroupTitleEvent } from './event/notice/OB11GroupTitleEvent';
import { OB11GroupCardEvent } from './event/notice/OB11GroupCardEvent';
import { OB11GroupDecreaseEvent } from './event/notice/OB11GroupDecreaseEvent';
import { ob11Config } from '@/onebot11/config';
import { deleteGroup, getFriend, getGroupMember, groupMembers, selfInfo, tempGroupCodeMap } from '@/common/data';
import { deleteGroup, getFriend, getGroupMember, groupMembers, selfInfo, tempGroupCodeMap } from '@/core/data';
import { NTQQFileApi, NTQQGroupApi, NTQQUserApi } from '../core/src/apis';
import http from 'http';
import { OB11GroupMsgEmojiLikeEvent } from '@/onebot11/event/notice/OB11MsgEmojiLikeEvent';

View File

@@ -1,4 +1,4 @@
import { selfInfo } from '@/common/data';
import { selfInfo } from '@/core/data';
export enum EventType {
META = 'meta_event',
@@ -13,4 +13,4 @@ export abstract class OB11BaseEvent {
time = Math.floor(Date.now() / 1000);
self_id = parseInt(selfInfo.uin);
post_type: EventType = EventType.META;
}
}

View File

@@ -1,5 +1,5 @@
import { OB11BaseNoticeEvent } from './OB11BaseNoticeEvent';
import { selfInfo } from '../../../common/data';
import { selfInfo } from '@/core/data';
import { OB11BaseEvent } from '../OB11BaseEvent';
class OB11PokeEvent extends OB11BaseNoticeEvent{

View File

@@ -1,6 +1,6 @@
import { OB11Message } from '@/onebot11/types';
import { log } from '@/common/utils/log';
import { getGroup, getGroupMember } from '@/common/data';
import { getGroup, getGroupMember } from '@/core/data';
import exp from 'constants';
export async function logMessage(ob11Message: OB11Message){

View File

@@ -15,7 +15,7 @@ import { ob11Config } from '@/onebot11/config';
import { httpHeart, ob11HTTPServer } from '@/onebot11/server/http';
import { ob11WebsocketServer } from '@/onebot11/server/ws/WebsocketServer';
import { ob11ReverseWebsockets } from '@/onebot11/server/ws/ReverseWebsocket';
import { friendRequests, getFriend, getGroup, getGroupMember, groupNotifies, selfInfo } from '@/common/data';
import { friendRequests, getFriend, getGroup, getGroupMember, groupNotifies, selfInfo } from '@/core/data';
import { dbUtil } from '@/common/utils/db';
import { BuddyListener, GroupListener, NodeIKernelBuddyListener } from '../core/src/listeners';
import { OB11FriendRequestEvent } from '@/onebot11/event/request/OB11FriendRequest';

View File

@@ -3,7 +3,7 @@ import { OB11Response } from '../action/OB11Response';
import { HttpServerBase } from '@/common/server/http';
import { actionHandlers, actionMap } from '../action';
import { ob11Config } from '@/onebot11/config';
import { selfInfo } from '@/common/data';
import { selfInfo } from '@/core/data';
import { OB11HeartbeatEvent } from '@/onebot11/event/meta/OB11HeartbeatEvent';
import { postOB11Event } from '@/onebot11/server/postOB11Event';
import { napCatCore } from '@/core';

View File

@@ -12,7 +12,7 @@ import { OB11FriendRequestEvent } from '../event/request/OB11FriendRequest';
import { OB11GroupRequestEvent } from '../event/request/OB11GroupRequest';
import { isNull } from '@/common/utils/helper';
import { dbUtil } from '@/common/utils/db';
import { friendRequests, getGroup, groupNotifies, selfInfo } from '@/common/data';
import { friendRequests, getGroup, groupNotifies, selfInfo } from '@/core/data';
import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi } from '../../core/src/apis';
import { logMessage, logNotice, logRequest } from '@/onebot11/log';

View File

@@ -10,7 +10,7 @@ import { OB11HeartbeatEvent } from '../../event/meta/OB11HeartbeatEvent';
import { log, logDebug, logError } from '../../../common/utils/log';
import { ob11Config } from '@/onebot11/config';
import { napCatCore } from '@/core';
import { selfInfo } from '@/common/data';
import { selfInfo } from '@/core/data';
export const rwsList: ReverseWebsocket[] = [];

View File

@@ -12,7 +12,7 @@ import { wsReply } from './reply';
import { napCatCore } from '@/core';
import { log, logDebug, logError } from '../../../common/utils/log';
import { ob11Config } from '@/onebot11/config';
import { selfInfo } from '@/common/data';
import { selfInfo } from '@/core/data';
const heartbeatRunning = false;