Skip to content

Commit

Permalink
Clear history on both sides
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeny-nadymov committed Oct 22, 2020
1 parent c6ac037 commit 84cad5d
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 165 deletions.
66 changes: 60 additions & 6 deletions src/Components/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { withTranslation } from 'react-i18next';
import Button from '@material-ui/core/Button';
import IconButton from '@material-ui/core/IconButton';
import ClearHistoryDialog from './Popup/ClearHistoryDialog';
import DeleteMessagesDialog from './Popup/DeleteMessagesDialog';
import LeaveChatDialog from './Popup/LeaveChatDialog';
import NotificationTimer from './Additional/NotificationTimer';
import { isChatMember, isCreator } from '../Utils/Chat';
Expand All @@ -21,23 +22,39 @@ import ChatStore from '../Stores/ChatStore';
import SupergroupStore from '../Stores/SupergroupStore';
import UserStore from '../Stores/UserStore';
import TdLibController from '../Controllers/TdLibController';
import MessageStore from '../Stores/MessageStore';
import { clearSelection } from '../Actions/Client';

class Actions extends React.PureComponent {
state = {
leaveChat: null,
clearHistory: null
clearHistory: null,
deleteMessages: null
}

componentDidMount() {
AppStore.on('clientUpdateRequestLeaveChat', this.onClientUpdateLeaveChat);
AppStore.on('clientUpdateRequestClearHistory', this.onClientUpdateClearHistory);
AppStore.on('clientUpdateDeleteMessages', this.onClientUpdateDeleteMessages);
}

componentWillUnmount() {
AppStore.off('clientUpdateRequestLeaveChat', this.onClientUpdateLeaveChat);
AppStore.off('clientUpdateRequestClearHistory', this.onClientUpdateClearHistory);
AppStore.off('clientUpdateDeleteMessages', this.onClientUpdateDeleteMessages);
}

onClientUpdateDeleteMessages = update => {
const { chatId, messageIds } = update;

this.setState({
deleteMessages: {
chatId,
messageIds
}
});
};

onClientUpdateLeaveChat = update => {
const { chatId } = update;

Expand All @@ -50,7 +67,7 @@ class Actions extends React.PureComponent {
this.setState({ clearHistory : { chatId } });
};

handleClearHistoryContinue = result => {
handleClearHistoryContinue = (result, revoke) => {
const { t } = this.props;

const { clearHistory } = this.state;
Expand All @@ -65,7 +82,8 @@ class Actions extends React.PureComponent {
const request = {
'@type': 'deleteChatHistory',
chat_id: chatId,
remove_from_chat_list: false
remove_from_chat_list: false,
revoke
};

this.handleScheduledAction(chatId, 'clientUpdateClearHistory', message, [request]);
Expand Down Expand Up @@ -116,6 +134,25 @@ class Actions extends React.PureComponent {
this.handleScheduledAction(chatId, 'clientUpdateLeaveChat', message, requests);
};

handleDeleteMessagesContinue = (result, revoke) => {
const { deleteMessages } = this.state;
if (!deleteMessages) return;

const { chatId, messageIds } = deleteMessages;

clearSelection();
this.setState({ deleteMessages: null });

if (!result) return;

TdLibController.send({
'@type': 'deleteMessages',
chat_id: chatId,
message_ids: messageIds,
revoke
});
};

getLeaveChatNotification = chatId => {
const { t } = this.props;

Expand Down Expand Up @@ -196,15 +233,32 @@ class Actions extends React.PureComponent {
};

render() {
const { leaveChat, clearHistory } = this.state;
const { leaveChat, clearHistory, deleteMessages } = this.state;
if (leaveChat) {
const { chatId } = leaveChat;

return <LeaveChatDialog chatId={chatId} onClose={this.handleLeaveContinue} />
return (
<LeaveChatDialog
chatId={chatId}
onClose={this.handleLeaveContinue} />
);
} else if (clearHistory) {
const { chatId } = clearHistory;

return <ClearHistoryDialog chatId={chatId} onClose={this.handleClearHistoryContinue} />;
return (
<ClearHistoryDialog
chatId={chatId}
onClose={this.handleClearHistoryContinue} />
);
} else if (deleteMessages) {
const { chatId, messageIds } = deleteMessages;

return (
<DeleteMessagesDialog
chatId={chatId}
messageIds={messageIds}
onClose={this.handleDeleteMessagesContinue} />
);
}

return null;
Expand Down
153 changes: 7 additions & 146 deletions src/Components/ColumnMiddle/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
import React, { Component } from 'react';
import classNames from 'classnames';
import { withTranslation } from 'react-i18next';
import Button from '@material-ui/core/Button';
import Checkbox from '@material-ui/core/Checkbox';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import IconButton from '@material-ui/core/IconButton';
import SearchIcon from '../../Assets/Icons/Search';
import MainMenuButton from './MainMenuButton';
Expand All @@ -25,49 +17,24 @@ import HeaderProgress from './HeaderProgress';
import PinnedMessage from './PinnedMessage';
import { changeChatDetailsVisibility } from '../../Actions/Chat';
import {
getChatShortTitle,
getChatSubtitle,
getChatTitle,
isAccentChatSubtitle, isChannelChat,
isPrivateChat, isSupergroup
isAccentChatSubtitle
} from '../../Utils/Chat';
import { clearSelection, openChat, searchChat } from '../../Actions/Client';
import { openChat, searchChat } from '../../Actions/Client';
import AppStore from '../../Stores/ApplicationStore';
import ChatStore from '../../Stores/ChatStore';
import MessageStore from '../../Stores/MessageStore';
import TdLibController from '../../Controllers/TdLibController';
import './Header.css';

class Header extends Component {
constructor(props) {
super(props);

this.state = {
authorizationState: AppStore.getAuthorizationState(),
connectionState: AppStore.getConnectionState(),
openDeleteDialog: false
};
}

shouldComponentUpdate(nextProps, nextState) {
if (nextState !== this.state) {
return true;
}

if (nextProps.theme !== this.props.theme) {
return true;
}

if (nextProps.t !== this.props.t) {
return true;
}

return false;
}
state = {
authorizationState: AppStore.getAuthorizationState(),
connectionState: AppStore.getConnectionState()
};

componentDidMount() {
AppStore.on('clientUpdateChatId', this.onClientUpdateChatId);
AppStore.on('clientUpdateDeleteMessages', this.onClientUpdateDeleteMessages);
AppStore.on('updateAuthorizationState', this.onUpdateAuthorizationState);
AppStore.on('updateConnectionState', this.onUpdateConnectionState);

Expand All @@ -77,61 +44,13 @@ class Header extends Component {

componentWillUnmount() {
AppStore.off('clientUpdateChatId', this.onClientUpdateChatId);
AppStore.off('clientUpdateDeleteMessages', this.onClientUpdateDeleteMessages);
AppStore.off('updateAuthorizationState', this.onUpdateAuthorizationState);
AppStore.off('updateConnectionState', this.onUpdateConnectionState);

MessageStore.off('clientUpdateClearSelection', this.onClientUpdateMessageSelected);
MessageStore.off('clientUpdateMessageSelected', this.onClientUpdateMessageSelected);
}

onClientUpdateDeleteMessages = update => {
const { chatId, messageIds } = update;

let canBeDeletedForAllUsers = true;
for (let messageId of messageIds) {
const message = MessageStore.get(chatId, messageId);
if (!message) {
canBeDeletedForAllUsers = false;
break;
}
if (!message.can_be_deleted_for_all_users) {
canBeDeletedForAllUsers = false;
break;
}
}

this.setState({
openDeleteDialog: true,
chatId,
messageIds,
canBeDeletedForAllUsers: canBeDeletedForAllUsers,
revoke: canBeDeletedForAllUsers
});
};

handleRevokeChange = () => {
this.setState({ revoke: !this.state.revoke });
};

handleCloseDelete = () => {
this.setState({ openDeleteDialog: false });
};

handleDeleteContinue = () => {
const { revoke, chatId, messageIds } = this.state;

clearSelection();
this.handleCloseDelete();

TdLibController.send({
'@type': 'deleteMessages',
chat_id: chatId,
message_ids: messageIds,
revoke: revoke
});
};

onClientUpdateMessageSelected = update => {
this.setState({ selectionCount: MessageStore.selectedItems.size });
};
Expand Down Expand Up @@ -184,14 +103,8 @@ class Header extends Component {
authorizationState,
connectionState,
selectionCount,
openDeleteDialog,
canBeDeletedForAllUsers,
revoke,
messageIds
} = this.state;

const count = messageIds ? messageIds.length : 0;

let control = null;
if (selectionCount) {
control = <HeaderCommand count={selectionCount} />;
Expand Down Expand Up @@ -302,59 +215,7 @@ class Header extends Component {
</div>
);

return (
<>
{control}
<Dialog
transitionDuration={0}
open={openDeleteDialog}
onClose={this.handleCloseDelete}
aria-labelledby='delete-dialog-title'>
<DialogTitle id='delete-dialog-title'>Confirm</DialogTitle>
<DialogContent>
<DialogContentText>
{count === 1
? 'Do you want to delete this message?'
: `Do you want to delete ${count} messages?`}
</DialogContentText>
{ isSupergroup(chatId) ? (
<DialogContentText>
{ !isChannelChat(chatId) && (count === 1
? 'This will delete it for everyone in this chat'
: 'This will delete them for everyone in this chat')
}
</DialogContentText>
) : (
<>
{
canBeDeletedForAllUsers && (
<FormControlLabel
control={
<Checkbox checked={revoke} onChange={this.handleRevokeChange} color='primary' />
}
label={
isPrivateChat(chatId)
? `Delete for ${getChatShortTitle(chatId, false, t)}`
: 'Delete for all'
}
/>
)}
</>
)}


</DialogContent>
<DialogActions>
<Button onClick={this.handleCloseDelete} color='primary'>
{t('Cancel')}
</Button>
<Button onClick={this.handleDeleteContinue} color='primary'>
{t('Ok')}
</Button>
</DialogActions>
</Dialog>
</>
);
return control;
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/Components/Message/MessageMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,6 @@ class MessageMenu extends React.PureComponent {
<ListItemText primary={t('Select')} />
</MenuItem>
)}
<MenuItem onClick={this.handleSelection}>
<ListItemIcon>
<CheckBoxOutlinedIcon />
</ListItemIcon>
<ListItemText primary={t('Select')} />
</MenuItem>
{canCopyPublicMessageLink && (
<MenuItem onClick={this.handleCopyPublicMessageLink}>
<ListItemIcon>
Expand Down
Loading

0 comments on commit 84cad5d

Please sign in to comment.