@ -141,18 +141,7 @@ func (c *Clients) updateClientInDB(newConfig types.ClientConfig) (new clientEntr
return
}
func ( c * Clients ) newClient ( config types . ClientConfig ) ( * matrix . Client , error ) {
homeserverURL , err := url . Parse ( config . HomeserverURL )
if err != nil {
return nil , err
}
client := matrix . NewClient ( homeserverURL , config . AccessToken , config . UserID )
// TODO: Check that the access token is valid for the userID by peforming
// a request against the server.
client . Worker . OnEventType ( "m.room.message" , func ( event * matrix . Event ) {
func ( c * Clients ) onMessageEvent ( client * matrix . Client , event * matrix . Event ) {
services , err := c . db . LoadServicesForUser ( client . UserID )
if err != nil {
log . WithFields ( log . Fields {
@ -166,9 +155,9 @@ func (c *Clients) newClient(config types.ClientConfig) (*matrix.Client, error) {
plugins = append ( plugins , service . Plugin ( client , event . RoomID ) )
}
plugin . OnMessage ( plugins , client , event )
} )
}
client . Worker . OnEventType ( "m.room.bot.options" , func ( event * matrix . Event ) {
func ( c * Clients ) onBotOptionsEvent ( client * matrix . Client , event * matrix . Event ) {
// see if these options are for us. The state key is the user ID with a leading _
// to get around restrictions in the HS about having user IDs as state keys.
targetUserID := strings . TrimPrefix ( event . StateKey , "_" )
@ -190,11 +179,10 @@ func (c *Clients) newClient(config types.ClientConfig) (*matrix.Client, error) {
"set_by_user_id" : event . Sender ,
} ) . Error ( "Failed to persist bot options" )
}
} )
}
if config . AutoJoinRooms {
client . Worker . OnEventType ( "m.room.member" , func ( event * matrix . Event ) {
if event . StateKey != config . UserID {
func ( c * Clients ) onRoomMemberEvent ( client * matrix . Client , event * matrix . Event ) {
if event . StateKey != client . UserID {
return // not our member event
}
m := event . Content [ "membership" ]
@ -205,7 +193,7 @@ func (c *Clients) newClient(config types.ClientConfig) (*matrix.Client, error) {
if membership == "invite" {
logger := log . WithFields ( log . Fields {
"room_id" : event . RoomID ,
"service_user_id" : config . UserID ,
"service_user_id" : client . UserID ,
"inviter" : event . Sender ,
} )
logger . Print ( "Accepting invite from user" )
@ -216,6 +204,39 @@ func (c *Clients) newClient(config types.ClientConfig) (*matrix.Client, error) {
logger . Print ( "Joined room" )
}
}
}
func ( c * Clients ) newClient ( config types . ClientConfig ) ( * matrix . Client , error ) {
homeserverURL , err := url . Parse ( config . HomeserverURL )
if err != nil {
return nil , err
}
client := matrix . NewClient ( homeserverURL , config . AccessToken , config . UserID )
client . OnSaveNextBatch ( func ( nextBatch string ) {
if err := c . db . UpdateNextBatch ( client . UserID , nextBatch ) ; err != nil {
log . WithFields ( log . Fields {
log . ErrorKey : err ,
"next_batch" : nextBatch ,
} ) . Error ( "Failed to persist next_batch token" )
}
} )
// TODO: Check that the access token is valid for the userID by peforming
// a request against the server.
client . Worker . OnEventType ( "m.room.message" , func ( event * matrix . Event ) {
c . onMessageEvent ( client , event )
} )
client . Worker . OnEventType ( "m.room.bot.options" , func ( event * matrix . Event ) {
c . onBotOptionsEvent ( client , event )
} )
if config . AutoJoinRooms {
client . Worker . OnEventType ( "m.room.member" , func ( event * matrix . Event ) {
c . onRoomMemberEvent ( client , event )
} )
}