github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/wsapi/user.go (about)

     1  // Copyright (c) 2017-present Xenia, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package wsapi
     5  
     6  import (
     7  	"github.com/xzl8028/xenia-server/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  	var ok bool
    17  	var channelId string
    18  	if channelId, ok = req.Data["channel_id"].(string); !ok || len(channelId) != 26 {
    19  		return nil, NewInvalidWebSocketParamError(req.Action, "channel_id")
    20  	}
    21  
    22  	var parentId string
    23  	if parentId, ok = req.Data["parent_id"].(string); !ok {
    24  		parentId = ""
    25  	}
    26  
    27  	omitUsers := make(map[string]bool, 1)
    28  	omitUsers[req.Session.UserId] = true
    29  
    30  	event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_TYPING, "", channelId, "", omitUsers)
    31  	event.Add("parent_id", parentId)
    32  	event.Add("user_id", req.Session.UserId)
    33  	api.App.Publish(event)
    34  
    35  	return nil, nil
    36  }
    37  
    38  func (api *API) userUpdateActiveStatus(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
    39  	var ok bool
    40  	var userIsActive bool
    41  	if userIsActive, ok = req.Data["user_is_active"].(bool); !ok {
    42  		return nil, NewInvalidWebSocketParamError(req.Action, "user_is_active")
    43  	}
    44  
    45  	var manual bool
    46  	if manual, ok = req.Data["manual"].(bool); !ok {
    47  		manual = false
    48  	}
    49  
    50  	if userIsActive {
    51  		api.App.SetStatusOnline(req.Session.UserId, manual)
    52  	} else {
    53  		api.App.SetStatusAwayIfNeeded(req.Session.UserId, manual)
    54  	}
    55  
    56  	return nil, nil
    57  }