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-03-29 20:42:01 +02:00
import { addCancelButton , PopupButton } from "." ;
2020-12-18 04:07:32 +01:00
import PopupPeer from "./peer" ;
import { ChatType } from "../chat/chat" ;
2021-03-29 20:42:01 +02:00
import { i18n , LangPackKey } from "../../lib/langPack" ;
import PeerTitle from "../peerTitle" ;
2020-10-21 01:25:36 +02:00
export default class PopupDeleteMessages {
2020-12-18 04:07:32 +01:00
constructor ( peerId : number , mids : number [ ] , type : ChatType , onConfirm ? : ( ) = > void ) {
2021-03-29 20:42:01 +02:00
const peerTitleElement = new PeerTitle ( {
peerId ,
onlyFirstName : true
} ) . element ;
2020-10-21 01:25:36 +02:00
mids = mids . slice ( ) ;
2020-12-20 04:54:35 +01:00
const callback = ( revoke? : true ) = > {
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 {
appMessagesManager . deleteMessages ( peerId , mids , revoke ) ;
}
2020-10-21 01:25:36 +02:00
} ;
2021-03-29 20:42:01 +02:00
let title : LangPackKey , titleArgs : any [ ] , description : LangPackKey , descriptionArgs : any [ ] , buttons : PopupButton [ ] ;
if ( mids . length === 1 ) {
title = 'DeleteSingleMessagesTitle' ;
} else {
title = 'DeleteMessagesTitle' ;
titleArgs = [ i18n ( 'messages' , [ mids . length ] ) ] ;
}
description = mids . length === 1 ? 'AreYouSureDeleteSingleMessage' : 'AreYouSureDeleteFewMessages' ;
buttons = [ {
langKey : 'Delete' ,
isDanger : true ,
callback : ( ) = > callback ( )
} ] ;
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 {
2020-12-11 03:06:16 +01:00
if ( peerId > 0 ) {
2020-10-21 01:25:36 +02:00
buttons . push ( {
2021-03-29 20:42:01 +02:00
langKey : 'DeleteMessagesOptionAlso' ,
langArgs : [ peerTitleElement ] ,
2020-10-21 01:25:36 +02:00
isDanger : true ,
callback : ( ) = > callback ( true )
} ) ;
2020-11-12 03:11:43 +01:00
} else {
2020-12-11 03:06:16 +01:00
const chat = appChatsManager . getChat ( - peerId ) ;
2020-11-12 03:11:43 +01:00
2021-03-17 14:05:32 +01:00
const hasRights = appChatsManager . hasRights ( - peerId , '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 ) {
2020-11-12 03:11:43 +01:00
buttons . push ( {
2021-03-29 20:42:01 +02:00
langKey : 'DeleteForAll' ,
2020-11-12 03:11:43 +01:00
isDanger : true ,
callback : ( ) = > callback ( true )
} ) ;
} else {
buttons . push ( {
2021-03-29 20:42:01 +02:00
langKey : 'DeleteMessagesOption' ,
2020-11-12 03:11:43 +01:00
isDanger : true ,
callback : ( ) = > callback ( true )
} ) ;
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-03-29 20:42:01 +02:00
buttons [ 0 ] . callback = ( ) = > callback ( 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 ,
2020-10-23 03:01:26 +02:00
buttons
2020-10-21 01:25:36 +02:00
} ) ;
popup . show ( ) ;
}
2021-03-29 20:42:01 +02:00
}