/
Memory.ts
310 lines (279 loc) · 8.76 KB
/
Memory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { Session } from "./Abstract";
import type { AuthKey } from "../crypto/AuthKey";
import { Api } from "../tl";
import bigInt from "big-integer";
import { getDisplayName, getInputPeer, getPeerId } from "../Utils";
import { isArrayLike, returnBigInt } from "../Helpers";
import { utils } from "../";
import type { EntityLike } from "../define";
export class MemorySession extends Session {
protected _serverAddress?: string;
protected _dcId: number;
protected _port?: number;
protected _takeoutId: undefined;
protected _entities: Set<any>;
protected _updateStates: {};
protected _authKey?: AuthKey;
constructor() {
super();
this._serverAddress = undefined;
this._dcId = 0;
this._port = undefined;
this._takeoutId = undefined;
this._entities = new Set();
this._updateStates = {};
}
setDC(dcId: number, serverAddress: string, port: number) {
this._dcId = dcId | 0;
this._serverAddress = serverAddress;
this._port = port;
}
get dcId() {
return this._dcId;
}
get serverAddress() {
return this._serverAddress!;
}
get port() {
return this._port!;
}
get authKey() {
return this._authKey;
}
set authKey(value) {
this._authKey = value;
}
get takeoutId() {
return this._takeoutId;
}
set takeoutId(value) {
this._takeoutId = value;
}
getAuthKey(dcId?: number) {
if (dcId && dcId !== this.dcId) {
// Not supported.
return undefined;
}
return this.authKey;
}
setAuthKey(authKey?: AuthKey, dcId?: number) {
if (dcId && dcId !== this.dcId) {
// Not supported.
return undefined;
}
this.authKey = authKey;
}
close() {}
save() {}
async load() {}
delete() {}
_entityValuesToRow(
id: bigInt.BigInteger | string,
hash: bigInt.BigInteger | string,
username: string,
phone: string,
name: string
) {
// While this is a simple implementation it might be overrode by,
// other classes so they don't need to implement the plural form
// of the method. Don't remove.
return [id, hash, username, phone, name];
}
_entityToRow(e: any) {
if (!(e.classType === "constructor")) {
return;
}
let p;
let markedId;
try {
p = getInputPeer(e, false);
markedId = getPeerId(p);
} catch (e) {
return;
}
let pHash;
if (
p instanceof Api.InputPeerUser ||
p instanceof Api.InputPeerChannel
) {
pHash = p.accessHash;
} else if (p instanceof Api.InputPeerChat) {
pHash = bigInt.zero;
} else {
return;
}
let username = e.username;
if (username) {
username = username.toLowerCase();
}
const phone = e.phone;
const name = getDisplayName(e);
return this._entityValuesToRow(markedId, pHash, username, phone, name);
}
_entitiesToRows(tlo: any) {
let entities: any = [];
if (!(tlo.classType === "constructor") && isArrayLike(tlo)) {
// This may be a list of users already for instance
entities = tlo;
} else {
if (typeof tlo === "object") {
if ("user" in tlo) {
entities.push(tlo.user);
}
if ("chat" in tlo) {
entities.push(tlo.chat);
}
if ("channel" in tlo) {
entities.push(tlo.channel);
}
if ("chats" in tlo && isArrayLike(tlo.chats)) {
entities = entities.concat(tlo.chats);
}
if ("users" in tlo && isArrayLike(tlo.users)) {
entities = entities.concat(tlo.users);
}
}
}
const rows = []; // Rows to add (id, hash, username, phone, name)
for (const e of entities) {
const row = this._entityToRow(e);
if (row) {
rows.push(row);
}
}
return rows;
}
processEntities(tlo: any) {
const entitiesSet = this._entitiesToRows(tlo);
for (const e of entitiesSet) {
this._entities.add(e);
}
}
getEntityRowsByPhone(phone: string) {
for (const e of this._entities) {
// id, hash, username, phone, name
if (e[3] === phone) {
return [e[0], e[1]];
}
}
}
getEntityRowsByUsername(username: string) {
for (const e of this._entities) {
// id, hash, username, phone, name
if (e[2] === username) {
return [e[0], e[1]];
}
}
}
getEntityRowsByName(name: string) {
for (const e of this._entities) {
// id, hash, username, phone, name
if (e[4] === name) {
return [e[0], e[1]];
}
}
}
getEntityRowsById(id: string | bigInt.BigInteger, exact = true) {
if (exact) {
for (const e of this._entities) {
// id, hash, username, phone, name
if (e[0] === id) {
return [e[0], e[1]];
}
}
} else {
const ids = [
utils.getPeerId(new Api.PeerUser({ userId: returnBigInt(id) })),
utils.getPeerId(new Api.PeerChat({ chatId: returnBigInt(id) })),
utils.getPeerId(
new Api.PeerChannel({ channelId: returnBigInt(id) })
),
];
for (const e of this._entities) {
// id, hash, username, phone, name
if (ids.includes(e[0])) {
return [e[0], e[1]];
}
}
}
}
getInputEntity(key: EntityLike): Api.TypeInputPeer {
let exact;
if (
typeof key === "object" &&
!bigInt.isInstance(key) &&
key.SUBCLASS_OF_ID
) {
if (
key.SUBCLASS_OF_ID == 0xc91c90b6 ||
key.SUBCLASS_OF_ID == 0xe669bf46 ||
key.SUBCLASS_OF_ID == 0x40f202fd
) {
// @ts-ignore
return key;
}
// Try to early return if this key can be casted as input peer
return utils.getInputPeer(key);
} else {
// Not a TLObject or can't be cast into InputPeer
if (typeof key === "object") {
key = utils.getPeerId(key);
exact = true;
} else {
exact = false;
}
}
if (
bigInt.isInstance(key) ||
typeof key == "bigint" ||
typeof key == "number"
) {
key = key.toString();
}
let result = undefined;
if (typeof key === "string") {
const phone = utils.parsePhone(key);
if (phone) {
result = this.getEntityRowsByPhone(phone);
} else {
const { username, isInvite } = utils.parseUsername(key);
if (username && !isInvite) {
result = this.getEntityRowsByUsername(username);
}
}
if (!result) {
const id = utils.parseID(key);
if (id) {
result = this.getEntityRowsById(id, exact);
}
}
if (!result) {
result = this.getEntityRowsByName(key);
}
}
if (result) {
let entityId = result[0]; // unpack resulting tuple
const entityHash = bigInt(result[1]);
const resolved = utils.resolveId(returnBigInt(entityId));
entityId = resolved[0];
const kind = resolved[1];
// removes the mark and returns type of entity
if (kind === Api.PeerUser) {
return new Api.InputPeerUser({
userId: entityId,
accessHash: entityHash,
});
} else if (kind === Api.PeerChat) {
return new Api.InputPeerChat({ chatId: entityId });
} else if (kind === Api.PeerChannel) {
return new Api.InputPeerChannel({
channelId: entityId,
accessHash: entityHash,
});
}
} else {
throw new Error("Could not find input entity with key " + key);
}
throw new Error("Could not find input entity with key " + key);
}
}