Skip to content

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 9, 2022
1 parent edfd70a commit 1f5776b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
10 changes: 5 additions & 5 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 @@ -97,7 +96,7 @@ class ReactionPicker extends React.Component<IProps, IState> {
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());
dis.dispatch<FocusComposerPayload>({
Expand All @@ -124,10 +123,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
6 changes: 5 additions & 1 deletion src/components/views/messages/ReactionsRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export default class ReactionsRow extends React.PureComponent<IProps, IState> {
render() {
const { mxEvent, reactions } = this.props;
const { myReactions, showAll } = this.state;
console.log(`LOG rendering ${isContentActionable(mxEvent)}; ${reactions}`);

if (!reactions || !isContentActionable(mxEvent)) {
return null;
Expand All @@ -183,7 +184,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
4 changes: 1 addition & 3 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 @@ -59,7 +58,6 @@ export default class ReactionsRowButton extends React.PureComponent<IProps, ISta
const { mxEvent, myReactionEvent, content } = this.props;
if (myReactionEvent) {
const roomId = mxEvent.getRoomId();
if (!canRedact(roomId, myReactionEvent)) return;

this.context.redactEvent(
roomId,
Expand Down Expand Up @@ -131,7 +129,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 1f5776b

Please sign in to comment.