Use custom type for EventHandler output

This commit is contained in:
Tulir Asokan 2018-06-14 20:21:44 +03:00
parent feaf75b327
commit 944b384633
3 changed files with 15 additions and 12 deletions

View File

@ -53,7 +53,13 @@ const (
const FormatHTML = "org.matrix.custom.html" const FormatHTML = "org.matrix.custom.html"
type EventHandler func(*Event) bool type EventHandler func(*Event) EventHandlerResult
type EventHandlerResult bool
const (
Continue EventHandlerResult = false
StopPropagation EventHandlerResult = true
)
type MatrixClient interface { type MatrixClient interface {
AddEventHandler(EventType, EventHandler) AddEventHandler(EventType, EventHandler)

View File

@ -57,9 +57,9 @@ func (client *Client) ParseEvent(evt *gomatrix.Event) *Event {
} }
func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) { func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) {
client.syncer.OnEventType(evt, func(evt *maubot.Event) bool { client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult {
if evt.Sender == client.UserID { if evt.Sender == client.UserID {
return false return maubot.StopPropagation
} }
return handler(evt) return handler(evt)
}) })
@ -68,21 +68,18 @@ func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.Event
func (client *Client) GetEvent(roomID, eventID string) *maubot.Event { func (client *Client) GetEvent(roomID, eventID string) *maubot.Event {
evt, err := client.Client.GetEvent(roomID, eventID) evt, err := client.Client.GetEvent(roomID, eventID)
if err != nil { if err != nil {
log.Warnf("Failed to get event %s @ %s: %v", eventID, roomID, err) log.Warnf("Failed to get event %s @ %s: %v\n", eventID, roomID, err)
return nil return nil
} }
return client.ParseEvent(evt).Interface() return client.ParseEvent(evt).Interface()
} }
func (client *Client) onJoin(evt *maubot.Event) bool { func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult {
if !client.DB.AutoJoinRooms || evt.StateKey != client.DB.UserID { if client.DB.AutoJoinRooms && evt.StateKey == client.DB.UserID && evt.Content.Membership == "invite" {
return true
}
if evt.Content.Membership == "invite" {
client.JoinRoom(evt.RoomID) client.JoinRoom(evt.RoomID)
return false return maubot.StopPropagation
} }
return true return maubot.Continue
} }
func (client *Client) JoinRoom(roomID string) { func (client *Client) JoinRoom(roomID string) {

View File

@ -135,7 +135,7 @@ func (s *MaubotSyncer) notifyListeners(mxEvent *gomatrix.Event) {
return return
} }
for _, fn := range listeners { for _, fn := range listeners {
if !fn(event.Interface()) { if fn(event.Interface()) {
break break
} }
} }