refactor mappings getter

This commit is contained in:
Aine
2022-08-22 23:24:51 +03:00
parent 0decd4fad6
commit 848d6a7187
4 changed files with 14 additions and 18 deletions

View File

@@ -76,8 +76,8 @@ func (b *Bot) Start() error {
// Send email to matrix room
func (b *Bot) Send(ctx context.Context, from, to, subject, body string, files []*utils.File) error {
roomID, ok := b.rooms[utils.Mailbox(to)]
if !ok || roomID == "" {
roomID, ok := b.GetMapping(ctx, utils.Mailbox(to))
if !ok {
return errors.New("room not found")
}
@@ -117,15 +117,16 @@ func (b *Bot) Send(ctx context.Context, from, to, subject, body string, files []
}
// GetMappings returns mapping of mailbox = room
func (b *Bot) GetMappings(ctx context.Context) (map[string]id.RoomID, error) {
func (b *Bot) GetMapping(ctx context.Context, mailbox string) (id.RoomID, bool) {
if len(b.rooms) == 0 {
err := b.syncRooms(ctx)
if err != nil {
return nil, err
return "", false
}
}
return b.rooms, nil
roomID, ok := b.rooms[mailbox]
return roomID, ok
}
// Stop the bot