mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 06:53:09 -06:00
Some cleanup and moving around parts
This commit is contained in:
parent
00da9b50b2
commit
f8f0531bfc
|
|
@ -124,32 +124,30 @@ 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) {
|
||||||
//allow checking back on presence to set offline if needed
|
// allow checking back on presence to set offline if needed
|
||||||
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()
|
||||||
}
|
}
|
||||||
|
|
@ -165,48 +163,41 @@ func (rp *RequestPool) updatePresenceInternal(db storage.Presence, presence stri
|
||||||
LastActiveTS: spec.AsTimestamp(time.Now()),
|
LastActiveTS: spec.AsTimestamp(time.Now()),
|
||||||
}
|
}
|
||||||
|
|
||||||
//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)
|
||||||
}
|
}
|
||||||
|
|
||||||
//update time for each presence
|
now := time.Now()
|
||||||
lastPresence.seen[userID][int(presenceID)] = workingTime
|
// update time for each presence
|
||||||
|
lastPresence.seen[userID][presenceID] = now
|
||||||
|
|
||||||
var presenceToSet types.Presence
|
// Default to unknown presence
|
||||||
|
presenceToSet := types.PresenceUnknown
|
||||||
//online will always get priority
|
switch {
|
||||||
if (workingTime - lastPresence.seen[userID][int(types.PresenceOnline)]) < presenceTimeout {
|
case now.Sub(lastPresence.seen[userID][types.PresenceOnline]) < presenceTimeout:
|
||||||
|
// online will always get priority
|
||||||
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 {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue