From 4101b28de58ab8c2e821cda5f8e7bc8e836d7bb8 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Tue, 10 Jan 2017 11:38:03 -0500 Subject: Use status cache for checking @here notifications (#5035) --- api/post.go | 25 ++++++++++--------------- api/status.go | 52 +++++++++++++++++++++++++++------------------------- model/status.go | 11 +++++++++++ 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/api/post.go b/api/post.go index 8d6fa0b02..45ead08e0 100644 --- a/api/post.go +++ b/api/post.go @@ -787,23 +787,18 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * } if hereNotification { - if result := <-Srv.Store.Status().GetOnline(); result.Err != nil { - l4g.Warn(utils.T("api.post.notification.here.warn"), result.Err) - return nil - } else { - statuses := result.Data.([]*model.Status) - for _, status := range statuses { - if status.UserId == post.UserId { - continue - } + statuses := GetAllStatuses() + for _, status := range statuses { + if status.UserId == post.UserId { + continue + } - _, profileFound := profileMap[status.UserId] - _, alreadyMentioned := mentionedUserIds[status.UserId] + _, profileFound := profileMap[status.UserId] + _, alreadyMentioned := mentionedUserIds[status.UserId] - if status.Status == model.STATUS_ONLINE && profileFound && !alreadyMentioned { - mentionedUsersList = append(mentionedUsersList, status.UserId) - updateMentionChans = append(updateMentionChans, Srv.Store.Channel().IncrementMentionCount(post.ChannelId, status.UserId)) - } + if status.Status == model.STATUS_ONLINE && profileFound && !alreadyMentioned { + mentionedUsersList = append(mentionedUsersList, status.UserId) + updateMentionChans = append(updateMentionChans, Srv.Store.Channel().IncrementMentionCount(post.ChannelId, status.UserId)) } } } diff --git a/api/status.go b/api/status.go index 99d5bc417..909ab50ec 100644 --- a/api/status.go +++ b/api/status.go @@ -42,38 +42,31 @@ func InitStatus() { } func getStatusesHttp(c *Context, w http.ResponseWriter, r *http.Request) { - statusMap, err := GetAllStatuses() - if err != nil { - c.Err = err - return - } - + statusMap := model.StatusMapToInterfaceMap(GetAllStatuses()) w.Write([]byte(model.StringInterfaceToJson(statusMap))) } func getStatusesWebSocket(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) { - statusMap, err := GetAllStatuses() - if err != nil { - return nil, err - } - - return statusMap, nil + statusMap := GetAllStatuses() + return model.StatusMapToInterfaceMap(statusMap), nil } -// Only returns 300 statuses max -func GetAllStatuses() (map[string]interface{}, *model.AppError) { - if result := <-Srv.Store.Status().GetOnlineAway(); result.Err != nil { - return nil, result.Err - } else { - statuses := result.Data.([]*model.Status) +func GetAllStatuses() map[string]*model.Status { + userIds := statusCache.Keys() + statusMap := map[string]*model.Status{} - statusMap := map[string]interface{}{} - for _, s := range statuses { - statusMap[s.UserId] = s.Status + for _, userId := range userIds { + if id, ok := userId.(string); !ok { + continue + } else { + status := GetStatusFromCache(id) + if status != nil { + statusMap[id] = status + } } - - return statusMap, nil } + + return statusMap } func getStatusesByIdsHttp(c *Context, w http.ResponseWriter, r *http.Request) { @@ -268,12 +261,21 @@ func SetStatusAwayIfNeeded(userId string, manual bool) { go Publish(event) } -func GetStatus(userId string) (*model.Status, *model.AppError) { +func GetStatusFromCache(userId string) *model.Status { if result, ok := statusCache.Get(userId); ok { status := result.(*model.Status) statusCopy := &model.Status{} *statusCopy = *status - return statusCopy, nil + return statusCopy + } + + return nil +} + +func GetStatus(userId string) (*model.Status, *model.AppError) { + status := GetStatusFromCache(userId) + if status != nil { + return status, nil } if result := <-Srv.Store.Status().Get(userId); result.Err != nil { diff --git a/model/status.go b/model/status.go index 8637f60a3..fec3a5f70 100644 --- a/model/status.go +++ b/model/status.go @@ -44,3 +44,14 @@ func StatusFromJson(data io.Reader) *Status { return nil } } + +func StatusMapToInterfaceMap(statusMap map[string]*Status) map[string]interface{} { + interfaceMap := map[string]interface{}{} + for _, s := range statusMap { + // Omitted statues mean offline + if s.Status != STATUS_OFFLINE { + interfaceMap[s.UserId] = s.Status + } + } + return interfaceMap +} -- cgit v1.2.3-1-g7c22