From 1d50da4b1c43273ab2dc8c47829e03f5f6981f92 Mon Sep 17 00:00:00 2001 From: Gary Kim Date: Tue, 1 Jun 2021 17:17:07 -0400 Subject: Add support for message deletion (nctalk) (#1492) * nctalk: add message deletion support Signed-off-by: Gary Kim * nctalk: seperate out deletion and sending logic Signed-off-by: Gary Kim * nctalk: update library to v0.2.0 Signed-off-by: Gary Kim * Rename functions to be clearer Signed-off-by: Gary Kim * Update to go-nc-talk v0.2.1 Signed-off-by: Gary Kim * Update to go-nc-talk v0.2.2 Signed-off-by: Gary Kim * Make deletions easier to debug Signed-off-by: Gary Kim --- bridge/nctalk/nctalk.go | 131 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 87 insertions(+), 44 deletions(-) (limited to 'bridge/nctalk/nctalk.go') diff --git a/bridge/nctalk/nctalk.go b/bridge/nctalk/nctalk.go index 4537989c..9d0d4517 100644 --- a/bridge/nctalk/nctalk.go +++ b/bridge/nctalk/nctalk.go @@ -74,12 +74,6 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error { } b.rooms = append(b.rooms, newRoom) - // Config - guestSuffix := " (Guest)" - if b.IsKeySet("GuestSuffix") { - guestSuffix = b.GetString("GuestSuffix") - } - go func() { for msg := range c { msg := msg @@ -90,35 +84,23 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error { return } - // ignore messages that are one of the following - // * not a message from a user - // * from ourselves - if msg.MessageType != ocs.MessageComment || msg.ActorID == b.user.User { + // Ignore messages that are from the bot user + if msg.ActorID == b.user.User { continue } - remoteMessage := config.Message{ - Text: formatRichObjectString(msg.Message, msg.MessageParameters), - Channel: newRoom.room.Token, - Username: DisplayName(msg, guestSuffix), - UserID: msg.ActorID, - Account: b.Account, - } - // It is possible for the ID to not be set on older versions of Talk so we only set it if - // the ID is not blank - if msg.ID != 0 { - remoteMessage.ID = strconv.Itoa(msg.ID) - } - // Handle Files - err = b.handleFiles(&remoteMessage, &msg) - if err != nil { - b.Log.Errorf("Error handling file: %#v", msg) + // Handle deleting messages + if msg.MessageType == ocs.MessageSystem && msg.Parent != nil && msg.Parent.MessageType == ocs.MessageDelete { + b.handleDeletingMessage(&msg, &newRoom) + continue + } + // Handle sending messages + if msg.MessageType == ocs.MessageComment { + b.handleSendingMessage(&msg, &newRoom) continue } - b.Log.Debugf("<= Message is %#v", remoteMessage) - b.Remote <- remoteMessage } }() return nil @@ -131,26 +113,40 @@ func (b *Btalk) Send(msg config.Message) (string, error) { return "", nil } - // Talk currently only supports sending normal messages - if msg.Event != "" { - return "", nil - } + // Standard Message Send + if msg.Event == "" { + // Handle sending files if they are included + err := b.handleSendingFile(&msg, r) + if err != nil { + b.Log.Errorf("Could not send files in message to room %v from %v: %v", msg.Channel, msg.Username, err) - // Handle sending files if they are included - err := b.handleSendingFile(&msg, r) - if err != nil { - b.Log.Errorf("Could not send files in message to room %v from %v: %v", msg.Channel, msg.Username, err) + return "", nil + } - return "", nil - } + sentMessage, err := r.room.SendMessage(msg.Username + msg.Text) + if err != nil { + b.Log.Errorf("Could not send message to room %v from %v: %v", msg.Channel, msg.Username, err) - sentMessage, err := r.room.SendMessage(msg.Username + msg.Text) - if err != nil { - b.Log.Errorf("Could not send message to room %v from %v: %v", msg.Channel, msg.Username, err) + return "", nil + } + return strconv.Itoa(sentMessage.ID), nil + } - return "", nil + // Message Deletion + if msg.Event == config.EventMsgDelete { + messageID, err := strconv.Atoi(msg.ID) + if err != nil { + return "", err + } + data, err := r.room.DeleteMessage(messageID) + if err != nil { + return "", err + } + return strconv.Itoa(data.ID), nil } - return strconv.Itoa(sentMessage.ID), nil + + // Message is not a type that is currently supported + return "", nil } func (b *Btalk) getRoom(token string) *Broom { @@ -208,6 +204,53 @@ func (b *Btalk) handleSendingFile(msg *config.Message, r *Broom) error { return nil } +func (b *Btalk) handleSendingMessage(msg *ocs.TalkRoomMessageData, r *Broom) { + remoteMessage := config.Message{ + Text: formatRichObjectString(msg.Message, msg.MessageParameters), + Channel: r.room.Token, + Username: DisplayName(msg, b.guestSuffix()), + UserID: msg.ActorID, + Account: b.Account, + } + // It is possible for the ID to not be set on older versions of Talk so we only set it if + // the ID is not blank + if msg.ID != 0 { + remoteMessage.ID = strconv.Itoa(msg.ID) + } + + // Handle Files + err := b.handleFiles(&remoteMessage, msg) + if err != nil { + b.Log.Errorf("Error handling file: %#v", msg) + + return + } + + b.Log.Debugf("<= Message is %#v", remoteMessage) + b.Remote <- remoteMessage +} + +func (b *Btalk) handleDeletingMessage(msg *ocs.TalkRoomMessageData, r *Broom) { + remoteMessage := config.Message{ + Event: config.EventMsgDelete, + Text: config.EventMsgDelete, + Channel: r.room.Token, + ID: strconv.Itoa(msg.Parent.ID), + Account: b.Account, + } + b.Log.Debugf("<= Message being deleted is %#v", remoteMessage) + b.Remote <- remoteMessage +} + +func (b *Btalk) guestSuffix() string { + guestSuffix := " (Guest)" + if b.IsKeySet("GuestSuffix") { + guestSuffix = b.GetString("GuestSuffix") + } + + return guestSuffix +} + // Spec: https://github.com/nextcloud/server/issues/1706#issue-182308785 func formatRichObjectString(message string, parameters map[string]ocs.RichObjectString) string { for id, parameter := range parameters { @@ -228,7 +271,7 @@ func formatRichObjectString(message string, parameters map[string]ocs.RichObject return message } -func DisplayName(msg ocs.TalkRoomMessageData, suffix string) string { +func DisplayName(msg *ocs.TalkRoomMessageData, suffix string) string { if msg.ActorType == ocs.ActorGuest { if msg.ActorDisplayName == "" { return "Guest" -- cgit v1.2.3