Skip to content

Commit

Permalink
Chunk userId list on initializing channel with many user. #144
Browse files Browse the repository at this point in the history
  • Loading branch information
storycraft committed Feb 18, 2021
1 parent 0f9155e commit bd18590
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
55 changes: 54 additions & 1 deletion src/talk/channel/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* Copyright (c) storycraft. Licensed under the MIT Licence.
*/

import { Long } from 'bson';
import { Chatlog, ChatType } from '../../chat';
import { MediaKeyComponent } from '../../media';
import { AsyncCommandResult, CommandResultDone } from '../../request';
import { OpenChannelSession } from '../../openlink';
import { AsyncCommandResult, CommandResultDone, KnownDataStatusCode } from '../../request';
import { NormalChannelUserInfo, OpenChannelUserInfo } from '../../user';
import { MediaUploadTemplate } from '../media/upload';
import { TalkChannelSession } from './talk-channel-session';

Expand Down Expand Up @@ -41,3 +44,53 @@ export async function sendMultiMedia(
},
});
}

export async function initNormalUserList(
session: TalkChannelSession,
userIdList: Long[]
): AsyncCommandResult<NormalChannelUserInfo[]> {
const userList = userIdList.map(userId => {
return { userId };
});

const infoList: NormalChannelUserInfo[] = [];

const len = userList.length;
for (let i = 0; i < len; i += 300) {
const userRes = await session.getLatestUserInfo(...userList.slice(i, i + 300));
if (!userRes.success) return userRes;

infoList.push(...userRes.result as NormalChannelUserInfo[]);
}

return {
success: true,
status: KnownDataStatusCode.SUCCESS,
result: infoList
};
}

export async function initOpenUserList(
session: OpenChannelSession,
userIdList: Long[]
): AsyncCommandResult<OpenChannelUserInfo[]> {
const userList = userIdList.map(userId => {
return { userId };
});

const infoList: OpenChannelUserInfo[] = [];

const len = userList.length;
for (let i = 0; i < len; i += 300) {
const userRes = await session.getLatestUserInfo(...userList.slice(i, i + 300));
if (!userRes.success) return userRes;

infoList.push(...userRes.result);
}

return {
success: true,
status: KnownDataStatusCode.SUCCESS,
result: infoList
};
}
21 changes: 17 additions & 4 deletions src/talk/channel/talk-normal-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
import { JsonUtil } from '../../util';
import { ChatOnRoomRes } from '../../packet/chat';
import { MediaUploadTemplate } from '../media/upload';
import { sendMultiMedia } from './common';
import { initNormalUserList, sendMultiMedia } from './common';
import { MediaDownloader, MediaUploader, MultiMediaUploader } from '../media';

export class TalkNormalChannel extends TypedEmitter<ChannelEvents> implements TalkChannel, Managed<ChannelEvents> {
Expand Down Expand Up @@ -265,8 +265,6 @@ export class TalkNormalChannel extends TypedEmitter<ChannelEvents> implements Ta
});

this._userInfoMap = userInfoMap;
} else if (result.mi) {
await this.getAllLatestUserInfo();
}
}

Expand Down Expand Up @@ -335,7 +333,22 @@ export class TalkNormalChannel extends TypedEmitter<ChannelEvents> implements Ta
const infoRes = await this.getLatestChannelInfo();
if (!infoRes.success) return infoRes;

return this.chatON();
const chatONRes = await this.chatON();
if (!chatONRes.success) return chatONRes;

if (chatONRes.result.mi) {
const userInitres = await initNormalUserList(this._channelSession, chatONRes.result.mi);

if (!userInitres.success) return userInitres;

const userInfoMap = new Map();
for (const info of userInitres.result) {
userInfoMap.set(info.userId.toString(), info);
}
this._userInfoMap = userInfoMap;
}

return chatONRes;
}

pushReceived(method: string, data: DefaultRes, parentCtx: EventContext<ChannelEvents>): void {
Expand Down
28 changes: 24 additions & 4 deletions src/talk/openlink/talk-open-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ import {
} from '../../packet/struct';
import { RelayEventType } from '../../relay';
import { ChannelUser, OpenChannelUserInfo } from '../../user';
import { ChannelInfoUpdater, sendMultiMedia, TalkChannel, TalkChannelHandler, TalkChannelSession } from '../channel';
import {
ChannelInfoUpdater,
initOpenUserList,
sendMultiMedia,
TalkChannel,
TalkChannelHandler,
TalkChannelSession
} from '../channel';
import { TalkOpenChannelSession } from './talk-open-channel-session';
import { OpenChannelEvents } from '../event';
import { Managed } from '../managed';
Expand Down Expand Up @@ -302,8 +309,6 @@ export class TalkOpenChannel
});

this._userInfoMap = userInfoMap;
} else if (result.mi) {
await this.getAllLatestUserInfo();
}

if (result.olu) {
Expand Down Expand Up @@ -472,7 +477,22 @@ export class TalkOpenChannel
const linkRes = await this.getLatestOpenLink();
if (!linkRes.success) return linkRes;

return this.chatON();
const chatONRes = await this.chatON();
if (!chatONRes.success) return chatONRes;

if (chatONRes.result.mi) {
const userInitres = await initOpenUserList(this._openChannelSession, chatONRes.result.mi);

if (!userInitres.success) return userInitres;

const userInfoMap = new Map();
for (const info of userInitres.result) {
userInfoMap.set(info.userId.toString(), info);
}
this._userInfoMap = userInfoMap;
}

return chatONRes;
}

// Called when broadcast packets are recevied.
Expand Down

0 comments on commit bd18590

Please sign in to comment.