From 1553ab525eedd28728f4b9ad70ee76aaf52c8f7c Mon Sep 17 00:00:00 2001 From: Janne Mareike Koschinski <janne@kuschku.de> Date: Fri, 28 Feb 2025 18:44:22 +0100 Subject: [PATCH] fix: make pushurl/token public --- matrixbot.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/matrixbot.go b/matrixbot.go index 3c6237c..9c7afb2 100644 --- a/matrixbot.go +++ b/matrixbot.go @@ -11,28 +11,28 @@ import ( ) type MatrixBot struct { - token *MatrixToken - pushUrl *url.URL + Token *MatrixToken + PushUrl *url.URL handlers map[string]func(bot *MatrixBot, notification Notification) error } func NewMatrixBot(pushUrl *url.URL) *MatrixBot { return &MatrixBot{ - token: nil, - pushUrl: pushUrl, + Token: nil, + PushUrl: pushUrl, handlers: make(map[string]func(bot *MatrixBot, notification Notification) error), } } func (bot *MatrixBot) RefreshToken() error { - if bot.token == nil { + if bot.Token == nil { return fmt.Errorf("no refresh token available") } - userData, err := Refresh(bot.token.RefreshToken) + userData, err := Refresh(bot.Token.RefreshToken) if err != nil { return err } - bot.token = &MatrixToken{ + bot.Token = &MatrixToken{ AccessToken: userData.AccessToken, Expires: time.Now().Add(time.Duration(userData.ExpiresInMs) / 2 * time.Millisecond), RefreshToken: userData.RefreshToken, @@ -45,7 +45,7 @@ func (bot *MatrixBot) Login(username string, password string, deviceId string) e if err != nil { return err } - bot.token = &MatrixToken{ + bot.Token = &MatrixToken{ AccessToken: userData.AccessToken, Expires: time.Now().Add(time.Duration(userData.ExpiresInMs) / 2 * time.Millisecond), RefreshToken: userData.RefreshToken, @@ -55,7 +55,7 @@ func (bot *MatrixBot) Login(username string, password string, deviceId string) e func (bot *MatrixBot) RefreshTask() { for true { - if bot.token != nil && time.Now().After(bot.token.Expires) { + if bot.Token != nil && time.Now().After(bot.Token.Expires) { if err := bot.RefreshToken(); err != nil { log.Printf("error refresh token: %s\n", err.Error()) } @@ -65,7 +65,7 @@ func (bot *MatrixBot) RefreshTask() { } func (bot *MatrixBot) RegisterPusher() error { - return SetPusher(*bot.token, bot.pushUrl.String()) + return SetPusher(*bot.Token, bot.PushUrl.String()) } func (bot *MatrixBot) HandleFunc(command string, handler func(bot *MatrixBot, notification Notification) error) { @@ -76,8 +76,8 @@ func (bot *MatrixBot) Serve(endpoint string) { http.HandleFunc("/healthz", func(writer http.ResponseWriter, request *http.Request) { _, _ = io.WriteString(writer, "OK\n") }) - log.Printf("listening for push notifications on %s\n", bot.pushUrl.Path) - http.HandleFunc(bot.pushUrl.Path, func(writer http.ResponseWriter, request *http.Request) { + log.Printf("listening for push notifications on %s\n", bot.PushUrl.Path) + http.HandleFunc(bot.PushUrl.Path, func(writer http.ResponseWriter, request *http.Request) { log.Println("Received push notification") notification, err := ParseNotification(request.Body) if err != nil { @@ -87,7 +87,7 @@ func (bot *MatrixBot) Serve(endpoint string) { if notification.EventId == "" { return } - err = SetReadReceipt(*bot.token, notification.RoomId, notification.EventId) + err = SetReadReceipt(*bot.Token, notification.RoomId, notification.EventId) if err != nil { log.Println(err.Error()) return -- GitLab