mtcute/packages/core/src/storage/abstract.ts

180 lines
5.6 KiB
TypeScript
Raw Normal View History

2021-08-05 20:38:24 +03:00
import { tl } from '@mtcute/tl'
import { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime'
2021-04-08 12:19:38 +03:00
import { BasicPeerType, MaybeAsync } from '../types/index.js'
import { Logger } from '../utils/index.js'
// eslint-disable-next-line @typescript-eslint/no-namespace
2021-04-08 12:19:38 +03:00
export namespace ITelegramStorage {
export interface PeerInfo {
// marked id
id: number
accessHash: tl.Long
type: BasicPeerType
username?: string
phone?: string
full: tl.TypeUser | tl.TypeChat
2021-04-08 12:19:38 +03:00
}
export interface SelfInfo {
isBot: boolean
userId: number
}
export interface DcOptions {
main: tl.RawDcOption
media: tl.RawDcOption
}
2021-04-08 12:19:38 +03:00
}
/**
* Abstract interface for persistent storage.
*
* In some cases you may want to extend existing MemorySession
* and override save()/load()/destroy() methods, but you are also free
* to implement your own session (be sure to refer to MemorySession
* source code to avoid shooting your leg though)
*
* Note that even though set methods *can* be async, you should only
* write updates to the disk when `save()` is called.
*/
export interface ITelegramStorage {
/**
* This method is called before any other.
* For storages that use logging, logger instance.
* For storages that use binary storage, binary maps
*/
setup?(log: Logger, readerMap: TlReaderMap, writerMap: TlWriterMap): void
2021-04-08 12:19:38 +03:00
/**
* Load session from some external storage.
* Should be used either to load session content from file/network/etc
* to memory, or to open required connections to fetch session content later
*/
load?(): MaybeAsync<void>
/**
* Save session to some external storage.
* Should be used to commit pending changes in the session.
* For example, saving session content to file/network/etc,
* or committing a database transaction
*/
save?(): MaybeAsync<void>
/**
* Cleanup session and release all used resources.
*/
destroy?(): MaybeAsync<void>
/**
* Reset session to its default state
*/
reset(): void
/**
* Set default datacenter to use with this session.
*/
setDefaultDcs(dcs: ITelegramStorage.DcOptions | null): MaybeAsync<void>
2021-04-08 12:19:38 +03:00
/**
* Get default datacenter for this session
* (by default should return null)
*/
getDefaultDcs(): MaybeAsync<ITelegramStorage.DcOptions | null>
2021-04-08 12:19:38 +03:00
/**
* Get auth_key for a given DC
* (returning null will start authorization)
2022-11-07 00:08:59 +03:00
* For temp keys: should also return null if the key has expired
*
* @param dcId DC ID
* @param tempIndex Index of the temporary key (usually 0, used for multi-connections)
2021-04-08 12:19:38 +03:00
*/
getAuthKeyFor(dcId: number, tempIndex?: number): MaybeAsync<Uint8Array | null>
2021-04-08 12:19:38 +03:00
/**
* Set auth_key for a given DC
*/
setAuthKeyFor(dcId: number, key: Uint8Array | null): MaybeAsync<void>
2022-11-07 00:08:59 +03:00
/**
* Set temp_auth_key for a given DC
* expiresAt is unix time in ms
*/
setTempAuthKeyFor(dcId: number, index: number, key: Uint8Array | null, expiresAt: number): MaybeAsync<void>
2022-11-07 00:08:59 +03:00
/**
* Remove all saved auth keys (both temp and perm)
* for the given DC. Used when perm_key becomes invalid,
* meaning all temp_keys also become invalid
*/
dropAuthKeysFor(dcId: number): MaybeAsync<void>
2021-04-08 12:19:38 +03:00
/**
* Get information about currently logged in user (if available)
*/
getSelf(): MaybeAsync<ITelegramStorage.SelfInfo | null>
/**
* Save information about currently logged in user
*/
setSelf(self: ITelegramStorage.SelfInfo | null): MaybeAsync<void>
/**
* Update local database of input peers from the peer info list
*/
updatePeers(peers: ITelegramStorage.PeerInfo[]): MaybeAsync<void>
/**
* Find a peer in local database by its marked ID
*/
getPeerById(peerId: number): MaybeAsync<tl.TypeInputPeer | null>
/**
* Find a peer in local database by its username
*/
getPeerByUsername(username: string): MaybeAsync<tl.TypeInputPeer | null>
/**
* Find a peer in local database by its phone number
*/
getPeerByPhone(phone: string): MaybeAsync<tl.TypeInputPeer | null>
/**
* Get updates state (if available), represented as a tuple
* containing: `pts, qts, date, seq`
2021-04-08 12:19:38 +03:00
*/
getUpdatesState(): MaybeAsync<[number, number, number, number] | null>
2021-04-08 12:19:38 +03:00
/**
* Set common `pts` value
2021-04-08 12:19:38 +03:00
*/
setUpdatesPts(val: number): MaybeAsync<void>
/**
* Set common `qts` value
*/
setUpdatesQts(val: number): MaybeAsync<void>
/**
* Set updates `date` value
*/
setUpdatesDate(val: number): MaybeAsync<void>
2021-04-08 12:19:38 +03:00
/**
* Set updates `seq` value
2021-04-08 12:19:38 +03:00
*/
setUpdatesSeq(val: number): MaybeAsync<void>
/**
* Get channel `pts` value
*/
getChannelPts(entityId: number): MaybeAsync<number | null>
2021-04-08 12:19:38 +03:00
/**
* Set channels `pts` values in batch.
* Storage is supposed to replace stored channel `pts` values
* with given in the object (key is unmarked peer id, value is the `pts`)
2021-04-08 12:19:38 +03:00
*/
setManyChannelPts(values: Map<number, number>): MaybeAsync<void>
2021-04-08 12:19:38 +03:00
/**
* Get cached peer information by their marked ID.
* Return `null` if caching is not supported, or the entity
* is not cached (yet).
*
* This is primarily used when a `min` entity is encountered
* in an update, or when a *short* update is encountered.
* Returning `null` will require re-fetching that
* update with the peers added, which might not be very efficient.
*/
getFullPeerById(id: number): MaybeAsync<tl.TypeUser | tl.TypeChat | null>
2021-04-08 12:19:38 +03:00
}