diff --git a/matterclient.go b/matterclient.go index 6ca0d80..b06afe1 100644 --- a/matterclient.go +++ b/matterclient.go @@ -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) } } diff --git a/users.go b/users.go index 0c31ce3..f12c9ec 100644 --- a/users.go +++ b/users.go @@ -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] @@ -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() @@ -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] = "" @@ -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 @@ -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 + ")" } }