diff --git a/README.md b/README.md index 6ccd7fa..86a075a 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,6 @@ docker run -d --name matrix-gpt \ -e SQLITE_PATH="persistent path for sqlite database" -e USER_IDS="allowed user ids" ghcr.io/mazzz1y/matrix-gpt:latest - ``` ## Configuration diff --git a/internal/bot/bot.go b/internal/bot/bot.go index d118e66..21debdc 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -1,8 +1,6 @@ package bot import ( - "sync" - _ "github.com/mattn/go-sqlite3" "github.com/mazzz1y/matrix-gpt/internal/gpt" "github.com/rs/zerolog/log" @@ -11,7 +9,6 @@ import ( ) type Bot struct { - sync.Mutex client *mautrix.Client gptClient *gpt.Gpt selfProfile mautrix.RespUserProfile diff --git a/internal/bot/handlers.go b/internal/bot/handlers.go index d29c246..2f53fee 100644 --- a/internal/bot/handlers.go +++ b/internal/bot/handlers.go @@ -43,10 +43,12 @@ func (b *Bot) historyResetHandler(user *gpt.User, evt *event.Event) (ok bool) { } // historyExpireHandler checks if the history for a user has expired and resets if necessary. -func (b *Bot) historyExpireHandler(user *gpt.User) { - if user.LastMsg.Add(time.Duration(b.historyExpire) * time.Hour).Before(time.Now()) { +func (b *Bot) historyExpireHandler(user *gpt.User) (ok bool) { + if user.GetLastMsgTime().Add(time.Duration(b.historyExpire) * time.Hour).Before(time.Now()) { user.History.ResetHistory() + return true } + return false } // msgEvtDispatcher dispatches incoming messages to their appropriate handlers. @@ -67,15 +69,20 @@ func (b *Bot) msgEvtDispatcher(source mautrix.EventSource, evt *event.Event) { return } + if b.historyExpireHandler(user) { + l.Info().Msg("history has expired, resetting") + } if b.historyResetHandler(user, evt) { - l.Info().Msg("reset history") + l.Info().Msg("history reset by user command") return } err := b.sendAnswer(user, evt) if err != nil { l.Err(err).Msg("failed to send message") - } else { - l.Info().Msg("sending answer") + return } + + l.Info().Msg("sending answer") + user.UpdateLastMsgTime() } diff --git a/internal/gpt/users.go b/internal/gpt/users.go index 5df8600..38e5659 100644 --- a/internal/gpt/users.go +++ b/internal/gpt/users.go @@ -1,18 +1,37 @@ package gpt import ( + "sync" "time" ) // User represents a GPT user with a chat history and last message timestamp. type User struct { + sync.RWMutex History *HistoryManager - LastMsg time.Time + lastMsg time.Time } // NewGptUser creates a new GPT user instance with a given history size. func NewGptUser(historySize int) *User { return &User{ History: NewHistoryManager(historySize), + lastMsg: time.Now(), } } + +// GetLastMsgTime retrieves the timestamp of the user's last message. +func (u *User) GetLastMsgTime() time.Time { + u.RLock() + defer u.RUnlock() + + return u.lastMsg +} + +// UpdateLastMsgTime updates the timestamp of the user's last message to the current time. +func (u *User) UpdateLastMsgTime() { + u.Lock() + defer u.Unlock() + + u.lastMsg = time.Now() +}