Move WebSocket API to it's own package and add websocket v4 endpoint (#5881)
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
ca8b8d1245
Коммит
daca0d93f6
@@ -61,7 +61,6 @@ var BaseRoutes *Routes
|
||||
func InitRouter() {
|
||||
app.Srv.Router = mux.NewRouter()
|
||||
app.Srv.Router.NotFoundHandler = http.HandlerFunc(Handle404)
|
||||
app.Srv.WebSocketRouter = app.NewWebSocketRouter()
|
||||
}
|
||||
|
||||
func InitApi() {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/store"
|
||||
"github.com/mattermost/platform/utils"
|
||||
"github.com/mattermost/platform/wsapi"
|
||||
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
)
|
||||
@@ -42,10 +43,12 @@ func SetupEnterprise() *TestHelper {
|
||||
app.NewServer()
|
||||
app.InitStores()
|
||||
InitRouter()
|
||||
wsapi.InitRouter()
|
||||
app.StartServer()
|
||||
utils.InitHTML()
|
||||
api4.InitApi(false)
|
||||
InitApi()
|
||||
wsapi.InitApi()
|
||||
utils.EnableDebugLogForTest()
|
||||
app.Srv.Store.MarkSystemRanUnitTests()
|
||||
|
||||
@@ -70,8 +73,10 @@ func Setup() *TestHelper {
|
||||
app.NewServer()
|
||||
app.InitStores()
|
||||
InitRouter()
|
||||
wsapi.InitRouter()
|
||||
app.StartServer()
|
||||
InitApi()
|
||||
wsapi.InitApi()
|
||||
utils.EnableDebugLogForTest()
|
||||
app.Srv.Store.MarkSystemRanUnitTests()
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
|
||||
"github.com/mattermost/platform/app"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/utils"
|
||||
)
|
||||
@@ -21,8 +20,6 @@ func InitGeneral() {
|
||||
BaseRoutes.General.Handle("/client_props", ApiAppHandler(getClientConfig)).Methods("GET")
|
||||
BaseRoutes.General.Handle("/log_client", ApiAppHandler(logClient)).Methods("POST")
|
||||
BaseRoutes.General.Handle("/ping", ApiAppHandler(ping)).Methods("GET")
|
||||
|
||||
app.Srv.WebSocketRouter.Handle("ping", ApiWebSocketHandler(webSocketPing))
|
||||
}
|
||||
|
||||
func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -72,13 +69,3 @@ func ping(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
m["server_time"] = fmt.Sprintf("%v", model.GetMillis())
|
||||
w.Write([]byte(model.MapToJson(m)))
|
||||
}
|
||||
|
||||
func webSocketPing(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||
data := map[string]interface{}{}
|
||||
data["text"] = "pong"
|
||||
data["version"] = model.CurrentVersion
|
||||
data["server_time"] = model.GetMillis()
|
||||
data["node_id"] = ""
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
@@ -18,8 +18,6 @@ func InitStatus() {
|
||||
|
||||
BaseRoutes.Users.Handle("/status", ApiUserRequired(getStatusesHttp)).Methods("GET")
|
||||
BaseRoutes.Users.Handle("/status/ids", ApiUserRequired(getStatusesByIdsHttp)).Methods("POST")
|
||||
app.Srv.WebSocketRouter.Handle("get_statuses", ApiWebSocketHandler(getStatusesWebSocket))
|
||||
app.Srv.WebSocketRouter.Handle("get_statuses_by_ids", ApiWebSocketHandler(getStatusesByIdsWebSocket))
|
||||
}
|
||||
|
||||
func getStatusesHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -27,11 +25,6 @@ func getStatusesHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(model.StringInterfaceToJson(statusMap)))
|
||||
}
|
||||
|
||||
func getStatusesWebSocket(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||
statusMap := app.GetAllStatuses()
|
||||
return model.StatusMapToInterfaceMap(statusMap), nil
|
||||
}
|
||||
|
||||
func getStatusesByIdsHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
userIds := model.ArrayFromJson(r.Body)
|
||||
|
||||
@@ -48,18 +41,3 @@ func getStatusesByIdsHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Write([]byte(model.StringInterfaceToJson(statusMap)))
|
||||
}
|
||||
|
||||
func getStatusesByIdsWebSocket(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||
var userIds []string
|
||||
if userIds = model.ArrayFromInterface(req.Data["user_ids"]); len(userIds) == 0 {
|
||||
l4g.Error(model.StringInterfaceToJson(req.Data))
|
||||
return nil, NewInvalidWebSocketParamError(req.Action, "user_ids")
|
||||
}
|
||||
|
||||
statusMap, err := app.GetStatusesByIds(userIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return statusMap, nil
|
||||
}
|
||||
|
||||
25
api/user.go
25
api/user.go
@@ -71,8 +71,6 @@ func InitUser() {
|
||||
|
||||
BaseRoutes.Root.Handle("/login/sso/saml", AppHandlerIndependent(loginWithSaml)).Methods("GET")
|
||||
BaseRoutes.Root.Handle("/login/sso/saml", AppHandlerIndependent(completeSaml)).Methods("POST")
|
||||
|
||||
app.Srv.WebSocketRouter.Handle("user_typing", ApiWebSocketHandler(userTyping))
|
||||
}
|
||||
|
||||
func createUser(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -1442,29 +1440,6 @@ func completeSaml(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func userTyping(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||
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)
|
||||
go app.Publish(event)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func sanitizeProfile(c *Context, user *model.User) *model.User {
|
||||
options := utils.Cfg.GetSanitizeOptions()
|
||||
|
||||
|
||||
@@ -23,8 +23,6 @@ func InitWebrtc() {
|
||||
l4g.Debug(utils.T("api.webrtc.init.debug"))
|
||||
|
||||
BaseRoutes.Webrtc.Handle("/token", ApiUserRequired(webrtcToken)).Methods("POST")
|
||||
|
||||
app.Srv.WebSocketRouter.Handle("webrtc", ApiWebSocketHandler(webrtcMessage))
|
||||
}
|
||||
|
||||
func webrtcToken(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -52,20 +50,6 @@ func webrtcToken(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func webrtcMessage(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||
var ok bool
|
||||
var toUserId string
|
||||
if toUserId, ok = req.Data["to_user_id"].(string); !ok || len(toUserId) != 26 {
|
||||
return nil, NewInvalidWebSocketParamError(req.Action, "to_user_id")
|
||||
}
|
||||
|
||||
event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_WEBRTC, "", "", toUserId, nil)
|
||||
event.Data = req.Data
|
||||
go app.Publish(event)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func getWebrtcToken(sessionId string) (string, *model.AppError) {
|
||||
if !*utils.Cfg.WebrtcSettings.Enable {
|
||||
return "", model.NewLocAppError("WebRTC.getWebrtcToken", "api.webrtc.disabled.app_error", nil, "")
|
||||
|
||||
@@ -5,7 +5,6 @@ package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -17,23 +16,10 @@ import (
|
||||
func InitWebSocket() {
|
||||
l4g.Debug(utils.T("api.web_socket.init.debug"))
|
||||
BaseRoutes.Users.Handle("/websocket", ApiAppHandlerTrustRequester(connect)).Methods("GET")
|
||||
app.HubStart()
|
||||
}
|
||||
|
||||
type OriginCheckerProc func(*http.Request) bool
|
||||
|
||||
func OriginChecker(r *http.Request) bool {
|
||||
origin := r.Header.Get("Origin")
|
||||
return *utils.Cfg.ServiceSettings.AllowCorsFrom == "*" || strings.Contains(origin, *utils.Cfg.ServiceSettings.AllowCorsFrom)
|
||||
}
|
||||
|
||||
func connect(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var originChecker OriginCheckerProc = nil
|
||||
|
||||
if len(*utils.Cfg.ServiceSettings.AllowCorsFrom) > 0 {
|
||||
originChecker = OriginChecker
|
||||
}
|
||||
originChecker := utils.GetOriginChecker(r)
|
||||
|
||||
upgrader := websocket.Upgrader{
|
||||
ReadBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
|
||||
"github.com/mattermost/platform/app"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/utils"
|
||||
)
|
||||
|
||||
func ApiWebSocketHandler(wh func(*model.WebSocketRequest) (map[string]interface{}, *model.AppError)) webSocketHandler {
|
||||
return webSocketHandler{wh}
|
||||
}
|
||||
|
||||
type webSocketHandler struct {
|
||||
handlerFunc func(*model.WebSocketRequest) (map[string]interface{}, *model.AppError)
|
||||
}
|
||||
|
||||
func (wh webSocketHandler) ServeWebSocket(conn *app.WebConn, r *model.WebSocketRequest) {
|
||||
l4g.Debug("/api/v3/users/websocket:%s", r.Action)
|
||||
|
||||
session, sessionErr := app.GetSession(conn.SessionToken)
|
||||
if sessionErr != nil {
|
||||
l4g.Error(utils.T("api.web_socket_handler.log.error"), "/api/v3/users/websocket", r.Action, r.Seq, conn.UserId, sessionErr.SystemMessage(utils.T), sessionErr.Error())
|
||||
sessionErr.DetailedError = ""
|
||||
errResp := model.NewWebSocketError(r.Seq, sessionErr)
|
||||
errResp.DoPreComputeJson()
|
||||
|
||||
conn.Send <- errResp
|
||||
return
|
||||
}
|
||||
|
||||
r.Session = *session
|
||||
r.T = conn.T
|
||||
r.Locale = conn.Locale
|
||||
|
||||
var data map[string]interface{}
|
||||
var err *model.AppError
|
||||
|
||||
if data, err = wh.handlerFunc(r); err != nil {
|
||||
l4g.Error(utils.T("api.web_socket_handler.log.error"), "/api/v3/users/websocket", r.Action, r.Seq, r.Session.UserId, err.SystemMessage(utils.T), err.DetailedError)
|
||||
err.DetailedError = ""
|
||||
errResp := model.NewWebSocketError(r.Seq, err)
|
||||
errResp.DoPreComputeJson()
|
||||
|
||||
conn.Send <- errResp
|
||||
return
|
||||
}
|
||||
|
||||
resp := model.NewWebSocketResponse(model.STATUS_OK, r.Seq, data)
|
||||
resp.DoPreComputeJson()
|
||||
|
||||
conn.Send <- resp
|
||||
}
|
||||
|
||||
func NewInvalidWebSocketParamError(action string, name string) *model.AppError {
|
||||
return model.NewLocAppError("/api/v3/users/websocket:"+action, "api.websocket_handler.invalid_param.app_error", map[string]interface{}{"Name": name}, "")
|
||||
}
|
||||
Ссылка в новой задаче
Block a user