github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/wsapi/user.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package wsapi 5 6 import ( 7 "github.com/vnforks/kid/v5/model" 8 ) 9 10 func (api *API) InitUser() { 11 api.Router.Handle("user_typing", api.ApiWebSocketHandler(api.userTyping)) 12 api.Router.Handle("user_update_active_status", api.ApiWebSocketHandler(api.userUpdateActiveStatus)) 13 } 14 15 func (api *API) userTyping(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) { 16 if api.App.Srv().Busy.IsBusy() { 17 // this is considered a non-critical service and will be disabled when server busy. 18 return nil, NewServerBusyWebSocketError(req.Action) 19 } 20 21 var ok bool 22 var classId string 23 if classId, ok = req.Data["class_id"].(string); !ok || len(classId) != 26 { 24 return nil, NewInvalidWebSocketParamError(req.Action, "class_id") 25 } 26 27 if !api.App.SessionHasPermissionToClass(req.Session, classId, model.PERMISSION_CREATE_POST) { 28 return nil, NewInvalidWebSocketParamError(req.Action, "class_id") 29 } 30 31 var parentId string 32 if parentId, ok = req.Data["parent_id"].(string); !ok { 33 parentId = "" 34 } 35 36 omitUsers := make(map[string]bool, 1) 37 omitUsers[req.Session.UserId] = true 38 39 event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_TYPING, "", classId, "", omitUsers) 40 event.Add("parent_id", parentId) 41 event.Add("user_id", req.Session.UserId) 42 api.App.Publish(event) 43 44 return nil, nil 45 } 46 47 func (api *API) userUpdateActiveStatus(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) { 48 var ok bool 49 var userIsActive bool 50 if userIsActive, ok = req.Data["user_is_active"].(bool); !ok { 51 return nil, NewInvalidWebSocketParamError(req.Action, "user_is_active") 52 } 53 54 var manual bool 55 if manual, ok = req.Data["manual"].(bool); !ok { 56 manual = false 57 } 58 59 if userIsActive { 60 api.App.SetStatusOnline(req.Session.UserId, manual) 61 } else { 62 api.App.SetStatusAwayIfNeeded(req.Session.UserId, manual) 63 } 64 65 return nil, nil 66 }