From 1b67588fc08b0e43f31adc09bf850eb5dd4f0106 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Sat, 19 Dec 2020 03:44:41 +0200 Subject: [PATCH] Fix bubble eposition Fix audio first click after peer change Fix reply in scheduled --- src/components/audio.ts | 26 ++++++++++++++--------- src/components/chat/bubbles.ts | 23 +++++++++++++------- src/helpers/dom.ts | 6 +++--- src/lib/appManagers/appMessagesManager.ts | 6 +++--- src/lib/appManagers/appPollsManager.ts | 6 +++--- src/lib/rootScope.ts | 2 +- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/components/audio.ts b/src/components/audio.ts index 61ccae54..ee3a4129 100644 --- a/src/components/audio.ts +++ b/src/components/audio.ts @@ -30,23 +30,24 @@ export function decodeWaveform(waveform: Uint8Array | number[]) { waveform = new Uint8Array(waveform); } - var bitCount = waveform.length * 8; - var valueCount = bitCount / 5 | 0; + const bitCount = waveform.length * 8; + const valueCount = bitCount / 5 | 0; if(!valueCount) { return new Uint8Array([]); } + let result: Uint8Array; try { - var dataView = new DataView(waveform.buffer); - var result = new Uint8Array(valueCount); - for(var i = 0; i < valueCount; i++) { - var byteIndex = i * 5 / 8 | 0; - var bitShift = i * 5 % 8; - var value = dataView.getUint16(byteIndex, true); + const dataView = new DataView(waveform.buffer); + result = new Uint8Array(valueCount); + for(let i = 0; i < valueCount; i++) { + const byteIndex = i * 5 / 8 | 0; + const bitShift = i * 5 % 8; + const value = dataView.getUint16(byteIndex, true); result[i] = (value >> bitShift) & 0b00011111; } } catch(err) { - return new Uint8Array([]); + result = new Uint8Array([]); } /* var byteIndex = (valueCount - 1) / 8 | 0; @@ -448,6 +449,9 @@ export default class AudioElement extends HTMLElement { //onLoad(); //} else { const r = (e: Event) => { + if(this.audio.src) { + return; + } //onLoad(); //cancelEvent(e); appMediaPlaybackController.resolveWaitingForLoadMedia(this.message.peerId, this.message.mid); @@ -482,7 +486,9 @@ export default class AudioElement extends HTMLElement { }); }; - attachClickEvent(this, r, {once: true, capture: true, passive: false}); + if(!this.audio.src) { + attachClickEvent(this, r, {once: true, capture: true, passive: false}); + } //} } } else { diff --git a/src/components/chat/bubbles.ts b/src/components/chat/bubbles.ts index 743fafdc..a1715386 100644 --- a/src/components/chat/bubbles.ts +++ b/src/components/chat/bubbles.ts @@ -254,7 +254,9 @@ export default class ChatBubbles { this.renderMessage(mounted.message, true, false, mounted.bubble, updatePosition); if(updatePosition) { - this.deleteEmptyDateGroups(); + (this.messagesQueuePromise || Promise.resolve()).then(() => { + this.deleteEmptyDateGroups(); + }); } }); @@ -271,9 +273,13 @@ export default class ChatBubbles { }); this.listenerSetter.add(rootScope, 'messages_downloaded', (e) => { - const mids: number[] = e.detail; + const {peerId, mids} = e.detail; - mids.forEach(mid => { + if(peerId !== this.peerId) { + return; + } + + (mids as number[]).forEach(mid => { /* const promise = (this.scrollable.scrollLocked && this.scrollable.scrollLockedPromise) || Promise.resolve(); promise.then(() => { @@ -288,7 +294,7 @@ export default class ChatBubbles { const message = this.chat.getMessage(mid); - const repliedMessage = this.chat.getMessage(replyMid); + const repliedMessage = this.chat.type === 'scheduled' ? this.appMessagesManager.getMessageByPeer(this.peerId, replyMid) : this.chat.getMessage(replyMid); if(repliedMessage.deleted) { // чтобы не пыталось бесконечно загрузить удалённое сообщение delete message.reply_to_mid; // WARNING! } @@ -628,7 +634,8 @@ export default class ChatBubbles { if(isReplyClick && bubble.classList.contains('is-reply')/* || bubble.classList.contains('forwarded') */) { this.replyFollowHistory.push(+bubble.dataset.mid); let originalMessageId = +bubble.getAttribute('data-original-mid'); - this.chat.setPeer(this.peerId, originalMessageId); + this.chat.appImManager.setInnerPeer(this.peerId, originalMessageId); + //this.chat.setPeer(this.peerId, originalMessageId); } } else if(target.tagName == 'IMG' && target.parentElement.tagName == "AVATAR-ELEMENT") { let peerId = +target.parentElement.getAttribute('peer'); @@ -1369,12 +1376,12 @@ export default class ChatBubbles { // * 1 for date, 1 for date sentinel let index = 2 + i; - if(bubble.parentElement) { // * if already mounted + /* if(bubble.parentElement) { // * if already mounted const currentIndex = whichChild(bubble); if(index > currentIndex) { index -= 1; // * minus for already mounted } - } + } */ positionElementByIndex(bubble, dateMessage.container, index); } else { @@ -2111,7 +2118,7 @@ export default class ChatBubbles { } } else { if(message.reply_to_mid) { - let originalMessage = this.chat.getMessage(message.reply_to_mid); + let originalMessage = this.chat.type === 'scheduled' ? this.appMessagesManager.getMessageByPeer(this.peerId, message.reply_to_mid) : this.chat.getMessage(message.reply_to_mid); let originalPeerTitle = this.appPeersManager.getPeerTitle(originalMessage.fromId || originalMessage.fwdFromId, true) || ''; /////////this.log('message to render reply', originalMessage, originalPeerTitle, bubble, message); diff --git a/src/helpers/dom.ts b/src/helpers/dom.ts index ee0e7dfd..198cadd0 100644 --- a/src/helpers/dom.ts +++ b/src/helpers/dom.ts @@ -415,11 +415,11 @@ export function calcImageInBox(imageW: number, imageH: number, boxW: number, box } export function positionElementByIndex(element: HTMLElement, container: HTMLElement, pos: number) { - const prevPos = whichChild(element); + const prevPos = element.parentElement === container ? whichChild(element) : -1; - if(prevPos == pos) { + if(prevPos === pos) { return false; - } else if(prevPos != -1 && prevPos < pos) { // was higher + } else if(prevPos !== -1 && prevPos < pos) { // was higher pos += 1; } diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index 4eebf679..d0841768 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -4373,7 +4373,7 @@ export class AppMessagesManager { const msgIds: InputMessage[] = mids.map((msgId: number) => { return { _: 'inputMessageID', - id: msgId + id: this.getLocalMessageId(msgId) }; }); @@ -4396,7 +4396,7 @@ export class AppMessagesManager { this.saveMessages(getMessagesResult.messages); } - rootScope.broadcast('messages_downloaded', mids); + rootScope.broadcast('messages_downloaded', {peerId: +peerId, mids}); })); } @@ -4411,7 +4411,7 @@ export class AppMessagesManager { public wrapSingleMessage(peerId: number, msgId: number, overwrite = false): Promise { if(this.getMessagesStorage(peerId)[msgId] && !overwrite) { - rootScope.broadcast('messages_downloaded', [msgId]); + rootScope.broadcast('messages_downloaded', {peerId, mids: [msgId]}); return Promise.resolve(); } else if(!this.needSingleMessages[peerId] || this.needSingleMessages[peerId].indexOf(msgId) == -1) { (this.needSingleMessages[peerId] ?? (this.needSingleMessages[peerId] = [])).push(msgId); diff --git a/src/lib/appManagers/appPollsManager.ts b/src/lib/appManagers/appPollsManager.ts index a3327cfa..4f61a344 100644 --- a/src/lib/appManagers/appPollsManager.ts +++ b/src/lib/appManagers/appPollsManager.ts @@ -179,7 +179,7 @@ export class AppPollsManager { return apiManager.invokeApi('messages.sendVote', { peer: inputPeer, - msg_id: messageId, + msg_id: message.id, options }).then(updates => { this.log('sendVote updates:', updates); @@ -192,7 +192,7 @@ export class AppPollsManager { return apiManager.invokeApi('messages.getPollResults', { peer: inputPeer, - msg_id: message.mid + msg_id: message.id }).then(updates => { apiUpdatesManager.processUpdateMessage(updates); this.log('getResults updates:', updates); @@ -202,7 +202,7 @@ export class AppPollsManager { public getVotes(message: any, option?: Uint8Array, offset?: string, limit = 20) { return apiManager.invokeApi('messages.getPollVotes', { peer: appPeersManager.getInputPeerById(message.peerId), - id: message.mid, + id: message.id, option, offset, limit diff --git a/src/lib/rootScope.ts b/src/lib/rootScope.ts index abe61741..a98edc2f 100644 --- a/src/lib/rootScope.ts +++ b/src/lib/rootScope.ts @@ -43,7 +43,7 @@ type BroadcastEvents = { 'message_sent': {storage: MessagesStorage, tempId: number, tempMessage: any, mid: number}, 'messages_pending': void, 'messages_read': void, - 'messages_downloaded': number[], + 'messages_downloaded': {peerId: number, mids: number[]}, 'messages_media_read': {peerId: number, mids: number[]}, 'scheduled_new': {peerId: number, mid: number},