Browse Source

Fix senderId when sending a message

painor 3 years ago
parent
commit
99b3a35307

+ 1 - 1
gramjs/Version.ts

@@ -1 +1 @@
-export const version = "2.0.10";
+export const version = "2.0.11";

+ 3 - 0
gramjs/client/dialogs.ts

@@ -77,6 +77,9 @@ export class _DialogsIter extends RequestIter {
         this.offsetDate = offsetDate;
         this.ignoreMigrated = ignoreMigrated;
     }
+    [Symbol.asyncIterator](): AsyncIterator<Dialog, any, undefined> {
+        return super[Symbol.asyncIterator]();
+    }
 
     async _loadNextChunk(): Promise<boolean | undefined> {
         if (!this.request || !this.seen || !this.buffer) {

+ 8 - 2
gramjs/client/updates.ts

@@ -5,7 +5,7 @@ import bigInt from "big-integer";
 import { UpdateConnectionState } from "../network";
 import type { Raw } from "../events";
 import { utils } from "../index";
-import { getRandomInt, sleep } from "../Helpers";
+import { getRandomInt, returnBigInt, sleep } from "../Helpers";
 
 const PING_INTERVAL = 9000; // 9 sec
 const PING_TIMEOUT = 10000; // 10 sec
@@ -132,7 +132,13 @@ export async function _dispatchUpdate(
                 // TODO fix me
             }
             // TODO fix others not being passed
-            event = builder.build(event, undefined, callback);
+            event = builder.build(
+                event,
+                callback,
+                client._selfInputPeer
+                    ? returnBigInt(client._selfInputPeer.userId)
+                    : undefined
+            );
             if (event) {
                 if ("_eventName" in event) {
                     event._setClient(client);

+ 2 - 2
gramjs/client/users.ts

@@ -243,7 +243,7 @@ export async function getInputEntity(
             typeof peer === "bigint" ||
             bigInt.isInstance(peer)
         ) {
-            const res = client._entityCache.get(peer);
+            const res = client._entityCache.get(peer.toString());
             if (res) {
                 return res;
             }
@@ -254,7 +254,7 @@ export async function getInputEntity(
             !bigInt.isInstance(peer) &&
             peer.SUBCLASS_OF_ID === 0x2d45687
         ) {
-            const res = client._entityCache.get(peer);
+            const res = client._entityCache.get(utils.getPeerId(peer));
             if (res) {
                 return res;
             }

+ 8 - 3
gramjs/entityCache.ts

@@ -1,8 +1,9 @@
 // Which updates have the following fields?
 
 import { getInputPeer, getPeerId } from "./Utils";
-import { isArrayLike } from "./Helpers";
+import { isArrayLike, returnBigInt } from "./Helpers";
 import { Api } from "./tl";
+import bigInt from "big-integer";
 
 export class EntityCache {
     private cacheMap: Map<string, any>;
@@ -43,8 +44,12 @@ export class EntityCache {
         }
     }
 
-    get(item: any) {
-        if (!(typeof item === "number") || item < 0) {
+    get(item: bigInt.BigInteger | string | undefined) {
+        if (item == undefined) {
+            throw new Error("No cached entity for the given key");
+        }
+        item = returnBigInt(item);
+        if (item.lesser(bigInt.zero)) {
             let res;
             try {
                 res = this.cacheMap.get(getPeerId(item).toString());

+ 1 - 5
gramjs/events/Album.ts

@@ -30,11 +30,7 @@ export class Album extends EventBuilder {
         super({ chats, blacklistChats, func });
     }
 
-    build(
-        update: Api.TypeUpdate,
-        others: any = null,
-        dispatch?: CallableFunction
-    ): any {
+    build(update: Api.TypeUpdate, dispatch?: CallableFunction): any {
         if (!("message" in update && update.message instanceof Api.Message)) {
             return;
         }

+ 5 - 1
gramjs/events/CallbackQuery.ts

@@ -51,7 +51,11 @@ export class CallbackQuery extends EventBuilder {
         );
     }
 
-    build(update: Api.TypeUpdate | Api.TypeUpdates, others: any = null) {
+    build(
+        update: Api.TypeUpdate | Api.TypeUpdates,
+        callback: undefined,
+        selfId = undefined
+    ) {
         if (update instanceof Api.UpdateBotCallbackQuery) {
             return new CallbackQueryEvent(update, update.peer, update.msgId);
         } else if (update instanceof Api.UpdateInlineBotCallbackQuery) {

+ 14 - 5
gramjs/events/NewMessage.ts

@@ -7,6 +7,7 @@ import {
 import type { Entity, EntityLike } from "../define";
 import type { TelegramClient } from "..";
 import { Api } from "../tl";
+import bigInt from "big-integer";
 
 export interface NewMessageInterface extends DefaultEventInterface {
     func?: { (event: NewMessageEvent): boolean };
@@ -121,7 +122,11 @@ export class NewMessage extends EventBuilder {
         this.fromUsers = await _intoIdSet(client, this.fromUsers);
     }
 
-    build(update: Api.TypeUpdate | Api.TypeUpdates, others: any = null) {
+    build(
+        update: Api.TypeUpdate | Api.TypeUpdates,
+        callback: undefined,
+        selfId: bigInt.BigInteger
+    ) {
         if (
             update instanceof Api.UpdateNewMessage ||
             update instanceof Api.UpdateNewChannelMessage
@@ -141,14 +146,16 @@ export class NewMessage extends EventBuilder {
                     silent: update.silent,
                     id: update.id,
                     peerId: new Api.PeerUser({ userId: update.userId }),
-                    fromId: new Api.PeerUser({ userId: update.userId }),
+                    fromId: new Api.PeerUser({
+                        userId: update.out ? selfId : update.userId,
+                    }),
                     message: update.message,
                     date: update.date,
                     fwdFrom: update.fwdFrom,
                     viaBotId: update.viaBotId,
                     replyTo: update.replyTo,
                     entities: update.entities,
-                    // ttlPeriod:update.ttlPeriod
+                    ttlPeriod: update.ttlPeriod,
                 }),
                 update
             );
@@ -161,14 +168,16 @@ export class NewMessage extends EventBuilder {
                     silent: update.silent,
                     id: update.id,
                     peerId: new Api.PeerChat({ chatId: update.chatId }),
-                    fromId: new Api.PeerUser({ userId: update.fromId }),
+                    fromId: new Api.PeerUser({
+                        userId: update.out ? selfId : update.fromId,
+                    }),
                     message: update.message,
                     date: update.date,
                     fwdFrom: update.fwdFrom,
                     viaBotId: update.viaBotId,
                     replyTo: update.replyTo,
                     entities: update.entities,
-                    // ttlPeriod:update.ttlPeriod
+                    ttlPeriod: update.ttlPeriod,
                 }),
                 update
             );

+ 1 - 1
gramjs/events/Raw.ts

@@ -34,7 +34,7 @@ export class Raw extends EventBuilder {
         this.resolved = true;
     }
 
-    build(update: Api.TypeUpdate, others: any = null): Api.TypeUpdate {
+    build(update: Api.TypeUpdate): Api.TypeUpdate {
         return update;
     }
 

+ 3 - 2
gramjs/events/common.ts

@@ -110,8 +110,8 @@ export class EventBuilder {
 
     build(
         update: Api.TypeUpdate,
-        others?: any,
-        callback?: CallableFunction
+        callback?: CallableFunction,
+        selfId?: bigInt.BigInteger
     ): any {
         if (update) return update;
     }
@@ -183,6 +183,7 @@ export class EventCommon extends ChatGetter {
         return this._client;
     }
 }
+
 export class EventCommonSender extends SenderGetter {
     _eventName = "Event";
     _entities: Map<string, Entity>;

+ 4 - 3
gramjs/tl/custom/chatGetter.ts

@@ -4,7 +4,6 @@ import { utils } from "../../";
 import { Api } from "../api";
 import { inspect } from "util";
 import { betterConsoleLog, returnBigInt } from "../../Helpers";
-import bigInt from "big-integer";
 
 export interface ChatGetterConstructorParams {
     chatPeer?: EntityLike;
@@ -58,7 +57,9 @@ export class ChatGetter {
     get inputChat() {
         if (!this._inputChat && this._chatPeer && this._client) {
             try {
-                this._inputChat = this._client._entityCache.get(this._chatPeer);
+                this._inputChat = this._client._entityCache.get(
+                    utils.getPeerId(this._chatPeer)
+                );
             } catch (e) {}
         }
         return this._inputChat;
@@ -71,7 +72,7 @@ export class ChatGetter {
                 for await (const dialog of this._client.iterDialogs({
                     limit: 100,
                 })) {
-                    if (dialog.id === target) {
+                    if (dialog.id!.eq(target!)) {
                         this._chat = dialog.entity;
                         this._inputChat = dialog.inputEntity;
                         break;

+ 1 - 1
package.json

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