Browse Source

Update text entities when message is sent

master
morethanwords 4 years ago
parent
commit
58e8c41adb
  1. 3
      src/layer.d.ts
  2. 34
      src/lib/appManagers/appMessagesManager.ts
  3. 5
      src/scripts/in/schema_additional_params.json
  4. 1
      src/types.d.ts

3
src/layer.d.ts vendored

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

34
src/lib/appManagers/appMessagesManager.ts

@ -17,7 +17,7 @@ import { createPosterForVideo } from "../../helpers/files";
import { copy, getObjectKeysAndSort } from "../../helpers/object"; import { copy, getObjectKeysAndSort } from "../../helpers/object";
import { randomLong } from "../../helpers/random"; import { randomLong } from "../../helpers/random";
import { splitStringByLength, limitSymbols, escapeRegExp } from "../../helpers/string"; 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 { InvokeApiOptions } from "../../types";
import I18n, { i18n, join, langPack, LangPackKey, _i18n } from "../langPack"; import I18n, { i18n, join, langPack, LangPackKey, _i18n } from "../langPack";
import { logger, LogTypes } from "../logger"; import { logger, LogTypes } from "../logger";
@ -488,14 +488,24 @@ export class AppMessagesManager {
}, sentRequestOptions); }, sentRequestOptions);
} }
/* function is<T>(value: any, condition: boolean): value is T {
return condition;
} */
//this.log('sendText', message.mid); //this.log('sendText', message.mid);
apiPromise.then((updates: any) => { apiPromise.then((updates: Updates) => {
//this.log('sendText sent', message.mid); //this.log('sendText sent', message.mid);
//if(is<Updates.updateShortSentMessage>(updates, updates._ === 'updateShortSentMessage')) {
if(updates._ === 'updateShortSentMessage') { if(updates._ === 'updateShortSentMessage') {
//assumeType<Updates.updateShortSentMessage>(updates);
message.date = updates.date; message.date = updates.date;
message.id = updates.id; message.id = updates.id;
message.media = updates.media; message.media = updates.media;
message.entities = updates.entities; message.entities = updates.entities;
this.wrapMessageEntities(message);
if(updates.pFlags.out) {
message.pFlags.out = true;
}
// * override with new updates // * override with new updates
updates = { updates = {
@ -513,9 +523,9 @@ export class AppMessagesManager {
pts: updates.pts, pts: updates.pts,
pts_count: updates.pts_count pts_count: updates.pts_count
}] }]
}; } as any;
} else if(updates.updates) { } else if((updates as Updates.updates).updates) {
updates.updates.forEach((update: any) => { (updates as Updates.updates).updates.forEach((update) => {
if(update._ === 'updateDraftMessage') { if(update._ === 'updateDraftMessage') {
update.local = true; update.local = true;
} }
@ -2404,11 +2414,7 @@ export class AppMessagesManager {
} */ } */
if(message.message && message.message.length && !message.totalEntities) { if(message.message && message.message.length && !message.totalEntities) {
const apiEntities = message.entities ? message.entities.slice() : []; this.wrapMessageEntities(message);
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
} }
storage[mid] = 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: true, highlightWord?: string): string;
public wrapMessageForReply(message: any, text?: string, usingMids?: number[], plain?: false, highlightWord?: string): DocumentFragment; 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 { public wrapMessageForReply(message: any, text: string = message.message, usingMids?: number[], plain?: boolean, highlightWord?: string): DocumentFragment | string {

5
src/scripts/in/schema_additional_params.json

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

1
src/types.d.ts vendored

@ -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 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 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 AnyLiteral = Record<string, any>;
export type AnyClass = new (...args: any[]) => any; export type AnyClass = new (...args: any[]) => any;

Loading…
Cancel
Save