Skip to content

Commit 4bf24c5

Browse files
committed
Улучшения
1 parent 09fe4fa commit 4bf24c5

4 files changed

Lines changed: 168 additions & 7 deletions

File tree

src/core.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var { EventEmitter } = require("events");
44
var util = require("./util.js");
55
var { Opcode } = require("./enums.js");
66
var { InvalidPhoneError, AppOldError, FailedToLoginError, InvalidPayloadError, InvalidQRDataError, QRExpiredError } = require("./errors.js");
7-
var { Dialog, Chat, Channel, User, Me } = require("./types.js");
7+
var { Dialog, Chat, Channel, User, Me, Message } = require("./types.js");
88
var chalk = require("chalk");
99
var WebSocket = require("ws");
1010
var UserAgent = require("user-agents");
@@ -241,6 +241,47 @@ class BaseClient extends EventEmitter {
241241
}
242242
this._log("info", `Sync completed: dialogs=${this.dialogs.length} chats=${this.chats.length} channels=${this.channels.length}`);
243243
}
244+
async _handlePacket(packet) {
245+
var { payload } = packet;
246+
if (packet.opcode == Opcode.NOTIF_MESSAGE) {
247+
var msg = new Message(payload);
248+
if (msg.chatId && msg.id) {
249+
this._send(Opcode.NOTIF_MESSAGE, {
250+
"chatId": msg.chatId,
251+
"messageId": msg.id.toString()
252+
}, 1);
253+
this._log("debug", `Sent NOTIF_MESSAGE_RECEIVED for chat_id=${msg.chatId} message_id=${msg.id}`);
254+
}
255+
if (payload.status == "EDITED") {
256+
this.emit("messageEdit", msg);
257+
} else if (payload.status == "REMOVED") {
258+
this.emit("messageDelete", msg);
259+
} else {
260+
this.emit("message", msg);
261+
}
262+
}
263+
}
264+
sendMessage(chatId, data) {
265+
if (typeof data === "string") {
266+
data = {
267+
"text": data
268+
};
269+
}
270+
if (typeof data.notify === "undefined") {
271+
data.notify = true;
272+
}
273+
this._log("info", `Sending message to chat_id=${chatId} notify=${data.notify}`);
274+
this._send(Opcode.MSG_SEND, {
275+
chatId,
276+
"message": {
277+
"text": data.text,
278+
"cid": -Date.now(),
279+
"elements": [],
280+
"attaches": []
281+
},
282+
"notify": data.notify
283+
});
284+
}
244285
}
245286

246287
class MaxClient extends BaseClient {
@@ -269,6 +310,9 @@ class MaxClient extends BaseClient {
269310
setTimeout(() => this.start(), this.reconnectDelay * 1000);
270311
}
271312
});
313+
this._connection.on("message", data => {
314+
this._handlePacket(JSON.parse(data.toString("utf-8")));
315+
});
272316
return new Promise(res => {
273317
this._connection.on("open", () => {
274318
this.isConnected = true;

src/index.d.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,41 @@ declare namespace NodeMax {
241241
})
242242
names: Name[]
243243
}
244-
class Message {}
244+
class Message {
245+
constructor(data: {
246+
public chatId: int | null
247+
public sender: int | null
248+
elements: {
249+
type: "EMPHASIZED" | "STRIKETHROUGH" | "STRONG" | "UNDERLINE"
250+
length: int
251+
from: int | null
252+
}[]
253+
reactionInfo: {
254+
totalCount: int
255+
counters: {
256+
count: int
257+
reaction: string
258+
}[]
259+
yourReaction: string | null
260+
}
261+
public options: int | null
262+
public id: int
263+
public time: int
264+
link: {
265+
public chatId: int
266+
message: {}
267+
public type: string
268+
}
269+
public text: string
270+
public status: "EDITED" | "REMOVED" | null
271+
public type: "SERVICE" | "SYSTEM" | "TEXT"
272+
attaches: {}[]
273+
})
274+
elements: Element[]
275+
reactionInfo: ReactionInfo
276+
link: MessageLink
277+
attaches: {}[]
278+
}
245279
class Name {
246280
constructor(data: {
247281
public name: string
@@ -250,6 +284,38 @@ declare namespace NodeMax {
250284
public type: string
251285
})
252286
}
287+
class Element {
288+
constructor(data: {
289+
public type: "EMPHASIZED" | "STRIKETHROUGH" | "STRONG" | "UNDERLINE"
290+
public length: int
291+
public from: int | null
292+
})
293+
}
294+
class MessageLink {
295+
constructor(data: {
296+
public chatId: int
297+
message: {}
298+
public type: string
299+
})
300+
message: Message
301+
}
302+
class ReactionInfo {
303+
constructor(data: {
304+
public totalCount: int
305+
counters: {
306+
count: int
307+
reaction: string
308+
}[]
309+
public yourReaction: string | null
310+
})
311+
counters: ReactionCounter[]
312+
}
313+
class ReactionCounter {
314+
constructor(data: {
315+
public count: int
316+
public reaction: string
317+
})
318+
}
253319
}
254320

255321
export = NodeMax;

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { MaxClient, SocketMaxClient } = require("./core.js");
22
const { InvalidPhoneError, AppOldError, FailedToLoginError, InvalidPayloadError, InvalidQRDataError, QRExpiredError } = require("./errors.js");
33
const { Opcode } = require("./enums.js");
4-
const { Dialog, Chat, Channel, User, Me } = require("./types.js");
4+
const { Dialog, Chat, Channel, User, Me, Message, Name, Element, MessageLink, ReactionInfo, ReactionCounter } = require("./types.js");
55

6-
module.exports = { MaxClient, SocketMaxClient, InvalidPhoneError, AppOldError, FailedToLoginError, InvalidPayloadError, InvalidQRDataError, QRExpiredError, Opcode, Dialog, Chat, Channel, User, Me };
6+
module.exports = { MaxClient, SocketMaxClient, InvalidPhoneError, AppOldError, FailedToLoginError, InvalidPayloadError, InvalidQRDataError, QRExpiredError, Opcode, Dialog, Chat, Channel, User, Me, Message, Name, Element, MessageLink, ReactionInfo, ReactionCounter };

src/types.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,28 @@ class Me {
5656
}
5757
}
5858

59-
// TODO
60-
class Message {}
59+
class Message {
60+
constructor(data) {
61+
if (data.id) {
62+
data = {
63+
"message": data
64+
};
65+
}
66+
this.chatId = data.chatId;
67+
this.sender = data.message.sender;
68+
this.elements = (data.elements || []).map(element => new Element(element));
69+
this.options = data.message.options;
70+
this.id = data.message.id;
71+
this.time = data.message.time;
72+
this.text = data.message.text;
73+
this.type = data.message.type;
74+
// TODO
75+
this.attaches = [];
76+
this.status = data.message.status;
77+
this.link = (data.message.link ? new MessageLink(data.message.link) : null);
78+
this.reactionInfo = (data.message.reactionInfo ? new ReactionInfo(data.message.reactionInfo) : null);
79+
}
80+
}
6181

6282
class Name {
6383
constructor(data) {
@@ -68,4 +88,35 @@ class Name {
6888
}
6989
}
7090

71-
module.exports = { Dialog, Chat, Channel, User, Me, Message, Name };
91+
class Element {
92+
constructor(data) {
93+
this.type = data.type;
94+
this.length = data.length;
95+
this.from = data.from;
96+
}
97+
}
98+
99+
class MessageLink {
100+
constructor(data) {
101+
this.chatId = data.chatId;
102+
this.message = (data.message ? new Message(data.message) : null);
103+
this.type = data.type;
104+
}
105+
}
106+
107+
class ReactionInfo {
108+
constructor(data) {
109+
this.totalCount = data.totalCount;
110+
this.counters = (data.counters || []).map(counter => new ReactionCounter(counter));
111+
this.yourReaction = data.yourReaction;
112+
}
113+
}
114+
115+
class ReactionCounter {
116+
constructor(data) {
117+
this.count = data.count;
118+
this.reaction = data.reaction;
119+
}
120+
}
121+
122+
module.exports = { Dialog, Chat, Channel, User, Me, Message, Name, Element, MessageLink, ReactionInfo, ReactionCounter };

0 commit comments

Comments
 (0)