Explorar o código

Implement message editing from the matrix side

Gary Kramlich %!s(int64=3) %!d(string=hai) anos
pai
achega
11bd655027
Modificáronse 1 ficheiros con 28 adicións e 12 borrados
  1. 28 12
      bridge/portal.go

+ 28 - 12
bridge/portal.go

@@ -413,20 +413,36 @@ func (p *Portal) handleMatrixMessage(sender *User, evt *event.Event) {
 		return
 	}
 
-	msg, err := sender.Session.ChannelMessageSend(p.Key.ChannelID, content.Body)
-	if err != nil {
-		p.log.Errorfln("Failed to send message: %v", err)
+	if content.RelatesTo != nil && content.RelatesTo.Type == event.RelReplace {
+		existing := p.bridge.db.Message.GetByMatrixID(p.Key, content.RelatesTo.EventID)
+
+		if existing != nil && existing.DiscordID != "" {
+			// we don't have anything to save for the update message right now
+			// as we're not tracking edited timestamps.
+			_, err := sender.Session.ChannelMessageEdit(p.Key.ChannelID,
+				existing.DiscordID, content.NewContent.Body)
+			if err != nil {
+				p.log.Errorln("Failed to update message %s: %v", existing.DiscordID, err)
 
-		return
-	}
+				return
+			}
+		}
+	} else {
+		msg, err := sender.Session.ChannelMessageSend(p.Key.ChannelID, content.Body)
+		if err != nil {
+			p.log.Errorfln("Failed to send message: %v", err)
+
+			return
+		}
 
-	dbMsg := p.bridge.db.Message.New()
-	dbMsg.Channel = p.Key
-	dbMsg.DiscordID = msg.ID
-	dbMsg.MatrixID = evt.ID
-	dbMsg.AuthorID = sender.ID
-	dbMsg.Timestamp = time.Now()
-	dbMsg.Insert()
+		dbMsg := p.bridge.db.Message.New()
+		dbMsg.Channel = p.Key
+		dbMsg.DiscordID = msg.ID
+		dbMsg.MatrixID = evt.ID
+		dbMsg.AuthorID = sender.ID
+		dbMsg.Timestamp = time.Now()
+		dbMsg.Insert()
+	}
 }
 
 func (p *Portal) handleMatrixLeave(sender *User) {