Skip to content

Commit

Permalink
Merge branch 'main' of github.com:MTKruto/server
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed May 10, 2024
2 parents 6236895 + d378c3c commit 12b5dfd
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 100 deletions.
2 changes: 1 addition & 1 deletion add_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function addUser(apiId: number, apiHash: string): Promise<never> {
const id = "user" + crypto.randomUUID();
ClientManager.createKvPath();
const storage = new StorageDenoKV(path.join(ClientManager.KV_PATH, id));
const client = new Client(storage, apiId, apiHash);
const client = new Client({ storage, apiId, apiHash });

await client.start({
phone: () => prompt("Phone number:")!,
Expand Down
8 changes: 8 additions & 0 deletions allowed_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ export const ALLOWED_METHODS = [
"removeStoryFromHighlights",
"blockUser",
"unblockUser",
"downloadLiveStreamChunk",
"getLiveStreamChannels",
"getVideoChat",
"joinLiveStream",
"joinVideoChat",
"leaveVideoChat",
"scheduleVideoChat",
"startVideoChat",
] as const;

export type AllowedMethod = (typeof ALLOWED_METHODS)[number];
Expand Down
48 changes: 48 additions & 0 deletions benchmarks/disallowed_functions_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* MTKruto Server
* Copyright (C) 2024 Roj <https://roj.im/>
*
* This file is part of MTKruto Server.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { functions } from "mtkruto/mod.ts";
import { ALLOWED_METHODS } from "../allowed_methods.ts";
import { isFunctionDisallowed } from "../disallowed_functions.ts";

Deno.bench("isFunctionDisallowed(ping)", () => {
isFunctionDisallowed(new functions.ping({ ping_id: 0n }));
});

Deno.bench("isFunctionDisallowed(Allowed Method)", () => {
isFunctionDisallowed(ALLOWED_METHODS[0]);
});

Deno.bench("isFunctionDisallowed(MTProto Function)", () => {
isFunctionDisallowed(
new functions.req_DH_params({
nonce: 0n,
encrypted_data: new Uint8Array(),
p: new Uint8Array(),
q: new Uint8Array(),
public_key_fingerprint: 0n,
server_nonce: 0n,
}),
);
});

Deno.bench("isFunctionDisallowed(ALLOWED_METHODS)", () => {
ALLOWED_METHODS.map(isFunctionDisallowed);
});
56 changes: 56 additions & 0 deletions benchmarks/params_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* MTKruto Server
* Copyright (C) 2024 Roj <https://roj.im/>
*
* This file is part of MTKruto Server.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { parseFormDataParams, parseGetParams } from "../params.ts";

const formData = new FormData();
const params = new URLSearchParams();

const args = [
JSON.stringify(1),
"MTKruto",
false,
null,
[1, 2, 3, 4, 5, "", null, false],
{ _: [1, 2, 3, 4, 5, "", null, false] },
];
const kwargs = [
["a", "b"],
["c", "d"],
];
for (let i = 0; i < 2; ++i) {
for (const arg of args.map((v) => JSON.stringify(v))) {
params.set(arg, "");
formData.append("_", arg);
}
for (const [k, v] of kwargs) {
params.set(k, JSON.stringify(v));
}
}
formData.append("_", new Blob([new Uint8Array(1024)]));
formData.append("_", JSON.stringify(Object.fromEntries(kwargs)));

Deno.bench("parseFormDataParams", () => {
parseFormDataParams(formData);
});

Deno.bench("parseGetParams", () => {
parseGetParams(params);
});
49 changes: 49 additions & 0 deletions benchmarks/responses_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* MTKruto Server
* Copyright (C) 2024 Roj <https://roj.im/>
*
* This file is part of MTKruto Server.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { assertArgCount } from "../responses.ts";

Deno.bench("assertArgCount(2, 2)", () => {
assertArgCount(2, 2);
});

Deno.bench("assertArgCount(1, 2)", () => {
try {
assertArgCount(1, 2);
} catch {
//
}
});

Deno.bench("assertArgCount(0, 1)", () => {
try {
assertArgCount(0, 1);
} catch {
//
}
});

Deno.bench("assertArgCount(1, 0)", () => {
try {
assertArgCount(1, 0);
} catch {
//
}
});
40 changes: 40 additions & 0 deletions benchmarks/transform_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* MTKruto Server
* Copyright (C) 2024 Roj <https://roj.im/>
*
* This file is part of MTKruto Server.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { transform } from "../transform.ts";

const date = new Date();
const object = { _: { _: { _: date, a: [date, date] } } };

Deno.bench("transform", () => {
transform(object);
});

Deno.bench("JSON.stringify(transform)", () => {
JSON.stringify(transform(object));
});

Deno.bench("JSON.parse(JSON.stringify(transform))", () => {
JSON.parse(JSON.stringify(transform(object)));
});

Deno.bench("transform(JSON.parse(JSON.stringify(transform)))", () => {
transform(JSON.parse(JSON.stringify(transform(object))));
});
67 changes: 60 additions & 7 deletions client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
AllowedMethod,
BotCommand,
BusinessConnection,
InlineQueryAnswer,
CallbackQueryAnswer,
Chat,
ChatMember,
Expand All @@ -31,8 +30,10 @@ import {
Composer,
Context_,
InactiveChat,
InlineQueryAnswer,
InviteLink,
iterateReadableStream,
LiveStreamChannel,
Message,
MessageAnimation,
MessageAudio,
Expand All @@ -52,10 +53,14 @@ import {
resolve,
Sticker,
Story,
transform,
unimplemented,
unreachable,
Update,
User,
VideoChat,
VideoChatActive,
VideoChatScheduled,
} from "./deps.ts";
import { Queue } from "./queue.ts";

Expand Down Expand Up @@ -701,7 +706,7 @@ export class Client<C extends Context = Context> extends Composer<C> {
: { "content-type": "application/json" },
body,
});
const result = await response.json();
const result = transform(await response.json());
if (response.status == 200) {
return result;
} else {
Expand Down Expand Up @@ -1110,11 +1115,10 @@ export class Client<C extends Context = Context> extends Composer<C> {
// ========================= CALLBACK QUERIES ========================= //
//


sendCallbackQuery(
sendCallbackQuery(
...args: Parameters<Client_["sendCallbackQuery"]>
): Promise<CallbackQueryAnswer> {
return this.#request("sendCallbackQuery", args);
return this.#request("sendCallbackQuery", args);
}

async answerCallbackQuery(
Expand All @@ -1127,11 +1131,10 @@ export class Client<C extends Context = Context> extends Composer<C> {
// ========================= INLINE QUERIES ========================= //
//


sendInlineQuery(
...args: Parameters<Client_["sendInlineQuery"]>
): Promise<InlineQueryAnswer> {
return this.#request("sendInlineQuery", args);
return this.#request("sendInlineQuery", args);
}

async answerInlineQuery(
Expand Down Expand Up @@ -1289,4 +1292,54 @@ export class Client<C extends Context = Context> extends Composer<C> {
): Promise<void> {
await this.#request("unblockUser", args);
}

//
// ========================= VIDEO CHATS ========================= //
//

downloadLiveStreamChunk(): never {
unimplemented();
}

getLiveStreamChannels(
...args: Parameters<Client_["getLiveStreamChannels"]>
): Promise<LiveStreamChannel[]> {
return this.#request("getLiveStreamChannels", args);
}

getVideoChat(
...args: Parameters<Client_["getVideoChat"]>
): Promise<VideoChat> {
return this.#request("getVideoChat", args);
}

async joinLiveStream(
...args: Parameters<Client_["joinLiveStream"]>
): Promise<void> {
await this.#request("joinLiveStream", args);
}

joinVideoChat(
...args: Parameters<Client_["joinVideoChat"]>
): Promise<string> {
return this.#request("joinVideoChat", args);
}

async leaveVideoChat(
...args: Parameters<Client_["leaveVideoChat"]>
): Promise<void> {
await this.#request("leaveVideoChat", args);
}

scheduleVideoChat(
...args: Parameters<Client_["scheduleVideoChat"]>
): Promise<VideoChatScheduled> {
return this.#request("scheduleVideoChat", args);
}

startVideoChat(
...args: Parameters<Client_["startVideoChat"]>
): Promise<VideoChatActive> {
return this.#request("startVideoChat", args);
}
}
Loading

0 comments on commit 12b5dfd

Please sign in to comment.