Fix bulk invite button getting a negative count (#7122)
* Fix bulk invite button getting a negative counter Signed-off-by: Renan <renancleyson.f@gmail.com> * Change invites to be count on membership events Signed-off-by: Renan <renancleyson.f@gmail.com> * Remove unnecessary bind * Change invitedRooms state to a set of ids Signed-off-by: Renan <renancleyson.f@gmail.com>
This commit is contained in:
parent
645f900a36
commit
ac15661c3e
1 changed files with 48 additions and 19 deletions
|
@ -76,7 +76,7 @@ interface IState {
|
||||||
ignoredUserIds: string[];
|
ignoredUserIds: string[];
|
||||||
waitingUnignored: string[];
|
waitingUnignored: string[];
|
||||||
managingInvites: boolean;
|
managingInvites: boolean;
|
||||||
invitedRoomAmt: number;
|
invitedRoomIds: Set<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@replaceableComponent("views.settings.tabs.user.SecurityUserSettingsTab")
|
@replaceableComponent("views.settings.tabs.user.SecurityUserSettingsTab")
|
||||||
|
@ -86,14 +86,14 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||||
constructor(props: IProps) {
|
constructor(props: IProps) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
// Get number of rooms we're invited to
|
// Get rooms we're invited to
|
||||||
const invitedRooms = this.getInvitedRooms();
|
const invitedRoomIds = new Set(this.getInvitedRooms().map(room => room.roomId));
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
ignoredUserIds: MatrixClientPeg.get().getIgnoredUsers(),
|
ignoredUserIds: MatrixClientPeg.get().getIgnoredUsers(),
|
||||||
waitingUnignored: [],
|
waitingUnignored: [],
|
||||||
managingInvites: false,
|
managingInvites: false,
|
||||||
invitedRoomAmt: invitedRooms.length,
|
invitedRoomIds,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,10 +107,12 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||||
|
|
||||||
public componentDidMount(): void {
|
public componentDidMount(): void {
|
||||||
this.dispatcherRef = dis.register(this.onAction);
|
this.dispatcherRef = dis.register(this.onAction);
|
||||||
|
MatrixClientPeg.get().on("Room.myMembership", this.onMyMembership);
|
||||||
}
|
}
|
||||||
|
|
||||||
public componentWillUnmount(): void {
|
public componentWillUnmount(): void {
|
||||||
dis.unregister(this.dispatcherRef);
|
dis.unregister(this.dispatcherRef);
|
||||||
|
MatrixClientPeg.get().removeListener("Room.myMembership", this.onMyMembership);
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateAnalytics = (checked: boolean): void => {
|
private updateAnalytics = (checked: boolean): void => {
|
||||||
|
@ -119,6 +121,36 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||||
PosthogAnalytics.instance.updateAnonymityFromSettings(MatrixClientPeg.get().getUserId());
|
PosthogAnalytics.instance.updateAnonymityFromSettings(MatrixClientPeg.get().getUserId());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private onMyMembership = (room: Room, membership: string): void => {
|
||||||
|
if (room.isSpaceRoom()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (membership === "invite") {
|
||||||
|
this.addInvitedRoom(room);
|
||||||
|
} else if (this.state.invitedRoomIds.has(room.roomId)) {
|
||||||
|
// The user isn't invited anymore
|
||||||
|
this.removeInvitedRoom(room.roomId);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private addInvitedRoom = (room: Room): void => {
|
||||||
|
this.setState(({ invitedRoomIds }) => ({
|
||||||
|
invitedRoomIds: new Set(invitedRoomIds).add(room.roomId),
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
private removeInvitedRoom = (roomId: string): void => {
|
||||||
|
this.setState(({ invitedRoomIds }) => {
|
||||||
|
const newInvitedRoomIds = new Set(invitedRoomIds);
|
||||||
|
newInvitedRoomIds.delete(roomId);
|
||||||
|
|
||||||
|
return {
|
||||||
|
invitedRoomIds: newInvitedRoomIds,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
private onGoToUserProfileClick = (): void => {
|
private onGoToUserProfileClick = (): void => {
|
||||||
dis.dispatch({
|
dis.dispatch({
|
||||||
action: 'view_user_info',
|
action: 'view_user_info',
|
||||||
|
@ -150,21 +182,19 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||||
managingInvites: true,
|
managingInvites: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Compile array of invitation room ids
|
// iterate with a normal for loop in order to retry on action failure
|
||||||
const invitedRoomIds = this.getInvitedRooms().map((room) => {
|
const invitedRoomIdsValues = Array.from(this.state.invitedRoomIds);
|
||||||
return room.roomId;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Execute all acceptances/rejections sequentially
|
// Execute all acceptances/rejections sequentially
|
||||||
const cli = MatrixClientPeg.get();
|
const cli = MatrixClientPeg.get();
|
||||||
const action = accept ? cli.joinRoom.bind(cli) : cli.leave.bind(cli);
|
const action = accept ? cli.joinRoom.bind(cli) : cli.leave.bind(cli);
|
||||||
for (let i = 0; i < invitedRoomIds.length; i++) {
|
for (let i = 0; i < invitedRoomIdsValues.length; i++) {
|
||||||
const roomId = invitedRoomIds[i];
|
const roomId = invitedRoomIdsValues[i];
|
||||||
|
|
||||||
// Accept/reject invite
|
// Accept/reject invite
|
||||||
await action(roomId).then(() => {
|
await action(roomId).then(() => {
|
||||||
// No error, update invited rooms button
|
// No error, update invited rooms button
|
||||||
this.setState({ invitedRoomAmt: this.state.invitedRoomAmt - 1 });
|
this.removeInvitedRoom(roomId);
|
||||||
}, async (e) => {
|
}, async (e) => {
|
||||||
// Action failure
|
// Action failure
|
||||||
if (e.errcode === "M_LIMIT_EXCEEDED") {
|
if (e.errcode === "M_LIMIT_EXCEEDED") {
|
||||||
|
@ -221,21 +251,20 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||||
}
|
}
|
||||||
|
|
||||||
private renderManageInvites(): JSX.Element {
|
private renderManageInvites(): JSX.Element {
|
||||||
if (this.state.invitedRoomAmt === 0) {
|
const { invitedRoomIds } = this.state;
|
||||||
|
|
||||||
|
if (invitedRoomIds.size === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const invitedRooms = this.getInvitedRooms();
|
|
||||||
const onClickAccept = this.onAcceptAllInvitesClicked.bind(this, invitedRooms);
|
|
||||||
const onClickReject = this.onRejectAllInvitesClicked.bind(this, invitedRooms);
|
|
||||||
return (
|
return (
|
||||||
<div className='mx_SettingsTab_section mx_SecurityUserSettingsTab_bulkOptions'>
|
<div className='mx_SettingsTab_section mx_SecurityUserSettingsTab_bulkOptions'>
|
||||||
<span className='mx_SettingsTab_subheading'>{ _t('Bulk options') }</span>
|
<span className='mx_SettingsTab_subheading'>{ _t('Bulk options') }</span>
|
||||||
<AccessibleButton onClick={onClickAccept} kind='primary' disabled={this.state.managingInvites}>
|
<AccessibleButton onClick={this.onAcceptAllInvitesClicked} kind='primary' disabled={this.state.managingInvites}>
|
||||||
{ _t("Accept all %(invitedRooms)s invites", { invitedRooms: this.state.invitedRoomAmt }) }
|
{ _t("Accept all %(invitedRooms)s invites", { invitedRooms: invitedRoomIds.size }) }
|
||||||
</AccessibleButton>
|
</AccessibleButton>
|
||||||
<AccessibleButton onClick={onClickReject} kind='danger' disabled={this.state.managingInvites}>
|
<AccessibleButton onClick={this.onRejectAllInvitesClicked} kind='danger' disabled={this.state.managingInvites}>
|
||||||
{ _t("Reject all %(invitedRooms)s invites", { invitedRooms: this.state.invitedRoomAmt }) }
|
{ _t("Reject all %(invitedRooms)s invites", { invitedRooms: invitedRoomIds.size }) }
|
||||||
</AccessibleButton>
|
</AccessibleButton>
|
||||||
{ this.state.managingInvites ? <InlineSpinner /> : <div /> }
|
{ this.state.managingInvites ? <InlineSpinner /> : <div /> }
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue