From 3453451df9aafbd307d3a75d221076beb164f35e Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 Aug 2021 09:42:12 +0530 Subject: [PATCH] added msg deletion support --- .../organisms/channel/ChannelViewContent.jsx | 23 +++++++++++++++---- src/client/action/room.js | 15 +++++++++++- src/client/state/RoomTimeline.js | 7 ++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/app/organisms/channel/ChannelViewContent.jsx b/src/app/organisms/channel/ChannelViewContent.jsx index 737cbaa..7a8a2a3 100644 --- a/src/app/organisms/channel/ChannelViewContent.jsx +++ b/src/app/organisms/channel/ChannelViewContent.jsx @@ -7,6 +7,7 @@ import dateFormat from 'dateformat'; import initMatrix from '../../../client/initMatrix'; import cons from '../../../client/state/cons'; +import { redact } from '../../../client/action/room'; import { getUsername, doesRoomHaveUnread } from '../../../util/matrixUtil'; import colorMXID from '../../../util/colorMXID'; import { diffMinutes, isNotInSameDay } from '../../../util/common'; @@ -223,6 +224,9 @@ function ChannelViewContent({ && prevMEvent.getSender() === mEvent.getSender() ); + const myPowerlevel = roomTimeline.room.getMember(mx.getUserId()).powerLevel; + const canIRedact = roomTimeline.room.currentState.hasSufficientPowerLevelFor('redact', myPowerlevel); + let content = mEvent.getContent().body; if (typeof content === 'undefined') return null; let reply = null; @@ -293,12 +297,12 @@ function ChannelViewContent({ }); } - const userMXIDColor = colorMXID(mEvent.sender.userId); + const senderMXIDColor = colorMXID(mEvent.sender.userId); const userAvatar = isContentOnly ? null : ( ); @@ -306,7 +310,7 @@ function ChannelViewContent({ ); @@ -350,7 +354,18 @@ function ChannelViewContent({ size="extra-small" tooltip="Reply" /> - + {(canIRedact || mEvent.getSender() === mx.getUserId()) && ( + { + if (window.confirm('Are you sure you want to delete this event')) { + redact(roomId, mEvent.getId()); + } + }} + src={BinIC} + size="extra-small" + tooltip="Delete" + /> + )} ); diff --git a/src/client/action/room.js b/src/client/action/room.js index e18f197..5bd6777 100644 --- a/src/client/action/room.js +++ b/src/client/action/room.js @@ -189,6 +189,19 @@ async function invite(roomId, userId) { } } +async function redact(roomId, eventId, reason) { + const mx = initMatrix.matrixClient; + + try { + await mx.redactEvent(roomId, eventId, undefined, typeof reason === 'undefined' ? undefined : { reason }); + return true; + } catch (e) { + throw new Error(e); + } +} + export { - join, leave, create, invite, + join, leave, + create, invite, + redact, }; diff --git a/src/client/state/RoomTimeline.js b/src/client/state/RoomTimeline.js index edb19c4..1337454 100644 --- a/src/client/state/RoomTimeline.js +++ b/src/client/state/RoomTimeline.js @@ -35,6 +35,11 @@ class RoomTimeline extends EventEmitter { this.emit(cons.events.roomTimeline.EVENT); }; + this._listenRedaction = (event, room) => { + if (room.roomId !== this.roomId) return; + this.emit(cons.events.roomTimeline.EVENT); + }; + this._listenDecryptEvent = (event) => { if (event.getRoomId() !== this.roomId) return; @@ -67,6 +72,7 @@ class RoomTimeline extends EventEmitter { }; this.matrixClient.on('Room.timeline', this._listenRoomTimeline); + this.matrixClient.on('Room.redaction', this._listenRedaction); this.matrixClient.on('Event.decrypted', this._listenDecryptEvent); this.matrixClient.on('RoomMember.typing', this._listenTypingEvent); this.matrixClient.on('Room.receipt', this._listenReciptEvent); @@ -152,6 +158,7 @@ class RoomTimeline extends EventEmitter { removeInternalListeners() { this.matrixClient.removeListener('Room.timeline', this._listenRoomTimeline); + this.matrixClient.removeListener('Room.redaction', this._listenRedaction); this.matrixClient.removeListener('Event.decrypted', this._listenDecryptEvent); this.matrixClient.removeListener('RoomMember.typing', this._listenTypingEvent); this.matrixClient.removeListener('Room.receipt', this._listenReciptEvent);