dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

global.go (1149B)


      1 package global
      2 
      3 import (
      4 	"dkforest/pkg/cache"
      5 	"dkforest/pkg/database"
      6 	"time"
      7 )
      8 
      9 var notifCountCache = cache.NewWithKey[string, int64](30*time.Second, time.Minute)
     10 
     11 // Notifications count cache has to be set per user session.
     12 // Each session can have a different notif count.
     13 // This is due to the fact that we notify other sessions of new successful
     14 // logins on the user account.
     15 func cacheKey(userID database.UserID, sessionToken string) string {
     16 	return userID.String() + "_" + sessionToken
     17 }
     18 
     19 func DeleteUserNotificationCount(userID database.UserID, sessionToken string) {
     20 	notifCountCache.Delete(cacheKey(userID, sessionToken))
     21 }
     22 
     23 func GetUserNotificationCount(db *database.DkfDB, userID database.UserID, sessionToken string) int64 {
     24 	count, found := notifCountCache.Get(cacheKey(userID, sessionToken))
     25 	if found {
     26 		return count
     27 	}
     28 	count = db.GetUserInboxMessagesCount(userID)
     29 	count += db.GetUserNotificationsCount(userID)
     30 	// sessionToken can be empty when using the API
     31 	if sessionToken != "" {
     32 		count += db.GetUserSessionNotificationsCount(sessionToken)
     33 	}
     34 	notifCountCache.SetD(cacheKey(userID, sessionToken), count)
     35 	return count
     36 }