From 093769171afb8d5422e6fae984d573953b0bdd50 Mon Sep 17 00:00:00 2001 From: rxdn <29165304+rxdn@users.noreply.github.com> Date: Mon, 17 Jul 2023 19:41:05 +0100 Subject: [PATCH] Log last_seen time (so that we can delete old users) --- app/http/endpoints/api/reloadguilds.go | 5 +++-- app/http/middleware/updatelastseen.go | 29 ++++++++++++++++++++++++++ app/http/server.go | 4 ++-- app/http/session/redisstore.go | 7 +++++-- go.mod | 2 +- go.sum | 2 ++ 6 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 app/http/middleware/updatelastseen.go diff --git a/app/http/endpoints/api/reloadguilds.go b/app/http/endpoints/api/reloadguilds.go index d5ba10a..2edd3e9 100644 --- a/app/http/endpoints/api/reloadguilds.go +++ b/app/http/endpoints/api/reloadguilds.go @@ -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)) diff --git a/app/http/middleware/updatelastseen.go b/app/http/middleware/updatelastseen.go new file mode 100644 index 0000000..76141ae --- /dev/null +++ b/app/http/middleware/updatelastseen.go @@ -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() +} diff --git a/app/http/server.go b/app/http/server.go index 9f1a00f..92d3bb8 100644 --- a/app/http/server.go +++ b/app/http/server.go @@ -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) diff --git a/app/http/session/redisstore.go b/app/http/session/redisstore.go index 9f4e670..b2ea028 100644 --- a/app/http/session/redisstore.go +++ b/app/http/session/redisstore.go @@ -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() -} \ No newline at end of file +} diff --git a/go.mod b/go.mod index ce9d041..506e48c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b172c8c..6c731e9 100644 --- a/go.sum +++ b/go.sum @@ -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=