From 8579962aaf8533ba28e6920c277fd377b3a169c4 Mon Sep 17 00:00:00 2001 From: Painor Date: Thu, 28 Sep 2023 21:12:13 +0100 Subject: [PATCH] Update attributes to accept list for send album --- __tests__/extensions/MarkdownV2.spec.ts | 22 ++++----- gramjs/Utils.ts | 3 +- gramjs/Version.ts | 2 +- gramjs/client/dialogs.ts | 3 +- gramjs/client/updates.ts | 8 ++-- gramjs/client/uploads.ts | 13 +++++- gramjs/extensions/html.ts | 7 +-- gramjs/extensions/markdownv2.ts | 59 ++++++++++++++----------- package-lock.json | 4 +- package.json | 2 +- 10 files changed, 70 insertions(+), 53 deletions(-) diff --git a/__tests__/extensions/MarkdownV2.spec.ts b/__tests__/extensions/MarkdownV2.spec.ts index 8cf9341d..6b037cb1 100644 --- a/__tests__/extensions/MarkdownV2.spec.ts +++ b/__tests__/extensions/MarkdownV2.spec.ts @@ -50,17 +50,17 @@ describe("MarkdownV2Parser", () => { ); }); - test("it should parse custom emoji", () =>{ - const [text, entities] = MarkdownV2Parser.parse( - "![👍](tg://emoji?id=5368324170671202286)" - ); - expect(text).toEqual("👍"); - expect(entities.length).toEqual(1); - expect(entities[0]).toBeInstanceOf(types.MessageEntityCustomEmoji); - expect((entities[0] as types.MessageEntityCustomEmoji).documentId).toEqual( - "5368324170671202286" - ); - } ) + test("it should parse custom emoji", () => { + const [text, entities] = MarkdownV2Parser.parse( + "![👍](tg://emoji?id=5368324170671202286)" + ); + expect(text).toEqual("👍"); + expect(entities.length).toEqual(1); + expect(entities[0]).toBeInstanceOf(types.MessageEntityCustomEmoji); + expect( + (entities[0] as types.MessageEntityCustomEmoji).documentId + ).toEqual("5368324170671202286"); + }); test("it should parse multiple entities", () => { const [text, entities] = MarkdownV2Parser.parse("-Hello- *world*"); diff --git a/gramjs/Utils.ts b/gramjs/Utils.ts index 8285ee3d..ad72fcef 100644 --- a/gramjs/Utils.ts +++ b/gramjs/Utils.ts @@ -1120,7 +1120,6 @@ export function sanitizeParseMode( throw new Error(`Invalid parse mode type ${mode}`); } - /** Convert the given peer into its marked ID by default. @@ -1362,4 +1361,4 @@ export function isListLike(item) { ) ) } -*/ \ No newline at end of file +*/ diff --git a/gramjs/Version.ts b/gramjs/Version.ts index b8869400..2888ea02 100644 --- a/gramjs/Version.ts +++ b/gramjs/Version.ts @@ -1 +1 @@ -export const version = "2.18.33"; +export const version = "2.18.37"; diff --git a/gramjs/client/dialogs.ts b/gramjs/client/dialogs.ts index 1983c5d5..b4d14fb9 100644 --- a/gramjs/client/dialogs.ts +++ b/gramjs/client/dialogs.ts @@ -183,7 +183,8 @@ export class _DialogsIter extends RequestIter { this.request.excludePinned = true; this.request.offsetId = lastMessage ? lastMessage.id : 0; this.request.offsetDate = lastMessage ? lastMessage.date! : 0; - this.request.offsetPeer = this.buffer[this.buffer.length - 1]?.inputEntity; + this.request.offsetPeer = + this.buffer[this.buffer.length - 1]?.inputEntity; } } diff --git a/gramjs/client/updates.ts b/gramjs/client/updates.ts index fb40a9fa..972c1109 100644 --- a/gramjs/client/updates.ts +++ b/gramjs/client/updates.ts @@ -218,16 +218,14 @@ export async function _updateLoop(client: TelegramClient) { PING_FAIL_INTERVAL ); } else { - let wakeUpWarningTimeout: Timeout | undefined | number = setTimeout( - () => { + let wakeUpWarningTimeout: Timeout | undefined | number = + setTimeout(() => { _handleUpdate( client, UpdateConnectionState.disconnected ); wakeUpWarningTimeout = undefined; - }, - PING_WAKE_UP_WARNING_TIMEOUT - ); + }, PING_WAKE_UP_WARNING_TIMEOUT); await timeout(ping, PING_WAKE_UP_TIMEOUT); diff --git a/gramjs/client/uploads.ts b/gramjs/client/uploads.ts index 87f35e97..df98d055 100644 --- a/gramjs/client/uploads.ts +++ b/gramjs/client/uploads.ts @@ -249,7 +249,7 @@ export interface SendFileInterface { /** Same as `replyTo` from {@link sendMessage}. */ replyTo?: MessageIDLike; /** Optional attributes that override the inferred ones, like {@link Api.DocumentAttributeFilename} and so on.*/ - attributes?: Api.TypeDocumentAttribute[]; + attributes?: Api.TypeDocumentAttribute[] | Api.TypeDocumentAttribute[][]; /** Optional JPEG thumbnail (for documents). Telegram will ignore this parameter unless you pass a .jpg file!
* The file must also be small in dimensions and in disk size. Successful thumbnails were files below 20kB and 320x320px.
* Width/height and dimensions/size ratios may be important. @@ -545,6 +545,11 @@ export async function _sendAlbum( } else { replyTo = utils.getMessageId(replyTo); } + if (!attributes) { + attributes = []; + } + + let index = 0; const albumFiles = []; for (const file of files) { let { fileHandle, media, image } = await _fileToMedia(client, { @@ -552,13 +557,15 @@ export async function _sendAlbum( forceDocument: forceDocument, fileSize: fileSize, progressCallback: progressCallback, - attributes: attributes, + // @ts-ignore + attributes: attributes[index], thumb: thumb, voiceNote: voiceNote, videoNote: videoNote, supportsStreaming: supportsStreaming, workers: workers, }); + index++; if ( media instanceof Api.InputMediaUploadedPhoto || media instanceof Api.InputMediaPhotoExternal @@ -667,6 +674,7 @@ export async function sendFile( caption: caption, replyTo: replyTo, parseMode: parseMode, + attributes: attributes, silent: silent, scheduleDate: scheduleDate, supportsStreaming: supportsStreaming, @@ -695,6 +703,7 @@ export async function sendFile( forceDocument: forceDocument, fileSize: fileSize, progressCallback: progressCallback, + // @ts-ignore attributes: attributes, thumb: thumb, voiceNote: voiceNote, diff --git a/gramjs/extensions/html.ts b/gramjs/extensions/html.ts index 97f9ad27..a827024d 100644 --- a/gramjs/extensions/html.ts +++ b/gramjs/extensions/html.ts @@ -226,9 +226,10 @@ export class HTMLParser { `${entityText}` ); } else if (entity instanceof Api.MessageEntityCustomEmoji) { - html.push(`${entityText}`); - } - else { + html.push( + `${entityText}` + ); + } else { skipEntity = true; } lastOffset = relativeOffset + (skipEntity ? 0 : length); diff --git a/gramjs/extensions/markdownv2.ts b/gramjs/extensions/markdownv2.ts index 6a15162b..bf394f3d 100644 --- a/gramjs/extensions/markdownv2.ts +++ b/gramjs/extensions/markdownv2.ts @@ -4,67 +4,76 @@ import { HTMLParser } from "./html"; export class MarkdownV2Parser { static parse(message: string): [string, Api.TypeMessageEntity[]] { // Bold - message = message.replace(/\*(.*?)\*/g, '$1'); + message = message.replace(/\*(.*?)\*/g, "$1"); // underline - message = message.replace(/__(.*?)__/g, '$1'); + message = message.replace(/__(.*?)__/g, "$1"); // strikethrough - message = message.replace(/~(.*?)~/g, '$1'); + message = message.replace(/~(.*?)~/g, "$1"); // italic - message = message.replace(/-(.*?)-/g, '$1'); - + message = message.replace(/-(.*?)-/g, "$1"); + // pre - message = message.replace(/```(.*?)```/g, '
$1
'); - + message = message.replace(/```(.*?)```/g, "
$1
"); + // code - message = message.replace(/`(.*?)`/g, '$1'); + message = message.replace(/`(.*?)`/g, "$1"); - // Spoiler - message = message.replace(/\|\|(.*?)\|\|/g, '$1'); + // Spoiler + message = message.replace(/\|\|(.*?)\|\|/g, "$1"); // Inline URL - message = message.replace(/(?$1'); + message = message.replace( + /(?$1' + ); - // Emoji - message = message.replace(/!\[([^\]]+)\]\(tg:\/\/emoji\?id=(\d+)\)/g, '$1'); - return HTMLParser.parse(message) + // Emoji + message = message.replace( + /!\[([^\]]+)\]\(tg:\/\/emoji\?id=(\d+)\)/g, + '$1' + ); + return HTMLParser.parse(message); } static unparse( text: string, entities: Api.TypeMessageEntity[] | undefined ) { - text = HTMLParser.unparse(text, entities) + text = HTMLParser.unparse(text, entities); // Bold - text = text.replace(/(.*?)<\/b>/g, '*$1*'); + text = text.replace(/(.*?)<\/b>/g, "*$1*"); // Underline - text = text.replace(/(.*?)<\/u>/g, '__$1__'); + text = text.replace(/(.*?)<\/u>/g, "__$1__"); // Code - text = text.replace(/(.*?)<\/code>/g, '`$1`'); + text = text.replace(/(.*?)<\/code>/g, "`$1`"); // Pre - text = text.replace(/
(.*?)<\/pre>/g, '```$1```');
+        text = text.replace(/
(.*?)<\/pre>/g, "```$1```");
 
         // strikethrough
-        text = text.replace(/(.*?)<\/s>/g, '~$1~');
+        text = text.replace(/(.*?)<\/s>/g, "~$1~");
 
         // Italic
-        text = text.replace(/(.*?)<\/i>/g, '-$1-');
+        text = text.replace(/(.*?)<\/i>/g, "-$1-");
 
         // Spoiler
-        text = text.replace(/(.*?)<\/spoiler>/g, '||$1||');
+        text = text.replace(/(.*?)<\/spoiler>/g, "||$1||");
 
         // Inline URL
-        text = text.replace(/([^<]+)<\/a>/g, '[$2]($1)');
+        text = text.replace(/([^<]+)<\/a>/g, "[$2]($1)");
 
         // Emoji
-        text = text.replace(/([^<]+)<\/tg-emoji>/g, '![$2](tg://emoji?id=$1)');
+        text = text.replace(
+            /([^<]+)<\/tg-emoji>/g,
+            "![$2](tg://emoji?id=$1)"
+        );
 
-        return text
+        return text;
     }
 }
diff --git a/package-lock.json b/package-lock.json
index 8409429b..1603a6eb 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
 {
   "name": "telegram",
-  "version": "2.18.34",
+  "version": "2.18.38",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
       "name": "telegram",
-      "version": "2.18.34",
+      "version": "2.18.38",
       "license": "MIT",
       "dependencies": {
         "@cryptography/aes": "^0.1.1",
diff --git a/package.json b/package.json
index 4e39a126..c61d4369 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "telegram",
-  "version": "2.18.34",
+  "version": "2.18.38",
   "description": "NodeJS/Browser MTProto API Telegram client library,",
   "main": "index.js",
   "types": "index.d.ts",