Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion matterclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,12 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE
m.Users.mu.RUnlock()

if tracked {
m.Users.SetUserCustomStatus(u.Id, u.Props["customStatus"])
var tzLoc *time.Location
if m.User != nil {
tzLoc = m.User.GetTimezoneLocation()
}

m.Users.SetUserCustomStatus(u.Id, u.Props["customStatus"], tzLoc)
}
}

Expand Down
39 changes: 33 additions & 6 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ func (m *Client) GetStatus(ctx context.Context, userID string) string {
}
}

var tzLoc *time.Location
if m.User != nil {
tzLoc = m.User.GetTimezoneLocation()
}

// Parse & store in cache (permanently marks user as tracked)
m.Users.SetUserCustomStatus(userID, rawJSON)
m.Users.SetUserCustomStatus(userID, rawJSON, tzLoc)

m.Users.mu.RLock()
customStatus = m.Users.customStatuses[userID]
Expand Down Expand Up @@ -509,7 +514,8 @@ func (m *Client) SearchUsers(ctx context.Context, search *model.UserSearch) ([]*
}
}

func (c *UsersCache) SetUserCustomStatus(userID string, rawJSON string) {
//nolint:funlen,gocyclo,gocognit
func (c *UsersCache) SetUserCustomStatus(userID string, rawJSON string, tzLoc ...*time.Location) {
c.mu.Lock()
defer c.mu.Unlock()

Expand Down Expand Up @@ -549,8 +555,13 @@ func (c *UsersCache) SetUserCustomStatus(userID string, rawJSON string) {
if status.ExpiresAt != "" {
expiry, parseErr := time.Parse(time.RFC3339, status.ExpiresAt)
if parseErr == nil {
now := time.Now().Local()
expLocal := expiry.Local()
location := time.Local
if len(tzLoc) > 0 && tzLoc[0] != nil {
location = tzLoc[0]
}

now := time.Now().In(location)
expLocal := expiry.In(location)

if !expLocal.After(now) {
c.customStatuses[userID] = ""
Expand All @@ -559,8 +570,8 @@ func (c *UsersCache) SetUserCustomStatus(userID string, rawJSON string) {
}

timeStr := expLocal.Format("15:04")
nowDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
expDate := time.Date(expLocal.Year(), expLocal.Month(), expLocal.Day(), 0, 0, 0, 0, expLocal.Location())
nowDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, location)
expDate := time.Date(expLocal.Year(), expLocal.Month(), expLocal.Day(), 0, 0, 0, 0, location)
daysDiff := int(expDate.Sub(nowDate).Hours() / 24)

var dateStr string
Expand All @@ -574,6 +585,22 @@ func (c *UsersCache) SetUserCustomStatus(userID string, rawJSON string) {
dateStr = expLocal.Format("Mon, 02 Jan 15:04")
}

var userLoc *time.Location

if targetUser := c.users[userID]; targetUser != nil && len(targetUser.Timezone) > 0 {
if targetUser.Timezone["automaticTimezone"] != "" || targetUser.Timezone["manualTimezone"] != "" {
userLoc = targetUser.GetTimezoneLocation()
}
}

if userLoc != nil {
userTimeStr := expiry.In(userLoc).Format("15:04")

if userTimeStr != timeStr {
dateStr += ", local time: " + userTimeStr
}
}

formattedStatus += " (Until " + dateStr + ")"
}
}
Expand Down
Loading