Files
mostlymatter/wsapi/user.go
Doug Lauder 5abbe50258 MM-8607 Add ability to turn off non-critical services when under load (#13212)
* MM-8607: add ability to turn off non-critical services under load

* server busy invalid param unit tests

* MM-8607: rename server busy endpoints

* MM-8607: handle case where App not initialized

* MM-8607: additional unit test cases per feedback.

* MM-8607: use decorator to check isbusy when adding endpoint route

* MM-8607: rename endpoints, use struct for json

* Update api4/system.go

Fix misspelled log output

Co-Authored-By: Saturnino Abril <saturnino.abril@gmail.com>

* MM-8607: fix i18n order; max seconds for server busy expiry
2019-11-27 20:41:09 -05:00

63 строки
1.8 KiB
Go

// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package wsapi
import (
"github.com/mattermost/mattermost-server/model"
)
func (api *API) InitUser() {
api.Router.Handle("user_typing", api.ApiWebSocketHandler(api.userTyping))
api.Router.Handle("user_update_active_status", api.ApiWebSocketHandler(api.userUpdateActiveStatus))
}
func (api *API) userTyping(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
if api.App.Srv.Busy.IsBusy() {
// this is considered a non-critical service and will be disabled when server busy.
return nil, NewServerBusyWebSocketError(req.Action)
}
var ok bool
var channelId string
if channelId, ok = req.Data["channel_id"].(string); !ok || len(channelId) != 26 {
return nil, NewInvalidWebSocketParamError(req.Action, "channel_id")
}
var parentId string
if parentId, ok = req.Data["parent_id"].(string); !ok {
parentId = ""
}
omitUsers := make(map[string]bool, 1)
omitUsers[req.Session.UserId] = true
event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_TYPING, "", channelId, "", omitUsers)
event.Add("parent_id", parentId)
event.Add("user_id", req.Session.UserId)
api.App.Publish(event)
return nil, nil
}
func (api *API) userUpdateActiveStatus(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
var ok bool
var userIsActive bool
if userIsActive, ok = req.Data["user_is_active"].(bool); !ok {
return nil, NewInvalidWebSocketParamError(req.Action, "user_is_active")
}
var manual bool
if manual, ok = req.Data["manual"].(bool); !ok {
manual = false
}
if userIsActive {
api.App.SetStatusOnline(req.Session.UserId, manual)
} else {
api.App.SetStatusAwayIfNeeded(req.Session.UserId, manual)
}
return nil, nil
}