@tgsnake/core
Version:
Pure Telegram MTProto library for nodejs
100 lines (99 loc) • 2.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecretChat = void 0;
const platform_node_js_1 = require("../platform.node.js");
const index_js_1 = require("../raw/index.js");
class SecretChat {
id;
accessHash;
rekeyStep;
rekeyExchange;
created;
changed;
isAdmin;
authKey;
mtproto;
layer;
inSeqNo;
outSeqNo;
inSeqNoX;
outSeqNoX;
adminId;
timeRekey;
ttl;
_mutex;
constructor({ id, accessHash, isAdmin, authKey, }) {
this.id = id;
this.accessHash = accessHash;
this.isAdmin = isAdmin;
this.authKey = platform_node_js_1.Buffer.from(authKey);
this.created = Date.now() / 1000;
this.changed = 0;
this.mtproto = 2;
this.layer = index_js_1.Raw.Layer;
this.ttl = 0;
this.timeRekey = 100;
this.outSeqNoX = isAdmin ? 1 : 0;
this.inSeqNoX = isAdmin ? 0 : 1;
this._mutex = new platform_node_js_1.Mutex();
}
async update(storage) {
const release = await this._mutex.acquire();
try {
storage.updateSecretChats([this]);
}
finally {
release();
}
return true;
}
static save(storage, params) {
const tempChat = new SecretChat(params);
storage.updateSecretChats([tempChat]);
return tempChat;
}
static async remove(storage, id) {
return await storage.removeSecretChatById(id);
}
get input() {
return new index_js_1.Raw.InputEncryptedChat({
chatId: this.id,
accessHash: this.accessHash,
});
}
[Symbol.for('nodejs.util.inspect.custom')]() {
const toPrint = {
_: this.constructor.name,
};
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
if (!key.startsWith('_') && value !== undefined && value !== null) {
toPrint[key] = value;
}
}
}
return toPrint;
}
[Symbol.for('Deno.customInspect')]() {
return String((0, platform_node_js_1.inspect)(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
}
toJSON() {
const toPrint = {
_: this.constructor.name,
};
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
if (!key.startsWith('_') && value !== undefined && value !== null) {
toPrint[key] = typeof value === 'bigint' ? String(value) : value;
}
}
}
return toPrint;
}
toString() {
return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
}
}
exports.SecretChat = SecretChat;