mirror of
https://github.com/maubot/maubot.git
synced 2024-10-01 01:06:10 -04:00
Use custom type for EventHandler output
This commit is contained in:
parent
feaf75b327
commit
944b384633
@ -53,7 +53,13 @@ const (
|
||||
|
||||
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 {
|
||||
AddEventHandler(EventType, EventHandler)
|
||||
|
@ -57,9 +57,9 @@ func (client *Client) ParseEvent(evt *gomatrix.Event) *Event {
|
||||
}
|
||||
|
||||
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 {
|
||||
return false
|
||||
return maubot.StopPropagation
|
||||
}
|
||||
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 {
|
||||
evt, err := client.Client.GetEvent(roomID, eventID)
|
||||
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 client.ParseEvent(evt).Interface()
|
||||
}
|
||||
|
||||
func (client *Client) onJoin(evt *maubot.Event) bool {
|
||||
if !client.DB.AutoJoinRooms || evt.StateKey != client.DB.UserID {
|
||||
return true
|
||||
}
|
||||
if evt.Content.Membership == "invite" {
|
||||
func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult {
|
||||
if client.DB.AutoJoinRooms && evt.StateKey == client.DB.UserID && evt.Content.Membership == "invite" {
|
||||
client.JoinRoom(evt.RoomID)
|
||||
return false
|
||||
return maubot.StopPropagation
|
||||
}
|
||||
return true
|
||||
return maubot.Continue
|
||||
}
|
||||
|
||||
func (client *Client) JoinRoom(roomID string) {
|
||||
|
@ -135,7 +135,7 @@ func (s *MaubotSyncer) notifyListeners(mxEvent *gomatrix.Event) {
|
||||
return
|
||||
}
|
||||
for _, fn := range listeners {
|
||||
if !fn(event.Interface()) {
|
||||
if fn(event.Interface()) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user