From 0ff02268ee180c1fdc566f9af9b216713b7fcab0 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Mon, 21 Dec 2020 06:55:53 +0200 Subject: [PATCH] Fix chat menu opening Generate sending from_id --- src/components/buttonMenuToggle.ts | 6 +- src/components/chat/bubbles.ts | 5 +- src/components/chat/topbar.ts | 11 ++- src/lib/appManagers/appMessagesManager.ts | 109 ++++++++++------------ 4 files changed, 59 insertions(+), 72 deletions(-) diff --git a/src/components/buttonMenuToggle.ts b/src/components/buttonMenuToggle.ts index 8ca5fea9..ba2a7d63 100644 --- a/src/components/buttonMenuToggle.ts +++ b/src/components/buttonMenuToggle.ts @@ -4,7 +4,7 @@ import ButtonIcon from "./buttonIcon"; import ButtonMenu, { ButtonMenuItemOptions } from "./buttonMenu"; import { closeBtnMenu, openBtnMenu } from "./misc"; -const ButtonMenuToggle = (options: Partial<{noRipple: true, onlyMobile: true, listenerSetter: ListenerSetter}> = {}, direction: 'bottom-left' | 'top-left', buttons: ButtonMenuItemOptions[], onOpen?: () => void) => { +const ButtonMenuToggle = (options: Partial<{noRipple: true, onlyMobile: true, listenerSetter: ListenerSetter}> = {}, direction: 'bottom-left' | 'top-left', buttons: ButtonMenuItemOptions[], onOpen?: (e: Event) => void) => { const button = ButtonIcon('more btn-menu-toggle', options); const btnMenu = ButtonMenu(buttons, options.listenerSetter); @@ -15,7 +15,7 @@ const ButtonMenuToggle = (options: Partial<{noRipple: true, onlyMobile: true, li }; // TODO: refactor for attachClickEvent, because if move finger after touchstart, it will start anyway -const ButtonMenuToggleHandler = (el: HTMLElement, onOpen?: () => void, options?: AttachClickOptions) => { +const ButtonMenuToggleHandler = (el: HTMLElement, onOpen?: (e: Event) => void, options?: AttachClickOptions) => { const add = options?.listenerSetter ? options.listenerSetter.add.bind(options.listenerSetter, el) : el.addEventListener.bind(el); add(CLICK_EVENT_NAME, (e: Event) => { @@ -29,7 +29,7 @@ const ButtonMenuToggleHandler = (el: HTMLElement, onOpen?: () => void, options?: if(el.classList.contains('menu-open')) { closeBtnMenu(); } else { - onOpen && onOpen(); + onOpen && onOpen(e); openBtnMenu(openedMenu); } }); diff --git a/src/components/chat/bubbles.ts b/src/components/chat/bubbles.ts index 354e4187..223561a3 100644 --- a/src/components/chat/bubbles.ts +++ b/src/components/chat/bubbles.ts @@ -2120,7 +2120,8 @@ export default class ChatBubbles { let savedFrom = ''; - if((this.peerId < 0 && !our) || message.fwd_from || message.reply_to_mid) { // chat + const needName = (peerId < 0 && (peerId != message.fromId || our)) && message.fromId !== rootScope.myId; + if(needName || message.fwd_from || message.reply_to_mid) { // chat let title = this.appPeersManager.getPeerTitle(message.fwdFromId || message.fromId); const isForwardFromChannel = message.from_id && message.from_id._ == 'peerChannel' && message.fromId == message.fwdFromId; @@ -2186,7 +2187,7 @@ export default class ChatBubbles { bubble.classList.add('is-reply'); } - if(!bubble.classList.contains('sticker') && (peerId < 0 && (peerId != message.fromId || our)) && message.fromId !== rootScope.myId) { + if(!bubble.classList.contains('sticker') && needName) { let nameDiv = document.createElement('div'); nameDiv.classList.add('name'); nameDiv.innerHTML = title; diff --git a/src/components/chat/topbar.ts b/src/components/chat/topbar.ts index befeceae..c734c3ea 100644 --- a/src/components/chat/topbar.ts +++ b/src/components/chat/topbar.ts @@ -97,7 +97,8 @@ export default class ChatTopbar { this.chatAudio = new ChatAudio(this, this.chat, this.appMessagesManager, this.appPeersManager); if(this.menuButtons.length) { - this.btnMore = ButtonMenuToggle({listenerSetter: this.listenerSetter}, 'bottom-left', this.menuButtons, () => { + this.btnMore = ButtonMenuToggle({listenerSetter: this.listenerSetter}, 'bottom-left', this.menuButtons, (e) => { + cancelEvent(e); this.menuButtons.forEach(button => { button.element.classList.toggle('hide', !button.verify()); }); @@ -118,7 +119,7 @@ export default class ChatTopbar { this.listenerSetter.add(window, 'resize', this.onResize); mediaSizes.addListener('changeScreen', this.onChangeScreen); - this.listenerSetter.add(this.container, 'click', (e) => { + attachClickEvent(this.container, (e) => { const container: HTMLElement = findUpClassName(e.target, 'pinned-container'); if(container) { cancelEvent(e); @@ -135,12 +136,12 @@ export default class ChatTopbar { } else { this.appSidebarRight.toggleSidebar(true); } - }); + }, {listenerSetter: this.listenerSetter}); - this.listenerSetter.add(this.btnBack, 'click', (e) => { + attachClickEvent(this.btnBack, (e) => { cancelEvent(e); this.chat.appImManager.setPeer(0); - }); + }, {listenerSetter: this.listenerSetter}); } public constructPeerHelpers() { diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index dfe7d667..90e2c530 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -455,45 +455,28 @@ export class AppMessagesManager { peerId = appPeersManager.getPeerMigratedTo(peerId) || peerId; - var entities = options.entities || []; + let entities = options.entities || []; if(!options.viaBotId) { text = RichTextProcessor.parseMarkdown(text, entities); } - var sendEntites = this.getInputEntities(entities); + let sendEntites = this.getInputEntities(entities); if(!sendEntites.length) { sendEntites = undefined; } - var messageId = this.generateTempMessageId(peerId); - var randomIdS = randomLong(); - var pFlags: any = {}; - var replyToMsgId = options.replyToMsgId ? this.getLocalMessageId(options.replyToMsgId) : undefined; - var isChannel = appPeersManager.isChannel(peerId); - var isMegagroup = isChannel && appPeersManager.isMegagroup(peerId); - var asChannel = isChannel && !isMegagroup ? true : false; - var message: any; - - var fromId = appUsersManager.getSelf().id; - if(peerId != fromId) { - pFlags.out = true; - - if(!isChannel && !appUsersManager.isBot(peerId)) { - pFlags.unread = true; - } - } - - if(asChannel) { - fromId = 0; - pFlags.post = true; - } + const messageId = this.generateTempMessageId(peerId); + const randomIdS = randomLong(); + const replyToMsgId = options.replyToMsgId ? this.getLocalMessageId(options.replyToMsgId) : undefined; + const isChannel = appPeersManager.isChannel(peerId); + const isBroadcast = appPeersManager.isBroadcast(peerId); - message = { + const message: any = { _: 'message', id: messageId, - from_id: appPeersManager.getOutputPeer(fromId), + from_id: this.generateFromId(peerId), peer_id: appPeersManager.getOutputPeer(peerId), - pFlags, + pFlags: this.generateFlags(peerId), date: options.scheduleDate || (tsNow(true) + serverTimeManager.serverTimeOffset), message: text, random_id: randomIdS, @@ -501,7 +484,7 @@ export class AppMessagesManager { via_bot_id: options.viaBotId, reply_markup: options.reply_markup, entities: entities, - views: asChannel && 1, + views: isBroadcast && 1, pending: true }; @@ -512,7 +495,7 @@ export class AppMessagesManager { }; } - var toggleError = (on: any) => { + const toggleError = (on: any) => { if(on) { message.error = true; } else { @@ -523,12 +506,12 @@ export class AppMessagesManager { message.send = () => { toggleError(false); - var sentRequestOptions: any = {}; + const sentRequestOptions: any = {}; if(this.pendingAfterMsgs[peerId]) { sentRequestOptions.afterMessageId = this.pendingAfterMsgs[peerId].messageId; } - var apiPromise: any; + let apiPromise: any; if(options.viaBotId) { apiPromise = apiManager.invokeApiAfter('messages.sendInlineBotResult', { peer: appPeersManager.getInputPeerById(peerId), @@ -631,7 +614,7 @@ export class AppMessagesManager { //this.checkSendOptions(options); const messageId = this.generateTempMessageId(peerId); const randomIdS = randomLong(); - const pFlags: any = {}; + const pFlags = this.generateFlags(peerId); const replyToMsgId = options.replyToMsgId ? this.getLocalMessageId(options.replyToMsgId) : undefined; const isChannel = appPeersManager.isChannel(peerId); const isMegagroup = isChannel && appPeersManager.isMegagroup(peerId); @@ -777,20 +760,6 @@ export class AppMessagesManager { this.log('AMM: sendFile', attachType, apiFileName, file.type, options); - let fromId = appUsersManager.getSelf().id; - if(peerId != fromId) { - pFlags.out = true; - - if(!isChannel && !appUsersManager.isBot(peerId)) { - pFlags.unread = true; - } - } - - if(asChannel) { - fromId = 0; - pFlags.post = true; - } - const preloader = new ProgressivePreloader(null, true, false, 'prepend'); const media = { @@ -810,7 +779,7 @@ export class AppMessagesManager { const message: any = { _: 'message', id: messageId, - from_id: appPeersManager.getOutputPeer(fromId), + from_id: this.generateFromId(peerId), peer_id: appPeersManager.getOutputPeer(peerId), pFlags, date, @@ -1198,25 +1167,12 @@ export class AppMessagesManager { break; */ } - let pFlags: any = {}; - if(peerId != fromId) { - pFlags.out = true; - if(!appUsersManager.isBot(peerId)) { - pFlags.unread = true; - } - } - - if(asChannel) { - fromId = 0; - pFlags.post = true; - } - const message: any = { _: 'message', id: messageId, - from_id: appPeersManager.getOutputPeer(fromId), + from_id: this.generateFromId(peerId), peer_id: appPeersManager.getOutputPeer(peerId), - pFlags, + pFlags: this.generateFlags(peerId), date: options.scheduleDate || (tsNow(true) + serverTimeManager.serverTimeOffset), message: '', media, @@ -1352,6 +1308,35 @@ export class AppMessagesManager { return header; } + /** + * Generate correct from_id according to anonymous or broadcast + */ + public generateFromId(peerId: number) { + if(peerId < 0 && (appPeersManager.isBroadcast(peerId) || appPeersManager.getPeer(peerId).admin_rights?.pFlags?.anonymous)) { + return undefined; + } else { + return appPeersManager.getOutputPeer(peerId); + } + } + + public generateFlags(peerId: number) { + const pFlags: any = {}; + const fromId = appUsersManager.getSelf().id; + if(peerId !== fromId) { + pFlags.out = true; + + if(!appPeersManager.isChannel(peerId) && !appUsersManager.isBot(peerId)) { + pFlags.unread = true; + } + } + + if(appPeersManager.isBroadcast(peerId)) { + pFlags.post = true; + } + + return pFlags; + } + private setDialogIndexByMessage(dialog: MTDialog.dialog, message: MyMessage) { if(!dialog.pFlags.pinned || !dialog.index) { dialog.index = this.dialogsStorage.generateDialogIndex(message.date);