Преглед на файлове

fix getting entities from updates

painor преди 4 години
родител
ревизия
02c3690421
променени са 5 файла, в които са добавени 16 реда и са изтрити 14 реда
  1. 5 4
      gramjs/client/updates.ts
  2. 7 5
      gramjs/events/NewMessage.ts
  3. 3 3
      gramjs/events/common.ts
  4. 0 1
      gramjs/tl/custom/message.ts
  5. 1 1
      package.json

+ 5 - 4
gramjs/client/updates.ts

@@ -5,6 +5,7 @@ import type {TelegramClient} from "../";
 import bigInt from 'big-integer';
 import {UpdateConnectionState} from "../network";
 import type {Raw} from "../events";
+import {utils} from "../index";
 
 // export class UpdateMethods {
 export function on(client: TelegramClient, event: any) {
@@ -51,9 +52,9 @@ export function _handleUpdate(client: TelegramClient, update: Api.TypeUpdate | n
 
     if (update instanceof Api.Updates || update instanceof Api.UpdatesCombined) {
         // TODO deal with entities
-        const entities = []
+        const entities = new Map();
         for (const x of [...update.users, ...update.chats]) {
-            entities.push(x)
+            entities.set(utils.getPeerId(x),x);
         }
         for (const u of update.updates) {
             client._processUpdate(u, update.updates, entities)
@@ -66,11 +67,11 @@ export function _handleUpdate(client: TelegramClient, update: Api.TypeUpdate | n
 }
 
 export function _processUpdate(client: TelegramClient, update: any, others: any, entities?: any) {
-    update._entities = entities || {};
+    update._entities = entities || new Map();
     const args = {
         update: update,
         others: others,
-    }
+    };
 
     client._dispatchUpdate(args)
 }

+ 7 - 5
gramjs/events/NewMessage.ts

@@ -1,9 +1,8 @@
 import {_intoIdSet, EventBuilder, EventCommon} from "./common";
-import type {EntityLike} from "../define";
+import type {Entity, EntityLike} from "../define";
 import type {TelegramClient} from "../client/TelegramClient";
 import {Api} from "../tl";
 import {Message} from "../tl/patched";
-import {Mixin} from 'ts-mixer';
 
 interface NewMessageInterface {
     chats?: EntityLike[],
@@ -130,7 +129,7 @@ export class NewMessage extends EventBuilder {
 
 export class NewMessageEvent extends EventCommon {
     message: Message;
-    originalUpdate: Api.TypeUpdate;
+    originalUpdate: Api.TypeUpdate & { _entities?: Map<number, Entity> };
 
     constructor(message: Message, originalUpdate: Api.TypeUpdate) {
         super({
@@ -147,9 +146,12 @@ export class NewMessageEvent extends EventCommon {
         const m = this.message;
         try {
             // todo make sure this never fails
-            m._finishInit(client, this.message._entities, undefined);
+            m._finishInit(client, this.originalUpdate._entities || new Map(), undefined);
         } catch (e) {
-
+            client._log.error("Got error while trying to finish init message with id " + m.id);
+            if (client._log.canSend('error')){
+                console.log(e);
+            }
         }
     }
 }

+ 3 - 3
gramjs/events/common.ts

@@ -1,5 +1,5 @@
 import {Api} from "../tl";
-import type {EntityLike} from "../define";
+import type {Entity, EntityLike} from "../define";
 import {ChatGetter} from "../tl/custom";
 import type {TelegramClient} from "../client/TelegramClient";
 
@@ -107,12 +107,12 @@ interface EventCommonInterface {
 
 export class EventCommon extends ChatGetter {
     _eventName = "Event";
-    _entities: any;
+    _entities: Map<number, Entity>;
     _messageId?: number;
 
     constructor({chatPeer = undefined, msgId = undefined, broadcast = undefined}: EventCommonInterface) {
         super({chatPeer, broadcast});
-        this._entities = {};
+        this._entities = new Map();
         this._client = undefined;
         this._messageId = msgId;
     }

+ 0 - 1
gramjs/tl/custom/message.ts

@@ -171,7 +171,6 @@ export class Message extends Mixin(SenderGetter, ChatGetter) {
     _finishInit(client: TelegramClient, entities: Map<number, Entity>, inputChat?: EntityLike) {
         this._client = client;
         const cache = client._entityCache;
-
         if (this.senderId) {
             [this._sender, this._inputSender] = utils._getEntityPair(this.senderId, entities, cache);
         }

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "telegram",
-  "version": "1.5.23",
+  "version": "1.5.24",
   "description": "NodeJS MTProto API Telegram client library,",
   "main": "index.js",
   "types": "index.d.ts",