Some cleanup and moving around parts

This commit is contained in:
Till Faelligen 2024-06-19 12:55:18 +02:00
parent 00da9b50b2
commit f8f0531bfc
No known key found for this signature in database
GPG key ID: ACCDC9606D472758

View file

@ -124,14 +124,14 @@ func (rp *RequestPool) cleanPresence(db storage.Presence, cleanupTime time.Durat
// this way it can filter based on time // this way it can filter based on time
type PresenceMap struct { type PresenceMap struct {
mu sync.Mutex mu sync.Mutex
seen map[string]map[int]int64 seen map[string]map[types.Presence]time.Time
} }
var lastPresence PresenceMap var lastPresence PresenceMap
// how long before the online status expires // how long before the online status expires
// should be long enough that any client will have another sync before expiring // should be long enough that any client will have another sync before expiring
const presenceTimeout int64 = 10 const presenceTimeout = time.Second * 10
// updatePresence sends presence updates to the SyncAPI and FederationAPI // updatePresence sends presence updates to the SyncAPI and FederationAPI
func (rp *RequestPool) updatePresence(db storage.Presence, presence string, userID string) { func (rp *RequestPool) updatePresence(db storage.Presence, presence string, userID string) {
@ -139,17 +139,15 @@ func (rp *RequestPool) updatePresence(db storage.Presence, presence string, user
rp.updatePresenceInternal(db, presence, userID, true) rp.updatePresenceInternal(db, presence, userID, true)
} }
func (rp *RequestPool) updatePresenceInternal(db storage.Presence, presence string, userID string, check_again bool) { func (rp *RequestPool) updatePresenceInternal(db storage.Presence, presence string, userID string, checkAgain bool) {
//lock the map to this thread
lastPresence.mu.Lock()
//grab time for caching
workingTime := time.Now().Unix()
if !rp.cfg.Matrix.Presence.EnableOutbound { if !rp.cfg.Matrix.Presence.EnableOutbound {
return return
} }
// lock the map to this thread
lastPresence.mu.Lock()
defer lastPresence.mu.Unlock()
if presence == "" { if presence == "" {
presence = types.PresenceOnline.String() presence = types.PresenceOnline.String()
} }
@ -167,46 +165,39 @@ func (rp *RequestPool) updatePresenceInternal(db storage.Presence, presence stri
// make sure that the map is defined correctly as needed // make sure that the map is defined correctly as needed
if lastPresence.seen == nil { if lastPresence.seen == nil {
lastPresence.seen = make(map[string]map[int]int64) lastPresence.seen = make(map[string]map[types.Presence]time.Time)
} }
if lastPresence.seen[userID] == nil { if lastPresence.seen[userID] == nil {
lastPresence.seen[userID] = make(map[int]int64) lastPresence.seen[userID] = make(map[types.Presence]time.Time)
} }
now := time.Now()
// update time for each presence // update time for each presence
lastPresence.seen[userID][int(presenceID)] = workingTime lastPresence.seen[userID][presenceID] = now
var presenceToSet types.Presence
// Default to unknown presence
presenceToSet := types.PresenceUnknown
switch {
case now.Sub(lastPresence.seen[userID][types.PresenceOnline]) < presenceTimeout:
// online will always get priority // online will always get priority
if (workingTime - lastPresence.seen[userID][int(types.PresenceOnline)]) < presenceTimeout {
presenceToSet = types.PresenceOnline presenceToSet = types.PresenceOnline
case now.Sub(lastPresence.seen[userID][types.PresenceUnavailable]) < presenceTimeout:
// idle gets secondary priority because your presence shouldnt be idle if you are on a different device // idle gets secondary priority because your presence shouldnt be idle if you are on a different device
// kinda copying discord presence // kinda copying discord presence
} else if (workingTime - lastPresence.seen[userID][int(types.PresenceUnavailable)]) < presenceTimeout {
presenceToSet = types.PresenceUnavailable presenceToSet = types.PresenceUnavailable
case now.Sub(lastPresence.seen[userID][types.PresenceOffline]) < presenceTimeout:
// only set offline status if there is no known online devices // only set offline status if there is no known online devices
// clients may set offline to attempt to not alter the online status of the user // clients may set offline to attempt to not alter the online status of the user
} else if (workingTime - lastPresence.seen[userID][int(types.PresenceOffline)]) < presenceTimeout {
presenceToSet = types.PresenceOffline presenceToSet = types.PresenceOffline
if check_again { if checkAgain {
// after a timeout, check presence again to make sure it gets set as offline sooner or later // after a timeout, check presence again to make sure it gets set as offline sooner or later
time.AfterFunc(time.Second*time.Duration(presenceTimeout), func() { rp.updatePresenceInternal(db, types.PresenceOffline.String(), userID, false) }) time.AfterFunc(time.Second*time.Duration(presenceTimeout), func() {
rp.updatePresenceInternal(db, types.PresenceOffline.String(), userID, false)
})
} }
//set unknown if there is truly no devices that we know the state of
} else {
presenceToSet = types.PresenceUnknown
} }
//the map is no longer being written to or read from
//i assume let the rest happen without being held up as to keep things heading through
//as fast and smoothly as possible
lastPresence.mu.Unlock()
// ensure we also send the current status_msg to federated servers and not nil // ensure we also send the current status_msg to federated servers and not nil
dbPresence, err := db.GetPresences(context.Background(), []string{userID}) dbPresence, err := db.GetPresences(context.Background(), []string{userID})
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {