Log last_seen time (so that we can delete old users)

This commit is contained in:
rxdn 2023-07-17 19:41:05 +01:00
parent 7a809a2a0f
commit 093769171a
6 changed files with 42 additions and 7 deletions

View File

@ -51,7 +51,8 @@ func ReloadGuildsHandler(ctx *gin.Context) {
return
}
if store.Expiry > (time.Now().UnixNano() / int64(time.Second)) {
// What does this do?
if store.Expiry > time.Now().Unix() {
res, err := discord.RefreshToken(store.RefreshToken)
if err != nil { // Tell client to re-authenticate
ctx.JSON(200, gin.H{
@ -63,7 +64,7 @@ func ReloadGuildsHandler(ctx *gin.Context) {
store.AccessToken = res.AccessToken
store.RefreshToken = res.RefreshToken
store.Expiry = (time.Now().UnixNano() / int64(time.Second)) + int64(res.ExpiresIn)
store.Expiry = time.Now().Unix() + int64(res.ExpiresIn)
if err := session.Store.Set(userId, store); err != nil {
ctx.JSON(500, utils.ErrorJson(err))

View File

@ -0,0 +1,29 @@
package middleware
import (
"context"
"github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/utils"
"github.com/gin-gonic/gin"
"time"
)
// UpdateLastSeen We store the last time a user was seen in the dashboard so that we can delete their data if they
// haven't logged in for 30 days.
func UpdateLastSeen(req *gin.Context) {
userId, ok := req.Keys["userid"].(uint64) // ok=false if not present
if !ok {
req.AbortWithStatusJSON(500, utils.ErrorStr("userid not present in context"))
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1500)
defer cancel()
if err := database.Client.DashboardUsers.UpdateLastSeen(ctx, userId); err != nil {
req.AbortWithStatusJSON(500, utils.ErrorStr(err.Error()))
return
}
req.Next()
}

View File

@ -62,7 +62,7 @@ func StartServer() {
router.POST("/callback", middleware.VerifyXTicketsHeader, root.CallbackHandler)
router.POST("/logout", middleware.VerifyXTicketsHeader, middleware.AuthenticateToken, root.LogoutHandler)
apiGroup := router.Group("/api", middleware.VerifyXTicketsHeader, middleware.AuthenticateToken)
apiGroup := router.Group("/api", middleware.VerifyXTicketsHeader, middleware.AuthenticateToken, middleware.UpdateLastSeen)
{
apiGroup.GET("/session", api.SessionHandler)
@ -171,7 +171,7 @@ func StartServer() {
guildAuthApiAdmin.DELETE("/integrations/:integrationid", api_integrations.RemoveIntegrationHandler)
}
userGroup := router.Group("/user", middleware.AuthenticateToken)
userGroup := router.Group("/user", middleware.AuthenticateToken, middleware.UpdateLastSeen)
{
userGroup.GET("/guilds", api.GetGuilds)
userGroup.POST("/guilds/reload", api.ReloadGuildsHandler)

View File

@ -6,6 +6,7 @@ import (
"fmt"
wrapper "github.com/TicketsBot/GoPanel/redis"
"github.com/go-redis/redis/v8"
"time"
)
var ErrNoSession = errors.New("no session data found")
@ -46,9 +47,11 @@ func (s *RedisStore) Set(userId uint64, data SessionData) error {
return err
}
return s.client.Set(wrapper.DefaultContext(), fmt.Sprintf("%s:%d", keyPrefix, userId), encoded, 0).Err()
expiration := time.Unix(data.Expiry, 0).Sub(time.Now())
return s.client.Set(wrapper.DefaultContext(), fmt.Sprintf("%s:%d", keyPrefix, userId), encoded, expiration).Err()
}
func (s *RedisStore) Clear(userId uint64) error {
return s.client.Del(wrapper.DefaultContext(), fmt.Sprintf("%s:%d", keyPrefix, userId)).Err()
}
}

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/BurntSushi/toml v1.2.1
github.com/TicketsBot/archiverclient v0.0.0-20220326163414-558fd52746dc
github.com/TicketsBot/common v0.0.0-20230702161316-9b2fa80535aa
github.com/TicketsBot/database v0.0.0-20230715182338-2e9789a42c47
github.com/TicketsBot/database v0.0.0-20230717183936-7cd4f5a16a94
github.com/TicketsBot/logarchiver v0.0.0-20220326162808-cdf0310f5e1c
github.com/TicketsBot/worker v0.0.0-20230715182525-e90848f33c9c
github.com/apex/log v1.1.2

2
go.sum
View File

@ -48,6 +48,8 @@ github.com/TicketsBot/common v0.0.0-20230702161316-9b2fa80535aa h1:6lMp2fzZvLpIq
github.com/TicketsBot/common v0.0.0-20230702161316-9b2fa80535aa/go.mod h1:zN6qXS5AYkt4JTHtq7mHT3eBHomUWZoZ29dZ/CPMjHQ=
github.com/TicketsBot/database v0.0.0-20230715182338-2e9789a42c47 h1:pNH5CTEG7VJBO7qS6gygPbKcSK573J5YO3rFFAmLWOI=
github.com/TicketsBot/database v0.0.0-20230715182338-2e9789a42c47/go.mod h1:gAtOoQKZfCkQ4AoNWQUSl51Fnlqk+odzD/hZ1e1sXyI=
github.com/TicketsBot/database v0.0.0-20230717183936-7cd4f5a16a94 h1:qnbSkw62U/0GVmn0JzmwjbgDMFG8NPqS8CmS+hMOlzw=
github.com/TicketsBot/database v0.0.0-20230717183936-7cd4f5a16a94/go.mod h1:gAtOoQKZfCkQ4AoNWQUSl51Fnlqk+odzD/hZ1e1sXyI=
github.com/TicketsBot/logarchiver v0.0.0-20220326162808-cdf0310f5e1c h1:OqGjFH6mbE6gd+NqI2ARJdtH3UUvhiAkD0r0fhGJK2s=
github.com/TicketsBot/logarchiver v0.0.0-20220326162808-cdf0310f5e1c/go.mod h1:jgi2OXQKsd5nUnTIRkwvPmeuD/i7OhN68LKMssuQY1c=
github.com/TicketsBot/ttlcache v1.6.1-0.20200405150101-acc18e37b261 h1:NHD5GB6cjlkpZFjC76Yli2S63/J2nhr8MuE6KlYJpQM=