2019-11-29 12:59:40 +01:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
2017-03-28 04:58:19 -04:00
|
|
|
|
|
|
|
|
package wsapi
|
|
|
|
|
|
|
|
|
|
import (
|
2019-11-28 14:39:38 +01:00
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
2017-03-28 04:58:19 -04:00
|
|
|
)
|
|
|
|
|
|
2017-09-27 11:52:34 -05:00
|
|
|
func (api *API) InitUser() {
|
|
|
|
|
api.Router.Handle("user_typing", api.ApiWebSocketHandler(api.userTyping))
|
2019-06-21 17:46:06 -04:00
|
|
|
api.Router.Handle("user_update_active_status", api.ApiWebSocketHandler(api.userUpdateActiveStatus))
|
2017-03-28 04:58:19 -04:00
|
|
|
}
|
|
|
|
|
|
2017-09-27 11:52:34 -05:00
|
|
|
func (api *API) userTyping(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
2020-05-06 15:41:10 -04:00
|
|
|
api.App.ExtendSessionExpiryIfNeeded(&req.Session)
|
|
|
|
|
|
2020-02-13 13:26:58 +01:00
|
|
|
if api.App.Srv().Busy.IsBusy() {
|
2019-11-27 20:41:09 -05:00
|
|
|
// this is considered a non-critical service and will be disabled when server busy.
|
|
|
|
|
return nil, NewServerBusyWebSocketError(req.Action)
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 04:58:19 -04:00
|
|
|
var ok bool
|
|
|
|
|
var channelId string
|
2020-05-07 22:57:35 +05:30
|
|
|
if channelId, ok = req.Data["channel_id"].(string); !ok || !model.IsValidId(channelId) {
|
2017-03-28 04:58:19 -04:00
|
|
|
return nil, NewInvalidWebSocketParamError(req.Action, "channel_id")
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 20:18:36 +01:00
|
|
|
if !api.App.SessionHasPermissionToChannel(req.Session, channelId, model.PERMISSION_CREATE_POST) {
|
|
|
|
|
return nil, NewInvalidWebSocketParamError(req.Action, "channel_id")
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 04:58:19 -04:00
|
|
|
var parentId string
|
|
|
|
|
if parentId, ok = req.Data["parent_id"].(string); !ok {
|
|
|
|
|
parentId = ""
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 16:41:05 +07:00
|
|
|
appErr := api.App.PublishUserTyping(req.Session.UserId, channelId, parentId)
|
2017-03-28 04:58:19 -04:00
|
|
|
|
2020-06-16 16:41:05 +07:00
|
|
|
return nil, appErr
|
2017-03-28 04:58:19 -04:00
|
|
|
}
|
2019-06-21 17:46:06 -04:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|