tweb/src/components/popups/unpinMessage.ts

121 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-04-08 15:52:31 +02:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import appMessagesManager from "../../lib/appManagers/appMessagesManager";
2021-07-16 16:16:05 +02:00
import { addCancelButton } from ".";
import PopupPeer, { PopupPeerButtonCallbackCheckboxes, PopupPeerOptions } from "./peer";
import appPeersManager from "../../lib/appManagers/appPeersManager";
2020-12-21 23:49:21 +01:00
import rootScope from "../../lib/rootScope";
2021-07-22 14:07:19 +02:00
import { FormatterArguments, LangPackKey } from "../../lib/langPack";
import appChatsManager from "../../lib/appManagers/appChatsManager";
import PeerTitle from "../peerTitle";
export default class PopupPinMessage {
2021-10-21 15:16:43 +02:00
constructor(peerId: PeerId, mid: number, unpin?: true, onConfirm?: () => void) {
2021-07-22 14:07:19 +02:00
let title: LangPackKey, description: LangPackKey, descriptionArgs: FormatterArguments,
buttons: PopupPeerOptions['buttons'] = [], checkboxes: PopupPeerOptions['checkboxes'] = [];
const canUnpin = appPeersManager.canPinMessage(peerId);
2021-07-16 16:16:05 +02:00
const callback = (checked: PopupPeerButtonCallbackCheckboxes, oneSide?: boolean, silent?: boolean) => {
setTimeout(() => { // * костыль, потому что document.elementFromPoint вернёт popup-peer пока он будет закрываться
let promise: Promise<any>;
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);
};
if(unpin) {
let buttonText: LangPackKey = 'UnpinMessage';
if(!mid) {
if(canUnpin) {
title = 'Popup.Unpin.AllTitle';
description = 'Chat.UnpinAllMessagesConfirmation';
2021-07-22 14:07:19 +02:00
descriptionArgs = ['' + (appMessagesManager.pinnedMessages[peerId]?.count || 1)];
} else {
title = 'Popup.Unpin.HideTitle';
description = 'Popup.Unpin.HideDescription';
buttonText = 'Popup.Unpin.Hide';
}
} else {
title = 'UnpinMessageAlertTitle';
description = 'Chat.Confirm.Unpin';
}
buttons.push({
langKey: buttonText,
isDanger: true,
2021-07-16 16:16:05 +02:00
callback
});
} else {
title = 'PinMessageAlertTitle';
const pinButtonText: LangPackKey = 'PinMessage';
2021-10-21 15:16:43 +02:00
if(peerId.isAnyChat()) {
buttons.push({
langKey: pinButtonText,
2021-07-16 16:16:05 +02:00
callback: (checked) => callback(checked, false, !checked.size)
});
2021-10-21 15:16:43 +02:00
if(appChatsManager.isBroadcast(peerId.toChatId())) {
description = 'PinMessageAlertChannel';
} else {
description = 'PinMessageAlert';
2021-07-16 16:16:05 +02:00
checkboxes.push({
text: 'PinNotify',
checked: true
});
}
} else {
description = 'PinMessageAlertChat';
2020-12-21 23:49:21 +01:00
if(peerId === rootScope.myId) {
buttons.push({
langKey: pinButtonText,
2021-07-16 16:16:05 +02:00
callback
2020-12-21 23:49:21 +01:00
});
} else {
buttons.push({
langKey: pinButtonText,
2021-07-16 16:16:05 +02:00
callback: (checked) => callback(checked, !checked.size)
2020-12-21 23:49:21 +01:00
});
2021-07-16 16:16:05 +02:00
checkboxes.push({
text: 'PinAlsoFor',
2021-08-28 00:01:24 +02:00
textArgs: [new PeerTitle({peerId}).element],
2021-07-16 16:16:05 +02:00
checked: true
2020-12-21 23:49:21 +01:00
});
}
}
}
addCancelButton(buttons);
const popup = new PopupPeer('popup-delete-chat', {
peerId,
titleLangKey: title,
descriptionLangKey: description,
2021-07-22 14:07:19 +02:00
descriptionLangArgs: descriptionArgs,
2021-07-16 16:16:05 +02:00
buttons,
checkboxes
});
popup.show();
}
}