Selaa lähdekoodia

remove unnecessary methods from string and store sessions

painor 4 vuotta sitten
vanhempi
commit
0485a21053
4 muutettua tiedostoa jossa 183 lisäystä ja 231 poistoa
  1. 150 172
      gramjs/sessions/Abstract.ts
  2. 33 24
      gramjs/sessions/Memory.ts
  3. 0 18
      gramjs/sessions/StoreSession.ts
  4. 0 17
      gramjs/sessions/StringSession.ts

+ 150 - 172
gramjs/sessions/Abstract.ts

@@ -4,176 +4,154 @@ import {Api} from "../tl";
 
 export abstract class Session {
 
-  /**
-   * Creates a clone of this session file
-   * @param toInstance {Session|null}
-   * @returns {Session}
-   */
-
-  /* CONTEST
-  clone(toInstance = null) {
-      return toInstance || new this.constructor()
-  }*/
-
-  /**
-   * Sets the information of the data center address and port that
-   * the library should connect to, as well as the data center ID,
-   * which is currently unused.
-   * @param dcId {number}
-   * @param serverAddress {string}
-   * @param port {number}
-   */
-  abstract setDC(dcId: number, serverAddress: string, port: number): void;
-
-  /**
-   * Returns the currently-used data center ID.
-   */
-  abstract get dcId(): number ;
-
-  /**
-   * Returns the server address where the library should connect to.
-   */
-  abstract get serverAddress(): string | undefined;
-
-  /**
-   * Returns the port to which the library should connect to.
-   */
-  abstract get port(): number | undefined ;
-
-  /**
-   * Returns an ``AuthKey`` instance associated with the saved
-   * data center, or `None` if a new one should be generated.
-   */
-  abstract get authKey(): AuthKey | undefined ;
-
-  /**
-   * Sets the ``AuthKey`` to be used for the saved data center.
-   * @param value
-   */
-  abstract set authKey(value: AuthKey | undefined) ;
-
-  /**
-   * Called before using the session
-   */
-  abstract load(): Promise<void>
-
-  /**
-   *  sets auth key for a dc
-   */
-  abstract setAuthKey(authKey?: AuthKey, dcId?: number): void;
-
-  /**
-   *  gets auth key for a dc
-   */
-
-  abstract getAuthKey(dcId?: number): AuthKey | undefined ;
-
-  /**
-   *
-   * @param key
-   */
-  abstract getInputEntity(key: EntityLike): Api.TypeInputPeer;
-
-  /**
-   * Returns an ID of the takeout process initialized for this session,
-   * or `None` if there's no were any unfinished takeout requests.
-   */
-  /*CONTEST
-  get takeoutId() {
-      throw new Error('Not Implemented')
-  }
-  */
-  /**
-   * Sets the ID of the unfinished takeout process for this session.
-   * @param value
-   */
-  /*CONTEST
-  set takeoutId(value) {
-      throw new Error('Not Implemented')
-  }
-  */
-  /**
-   * Returns the ``UpdateState`` associated with the given `entity_id`.
-   * If the `entity_id` is 0, it should return the ``UpdateState`` for
-   * no specific channel (the "general" state). If no state is known
-   * it should ``return None``.
-   * @param entityId
-   */
-  /*CONTEST
-  getUpdateState(entityId) {
-      throw new Error('Not Implemented')
-  }
-
-   */
-
-  /**
-   * Sets the given ``UpdateState`` for the specified `entity_id`, which
-   * should be 0 if the ``UpdateState`` is the "general" state (and not
-   * for any specific channel).
-   * @param entityId
-   * @param state
-   */
-  /*CONTEST
-  setUpdateState(entityId, state) {
-      throw new Error('Not Implemented')
-  }
-
-   */
-
-  /**
-   * Called on client disconnection. Should be used to
-   * free any used resources. Can be left empty if none.
-   */
-
-  /*CONTEST
-  close() {
-
-  }
-
-   */
-
-  /**
-   * called whenever important properties change. It should
-   * make persist the relevant session information to disk.
-   */
-  abstract save(): void;
-
-  /**
-   * Called upon client.log_out(). Should delete the stored
-   * information from disk since it's not valid anymore.
-   */
-
-  abstract delete(): void;
-
-  /**
-   * Lists available sessions. Not used by the library itself.
-   */
-
-  /*CONTEST
-  listSessions() {
-      throw new Error('Not Implemented')
-  }
-
-   */
-
-  /**
-   * Processes the input ``TLObject`` or ``list`` and saves
-   * whatever information is relevant (e.g., ID or access hash).
-   * @param tlo
-   */
-  abstract processEntities(tlo: any): void;
-
-
-  /**
-   * Turns the given key into an ``InputPeer`` (e.g. ``InputPeerUser``).
-   * The library uses this method whenever an ``InputPeer`` is needed
-   * to suit several purposes (e.g. user only provided its ID or wishes
-   * to use a cached username to avoid extra RPC).
-   */
-  /*CONTEST
-  getInputEntity(key) {
-      throw new Error('Not Implemented')
-  }
-
-   */
+    /**
+     * Creates a clone of this session file
+     * @param toInstance {Session|null}
+     * @returns {Session}
+     */
+
+    /* CONTEST
+    clone(toInstance = null) {
+        return toInstance || new this.constructor()
+    }*/
+
+    /**
+     * Sets the information of the data center address and port that
+     * the library should connect to, as well as the data center ID,
+     * which is currently unused.
+     * @param dcId {number}
+     * @param serverAddress {string}
+     * @param port {number}
+     */
+    abstract setDC(dcId: number, serverAddress: string, port: number): void;
+
+    /**
+     * Returns the currently-used data center ID.
+     */
+    abstract get dcId(): number ;
+
+    /**
+     * Returns the server address where the library should connect to.
+     */
+    abstract get serverAddress(): string | undefined;
+
+    /**
+     * Returns the port to which the library should connect to.
+     */
+    abstract get port(): number | undefined ;
+
+    /**
+     * Returns an ``AuthKey`` instance associated with the saved
+     * data center, or `None` if a new one should be generated.
+     */
+    abstract get authKey(): AuthKey | undefined ;
+
+    /**
+     * Sets the ``AuthKey`` to be used for the saved data center.
+     * @param value
+     */
+    abstract set authKey(value: AuthKey | undefined) ;
+
+    /**
+     * Called before using the session
+     */
+    abstract load(): Promise<void>
+
+    /**
+     *  sets auth key for a dc
+     */
+    abstract setAuthKey(authKey?: AuthKey, dcId?: number): void;
+
+    /**
+     *  gets auth key for a dc
+     */
+
+    abstract getAuthKey(dcId?: number): AuthKey | undefined ;
+
+
+    /**
+     * Turns the given key into an ``InputPeer`` (e.g. ``InputPeerUser``).
+     * The library uses this method whenever an ``InputPeer`` is needed
+     * to suit several purposes (e.g. user only provided its ID or wishes
+     * to use a cached username to avoid extra RPC).
+     */
+    abstract getInputEntity(key: EntityLike): Api.TypeInputPeer;
+
+    /**
+     * Returns an ID of the takeout process initialized for this session,
+     * or `None` if there's no were any unfinished takeout requests.
+     */
+    /*CONTEST
+    get takeoutId() {
+        throw new Error('Not Implemented')
+    }
+    */
+    /**
+     * Sets the ID of the unfinished takeout process for this session.
+     * @param value
+     */
+    /*CONTEST
+    set takeoutId(value) {
+        throw new Error('Not Implemented')
+    }
+    */
+    /**
+     * Returns the ``UpdateState`` associated with the given `entity_id`.
+     * If the `entity_id` is 0, it should return the ``UpdateState`` for
+     * no specific channel (the "general" state). If no state is known
+     * it should ``return None``.
+     * @param entityId
+     */
+    /*CONTEST
+    getUpdateState(entityId) {
+        throw new Error('Not Implemented')
+    }
+
+     */
+
+    /**
+     * Sets the given ``UpdateState`` for the specified `entity_id`, which
+     * should be 0 if the ``UpdateState`` is the "general" state (and not
+     * for any specific channel).
+     * @param entityId
+     * @param state
+     */
+
+    /*CONTEST
+    setUpdateState(entityId, state) {
+        throw new Error('Not Implemented')
+    }
+
+     */
+
+    /**
+     * Called on client disconnection. Should be used to
+     * free any used resources. Can be left empty if none.
+     */
+
+    abstract close(): void;
+
+
+    /**
+     * called whenever important properties change. It should
+     * make persist the relevant session information to disk.
+     */
+    abstract save(): void;
+
+    /**
+     * Called upon client.log_out(). Should delete the stored
+     * information from disk since it's not valid anymore.
+     */
+
+    abstract delete(): void;
+
+
+    /**
+     * Processes the input ``TLObject`` or ``list`` and saves
+     * whatever information is relevant (e.g., ID or access hash).
+     * @param tlo
+     */
+    abstract processEntities(tlo: any): void;
+
+
 }

+ 33 - 24
gramjs/sessions/Memory.ts

@@ -1,4 +1,4 @@
-import  {Session} from './Abstract';
+import {Session} from './Abstract';
 import type {AuthKey} from "../crypto/AuthKey";
 import {Api} from "../tl";
 import bigInt from "big-integer";
@@ -63,15 +63,24 @@ export class MemorySession extends Session {
         this._takeoutId = value
     }
 
-    /*
-        getUpdateState(entityId:number) {
-            return this._updateStates[entityId]
+    getAuthKey(dcId?: number) {
+        if (dcId && dcId !== this.dcId) {
+            // Not supported.
+            return undefined
         }
 
-        setUpdateState(entityId, state) {
-            return this._updateStates[entityId] = state
+        return this.authKey
+    }
+
+    setAuthKey(authKey?: AuthKey, dcId?: number) {
+        if (dcId && dcId !== this.dcId) {
+            // Not supported.
+            return undefined
         }
-    */
+
+        this.authKey = authKey
+    }
+
     close() {
     }
 
@@ -128,7 +137,7 @@ export class MemorySession extends Session {
             // This may be a list of users already for instance
             entities = tlo;
         } else {
-            if (typeof tlo==="object") {
+            if (typeof tlo === "object") {
                 if ('user' in tlo) {
                     entities.push(tlo.user)
                 }
@@ -201,25 +210,25 @@ export class MemorySession extends Session {
         }
     }
 
-    getInputEntity(key: EntityLike):Api.TypeInputPeer {
+    getInputEntity(key: EntityLike): Api.TypeInputPeer {
         let exact;
-            if (typeof key === 'object' && key.SUBCLASS_OF_ID) {
-                if ([0xc91c90b6, 0xe669bf46, 0x40f202fd].includes(key.SUBCLASS_OF_ID)) {
-                    // hex(crc32(b'InputPeer', b'InputUser' and b'InputChannel'))
-                    // We already have an Input version, so nothing else required
-                    return key
-                }
-                // Try to early return if this key can be casted as input peer
-                return utils.getInputPeer(key)
+        if (typeof key === 'object' && key.SUBCLASS_OF_ID) {
+            if ([0xc91c90b6, 0xe669bf46, 0x40f202fd].includes(key.SUBCLASS_OF_ID)) {
+                // hex(crc32(b'InputPeer', b'InputUser' and b'InputChannel'))
+                // We already have an Input version, so nothing else required
+                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.classType === 'constructor') {
+                key = utils.getPeerId(key);
+                exact = true
             } else {
-                // Not a TLObject or can't be cast into InputPeer
-                if (typeof key === 'object' && key.classType === 'constructor') {
-                    key = utils.getPeerId(key);
-                    exact = true
-                } else {
-                    exact = !(typeof key == 'number') || key < 0
-                }
+                exact = !(typeof key == 'number') || key < 0
             }
+        }
 
 
         let result = undefined;

+ 0 - 18
gramjs/sessions/StoreSession.ts

@@ -57,24 +57,6 @@ export class StoreSession extends MemorySession {
         return this._authKey;
     }
 
-    getAuthKey(dcId?: number) {
-        if (dcId && dcId !== this.dcId) {
-            // Not supported.
-            throw  new Error("not supported");
-        }
-
-        return this.authKey
-    }
-
-    setAuthKey(authKey?: AuthKey, dcId?: number) {
-        if (dcId && dcId !== this.dcId) {
-            // Not supported.
-            throw  new Error("not supported");
-        }
-
-        this.authKey = authKey
-    }
-
     processEntities(tlo: any) {
         const rows = this._entitiesToRows(tlo);
         if (!rows) {

+ 0 - 17
gramjs/sessions/StringSession.ts

@@ -103,22 +103,5 @@ export class StringSession extends MemorySession {
         ]))
     }
 
-    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
-    }
 }