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
* /
2020-12-18 04:07:32 +01:00
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" ;
2020-12-18 04:07:32 +01:00
import { ChatType } from "../chat/chat" ;
2021-03-29 20:42:01 +02:00
import { i18n , LangPackKey } from "../../lib/langPack" ;
import PeerTitle from "../peerTitle" ;
2021-09-25 14:18:12 +02:00
import appPeersManager from "../../lib/appManagers/appPeersManager" ;
2020-10-21 01:25:36 +02:00
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 ;
2020-10-21 01:25:36 +02:00
mids = mids . slice ( ) ;
2021-07-16 16:16:05 +02:00
const callback = ( checked : PopupPeerButtonCallbackCheckboxes , revoke? : boolean ) = > {
2020-10-21 01:25:36 +02:00
onConfirm && onConfirm ( ) ;
2020-12-18 04:07:32 +01:00
if ( type === 'scheduled' ) {
appMessagesManager . deleteScheduledMessages ( peerId , mids ) ;
} else {
2021-07-16 16:16:05 +02:00
appMessagesManager . deleteMessages ( peerId , mids , ! ! checked . size || revoke ) ;
2020-12-18 04:07:32 +01:00
}
2020-10-21 01:25:36 +02:00
} ;
2021-07-16 16:16:05 +02:00
let title : LangPackKey , titleArgs : any [ ] , description : LangPackKey , descriptionArgs : any [ ] , buttons : PopupPeerOptions [ 'buttons' ] , checkboxes : PopupPeerOptions [ 'checkboxes' ] = [ ] ;
2021-03-29 20:42:01 +02:00
if ( mids . length === 1 ) {
title = 'DeleteSingleMessagesTitle' ;
} else {
title = 'DeleteMessagesTitle' ;
titleArgs = [ i18n ( 'messages' , [ mids . length ] ) ] ;
}
2021-09-25 14:18:12 +02:00
if ( appPeersManager . isMegagroup ( peerId ) ) {
description = mids . length === 1 ? 'AreYouSureDeleteSingleMessageMega' : 'AreYouSureDeleteFewMessagesMega' ;
} else {
description = mids . length === 1 ? 'AreYouSureDeleteSingleMessage' : 'AreYouSureDeleteFewMessages' ;
}
2021-03-29 20:42:01 +02:00
buttons = [ {
langKey : 'Delete' ,
isDanger : true ,
2021-07-16 16:16:05 +02:00
callback
2021-03-29 20:42:01 +02:00
} ] ;
2020-10-21 01:25:36 +02:00
2021-02-03 22:30:45 +01:00
if ( peerId === rootScope . myId || type === 'scheduled' ) {
2021-03-29 20:42:01 +02:00
2020-10-21 01:25:36 +02:00
} 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-10-21 01:25:36 +02:00
} ) ;
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' ) ;
2021-02-03 22:30:45 +01:00
if ( chat . _ === 'chat' ) {
2020-11-12 03:11:43 +01:00
const canRevoke = hasRights ? mids . slice ( ) : mids . filter ( mid = > {
2020-12-16 04:22:58 +01:00
const message = appMessagesManager . getMessageByPeer ( peerId , mid ) ;
2021-02-03 22:30:45 +01:00
return message . fromId === rootScope . myId ;
2020-11-12 03:11:43 +01:00
} ) ;
if ( canRevoke . length ) {
2021-02-03 22:30:45 +01:00
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
} ) ;
2021-03-29 20:42:01 +02: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
}
2020-10-21 01:25:36 +02:00
}
}
2021-03-29 20:42:01 +02:00
addCancelButton ( buttons ) ;
2020-10-21 01:25:36 +02:00
const popup = new PopupPeer ( 'popup-delete-chat' , {
2020-12-11 03:06:16 +01:00
peerId ,
2021-03-29 20:42:01 +02:00
titleLangKey : title ,
titleLangArgs : titleArgs ,
descriptionLangKey : description ,
descriptionLangArgs : descriptionArgs ,
2021-07-16 16:16:05 +02:00
buttons ,
checkboxes
2020-10-21 01:25:36 +02:00
} ) ;
popup . show ( ) ;
}
2021-03-29 20:42:01 +02:00
}