Update text entities when message is sent

This commit is contained in:
morethanwords 2021-05-29 15:23:45 +03:00
parent e131b0a42b
commit 58e8c41adb
4 changed files with 32 additions and 11 deletions

3
src/layer.d.ts vendored
View File

@ -2192,7 +2192,8 @@ export namespace Update {
export type updateDraftMessage = {
_: 'updateDraftMessage',
peer: Peer,
draft: DraftMessage
draft: DraftMessage,
local?: boolean
};
export type updateReadFeaturedStickers = {

View File

@ -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<T>(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.updateShortSentMessage>(updates, updates._ === 'updateShortSentMessage')) {
if(updates._ === 'updateShortSentMessage') {
//assumeType<Updates.updateShortSentMessage>(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 {

View File

@ -226,6 +226,11 @@
{"name": "channel_id", "type": "number"}
],
"type": "Update"
}, {
"predicate": "updateDraftMessage",
"params": [
{"name": "local", "type": "boolean"}
]
}, {
"predicate": "messages.stickerSet",
"params": [

1
src/types.d.ts vendored
View File

@ -35,6 +35,7 @@ export type Modify<T, R> = Omit<T, keyof R> & R;
export type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any ? A : never;
export type SuperReturnType<F extends Function> = F extends (...args: any) => any ? ReturnType<F> : never;
export declare function assumeType<T>(x: unknown): asserts x is T;
export type AnyLiteral = Record<string, any>;
export type AnyClass = new (...args: any[]) => any;