summaryrefslogtreecommitdiffstats
path: root/api/general.go
blob: ceb0b209f156c3ef291832acf678a446873fc22d (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api

import (
	"fmt"
	"net/http"
	"strings"

	l4g "github.com/alecthomas/log4go"

	"github.com/mattermost/platform/app"
	"github.com/mattermost/platform/model"
	"github.com/mattermost/platform/utils"
)

func InitGeneral() {
	l4g.Debug(utils.T("api.general.init.debug"))

	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")
}

func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(model.MapToJson(utils.ClientCfg)))
}

func logClient(c *Context, w http.ResponseWriter, r *http.Request) {
	forceToDebug := false

	if !*utils.Cfg.ServiceSettings.EnableDeveloper {
		if c.Session.UserId == "" {
			c.Err = model.NewAppError("Permissions", "api.context.permissions.app_error", nil, "", http.StatusForbidden)
			return
		}

		if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
			forceToDebug = true
		}
	}

	m := model.MapFromJson(r.Body)

	lvl := m["level"]
	msg := m["message"]

	// filter out javascript errors from franz that are poluting the log files
	if strings.Contains(msg, "/franz") {
		forceToDebug = true
	}

	if len(msg) > 400 {
		msg = msg[0:399]
	}

	if lvl == "ERROR" {
		err := &model.AppError{}
		err.Message = msg
		err.Id = msg
		err.Where = "client"

		if forceToDebug {
			c.LogDebug(err)
		} else {
			c.LogError(err)
		}
	}

	ReturnStatusOK(w)
}

func ping(c *Context, w http.ResponseWriter, r *http.Request) {
	m := make(map[string]string)
	m["version"] = model.CurrentVersion
	m["server_time"] = fmt.Sprintf("%v", model.GetMillis())
	w.Write([]byte(model.MapToJson(m)))
}