diff options
author | enahum <nahumhbl@gmail.com> | 2016-09-15 09:35:44 -0300 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-09-15 08:35:44 -0400 |
commit | b180bb46e3034d0ce75c9961a8ccea3eefbc855c (patch) | |
tree | 10cfc7affeca5b7c7634b73daf7817cc0c71cfd0 /api | |
parent | 3b4c9d7588e061b865dd5e35e785919962875fb9 (diff) | |
download | chat-b180bb46e3034d0ce75c9961a8ccea3eefbc855c.tar.gz chat-b180bb46e3034d0ce75c9961a8ccea3eefbc855c.tar.bz2 chat-b180bb46e3034d0ce75c9961a8ccea3eefbc855c.zip |
PLT-3412 WebRTC Server side & System Console (#3706)
* WebRTC Server side
* WebRTC System Console
* Consistency on variable names
* Add turn and stun uri validation
Diffstat (limited to 'api')
-rw-r--r-- | api/api.go | 4 | ||||
-rw-r--r-- | api/user.go | 12 | ||||
-rw-r--r-- | api/web_hub.go | 3 | ||||
-rw-r--r-- | api/webrtc.go | 51 | ||||
-rw-r--r-- | api/webrtc_test.go | 18 |
5 files changed, 88 insertions, 0 deletions
diff --git a/api/api.go b/api/api.go index 5373565de..492c3b0a9 100644 --- a/api/api.go +++ b/api/api.go @@ -49,6 +49,8 @@ type Routes struct { Emoji *mux.Router // 'api/v3/emoji' + Webrtc *mux.Router // 'api/v3/webrtc' + WebSocket *WebSocketRouter // websocket api } @@ -77,6 +79,7 @@ func InitApi() { BaseRoutes.License = BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter() BaseRoutes.Public = BaseRoutes.ApiRoot.PathPrefix("/public").Subrouter() BaseRoutes.Emoji = BaseRoutes.ApiRoot.PathPrefix("/emoji").Subrouter() + BaseRoutes.Webrtc = BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter() BaseRoutes.WebSocket = NewWebSocketRouter() @@ -95,6 +98,7 @@ func InitApi() { InitLicense() InitEmoji() InitStatus() + InitWebrtc() // 404 on any api route before web.go has a chance to serve it Srv.Router.Handle("/api/{anything:.*}", http.HandlerFunc(Handle404)) diff --git a/api/user.go b/api/user.go index 35cc3612e..a82fc5561 100644 --- a/api/user.go +++ b/api/user.go @@ -735,6 +735,10 @@ func RevokeSessionById(c *Context, sessionId string) { c.Err = result.Err } } + + if webrtcInterface := einterfaces.GetWebrtcInterface(); webrtcInterface != nil { + webrtcInterface.RevokeToken(session.Id) + } } } @@ -757,6 +761,10 @@ func RevokeAllSession(c *Context, userId string) { return } } + + if webrtcInterface := einterfaces.GetWebrtcInterface(); webrtcInterface != nil { + webrtcInterface.RevokeToken(session.Id) + } } } } @@ -778,6 +786,10 @@ func RevokeAllSessionsNoContext(userId string) *model.AppError { return result.Err } } + + if webrtcInterface := einterfaces.GetWebrtcInterface(); webrtcInterface != nil { + webrtcInterface.RevokeToken(session.Id) + } } } return nil diff --git a/api/web_hub.go b/api/web_hub.go index 452fa10f8..309d560a9 100644 --- a/api/web_hub.go +++ b/api/web_hub.go @@ -169,6 +169,9 @@ func shouldSendEvent(webCon *WebConn, msg *model.WebSocketEvent) bool { } else if msg.Event == model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE { // For now, ephemeral messages are sent directly to individual users return false + } else if msg.Event == model.WEBSOCKET_EVENT_WEBRTC { + // No need to tell anyone that a webrtc event is going on + return false } // Only report events to users who are in the team for the event diff --git a/api/webrtc.go b/api/webrtc.go new file mode 100644 index 000000000..4664524f4 --- /dev/null +++ b/api/webrtc.go @@ -0,0 +1,51 @@ +// 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/einterfaces" + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" + "net/http" +) + +func InitWebrtc() { + l4g.Debug(utils.T("api.webrtc.init.debug")) + + BaseRoutes.Webrtc.Handle("/token", ApiUserRequired(webrtcToken)).Methods("POST") + + BaseRoutes.WebSocket.Handle("webrtc", ApiWebSocketHandler(webrtcMessage)) +} + +func webrtcToken(c *Context, w http.ResponseWriter, r *http.Request) { + webrtcInterface := einterfaces.GetWebrtcInterface() + + if webrtcInterface == nil { + c.Err = model.NewLocAppError("webrtcToken", "api.webrtc.not_available.app_error", nil, "") + c.Err.StatusCode = http.StatusNotImplemented + return + } + + if result, err := webrtcInterface.Token(c.Session.Id); err != nil { + c.Err = err + return + } else { + w.Write([]byte(model.MapToJson(result))) + } +} + +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("", "", toUserId, model.WEBSOCKET_EVENT_WEBRTC) + event.Data = req.Data + go Publish(event) + + return nil, nil +} diff --git a/api/webrtc_test.go b/api/webrtc_test.go new file mode 100644 index 000000000..d6a690407 --- /dev/null +++ b/api/webrtc_test.go @@ -0,0 +1,18 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package api + +import "testing" + +func TestWebrtcToken(t *testing.T) { + th := Setup().InitBasic() + + if _, err := th.BasicClient.GetWebrtcToken(); err != nil { + if err.Id != "api.webrtc.not_available.app_error" { + t.Fatal("Should have fail, webrtc not availble") + } + } else { + t.Fatal("Should have fail, webrtc not availble") + } +} |