Skip to content

Commit

Permalink
Add handling and showing reaction events
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung committed Feb 24, 2024
1 parent 115f729 commit 552f997
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
81 changes: 72 additions & 9 deletions bridge/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,21 @@ func (m *Matrix) syncCallback(resp *mautrix.RespSync, since string) bool {
func (m *Matrix) handleMatrix(onConnect func()) {
syncer := m.mc.Syncer.(*mautrix.DefaultSyncer)

syncer.OnEventType(event.EventRedaction, m.handleEvent)
syncer.OnEventType(event.EventMessage, m.handleEvent)
syncer.OnEventType(event.EventMessage, m.handleMessageEvent)
syncer.OnEventType(event.EventReaction, m.handleReactionEvent)
syncer.OnEventType(event.EventRedaction, m.handleMessageEvent)
syncer.OnEventType(event.StateMember, m.handleMember)
syncer.OnEventType(event.StateCreate, m.handleCreate)
syncer.OnEventType(event.StateRoomName, m.handleRoomName)
// syncer.OnEventType(event.AccountDataDirectChats, m.handleDM)
syncer.OnEventType(event.StateCanonicalAlias, m.handleCanonicalAlias)
syncer.OnEvent(func(source mautrix.EventSource, evt *event.Event) {
syncer.OnEvent(func(source mautrix.EventSource, ev *event.Event) {
// sync is almost complete
if evt.RoomID.String() == "marker" {
if ev.RoomID.String() == "marker" {
m.firstSync = true
}
logger.Tracef("handleMatrix source.String() %s", source.String())
logger.Tracef("handleMatrix evt %s", spew.Sdump(evt))
logger.Tracef("handleMatrix ev %s", spew.Sdump(ev))
})

syncer.OnSync(m.syncCallback)
Expand Down Expand Up @@ -272,15 +273,30 @@ func (m *Matrix) handleCanonicalAlias(source mautrix.EventSource, ev *event.Even
// m.mc.JoinedMembers(ev.RoomID)
}

func (m *Matrix) handleEvent(source mautrix.EventSource, ev *event.Event) {
text, _ := ev.Content.Raw["body"].(string)
func (m *Matrix) handleMessageEvent(source mautrix.EventSource, ev *event.Event) {

Check failure on line 276 in bridge/matrix/matrix.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Function 'handleMessageEvent' is too long (61 > 60) (funlen)
logger.Tracef("handleMessageEvent ev %s", spew.Sdump(ev))

ghost := m.createUser(ev.Sender)

if ghost.Me {
logger.Trace("handleMessageEvent ghost.Me")
return
}

var text string
var parentID string

switch {
case ev.Type.String() == "m.text":
msgEventContent, _ := ev.Content.Parsed.(*event.MessageEventContent)
text = msgEventContent.Body
if msgEventContent.RelatesTo != nil {
parentID = msgEventContent.RelatesTo.EventID.String()
}
default:
logger.Debugf("handleMessageEvent unsupported event type %s", ev.Type.String())
}

m.RLock()
_, ok := m.dmChannels[ev.RoomID]
m.RUnlock()
Expand All @@ -296,7 +312,7 @@ func (m *Matrix) handleEvent(source mautrix.EventSource, ev *event.Event) {
// Files: m.getFilesFromData(data),
MessageID: string(ev.ID),
// Event: rmsg.Event,
// ParentID: mxEvent
ParentID: parentID,
},
}

Expand All @@ -314,7 +330,54 @@ func (m *Matrix) handleEvent(source mautrix.EventSource, ev *event.Event) {
// Files: m.getFilesFromData(data),
MessageID: string(ev.ID),
// Event: rmsg.Event,
// ParentID: mxEvent
ParentID: parentID,
},
}

m.eventChan <- event
}

func (m *Matrix) handleReactionEvent(source mautrix.EventSource, ev *event.Event) {
logger.Tracef("handleReactionEvent ev %s", spew.Sdump(ev))

ghost := m.createUser(ev.Sender)

if ghost.Me {
logger.Trace("handleReactionEvent ghost.Me")
return
}

var reaction string
var parentID string

switch {
case ev.Type.String() == "m.reaction":
reactionEventContent, _ := ev.Content.Parsed.(*event.ReactionEventContent)
reaction = reactionEventContent.RelatesTo.Key
parentID = reactionEventContent.RelatesTo.EventID.String()
default:
logger.Debugf("handleEvent unsupported event type %s", ev.Type.String())
}

m.RLock()
_, ok := m.dmChannels[ev.RoomID]
m.RUnlock()

channelType := ""
if ok {
channelType = "D"
}

event := &bridge.Event{ //nolint:gocritic
Type: "reaction_add",
Data: &bridge.ReactionAddEvent{
ChannelID: ev.RoomID.String(),
MessageID: string(ev.ID),
Sender: ghost,
Reaction: reaction,
ChannelType: channelType,
Message: "",
ParentID: parentID,
},
}

Expand Down
6 changes: 3 additions & 3 deletions mm-go-irckit/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func login(u *User, toUser *User, args []string, service string) {
if service == "matrix" { //nolint:nestif
var err error

warningMsg := "WARNING: Matrix support is very much experimental and a work in progress. Use at your own risk."
u.MsgUser(toUser, warningMsg)
logger.Warn(warningMsg)
wMsg := "Matrix support is very much experimental and a work in progress. Use at your own risk."
u.MsgUser(toUser, "WARNING: "+wMsg)
logger.Warn(wMsg)

if len(args) != 1 && len(args) != 3 {
u.MsgUser(toUser, "need LOGIN <server> <login> <pass> or LOGIN <token>")
Expand Down

0 comments on commit 552f997

Please sign in to comment.