summaryrefslogtreecommitdiffstats
path: root/api/webrtc.go
blob: ba6054125461abfda04fb0a90f0668ddf5f370c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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(model.WEBSOCKET_EVENT_WEBRTC, "", "", toUserId, nil)
	event.Data = req.Data
	go Publish(event)

	return nil, nil
}