Skip to content

Commit

Permalink
Update attributes to accept list for send album
Browse files Browse the repository at this point in the history
  • Loading branch information
painor committed Sep 28, 2023
1 parent 52d7997 commit 8579962
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 53 deletions.
22 changes: 11 additions & 11 deletions __tests__/extensions/MarkdownV2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*");
Expand Down
3 changes: 1 addition & 2 deletions gramjs/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1362,4 +1361,4 @@ export function isListLike(item) {
)
)
}
*/
*/
2 changes: 1 addition & 1 deletion gramjs/Version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = "2.18.33";
export const version = "2.18.37";
3 changes: 2 additions & 1 deletion gramjs/client/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
8 changes: 3 additions & 5 deletions gramjs/client/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 11 additions & 2 deletions gramjs/client/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!<br/>
* The file must also be small in dimensions and in disk size. Successful thumbnails were files below 20kB and 320x320px.<br/>
* Width/height and dimensions/size ratios may be important.
Expand Down Expand Up @@ -545,20 +545,27 @@ 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, {
file: file,
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
Expand Down Expand Up @@ -667,6 +674,7 @@ export async function sendFile(
caption: caption,
replyTo: replyTo,
parseMode: parseMode,
attributes: attributes,
silent: silent,
scheduleDate: scheduleDate,
supportsStreaming: supportsStreaming,
Expand Down Expand Up @@ -695,6 +703,7 @@ export async function sendFile(
forceDocument: forceDocument,
fileSize: fileSize,
progressCallback: progressCallback,
// @ts-ignore
attributes: attributes,
thumb: thumb,
voiceNote: voiceNote,
Expand Down
7 changes: 4 additions & 3 deletions gramjs/extensions/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,10 @@ export class HTMLParser {
`<a href="tg://user?id=${entity.userId}">${entityText}</a>`
);
} else if (entity instanceof Api.MessageEntityCustomEmoji) {
html.push(`<tg-emoji emoji-id="${entity.documentId}">${entityText}</tg-emoji>`);
}
else {
html.push(
`<tg-emoji emoji-id="${entity.documentId}">${entityText}</tg-emoji>`
);
} else {
skipEntity = true;
}
lastOffset = relativeOffset + (skipEntity ? 0 : length);
Expand Down
59 changes: 34 additions & 25 deletions gramjs/extensions/markdownv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,76 @@ import { HTMLParser } from "./html";
export class MarkdownV2Parser {
static parse(message: string): [string, Api.TypeMessageEntity[]] {
// Bold
message = message.replace(/\*(.*?)\*/g, '<b>$1</b>');
message = message.replace(/\*(.*?)\*/g, "<b>$1</b>");

// underline
message = message.replace(/__(.*?)__/g, '<u>$1</u>');
message = message.replace(/__(.*?)__/g, "<u>$1</u>");

// strikethrough
message = message.replace(/~(.*?)~/g, '<s>$1</s>');
message = message.replace(/~(.*?)~/g, "<s>$1</s>");

// italic
message = message.replace(/-(.*?)-/g, '<i>$1</i>');
message = message.replace(/-(.*?)-/g, "<i>$1</i>");

// pre
message = message.replace(/```(.*?)```/g, '<pre>$1</pre>');
message = message.replace(/```(.*?)```/g, "<pre>$1</pre>");

// code
message = message.replace(/`(.*?)`/g, '<code>$1</code>');
message = message.replace(/`(.*?)`/g, "<code>$1</code>");

// Spoiler
message = message.replace(/\|\|(.*?)\|\|/g, '<spoiler>$1</spoiler>');
// Spoiler
message = message.replace(/\|\|(.*?)\|\|/g, "<spoiler>$1</spoiler>");

// Inline URL
message = message.replace(/(?<!\!)\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
message = message.replace(
/(?<!\!)\[([^\]]+)\]\(([^)]+)\)/g,
'<a href="$2">$1</a>'
);

// Emoji
message = message.replace(/!\[([^\]]+)\]\(tg:\/\/emoji\?id=(\d+)\)/g, '<tg-emoji emoji-id="$2">$1</tg-emoji>');
return HTMLParser.parse(message)
// Emoji
message = message.replace(
/!\[([^\]]+)\]\(tg:\/\/emoji\?id=(\d+)\)/g,
'<tg-emoji emoji-id="$2">$1</tg-emoji>'
);
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>(.*?)<\/b>/g, '*$1*');
text = text.replace(/<b>(.*?)<\/b>/g, "*$1*");

// Underline
text = text.replace(/<u>(.*?)<\/u>/g, '__$1__');
text = text.replace(/<u>(.*?)<\/u>/g, "__$1__");

// Code
text = text.replace(/<code>(.*?)<\/code>/g, '`$1`');
text = text.replace(/<code>(.*?)<\/code>/g, "`$1`");

// Pre
text = text.replace(/<pre>(.*?)<\/pre>/g, '```$1```');
text = text.replace(/<pre>(.*?)<\/pre>/g, "```$1```");

// strikethrough
text = text.replace(/<s>(.*?)<\/s>/g, '~$1~');
text = text.replace(/<s>(.*?)<\/s>/g, "~$1~");

// Italic
text = text.replace(/<i>(.*?)<\/i>/g, '-$1-');
text = text.replace(/<i>(.*?)<\/i>/g, "-$1-");

// Spoiler
text = text.replace(/<spoiler>(.*?)<\/spoiler>/g, '||$1||');
text = text.replace(/<spoiler>(.*?)<\/spoiler>/g, "||$1||");

// Inline URL
text = text.replace(/<a href="([^"]+)">([^<]+)<\/a>/g, '[$2]($1)');
text = text.replace(/<a href="([^"]+)">([^<]+)<\/a>/g, "[$2]($1)");

// Emoji
text = text.replace(/<tg-emoji emoji-id="(\d+)">([^<]+)<\/tg-emoji>/g, '![$2](tg://emoji?id=$1)');
text = text.replace(
/<tg-emoji emoji-id="(\d+)">([^<]+)<\/tg-emoji>/g,
"![$2](tg://emoji?id=$1)"
);

return text
return text;
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 8579962

Please sign in to comment.