Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
  • Loading branch information
SimonBrandner committed Jun 10, 2022
1 parent edfd70a commit 1b92b88
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
17 changes: 8 additions & 9 deletions src/components/views/emojipicker/ReactionPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import dis from "../../../dispatcher/dispatcher";
import { Action } from '../../../dispatcher/actions';
import RoomContext from "../../../contexts/RoomContext";
import { FocusComposerPayload } from '../../../dispatcher/payloads/FocusComposerPayload';
import { canRedact } from "../../../utils/EventUtils";

interface IProps {
mxEvent: MatrixEvent;
Expand Down Expand Up @@ -74,15 +73,15 @@ class ReactionPicker extends React.Component<IProps, IState> {
}
}

private getReactions(): Record<string, MatrixEvent> {
private getReactions(): Record<string, string> {
if (!this.props.reactions) {
return {};
}
const userId = MatrixClientPeg.get().getUserId();
const myAnnotations = this.props.reactions.getAnnotationsBySender()[userId] || [];
return Object.fromEntries([...myAnnotations]
.filter(event => !event.isRedacted())
.map(event => [event.getRelation().key, event]));
.map(event => [event.getRelation().key, event.getId()]));
}

private onReactionsChange = () => {
Expand All @@ -94,12 +93,11 @@ class ReactionPicker extends React.Component<IProps, IState> {
private onChoose = (reaction: string) => {
this.componentWillUnmount();
this.props.onFinished();
const roomId = this.props.mxEvent.getRoomId();
const myReactions = this.getReactions();
if (myReactions.hasOwnProperty(reaction)) {
if (!canRedact(roomId, myReactions[reaction])) return;
if (this.props.mxEvent.isRedacted() || !this.context.canSelfRedact) return;

MatrixClientPeg.get().redactEvent(roomId, myReactions[reaction].getId());
MatrixClientPeg.get().redactEvent(this.props.mxEvent.getRoomId(), myReactions[reaction]);
dis.dispatch<FocusComposerPayload>({
action: Action.FocusAComposer,
context: this.context.timelineRenderingType,
Expand All @@ -124,10 +122,11 @@ class ReactionPicker extends React.Component<IProps, IState> {
};

private isEmojiDisabled = (unicode: string): boolean => {
const reaction = this.getReactions()[unicode];
if (!this.getReactions()[unicode]) return false;
if (this.props.mxEvent.isRedacted()) return false;
if (this.context.canSelfRedact) return false;

if (!reaction) return false;
return !canRedact(this.props.mxEvent.getRoomId(), reaction);
return true;
};

render() {
Expand Down
5 changes: 4 additions & 1 deletion src/components/views/messages/ReactionsRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ export default class ReactionsRow extends React.PureComponent<IProps, IState> {
mxEvent={mxEvent}
reactionEvents={events}
myReactionEvent={myReactionEvent}
disabled={!this.context.canReact}
disabled={
!this.context.canReact ||
(myReactionEvent && !myReactionEvent.isRedacted() && !this.context.canSelfRedact)
}
/>;
}).filter(item => !!item);

Expand Down
8 changes: 2 additions & 6 deletions src/components/views/messages/ReactionsRowButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import dis from "../../../dispatcher/dispatcher";
import ReactionsRowButtonTooltip from "./ReactionsRowButtonTooltip";
import AccessibleButton from "../elements/AccessibleButton";
import MatrixClientContext from "../../../contexts/MatrixClientContext";
import { canRedact } from "../../../utils/EventUtils";

interface IProps {
// The event we're displaying reactions for
Expand Down Expand Up @@ -58,11 +57,8 @@ export default class ReactionsRowButton extends React.PureComponent<IProps, ISta
onClick = () => {
const { mxEvent, myReactionEvent, content } = this.props;
if (myReactionEvent) {
const roomId = mxEvent.getRoomId();
if (!canRedact(roomId, myReactionEvent)) return;

this.context.redactEvent(
roomId,
mxEvent.getRoomId(),
myReactionEvent.getId(),
);
} else {
Expand Down Expand Up @@ -131,7 +127,7 @@ export default class ReactionsRowButton extends React.PureComponent<IProps, ISta
className={classes}
aria-label={label}
onClick={this.onClick}
disabled={this.props.disabled || (myReactionEvent && !canRedact(mxEvent.getRoomId(), myReactionEvent))}
disabled={this.props.disabled}
onMouseOver={this.onMouseOver}
onMouseLeave={this.onMouseLeave}
>
Expand Down
6 changes: 0 additions & 6 deletions src/utils/EventUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,3 @@ export function canForward(event: MatrixEvent): boolean {
export function hasThreadSummary(event: MatrixEvent): boolean {
return event.isThreadRoot && event.getThread()?.length && !!event.getThread().replyToEvent;
}

export function canRedact(roomId: string, mxEvent: MatrixEvent): boolean {
const client = MatrixClientPeg.get();
const room = client.getRoom(roomId);
return room.currentState.maySendRedactionForEvent(mxEvent, client.getUserId());
}

0 comments on commit 1b92b88

Please sign in to comment.