From 223fe64ac14c2bad9d079fa1983dbfb562cb0b1c Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Fri, 18 Dec 2020 05:51:45 +0200 Subject: [PATCH] Pinned message setting confirmations Fix hiding hidden pinned messages --- src/components/chat/input.ts | 10 +--- src/components/chat/pinnedMessage.ts | 7 +-- src/components/chat/topbar.ts | 21 +++++--- src/components/popups/unpinMessage.ts | 76 ++++++++++++++++++++++----- 4 files changed, 84 insertions(+), 30 deletions(-) diff --git a/src/components/chat/input.ts b/src/components/chat/input.ts index 76a757f5..6e5e072d 100644 --- a/src/components/chat/input.ts +++ b/src/components/chat/input.ts @@ -31,6 +31,7 @@ import Button from '../button'; import PopupSchedule from '../popups/schedule'; import SendMenu from './sendContextMenu'; import rootScope from '../../lib/rootScope'; +import PopupPinMessage from '../popups/unpinMessage'; const RECORD_MIN_TIME = 500; const POSTING_MEDIA_NOT_ALLOWED = 'Posting media content isn\'t allowed in this group.'; @@ -403,14 +404,7 @@ export default class ChatInput { this.listenerSetter.add(this.pinnedControlBtn, 'click', () => { const peerId = this.chat.peerId; - let promise: Promise; - if(this.appPeersManager.canPinMessage(peerId)) { - promise = this.appMessagesManager.unpinAllMessages(peerId); - } else { - promise = this.appMessagesManager.hidePinnedMessages(peerId); - } - - promise.then(() => { + new PopupPinMessage(peerId, 0, true, () => { this.chat.appImManager.setPeer(0); // * close tab // ! костыль, это скроет закреплённые сообщения сразу, вместо того, чтобы ждать пока анимация перехода закончится diff --git a/src/components/chat/pinnedMessage.ts b/src/components/chat/pinnedMessage.ts index 6c8d3f4d..71178ea3 100644 --- a/src/components/chat/pinnedMessage.ts +++ b/src/components/chat/pinnedMessage.ts @@ -244,13 +244,14 @@ export default class ChatPinnedMessage { constructor(private topbar: ChatTopbar, private chat: Chat, private appMessagesManager: AppMessagesManager, private appPeersManager: AppPeersManager) { this.listenerSetter = new ListenerSetter(); - this.pinnedMessageContainer = new PinnedContainer(topbar, chat, this.listenerSetter, 'message', new ReplyContainer('pinned-message'), () => { + this.pinnedMessageContainer = new PinnedContainer(topbar, chat, this.listenerSetter, 'message', new ReplyContainer('pinned-message'), async() => { if(appPeersManager.canPinMessage(this.topbar.peerId)) { new PopupPinMessage(this.topbar.peerId, this.pinnedMid, true); - return Promise.resolve(false); } else { - return this.appMessagesManager.hidePinnedMessages(this.topbar.peerId).then(() => true); + new PopupPinMessage(this.topbar.peerId, 0, true); } + + return false; }); this.pinnedMessageBorder = new PinnedMessageBorder(); diff --git a/src/components/chat/topbar.ts b/src/components/chat/topbar.ts index 5b0f7fd9..90167fc1 100644 --- a/src/components/chat/topbar.ts +++ b/src/components/chat/topbar.ts @@ -266,13 +266,20 @@ export default class ChatTopbar { }); this.chat.addListener('setPeer', (mid, isTopMessage) => { - if(isTopMessage) { - this.pinnedMessage.unsetScrollDownListener(); - this.pinnedMessage.testMid(mid, 0); // * because slider will not let get bubble by document.elementFromPoint - } else if(!this.pinnedMessage.locked) { - this.pinnedMessage.handleFollowingPinnedMessage(); - this.pinnedMessage.testMid(mid); - } + const middleware = this.chat.bubbles.getMiddleware(); + appStateManager.getState().then((state) => { + if(!middleware()) return; + + this.pinnedMessage.hidden = !!state.hiddenPinnedMessages[this.chat.peerId]; + + if(isTopMessage) { + this.pinnedMessage.unsetScrollDownListener(); + this.pinnedMessage.testMid(mid, 0); // * because slider will not let get bubble by document.elementFromPoint + } else if(!this.pinnedMessage.locked) { + this.pinnedMessage.handleFollowingPinnedMessage(); + this.pinnedMessage.testMid(mid); + } + }); }); this.setPeerStatusInterval = window.setInterval(this.setPeerStatus, 60e3); diff --git a/src/components/popups/unpinMessage.ts b/src/components/popups/unpinMessage.ts index 04fc9884..edbab051 100644 --- a/src/components/popups/unpinMessage.ts +++ b/src/components/popups/unpinMessage.ts @@ -1,31 +1,83 @@ import appMessagesManager from "../../lib/appManagers/appMessagesManager"; import { PopupButton } from "."; import PopupPeer from "./peer"; +import appPeersManager from "../../lib/appManagers/appPeersManager"; export default class PopupPinMessage { - constructor(peerId: number, mid: number, unpin?: true) { + constructor(peerId: number, mid: number, unpin?: true, onConfirm?: () => void) { let title: string, description: string, buttons: PopupButton[] = []; - const callback = () => { + const canUnpin = appPeersManager.canPinMessage(peerId); + + const callback = (oneSide?: true, silent?: true) => { setTimeout(() => { // * костыль, потому что document.elementFromPoint вернёт popup-peer пока он будет закрываться - appMessagesManager.updatePinnedMessage(peerId, mid, unpin); + let promise: Promise; + if(unpin && !mid) { + if(canUnpin) { + promise = appMessagesManager.unpinAllMessages(peerId); + } else { + promise = appMessagesManager.hidePinnedMessages(peerId); + } + } else { + promise = appMessagesManager.updatePinnedMessage(peerId, mid, unpin, silent, oneSide); + } + + if(onConfirm) { + promise.then(onConfirm); + } }, 300); }; + + const firstName = appPeersManager.getPeerTitle(peerId, false, true); + if(unpin) { - title = `Unpin Message?`; - description = 'Would you like to unpin this message?'; + let buttonText = 'UNPIN'; + if(!mid) { + if(canUnpin) { + title = 'Unpin All Messages?'; + description = 'Would you like to unpin all messages?'; + } else { + title = 'Hide Pinned Messages?'; + description = 'Do you want to hide the pinned message bar? It wil stay hidden until a new message is pinned.'; + buttonText = 'HIDE'; + } + } else { + title = `Unpin Message?`; + description = 'Would you like to unpin this message?'; + } + buttons.push({ - text: 'UNPIN', + text: buttonText, isDanger: true, - callback + callback: () => callback() }); } else { title = 'Pin Message?'; - description = 'Would you like to pin this message?'; - buttons.push({ - text: 'PIN', - callback - }); + + if(peerId < 0) { + description = 'Do you want to pin this message for all members in the group?'; + buttons.push({ + text: 'PIN AND NOTIFY', + callback: () => callback() + }); + + buttons.push({ + text: 'PIN WITHOUT NOTIFYING', + callback: () => callback(undefined, true) + }); + } else { + description = 'Would you like to pin this message?'; + + buttons.push({ + text: 'PIN JUST FOR ME', + callback: () => callback(true) + }); + + buttons.push({ + text: 'PIN FOR ME AND ' + firstName, + callback: () => callback() + }); + } } buttons.push({