tweb/src/components/popups/deleteMessages.ts

105 lines
3.6 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 appChatsManager from "../../lib/appManagers/appChatsManager";
import appMessagesManager from "../../lib/appManagers/appMessagesManager";
import rootScope from "../../lib/rootScope";
2021-07-16 16:16:05 +02:00
import { addCancelButton } from ".";
import PopupPeer, { PopupPeerButtonCallbackCheckboxes, PopupPeerOptions } from "./peer";
import { ChatType } from "../chat/chat";
import { i18n, LangPackKey } from "../../lib/langPack";
import PeerTitle from "../peerTitle";
import appPeersManager from "../../lib/appManagers/appPeersManager";
export default class PopupDeleteMessages {
2021-10-21 15:16:43 +02:00
constructor(peerId: PeerId, mids: number[], type: ChatType, onConfirm?: () => void) {
2021-08-28 00:01:24 +02:00
const peerTitleElement = new PeerTitle({peerId}).element;
mids = mids.slice();
2021-07-16 16:16:05 +02:00
const callback = (checked: PopupPeerButtonCallbackCheckboxes, revoke?: boolean) => {
onConfirm && onConfirm();
if(type === 'scheduled') {
appMessagesManager.deleteScheduledMessages(peerId, mids);
} else {
2021-07-16 16:16:05 +02:00
appMessagesManager.deleteMessages(peerId, mids, !!checked.size || revoke);
}
};
2021-07-16 16:16:05 +02:00
let title: LangPackKey, titleArgs: any[], description: LangPackKey, descriptionArgs: any[], buttons: PopupPeerOptions['buttons'], checkboxes: PopupPeerOptions['checkboxes'] = [];
if(mids.length === 1) {
title = 'DeleteSingleMessagesTitle';
} else {
title = 'DeleteMessagesTitle';
titleArgs = [i18n('messages', [mids.length])];
}
if(appPeersManager.isMegagroup(peerId)) {
description = mids.length === 1 ? 'AreYouSureDeleteSingleMessageMega' : 'AreYouSureDeleteFewMessagesMega';
} else {
description = mids.length === 1 ? 'AreYouSureDeleteSingleMessage' : 'AreYouSureDeleteFewMessages';
}
buttons = [{
langKey: 'Delete',
isDanger: true,
2021-07-16 16:16:05 +02:00
callback
}];
if(peerId === rootScope.myId || type === 'scheduled') {
} else {
2021-10-21 15:16:43 +02:00
if(peerId.isUser()) {
2021-07-16 16:16:05 +02:00
checkboxes.push({
text: 'DeleteMessagesOptionAlso',
textArgs: [peerTitleElement]
});
2020-11-12 03:11:43 +01:00
} else {
2021-10-21 15:16:43 +02:00
const chat = appChatsManager.getChat(peerId.toChatId());
2020-11-12 03:11:43 +01:00
2021-10-21 15:16:43 +02:00
const hasRights = appChatsManager.hasRights(peerId.toChatId(), 'delete_messages');
if(chat._ === 'chat') {
2020-11-12 03:11:43 +01:00
const canRevoke = hasRights ? mids.slice() : mids.filter(mid => {
const message = appMessagesManager.getMessageByPeer(peerId, mid);
return message.fromId === rootScope.myId;
2020-11-12 03:11:43 +01:00
});
if(canRevoke.length) {
if(canRevoke.length === mids.length) {
2021-07-16 16:16:05 +02:00
checkboxes.push({
text: 'DeleteForAll'
2020-11-12 03:11:43 +01:00
});
} else {
2021-07-16 16:16:05 +02:00
checkboxes.push({
text: 'DeleteMessagesOption'
2020-11-12 03:11:43 +01:00
});
description = 'DeleteMessagesTextGroup';
descriptionArgs = [i18n('messages', [canRevoke.length])];
//description = `You can also delete the ${canRevoke.length} message${canRevoke.length > 1 ? 's' : ''} you sent from the inboxes of other group members by pressing "${buttonText}".`;
2020-11-12 03:11:43 +01:00
}
}
} else {
2021-07-16 16:16:05 +02:00
buttons[0].callback = (checked) => callback(checked, true);
2020-11-12 03:11:43 +01:00
}
}
}
addCancelButton(buttons);
const popup = new PopupPeer('popup-delete-chat', {
peerId,
titleLangKey: title,
titleLangArgs: titleArgs,
descriptionLangKey: description,
descriptionLangArgs: descriptionArgs,
2021-07-16 16:16:05 +02:00
buttons,
checkboxes
});
popup.show();
}
}