From 61d9a60704a25c108576b442d52bb60f51336c0b Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sat, 9 Apr 2022 04:02:51 -0400 Subject: [PATCH] Outgoing message redactions --- .../kt/client/client.py | 11 ++++ matrix_appservice_kakaotalk/portal.py | 55 ++++++++++++++++--- node/src/client.js | 15 +++++ 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/matrix_appservice_kakaotalk/kt/client/client.py b/matrix_appservice_kakaotalk/kt/client/client.py index 69ace8a..390e472 100644 --- a/matrix_appservice_kakaotalk/kt/client/client.py +++ b/matrix_appservice_kakaotalk/kt/client/client.py @@ -343,6 +343,17 @@ class Client: is_secret=True ) + async def delete_chat( + self, + channel_props: ChannelProps, + chat_id: Long, + ) -> None: + return await self._api_user_request_void( + "delete_chat", + channel_props=channel_props.serialize(), + chat_id=chat_id.serialize(), + ) + # TODO Combine these into one diff --git a/matrix_appservice_kakaotalk/portal.py b/matrix_appservice_kakaotalk/portal.py index 6c798ee..577d5c3 100644 --- a/matrix_appservice_kakaotalk/portal.py +++ b/matrix_appservice_kakaotalk/portal.py @@ -879,24 +879,61 @@ class Portal(DBPortal, BasePortal): self, sender: u.User, event_id: EventID, redaction_event_id: EventID ) -> None: try: - await self._handle_matrix_redaction(sender, event_id, redaction_event_id) + await self._handle_matrix_redaction(sender, event_id) except Exception as e: - self.log.exception(f"Failed to handle Matrix event {event_id}: {e}") + self.log.error( + f"Failed to handle Matrix redaction {redaction_event_id}: {e}", + exc_info=not isinstance(e, NotImplementedError), + ) sender.send_remote_checkpoint( self._status_from_exception(e), - event_id, + redaction_event_id, self.mxid, EventType.ROOM_REDACTION, error=e, ) - await self._send_bridge_error(str(e)) + if not isinstance(e, NotImplementedError): + await self._send_bridge_error(str(e), thing="redaction") else: - await self._send_delivery_receipt(event_id) + await self._send_delivery_receipt(redaction_event_id) + sender.send_remote_checkpoint( + MessageSendCheckpointStatus.SUCCESS, + redaction_event_id, + self.mxid, + EventType.ROOM_REDACTION, + ) - async def _handle_matrix_redaction( - self, sender: u.User, event_id: EventID, redaction_event_id: EventID - ) -> None: - self.log.info("TODO: _handle_matrix_redaction") + async def _handle_matrix_redaction(self, sender: u.User, event_id: EventID) -> None: + sender, _ = await self.get_relay_sender(sender, f"redaction {event_id}") + if not sender: + raise Exception("not logged in") + message = await DBMessage.get_by_mxid(event_id, self.mxid) + if message: + if not message.ktid: + raise NotImplementedError("Tried to redact message whose ktid is unknown") + try: + await message.delete() + await sender.client.delete_chat(self.channel_props, message.ktid) + except Exception as e: + self.log.exception(f"Unsend failed: {e}") + raise + return + + raise NotImplementedError("Only message redactions are supported") + + """ TODO + reaction = await DBReaction.get_by_mxid(event_id, self.mxid) + if reaction: + try: + await reaction.delete() + await sender.client.react(reaction.kt_msgid, None) + except Exception as e: + self.log.exception(f"Removing reaction failed: {e}") + raise + return + + raise NotImplementedError("Only message and reaction redactions are supported") + """ async def handle_matrix_reaction( self, sender: u.User, event_id: EventID, reacting_to: EventID, reaction: str diff --git a/node/src/client.js b/node/src/client.js index 5353bab..4c40ae3 100644 --- a/node/src/client.js +++ b/node/src/client.js @@ -570,6 +570,20 @@ export default class PeerClient { }) } + /** + * @param {Object} req + * @param {string} req.mxid + * @param {ChannelProps} req.channel_props + * @param {Long} req.chat_id + */ + deleteChat = async (req) => { + const talkChannel = await this.#getUserChannel(req.mxid, req.channel_props) + + return await talkChannel.deleteChat({ + logId: req.chat_id, + }) + } + #makeCommandResult(result) { return { success: true, @@ -653,6 +667,7 @@ export default class PeerClient { get_memo_ids: this.getMemoIds, send_chat: this.sendChat, send_media: this.sendMedia, + delete_chat: this.deleteChat, }[req.command] || this.handleUnknownCommand } const resp = { id: req.id }