Skip to content

Commit

Permalink
8c23ff10aeb78da310283f3f5f678e59fa60e2af
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Apr 5, 2024
1 parent f7a35cf commit 3eb1143
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 22 deletions.
6 changes: 6 additions & 0 deletions esm/client/0_utilities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ export declare function getFileContents(source: FileSource, fileName?: string):
export declare function isHttpUrl(string: string): boolean;
export declare function getUsername(string: string): string;
export declare function getChatListId(chatList: string): 0 | 1;
export declare function checkMessageId(messageId: number): number;
export declare function checkStoryId(storyId: number): number;
export declare function checkPollOption(option: string): void;
export declare function checkArray<T>(array: T[], check: (value: T) => void): void;
export declare function checkCallbackQueryId(id: string): void;
export declare function checkInlineQueryId(id: string): void;
32 changes: 32 additions & 0 deletions esm/client/0_utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,35 @@ export function getChatListId(chatList) {
UNREACHABLE();
}
}
export function checkMessageId(messageId) {
if (typeof messageId !== "number" || isNaN(messageId) || !messageId) {
throw new InputError("Invalid message ID");
}
return messageId;
}
export function checkStoryId(storyId) {
if (typeof storyId !== "number" || isNaN(storyId) || !storyId) {
throw new InputError("Invalid story ID");
}
return storyId;
}
export function checkPollOption(option) {
if (!option.trim()) {
throw new InputError("Poll option must not be empty.");
}
}
export function checkArray(array, check) {
for (const item of array) {
check(item);
}
}
export function checkCallbackQueryId(id) {
if (typeof id !== "string" || !id.trim()) {
throw new InputError("Invalid callback query ID.");
}
}
export function checkInlineQueryId(id) {
if (typeof id !== "string" || !id.trim()) {
throw new InputError("Invalid inline query ID.");
}
}
37 changes: 30 additions & 7 deletions esm/client/2_message_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { assertMessageType, chatMemberRightsToTlObject, constructChatMember, con
import { messageSearchFilterToTlObject } from "../types/0_message_search_filter.js";
import { parseHtml } from "./0_html.js";
import { parseMarkdown } from "./0_markdown.js";
import { checkMessageId } from "./0_utilities.js";
import { checkArray } from "./0_utilities.js";
import { getFileContents, isHttpUrl } from "./0_utilities.js";
const FALLBACK_MIME_TYPE = "application/octet-stream";
const STICKER_MIME_TYPES = ["image/webp", "video/webm", "application/x-tgsticker"];
Expand All @@ -41,6 +43,7 @@ export class MessageManager {
__classPrivateFieldSet(this, _MessageManager_LresolveFileId, L.branch("resolveFileId"), "f");
}
async getMessages(chatId, messageIds) {
checkArray(messageIds, checkMessageId);
const peer = await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(chatId);
let messages_ = new Array();
const chatId_ = peerToChatId(peer);
Expand Down Expand Up @@ -119,6 +122,9 @@ export class MessageManager {
--entity.length;
}
}
if (!text.length) {
throw new InputError("Text must not be empty.");
}
return [text, entities];
}
async parseText(text_, params) {
Expand All @@ -130,6 +136,7 @@ export class MessageManager {
return await constructMessage_(message_, __classPrivateFieldGet(this, _MessageManager_c, "f").getEntity, this.getMessage.bind(this), __classPrivateFieldGet(this, _MessageManager_c, "f").fileManager.getStickerSetName.bind(__classPrivateFieldGet(this, _MessageManager_c, "f").fileManager), r, business);
}
async forwardMessages(from, to, messageIds, params) {
checkArray(messageIds, checkMessageId);
const result = await __classPrivateFieldGet(this, _MessageManager_c, "f").api.messages.forwardMessages({
from_peer: await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(from),
to_peer: await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(to),
Expand Down Expand Up @@ -456,6 +463,13 @@ export class MessageManager {
return null;
}
async sendPoll(chatId, question, options, params) {
question = question?.trim();
if (!question) {
throw new Error("Question must not be empty.");
}
if (!Array.isArray(options) || options.length < 2) {
throw new Error("There must be at least two options.");
}
const peer = await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(chatId);
const randomId = getRandomId();
const silent = params?.disableNotification ? true : undefined;
Expand Down Expand Up @@ -500,7 +514,7 @@ export class MessageManager {
}
async editMessageReplyMarkup(chatId, messageId, params) {
const result = await __classPrivateFieldGet(this, _MessageManager_c, "f").api.messages.editMessage({
id: messageId,
id: checkMessageId(messageId),
peer: await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(chatId),
reply_markup: await __classPrivateFieldGet(this, _MessageManager_instances, "m", _MessageManager_constructReplyMarkup).call(this, params),
});
Expand Down Expand Up @@ -528,7 +542,7 @@ export class MessageManager {
});
}
const result = await __classPrivateFieldGet(this, _MessageManager_c, "f").api.messages.editMessage({
id: messageId,
id: checkMessageId(messageId),
peer: await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(chatId),
entities,
message,
Expand Down Expand Up @@ -565,6 +579,7 @@ export class MessageManager {
});
}
async deleteMessages(chatId, messageIds, params) {
checkArray(messageIds, checkMessageId);
const peer = await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(chatId);
if (peer instanceof types.InputPeerChannel) {
await __classPrivateFieldGet(this, _MessageManager_c, "f").api.channels.deleteMessages({ channel: new types.InputChannel(peer), id: messageIds });
Expand All @@ -581,15 +596,15 @@ export class MessageManager {
async pinMessage(chatId, messageId, params) {
await __classPrivateFieldGet(this, _MessageManager_c, "f").api.messages.updatePinnedMessage({
peer: await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(chatId),
id: messageId,
id: checkMessageId(messageId),
silent: params?.disableNotification ? true : undefined,
pm_oneside: params?.bothSides ? undefined : true,
});
}
async unpinMessage(chatId, messageId) {
await __classPrivateFieldGet(this, _MessageManager_c, "f").api.messages.updatePinnedMessage({
peer: await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(chatId),
id: messageId,
id: checkMessageId(messageId),
unpin: true,
});
}
Expand All @@ -607,7 +622,11 @@ export class MessageManager {
await __classPrivateFieldGet(this, _MessageManager_instances, "m", _MessageManager_sendReaction).call(this, chatId, messageId, reactions, params);
}
async addReaction(chatId, messageId, reaction, params) {
const chosenReactions = await this.getMessage(chatId, messageId).then((v) => v?.reactions ?? []).then((v) => v.filter((v) => v.chosen));
const message = await this.getMessage(chatId, messageId);
if (!message) {
throw new InputError("Message not found.");
}
const chosenReactions = (message.reactions ?? []).filter((v) => v.chosen);
for (const r of chosenReactions) {
if (reactionEqual(r.reaction, reaction)) {
return;
Expand All @@ -617,7 +636,11 @@ export class MessageManager {
await this.setReactions(chatId, messageId, reactions, params);
}
async removeReaction(chatId, messageId, reaction) {
const chosenReactions = await this.getMessage(chatId, messageId).then((v) => v?.reactions ?? []).then((v) => v.filter((v) => v.chosen));
const message = await this.getMessage(chatId, messageId);
if (!message) {
throw new InputError("Message not found.");
}
const chosenReactions = (message.reactions ?? []).filter((v) => v.chosen);
for (const r of chosenReactions) {
if (reactionEqual(r.reaction, reaction)) {
const reactions = chosenReactions.filter((v) => v != r).map((v) => v.reaction);
Expand Down Expand Up @@ -1194,7 +1217,7 @@ _MessageManager_c = new WeakMap(), _MessageManager_LresolveFileId = new WeakMap(
}, _MessageManager_sendReaction = async function _MessageManager_sendReaction(chatId, messageId, reactions, params) {
await __classPrivateFieldGet(this, _MessageManager_c, "f").api.messages.sendReaction({
peer: await __classPrivateFieldGet(this, _MessageManager_c, "f").getInputPeer(chatId),
msg_id: messageId,
msg_id: checkMessageId(messageId),
reaction: reactions.map((v) => reactionToTlObject(v)),
big: params?.big ? true : undefined,
add_to_recent: params?.addToRecents ? true : undefined,
Expand Down
2 changes: 2 additions & 0 deletions esm/client/3_callback_query_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
var _CallbackQueryManager_c;
import { types } from "../2_tl.js";
import { constructCallbackQuery } from "../3_types.js";
import { checkCallbackQueryId } from "./0_utilities.js";
export class CallbackQueryManager {
constructor(c) {
_CallbackQueryManager_c.set(this, void 0);
__classPrivateFieldSet(this, _CallbackQueryManager_c, c, "f");
}
async answerCallbackQuery(id, params) {
await __classPrivateFieldGet(this, _CallbackQueryManager_c, "f").storage.assertBot("answerCallbackQuery");
checkCallbackQueryId(id);
await __classPrivateFieldGet(this, _CallbackQueryManager_c, "f").api.messages.setBotCallbackAnswer({
query_id: BigInt(id),
cache_time: params?.cacheTime ?? 0,
Expand Down
2 changes: 2 additions & 0 deletions esm/client/3_inline_query_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ var _InlineQueryManager_c;
import { UNREACHABLE } from "../1_utilities.js";
import { types } from "../2_tl.js";
import { constructChosenInlineResult, constructInlineQuery, inlineQueryResultToTlObject } from "../3_types.js";
import { checkInlineQueryId } from "./0_utilities.js";
export class InlineQueryManager {
constructor(c) {
_InlineQueryManager_c.set(this, void 0);
__classPrivateFieldSet(this, _InlineQueryManager_c, c, "f");
}
async answerInlineQuery(id, results, params) {
await __classPrivateFieldGet(this, _InlineQueryManager_c, "f").storage.assertBot("answerInlineQuery");
checkInlineQueryId(id);
await __classPrivateFieldGet(this, _InlineQueryManager_c, "f").api.messages.setInlineBotResults({
query_id: BigInt(id),
results: await Promise.all(results.map((v) => inlineQueryResultToTlObject(v, __classPrivateFieldGet(this, _InlineQueryManager_c, "f").messageManager.parseText.bind(__classPrivateFieldGet(this, _InlineQueryManager_c, "f").messageManager), __classPrivateFieldGet(this, _InlineQueryManager_c, "f").messageManager.usernameResolver.bind(__classPrivateFieldGet(this, _InlineQueryManager_c, "f").messageManager)))),
Expand Down
4 changes: 3 additions & 1 deletion esm/client/3_story_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { InputError } from "../0_errors.js";
import { getRandomId, UNREACHABLE } from "../1_utilities.js";
import { as, inputPeerToPeer, peerToChatId, types } from "../2_tl.js";
import { constructStory, FileType, storyInteractiveAreaToTlObject, storyPrivacyToTlObject } from "../3_types.js";
import { getFileContents, isHttpUrl } from "./0_utilities.js";
import { checkArray, checkStoryId, getFileContents, isHttpUrl } from "./0_utilities.js";
export class StoryManager {
constructor(c) {
_StoryManager_instances.add(this);
Expand Down Expand Up @@ -84,6 +84,7 @@ export class StoryManager {
}
async getStories(chatId, storyIds) {
await __classPrivateFieldGet(this, _StoryManager_c, "f").storage.assertUser("getStories");
checkArray(storyIds, checkStoryId);
const peer = await __classPrivateFieldGet(this, _StoryManager_c, "f").getInputPeer(chatId);
const stories_ = await __classPrivateFieldGet(this, _StoryManager_c, "f").api.stories.getStoriesByID({ peer, id: storyIds });
const stories = new Array();
Expand Down Expand Up @@ -148,6 +149,7 @@ _StoryManager_c = new WeakMap(), _StoryManager_instances = new WeakSet(), _Story
}
UNREACHABLE();
}, _StoryManager_togglePinned = async function _StoryManager_togglePinned(chatId, storyIds, pinned) {
checkArray(storyIds, checkStoryId);
const peer = await __classPrivateFieldGet(this, _StoryManager_c, "f").getInputPeer(chatId);
await __classPrivateFieldGet(this, _StoryManager_c, "f").api.stories.togglePinned({ peer, id: storyIds, pinned });
};
6 changes: 6 additions & 0 deletions script/client/0_utilities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ export declare function getFileContents(source: FileSource, fileName?: string):
export declare function isHttpUrl(string: string): boolean;
export declare function getUsername(string: string): string;
export declare function getChatListId(chatList: string): 0 | 1;
export declare function checkMessageId(messageId: number): number;
export declare function checkStoryId(storyId: number): number;
export declare function checkPollOption(option: string): void;
export declare function checkArray<T>(array: T[], check: (value: T) => void): void;
export declare function checkCallbackQueryId(id: string): void;
export declare function checkInlineQueryId(id: string): void;
40 changes: 39 additions & 1 deletion script/client/0_utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getChatListId = exports.getUsername = exports.isHttpUrl = exports.getFileContents = exports.resolve = void 0;
exports.checkInlineQueryId = exports.checkCallbackQueryId = exports.checkArray = exports.checkPollOption = exports.checkStoryId = exports.checkMessageId = exports.getChatListId = exports.getUsername = exports.isHttpUrl = exports.getFileContents = exports.resolve = void 0;
const dntShim = __importStar(require("../_dnt.shims.js"));
const _0_deps_js_1 = require("../0_deps.js");
const _0_errors_js_1 = require("../0_errors.js");
Expand Down Expand Up @@ -157,3 +157,41 @@ function getChatListId(chatList) {
}
}
exports.getChatListId = getChatListId;
function checkMessageId(messageId) {
if (typeof messageId !== "number" || isNaN(messageId) || !messageId) {
throw new _0_errors_js_1.InputError("Invalid message ID");
}
return messageId;
}
exports.checkMessageId = checkMessageId;
function checkStoryId(storyId) {
if (typeof storyId !== "number" || isNaN(storyId) || !storyId) {
throw new _0_errors_js_1.InputError("Invalid story ID");
}
return storyId;
}
exports.checkStoryId = checkStoryId;
function checkPollOption(option) {
if (!option.trim()) {
throw new _0_errors_js_1.InputError("Poll option must not be empty.");
}
}
exports.checkPollOption = checkPollOption;
function checkArray(array, check) {
for (const item of array) {
check(item);
}
}
exports.checkArray = checkArray;
function checkCallbackQueryId(id) {
if (typeof id !== "string" || !id.trim()) {
throw new _0_errors_js_1.InputError("Invalid callback query ID.");
}
}
exports.checkCallbackQueryId = checkCallbackQueryId;
function checkInlineQueryId(id) {
if (typeof id !== "string" || !id.trim()) {
throw new _0_errors_js_1.InputError("Invalid inline query ID.");
}
}
exports.checkInlineQueryId = checkInlineQueryId;
Loading

0 comments on commit 3eb1143

Please sign in to comment.