From 58e8c41adb48fa895ea1c5511f4d4367b068f7d6 Mon Sep 17 00:00:00 2001 From: morethanwords Date: Sat, 29 May 2021 15:23:45 +0300 Subject: [PATCH] Update text entities when message is sent --- src/layer.d.ts | 3 +- src/lib/appManagers/appMessagesManager.ts | 34 ++++++++++++++------ src/scripts/in/schema_additional_params.json | 5 +++ src/types.d.ts | 1 + 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/layer.d.ts b/src/layer.d.ts index e3443aff..39db1e50 100644 --- a/src/layer.d.ts +++ b/src/layer.d.ts @@ -2192,7 +2192,8 @@ export namespace Update { export type updateDraftMessage = { _: 'updateDraftMessage', peer: Peer, - draft: DraftMessage + draft: DraftMessage, + local?: boolean }; export type updateReadFeaturedStickers = { diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index 19477b20..ed6adfb5 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -17,7 +17,7 @@ import { createPosterForVideo } from "../../helpers/files"; import { copy, getObjectKeysAndSort } from "../../helpers/object"; import { randomLong } from "../../helpers/random"; import { splitStringByLength, limitSymbols, escapeRegExp } from "../../helpers/string"; -import { Chat, ChatFull, Dialog as MTDialog, DialogPeer, DocumentAttribute, InputMedia, InputMessage, InputPeerNotifySettings, InputSingleMedia, Message, MessageAction, MessageEntity, MessageFwdHeader, MessageMedia, MessageReplies, MessageReplyHeader, MessagesDialogs, MessagesFilter, MessagesMessages, MethodDeclMap, NotifyPeer, PeerNotifySettings, PhotoSize, SendMessageAction, Update, Photo } from "../../layer"; +import { Chat, ChatFull, Dialog as MTDialog, DialogPeer, DocumentAttribute, InputMedia, InputMessage, InputPeerNotifySettings, InputSingleMedia, Message, MessageAction, MessageEntity, MessageFwdHeader, MessageMedia, MessageReplies, MessageReplyHeader, MessagesDialogs, MessagesFilter, MessagesMessages, MethodDeclMap, NotifyPeer, PeerNotifySettings, PhotoSize, SendMessageAction, Update, Photo, Updates } from "../../layer"; import { InvokeApiOptions } from "../../types"; import I18n, { i18n, join, langPack, LangPackKey, _i18n } from "../langPack"; import { logger, LogTypes } from "../logger"; @@ -488,14 +488,24 @@ export class AppMessagesManager { }, sentRequestOptions); } + /* function is(value: any, condition: boolean): value is T { + return condition; + } */ + //this.log('sendText', message.mid); - apiPromise.then((updates: any) => { + apiPromise.then((updates: Updates) => { //this.log('sendText sent', message.mid); + //if(is(updates, updates._ === 'updateShortSentMessage')) { if(updates._ === 'updateShortSentMessage') { + //assumeType(updates); message.date = updates.date; message.id = updates.id; message.media = updates.media; message.entities = updates.entities; + this.wrapMessageEntities(message); + if(updates.pFlags.out) { + message.pFlags.out = true; + } // * override with new updates updates = { @@ -513,9 +523,9 @@ export class AppMessagesManager { pts: updates.pts, pts_count: updates.pts_count }] - }; - } else if(updates.updates) { - updates.updates.forEach((update: any) => { + } as any; + } else if((updates as Updates.updates).updates) { + (updates as Updates.updates).updates.forEach((update) => { if(update._ === 'updateDraftMessage') { update.local = true; } @@ -2404,11 +2414,7 @@ export class AppMessagesManager { } */ if(message.message && message.message.length && !message.totalEntities) { - const apiEntities = message.entities ? message.entities.slice() : []; - message.message = RichTextProcessor.fixEmoji(message.message, apiEntities); - - const myEntities = RichTextProcessor.parseEntities(message.message); - message.totalEntities = RichTextProcessor.mergeEntities(apiEntities, myEntities); // ! only in this order, otherwise bold and emoji formatting won't work + this.wrapMessageEntities(message); } storage[mid] = message; @@ -2425,6 +2431,14 @@ export class AppMessagesManager { } */ } + private wrapMessageEntities(message: any) { + const apiEntities = message.entities ? message.entities.slice() : []; + message.message = RichTextProcessor.fixEmoji(message.message, apiEntities); + + const myEntities = RichTextProcessor.parseEntities(message.message); + message.totalEntities = RichTextProcessor.mergeEntities(apiEntities, myEntities); // ! only in this order, otherwise bold and emoji formatting won't work + } + public wrapMessageForReply(message: any, text: string, usingMids: number[], plain: true, highlightWord?: string): string; public wrapMessageForReply(message: any, text?: string, usingMids?: number[], plain?: false, highlightWord?: string): DocumentFragment; public wrapMessageForReply(message: any, text: string = message.message, usingMids?: number[], plain?: boolean, highlightWord?: string): DocumentFragment | string { diff --git a/src/scripts/in/schema_additional_params.json b/src/scripts/in/schema_additional_params.json index e46e2da2..7e50b57a 100644 --- a/src/scripts/in/schema_additional_params.json +++ b/src/scripts/in/schema_additional_params.json @@ -226,6 +226,11 @@ {"name": "channel_id", "type": "number"} ], "type": "Update" +}, { + "predicate": "updateDraftMessage", + "params": [ + {"name": "local", "type": "boolean"} + ] }, { "predicate": "messages.stickerSet", "params": [ diff --git a/src/types.d.ts b/src/types.d.ts index 1c107f97..4964d120 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -35,6 +35,7 @@ export type Modify = Omit & R; export type ArgumentTypes = F extends (...args: infer A) => any ? A : never; export type SuperReturnType = F extends (...args: any) => any ? ReturnType : never; +export declare function assumeType(x: unknown): asserts x is T; export type AnyLiteral = Record; export type AnyClass = new (...args: any[]) => any;