@tgsnake/core
Version:
Pure Telegram MTProto library for nodejs
133 lines (132 loc) • 5.28 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadSession = loadSession;
exports.connect = connect;
exports.start = start;
exports.logout = logout;
exports.exportSession = exportSession;
exports.invoke = invoke;
const index_js_1 = require("../raw/index.js");
const index_js_2 = require("../session/index.js");
const Logger_js_1 = require("../Logger.js");
const Errors = __importStar(require("../errors/index.js"));
const _Auth = __importStar(require("./Auth.js"));
const Version = __importStar(require("../Version.node.js"));
const platform_node_js_1 = require("../platform.node.js");
async function loadSession() {
await this._storage.load();
if (!this._storage.authKey) {
const [ip, port] = await index_js_2.DataCenter.DataCenter(this._defaultDcId, this._testMode, this._ipv6, false);
const auth = new index_js_2.Auth(this._defaultDcId, this._testMode, this._ipv6);
this._storage.setAddress(this._defaultDcId, ip, port, this._testMode);
this._storage.setApiId(this._apiId);
this._storage.setAuthKey(await auth.create(), this._defaultDcId);
}
if (!this._storage.apiId) {
this._storage.setApiId(this._apiId);
}
if (this._storage.testMode === undefined) {
this._storage.setAddress(this._storage.dcId, this._storage.ip, this._storage.port, this._testMode);
}
}
async function connect() {
if (!this._isConnected) {
Logger_js_1.Logger.info(`[100] Using version: ${Version.version} - ${Version.getType()}`);
await loadSession.call(this);
this._session = new index_js_2.Session(this, this._storage.dcId, this._storage.authKey, this._storage.testMode, this._proxy, false, this._isCdn);
await this._session.start();
this._isConnected = true;
}
}
async function start(auth) {
await connect.call(this);
if (this._storage.userId === undefined) {
if (auth) {
if (auth.botToken) {
await _Auth.siginBot.call(this, await auth.botToken);
}
else {
await _Auth.siginUser.call(this, { ...auth });
}
}
}
if (!this._storage.authKey) {
throw new Errors.ClientError.AuthKeyMissing();
}
if (!this._storage.isBot && this._takeout) {
const takeout = await this.invoke(new index_js_1.Raw.account.InitTakeoutSession({}));
this._takeoutId = takeout.id;
Logger_js_1.Logger.warning(`[104] Takeout session ${this._takeoutId} initiated.`);
}
await this.invoke(new index_js_1.Raw.updates.GetState());
const me = await _Auth.getMe.call(this);
this._me = me;
Logger_js_1.Logger.log(`[161] Logined as (${me.fullUser.id})`);
return me;
}
async function logout() {
await this.invoke(new index_js_1.Raw.auth.LogOut());
await this._storage.delete();
Logger_js_1.Logger.info(`[105] Logged out.`);
return platform_node_js_1.sysprc.exit(0);
}
async function exportSession() {
if (!this._storage.userId) {
const me = this._me ?? (await _Auth.getMe.call(this));
this._storage.setUserId(me.fullUser.id);
this._storage.setIsBot(Boolean(me.users[0].bot));
}
return this._storage.exportString();
}
async function invoke(query, retries, timeout, sleepTreshold) {
if (!this._isConnected) {
throw new Errors.ClientError.ClientDisconnected();
}
if (this._noUpdates) {
query = new index_js_1.Raw.InvokeWithoutUpdates({ query });
}
if (this._takeoutId) {
query = new index_js_1.Raw.InvokeWithTakeout({ query, takeoutId: this._takeoutId });
}
const r = await this._session.invoke(query, retries, timeout, sleepTreshold);
if (typeof r === 'object' && 'users' in r) {
await this.fetchPeers(r.users);
}
if (typeof r === 'object' && 'chats' in r) {
await this.fetchPeers(r.chats);
}
return r;
}