[NNTPClient] Refactor getNewsGroupList method

This commit is contained in:
ChronosX88 2023-01-08 06:36:12 +03:00
parent bad5f439b6
commit 04f80c5833
Signed by: ChronosXYZ
GPG Key ID: 52A90DE5862D8321
2 changed files with 41 additions and 32 deletions

View File

@ -15,7 +15,7 @@ export class NNTPClient {
constructor(url: string) { constructor(url: string) {
this.ws = new WebsocketBuilder(url) this.ws = new WebsocketBuilder(url)
.onMessage((ins, evt) => { .onMessage((_, evt) => {
if ((evt.data as string).startsWith("201")) { if ((evt.data as string).startsWith("201")) {
console.debug("skipping welcome message"); console.debug("skipping welcome message");
} }
@ -52,7 +52,8 @@ export class NNTPClient {
private async sendCommand( private async sendCommand(
command: string, command: string,
args: string[] args: string[],
prepareResponse = false // remove unnecessary things in lines array
): Promise<CommandResponse> { ): Promise<CommandResponse> {
const cmd = { const cmd = {
request: { command: command, args: args } as CommandRequest, request: { command: command, args: args } as CommandRequest,
@ -67,43 +68,50 @@ export class NNTPClient {
} }
const result = await cmd.response.promise; const result = await cmd.response.promise;
if (prepareResponse) {
result.lines.shift();
result.lines.pop();
}
return result; return result;
} }
private async getNewsGroupList(): Promise<GroupInfo[]> { private completeGroupInfo(obj: Partial<GroupInfo>): GroupInfo {
const l: GroupInfo[] = []; return Object.assign(
{
name: "",
description: "",
lowWater: -1,
highWater: -1,
},
obj
);
}
public async getNewsGroupList(): Promise<GroupInfo[]> {
const groupMap: Record<string, Partial<GroupInfo>> = {}; const groupMap: Record<string, Partial<GroupInfo>> = {};
const [newsgroupsResponse, activeResponse]: CommandResponse[] =
await Promise.all([
this.sendCommand("LIST", ["NEWSGROUPS"], true),
this.sendCommand("LIST", ["ACTIVE"], true),
]);
await this.sendCommand("LIST", ["NEWSGROUPS"]).then((value) => { newsgroupsResponse.lines.forEach((val: string) => {
value.lines.shift(); const firstSpace = val.indexOf(" ");
value.lines.pop(); const name = val.substring(0, firstSpace);
value.lines.forEach((elem) => { groupMap[name] = { description: val.substring(firstSpace + 1) };
const firstSpace = elem.indexOf(" ");
const name = elem.substring(0, firstSpace);
groupMap[name] = { description: elem.substring(firstSpace + 1) };
});
}); });
await this.sendCommand("LIST", ["ACTIVE"]).then((value) => { activeResponse.lines.forEach((val: string) => {
value.lines.shift(); const splitted = val.split(" ");
value.lines.pop(); const [name, high, low]: string[] = splitted;
value.lines.forEach((elem) => {
const splitted = elem.split(" ");
const [name, high, low] = splitted;
groupMap[name].highWater = Number(high); groupMap[name].highWater = Number(high);
groupMap[name].lowWater = Number(low); groupMap[name].lowWater = Number(low);
}); });
});
Object.keys(groupMap).forEach((key) => { const result = Object.entries(groupMap).map(([, obj]) =>
l.push({ this.completeGroupInfo(obj)
name: groupMap[key].name!, );
description: groupMap[key].description!,
lowWater: groupMap[key].lowWater!,
highWater: groupMap[key].highWater!,
});
});
return l; return result;
} }
} }

View File

@ -5,12 +5,13 @@
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"@/*": ["./src/*"] "@/*": ["./src/*"]
} },
"lib": ["es2021"]
}, },
"references": [ "references": [
{ {
"path": "./tsconfig.vite-config.json" "path": "./tsconfig.vite-config.json"
} }
] ],
} }