Skip to content

Commit

Permalink
Unified code style
Browse files Browse the repository at this point in the history
  • Loading branch information
1395173231 committed May 30, 2023
1 parent a9476be commit 19c6dc7
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions src/ws.message.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import WebSocket from "isomorphic-ws";
import {
DefaultMessageConfig,
LoadingHandler,
MessageConfig,
MessageConfigParam,
MJMessage,
DefaultMessageConfig,
WaitMjEvent,
MJMessage,
LoadingHandler,
WsEventMsg,
} from "./interfaces";
import { VerifyHuman } from "./verify.human";
Expand Down Expand Up @@ -55,12 +55,11 @@ export class WsMessage {
JSON.stringify({
op: 1,
d: this.heartbeatInterval,
}),
})
);
await this.timeout(1000 * 40);
this.heartbeat(num);
}

// After opening ws
private async open() {
const num = this.reconnectTime.length;
Expand All @@ -76,7 +75,6 @@ export class WsMessage {
this.heartbeat(num);
}, 1000 * 10);
}

// auth
private auth() {
this.ws.send(
Expand All @@ -92,14 +90,12 @@ export class WsMessage {
},
compress: false,
},
}),
})
);
}

async timeout(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

private incomingMessage(data: Buffer) {
this.parseMessage(data);
}
Expand Down Expand Up @@ -143,11 +139,9 @@ export class WsMessage {
}
this.processingImage(message);
}

private messageUpdate(message: any) {
this.processingImage(message);
}

private processingImage(message: any) {
const { content, id, nonce, attachments } = message;
const event = this.getEventById(id);
Expand Down Expand Up @@ -181,6 +175,7 @@ export class WsMessage {
return;
}
if (!(msg.t === "MESSAGE_CREATE" || msg.t === "MESSAGE_UPDATE")) return;

const message = msg.d;
const {
channel_id,
Expand All @@ -194,7 +189,6 @@ export class WsMessage {
} = message;
if (!(author && author.id === this.MJBotId)) return;
if (channel_id !== this.config.ChannelId) return;

this.log("has message", content, nonce, id);

if (msg.t === "MESSAGE_CREATE") {
Expand All @@ -206,7 +200,6 @@ export class WsMessage {
return;
}
}

private async verifyHuman(message: any) {
const { HuggingFaceToken } = this.config;
if (HuggingFaceToken === "" || !HuggingFaceToken) {
Expand All @@ -221,18 +214,17 @@ export class WsMessage {
const category = await verifyClient.verify(uri, classify);
if (category) {
const custom_id = categories.find(
(c: any) => c.label === category,
(c: any) => c.label === category
).custom_id;
const httpStatus = await this.verifyHumanApi(custom_id, message.id);
this.log("verifyHumanApi", httpStatus, custom_id, message.id);
// this.log("verify success", category);
}
}

private async verifyHumanApi(
custom_id: string,
message_id: string,
nonce?: string,
nonce?: string
) {
const payload = {
type: 3,
Expand All @@ -250,10 +242,9 @@ export class WsMessage {
};
return this.interactions(payload);
}

protected async interactions(
payload: any,
callback?: (result: number) => void,
callback?: (result: number) => void
) {
try {
const headers = {
Expand All @@ -279,7 +270,6 @@ export class WsMessage {
callback && callback(500);
}
}

private EventError(id: string, error: Error) {
const event = this.getEventById(id);
if (!event) {
Expand Down Expand Up @@ -337,7 +327,6 @@ export class WsMessage {
};
this.emitImage(event.nonce, eventMsg);
}

private getEventByContent(content: string) {
const prompt = this.content2prompt(content);
for (const [key, value] of this.waitMjEvents.entries()) {
Expand All @@ -354,15 +343,13 @@ export class WsMessage {
}
}
}

private updateMjEventIdByNonce(id: string, nonce: string) {
if (nonce === "" || id === "") return;
let event = this.waitMjEvents.get(nonce);
if (!event) return;
event.id = id;
this.log("updateMjEventIdByNonce success", this.waitMjEvents.get(nonce));
}

uriToHash(uri: string) {
return uri.split("_").pop()?.split(".")[0] ?? "";
}
Expand All @@ -380,45 +367,38 @@ export class WsMessage {
on(event: string, callback: (message: any) => void) {
this.event.push({ event, callback });
}

once(event: string, callback: (message: any) => void) {
const once = (message: any) => {
this.remove(event, once);
callback(message);
};
this.event.push({ event, callback: once });
}

remove(event: string, callback: (message: any) => void) {
this.event = this.event.filter(
(e) => e.event !== event && e.callback !== callback,
(e) => e.event !== event && e.callback !== callback
);
}

removeEvent(event: string) {
this.event = this.event.filter((e) => e.event !== event);
}

onceInfo(callback: (message: any) => void) {
const once = (message: any) => {
this.remove("info", once);
callback(message);
};
this.event.push({ event: "info", callback: once });
}

removeInfo(callback: (message: any) => void) {
this.remove("info", callback);
}

private removeWaitMjEvent(nonce: string) {
this.waitMjEvents.delete(nonce);
}

private emitImage(type: string, message: WsEventMsg) {
this.emit(type, message);
}

onceImage(nonce: string, callback: (data: WsEventMsg) => void) {
const once = (data: WsEventMsg) => {
const { message, error } = data;
Expand Down Expand Up @@ -451,4 +431,4 @@ export class WsMessage {
});
});
}
}
}

0 comments on commit 19c6dc7

Please sign in to comment.