Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove remaining circular dependencies #28

Merged
merged 12 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove some circular dependencies
  • Loading branch information
rojvv committed Nov 19, 2022
commit 64b19ea71a66063b0a8b88f9257aa12007262f1c
26 changes: 10 additions & 16 deletions src/client/abstract_telegram_client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import * as types from "./types.ts";
import { Dialog, InlineResults } from "../tl/custom/mod.ts";
import type { ButtonLike } from "../define.d.ts";
import { Api } from "../tl/api.js";
import { MTProtoSender } from "../network/mtproto_sender.ts";
import { Session } from "../sessions/mod.ts";
import { LogLevel } from "../extensions/logger.ts";
import { TotalList } from "../helpers.ts";
import { DirectDownloadIter } from "./downloads.ts";
import { TelegramBaseClient, TelegramClientParams } from "./base_client.ts";
import { Buffer } from "../../deps.ts";

Expand Down Expand Up @@ -71,15 +68,13 @@ export abstract class AbstractTelegramClient extends TelegramBaseClient {
entity?: Api.InputPeerSelf,
offset?: string,
geoPoint?: Api.TypeInputGeoPoint,
): Promise<InlineResults>;
// deno-lint-ignore no-explicit-any
): Promise<any>;

abstract buildReplyMarkup(
buttons:
| Api.TypeReplyMarkup
| undefined
| ButtonLike
| ButtonLike[]
| ButtonLike[][],
| Api.TypeMarkupLike
| undefined,
inlineOnly?: boolean,
): Api.TypeReplyMarkup | undefined;

Expand All @@ -90,7 +85,8 @@ export abstract class AbstractTelegramClient extends TelegramBaseClient {

abstract iterDownload(
iterFileParams: types.IterDownloadFunction,
): DirectDownloadIter;
// deno-lint-ignore no-explicit-any
): AsyncIterable<any>;

abstract downloadProfilePhoto(
entity: Api.TypeEntityLike,
Expand Down Expand Up @@ -186,11 +182,13 @@ export abstract class AbstractTelegramClient extends TelegramBaseClient {

abstract iterDialogs(
iterDialogsParams: types.IterDialogsParams,
): AsyncIterable<Dialog>;
// deno-lint-ignore no-explicit-any
): AsyncIterable<any>;

abstract getDialogs(
params: types.IterDialogsParams,
): Promise<TotalList<Dialog>>;
// deno-lint-ignore no-explicit-any
): Promise<TotalList<any>>;

abstract iterParticipants(
entity: Api.TypeEntityLike,
Expand Down Expand Up @@ -362,8 +360,4 @@ export abstract class AbstractTelegramClient extends TelegramBaseClient {
| Map<number, Api.Message>
| (Api.Message | undefined)[]
| undefined;

static get events() {
return import("../events/mod.ts");
}
}
7 changes: 3 additions & 4 deletions src/client/buttons.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Api } from "../tl/api.js";
import type { ButtonLike } from "../define.d.ts";
import { Button } from "../tl/custom/button.ts";
import { MessageButton } from "../tl/custom/message_button.ts";
import { isArrayLike } from "../helpers.ts";
Expand All @@ -8,9 +7,9 @@ export function buildReplyMarkup(
buttons:
| Api.TypeReplyMarkup
| undefined
| ButtonLike
| ButtonLike[]
| ButtonLike[][],
| Api.TypeButtonLike
| Api.TypeButtonLike[]
| Api.TypeButtonLike[][],
inlineOnly = false,
): Api.TypeReplyMarkup | undefined {
if (buttons == undefined) {
Expand Down
11 changes: 3 additions & 8 deletions src/client/telegram_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import * as uploadMethods from "./uploads.ts";
import * as userMethods from "./users.ts";
import * as chatMethods from "./chats.ts";
import * as dialogMethods from "./dialogs.ts";
import type { ButtonLike } from "../define.d.ts";
import { Api } from "../tl/api.js";
import { sanitizeParseMode } from "../utils.ts";
import type { EventBuilder } from "../events/common.ts";
Expand Down Expand Up @@ -144,9 +143,9 @@ export class TelegramClient extends TelegramBaseClient
buttons:
| Api.TypeReplyMarkup
| undefined
| ButtonLike
| ButtonLike[]
| ButtonLike[][],
| Api.TypeButtonLike
| Api.TypeButtonLike[]
| Api.TypeButtonLike[][],
inlineOnly = false,
) {
return buttonsMethods.buildReplyMarkup(buttons, inlineOnly);
Expand Down Expand Up @@ -596,8 +595,4 @@ export class TelegramClient extends TelegramBaseClient
| undefined {
return parseMethods._getResponseMessage(this, req, result, inputChat);
}

static get events() {
return import("../events/mod.ts");
}
}
31 changes: 12 additions & 19 deletions src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { Api } from "../tl/api.js";
import {
DateLike,
FileLike,
MarkupLike,
MessageLike,
OutFile,
ProgressCallback,
} from "../define.d.ts";
import { DateLike, OutFile, ProgressCallback } from "../define.d.ts";
import { CustomFile } from "../classes.ts";
import { Buffer } from "../../deps.ts";

Expand All @@ -16,18 +9,18 @@ export interface ParseInterface {
}

export interface SendMessageParams {
message?: MessageLike;
message?: Api.TypeMessageLike;
replyTo?: number | Api.Message;
attributes?: Api.TypeDocumentAttribute[];
// deno-lint-ignore no-explicit-any
parseMode?: any;
formattingEntities?: Api.TypeMessageEntity[];
linkPreview?: boolean;
file?: FileLike | FileLike[];
thumb?: FileLike;
file?: Api.TypeFileLike | Api.TypeFileLike[];
thumb?: Api.TypeFileLike;
forceDocument?: false;
clearDraft?: false;
buttons?: MarkupLike;
buttons?: Api.TypeMarkupLike;
silent?: boolean;
supportStreaming?: boolean;
schedule?: DateLike;
Expand All @@ -50,9 +43,9 @@ export interface EditMessageParams {
parseMode?: any;
formattingEntities?: Api.TypeMessageEntity[];
linkPreview?: boolean;
file?: FileLike;
file?: Api.TypeFileLike;
forceDocument?: false;
buttons?: MarkupLike;
buttons?: Api.TypeMarkupLike;
schedule?: DateLike;
}

Expand Down Expand Up @@ -109,15 +102,15 @@ export interface UploadFileParams {
}

export interface SendFileInterface {
file: FileLike | FileLike[];
file: Api.TypeFileLike | Api.TypeFileLike[];
caption?: string | string[];
forceDocument?: boolean;
fileSize?: number;
clearDraft?: boolean;
progressCallback?: OnProgress;
replyTo?: Api.TypeMessageIDLike;
attributes?: Api.TypeDocumentAttribute[];
thumb?: FileLike;
thumb?: Api.TypeFileLike;
voiceNote?: boolean;
videoNote?: boolean;
supportsStreaming?: boolean;
Expand All @@ -126,7 +119,7 @@ export interface SendFileInterface {
formattingEntities?: Api.TypeMessageEntity[];
silent?: boolean;
scheduleDate?: number;
buttons?: MarkupLike;
buttons?: Api.TypeMarkupLike;
workers?: number;
noforwards?: boolean;
commentTo?: number | Api.Message;
Expand Down Expand Up @@ -261,12 +254,12 @@ export interface DownloadMediaInterface {
}

export interface FileToMediaInterface {
file: FileLike;
file: Api.TypeFileLike;
forceDocument?: boolean;
fileSize?: number;
progressCallback?: OnProgress;
attributes?: Api.TypeDocumentAttribute[];
thumb?: FileLike;
thumb?: Api.TypeFileLike;
voiceNote?: boolean;
videoNote?: boolean;
supportsStreaming?: boolean;
Expand Down
5 changes: 2 additions & 3 deletions src/client/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
getInputMedia,
getMessageId,
} from "../utils.ts";
import { FileLike } from "../define.d.ts";
import { AbstractTelegramClient } from "./abstract_telegram_client.ts";
import { _parseMessageText } from "./message_parse.ts";
import { getCommentData } from "./messages.ts";
Expand Down Expand Up @@ -130,12 +129,12 @@ export async function uploadFile(
}

interface FileToMediaInterface {
file: FileLike;
file: Api.TypeFileLike;
forceDocument?: boolean;
fileSize?: number;
progressCallback?: OnProgress;
attributes?: Api.TypeDocumentAttribute[];
thumb?: FileLike;
thumb?: Api.TypeFileLike;
voiceNote?: boolean;
videoNote?: boolean;
supportsStreaming?: boolean;
Expand Down
28 changes: 0 additions & 28 deletions src/define.d.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
import { Api } from "./tl/api.js";
import { CustomFile } from "./classes.ts";
import { Button } from "./tl/custom/button.ts";
import { bigInt, Buffer, WriteStream } from "../deps.ts";

type ValueOf<T> = T[keyof T];
type Phone = string;
type Username = string;
type PeerID = number;

type EntitiesLike = Api.TypeEntityLike[];

type MessageLike = string | Api.Message;

type LocalPath = string;
type ExternalUrl = string;
type BotFileID = string;

type FileLike =
| LocalPath
| ExternalUrl
| BotFileID
| Buffer
| Api.TypeMessageMedia
| Api.TypeInputMedia
| Api.TypeInputFile
| Api.TypeInputFileLocation
| File
| Api.TypePhoto
| Api.TypeDocument
| CustomFile;

type OutFile =
| string
| Buffer
Expand All @@ -38,12 +17,5 @@ type ProgressCallback = (
total: bigInt.BigInteger,
downloaded: bigInt.BigInteger,
) => void;
type ButtonLike = Api.TypeKeyboardButton | Button;

type MarkupLike =
| Api.TypeReplyMarkup
| ButtonLike
| ButtonLike[]
| ButtonLike[][];

type DateLike = number;
30 changes: 29 additions & 1 deletion src/tl/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import { CustomMessage } from "./custom/message.ts";
import { AbstractTelegramClient } from "../client/abstract_telegram_client.ts";
import { BinaryReader } from "../extensions/binary_reader.ts";
import { CustomFile } from "../classes.ts";
import { bigInt, Buffer } from "../../deps.ts";
import { BotFileID, ExternalUrl, LocalPath } from "../define.d.ts";

export namespace Api {
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -22444,7 +22446,7 @@ export namespace Api {
| messages.ChatFull
| ChatFull
| ChannelFull;
export type TypeEntityLike =
type TypeEntityLike =
| bigInt.BigInteger
| TypePhone
| TypeUsername
Expand All @@ -22457,6 +22459,32 @@ export namespace Api {
| TypeChat
| TypeInputChannel
| TypeInputUser;
type TypeFileLike =
| LocalPath
| ExternalUrl
| BotFileID
| Buffer
| Api.TypeMessageMedia
| Api.TypeInputMedia
| Api.TypeInputFile
| Api.TypeInputFileLocation
| File
| Api.TypePhoto
| Api.TypeDocument
| CustomFile;
interface Button {
button: Api.TypeKeyboardButton;
resize: boolean | undefined;
selective: boolean | undefined;
singleUse: boolean | undefined;
}
type TypeButtonLike = Api.TypeKeyboardButton | Button;
type TypeMarkupLike =
| Api.TypeReplyMarkup
| Api.TypeButtonLike
| Api.TypeButtonLike[]
| Api.TypeButtonLike[][];
type TypeMessageLike = string | Api.Message;
export type TypeInputPeer =
| InputPeerEmpty
| InputPeerSelf
Expand Down
2 changes: 1 addition & 1 deletion src/tl/custom/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Api } from "../api.js";
import { getInputUser } from "../../utils.ts";
import { Buffer } from "../../../deps.ts";

export class Button {
export class Button implements Api.Button {
public button: Api.TypeKeyboardButton;
public resize: boolean | undefined;
public selective: boolean | undefined;
Expand Down
6 changes: 3 additions & 3 deletions src/tl/custom/file.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Api } from "../api.js";
import { _photoSizeByteCount } from "../../utils.ts";
import type { FileLike } from "../../define.d.ts";


export class File {
private readonly media: FileLike;
private readonly media: Api.TypeFileLike;

constructor(media: FileLike) {
constructor(media: Api.TypeFileLike) {
this.media = media;
}

Expand Down
6 changes: 3 additions & 3 deletions src/tl/custom/message_button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import { Api } from "../api.js";
import { Button } from "./button.ts";
import { computeCheck } from "../../password.ts";
import { AbstractTelegramClient } from "../../client/abstract_telegram_client.ts";
import type { ButtonLike } from "../../define.d.ts";

export class MessageButton {
private readonly _client: AbstractTelegramClient;
private readonly _chat: Api.TypeEntityLike;
public readonly button: ButtonLike;
public readonly button: Api.TypeButtonLike;
private readonly _bot?: Api.TypeEntityLike;
private readonly _msgId: Api.TypeMessageIDLike;

constructor(
client: AbstractTelegramClient,
original: ButtonLike,
original: Api.TypeButtonLike,
chat: Api.TypeEntityLike,
bot: Api.TypeEntityLike | undefined,
msgId: Api.TypeMessageIDLike,
Expand All @@ -30,6 +29,7 @@ export class MessageButton {
}

get text() {
// @ts-expect-error: TODO
return !(this.button instanceof Button) ? this.button.text : "";
}

Expand Down
Loading