Use status cache for checking @here notifications (#5035)

This commit is contained in:
Joram Wilander
2017-01-10 11:38:03 -05:00
committed by Harrison Healey
parent 056be669fc
commit 4101b28de5
3 changed files with 48 additions and 40 deletions

View File

@@ -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))
}
}
}

View File

@@ -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 {

View File

@@ -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
}