mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Move WebSocket API to it's own package and add websocket v4 endpoint (#5881)
This commit is contained in:
committed by
George Goldberg
parent
ca8b8d1245
commit
daca0d93f6
21
wsapi/api.go
Normal file
21
wsapi/api.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
"github.com/mattermost/platform/app"
|
||||
)
|
||||
|
||||
func InitRouter() {
|
||||
app.Srv.WebSocketRouter = app.NewWebSocketRouter()
|
||||
}
|
||||
|
||||
func InitApi() {
|
||||
InitUser()
|
||||
InitSystem()
|
||||
InitStatus()
|
||||
InitWebrtc()
|
||||
|
||||
app.HubStart()
|
||||
}
|
||||
38
wsapi/status.go
Normal file
38
wsapi/status.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
"github.com/mattermost/platform/app"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/utils"
|
||||
)
|
||||
|
||||
func InitStatus() {
|
||||
l4g.Debug(utils.T("wsapi.status.init.debug"))
|
||||
|
||||
app.Srv.WebSocketRouter.Handle("get_statuses", ApiWebSocketHandler(getStatuses))
|
||||
app.Srv.WebSocketRouter.Handle("get_statuses_by_ids", ApiWebSocketHandler(getStatusesByIds))
|
||||
}
|
||||
|
||||
func getStatuses(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||
statusMap := app.GetAllStatuses()
|
||||
return model.StatusMapToInterfaceMap(statusMap), nil
|
||||
}
|
||||
|
||||
func getStatusesByIds(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
|
||||
}
|
||||
27
wsapi/system.go
Normal file
27
wsapi/system.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
"github.com/mattermost/platform/app"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/utils"
|
||||
)
|
||||
|
||||
func InitSystem() {
|
||||
l4g.Debug(utils.T("wsapi.system.init.debug"))
|
||||
|
||||
app.Srv.WebSocketRouter.Handle("ping", ApiWebSocketHandler(ping))
|
||||
}
|
||||
|
||||
func ping(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
|
||||
}
|
||||
40
wsapi/user.go
Normal file
40
wsapi/user.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
"github.com/mattermost/platform/app"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/utils"
|
||||
)
|
||||
|
||||
func InitUser() {
|
||||
l4g.Debug(utils.T("wsapi.user.init.debug"))
|
||||
|
||||
app.Srv.WebSocketRouter.Handle("user_typing", ApiWebSocketHandler(userTyping))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
31
wsapi/webrtc.go
Normal file
31
wsapi/webrtc.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
"github.com/mattermost/platform/app"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/utils"
|
||||
)
|
||||
|
||||
func InitWebrtc() {
|
||||
l4g.Debug(utils.T("wsapi.webtrc.init.debug"))
|
||||
|
||||
app.Srv.WebSocketRouter.Handle("webrtc", ApiWebSocketHandler(webrtcMessage))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
61
wsapi/websocket_handler.go
Normal file
61
wsapi/websocket_handler.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
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}, "")
|
||||
}
|
||||
Reference in New Issue
Block a user