Browse Source

Implement reactions from the matrix side

Gary Kramlich 3 years ago
parent
commit
3efe242d70
2 changed files with 35 additions and 0 deletions
  1. 12 0
      bridge/matrix.go
  2. 23 0
      bridge/portal.go

+ 12 - 0
bridge/matrix.go

@@ -29,6 +29,7 @@ func (b *Bridge) setupEvents() {
 	}
 
 	b.eventProcessor.On(event.EventMessage, b.matrixHandler.handleMessage)
+	b.eventProcessor.On(event.EventReaction, b.matrixHandler.handleReaction)
 	b.eventProcessor.On(event.StateMember, b.matrixHandler.handleMembership)
 }
 
@@ -230,3 +231,14 @@ func (mh *matrixHandler) handleMembership(evt *event.Event) {
 		portal.handleMatrixInvite(user, evt)
 	}
 }
+
+func (mh *matrixHandler) handleReaction(evt *event.Event) {
+	if mh.ignoreEvent(evt) {
+		return
+	}
+
+	portal := mh.bridge.GetPortalByMXID(evt.RoomID)
+	if portal != nil {
+		portal.handleMatrixReaction(evt)
+	}
+}

+ 23 - 0
bridge/portal.go

@@ -367,6 +367,8 @@ func (p *Portal) handleMatrixMessages(msg portalMatrixMessage) {
 	switch msg.evt.Type {
 	case event.EventMessage:
 		p.handleMatrixMessage(msg.user, msg.evt)
+	default:
+		p.log.Debugln("unknown event type", msg.evt.Type)
 	}
 }
 
@@ -514,3 +516,24 @@ func (p *Portal) handleMatrixKick(sender *User, target *Puppet) {
 	// TODO: need to learn how to make this happen as discordgo proper doesn't
 	// support group dms and it looks like it's a binary blob.
 }
+
+func (p *Portal) handleMatrixReaction(evt *event.Event) {
+	reaction := evt.Content.AsReaction()
+	if reaction.RelatesTo.Type != event.RelAnnotation {
+		p.log.Errorfln("Ignoring reaction %s due to unknown m.relates_to data", evt.ID)
+
+		return
+	}
+
+	msg := p.bridge.db.Message.GetByMatrixID(p.Key, reaction.RelatesTo.EventID)
+	if msg.DiscordID == "" {
+		p.log.Debugf("Message %s has not yet been sent to discord", reaction.RelatesTo.EventID)
+
+		return
+	}
+
+	user := p.bridge.GetUserByMXID(evt.Sender)
+	if user != nil {
+		user.Session.MessageReactionAdd(p.Key.ChannelID, msg.DiscordID, reaction.RelatesTo.Key)
+	}
+}