Mono repo -> Master (#22553)
Combines the following repositories into one: https://github.com/mattermost/mattermost-server https://github.com/mattermost/mattermost-webapp https://github.com/mattermost/focalboard https://github.com/mattermost/mattermost-plugin-playbooks
Этот коммит содержится в:
27
server/channels/wsapi/api.go
Обычный файл
27
server/channels/wsapi/api.go
Обычный файл
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/v6/server/channels/app"
|
||||
"github.com/mattermost/mattermost-server/v6/server/channels/app/platform"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
App *app.App
|
||||
Router *platform.WebSocketRouter
|
||||
}
|
||||
|
||||
func Init(s *app.Server) {
|
||||
a := app.New(app.ServerConnector(s.Channels()))
|
||||
router := s.Platform().WebSocketRouter
|
||||
api := &API{
|
||||
App: a,
|
||||
Router: router,
|
||||
}
|
||||
|
||||
api.InitUser()
|
||||
api.InitSystem()
|
||||
api.InitStatus()
|
||||
}
|
||||
34
server/channels/wsapi/status.go
Обычный файл
34
server/channels/wsapi/status.go
Обычный файл
@@ -0,0 +1,34 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/server/platform/shared/mlog"
|
||||
)
|
||||
|
||||
func (api *API) InitStatus() {
|
||||
api.Router.Handle("get_statuses", api.APIWebSocketHandler(api.getStatuses))
|
||||
api.Router.Handle("get_statuses_by_ids", api.APIWebSocketHandler(api.getStatusesByIds))
|
||||
}
|
||||
|
||||
func (api *API) getStatuses(req *model.WebSocketRequest) (map[string]any, *model.AppError) {
|
||||
statusMap := api.App.Srv().Platform().GetAllStatuses()
|
||||
return model.StatusMapToInterfaceMap(statusMap), nil
|
||||
}
|
||||
|
||||
func (api *API) getStatusesByIds(req *model.WebSocketRequest) (map[string]any, *model.AppError) {
|
||||
var userIds []string
|
||||
if userIds = model.ArrayFromInterface(req.Data["user_ids"]); len(userIds) == 0 {
|
||||
mlog.Debug("Error while parsing user_ids", mlog.String("data", model.StringInterfaceToJSON(req.Data)))
|
||||
return nil, NewInvalidWebSocketParamError(req.Action, "user_ids")
|
||||
}
|
||||
|
||||
statusMap, err := api.App.Srv().Platform().GetStatusesByIds(userIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return statusMap, nil
|
||||
}
|
||||
22
server/channels/wsapi/system.go
Обычный файл
22
server/channels/wsapi/system.go
Обычный файл
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
)
|
||||
|
||||
func (api *API) InitSystem() {
|
||||
api.Router.Handle("ping", api.APIWebSocketHandler(ping))
|
||||
}
|
||||
|
||||
func ping(req *model.WebSocketRequest) (map[string]any, *model.AppError) {
|
||||
data := map[string]any{}
|
||||
data["text"] = "pong"
|
||||
data["version"] = model.CurrentVersion
|
||||
data["server_time"] = model.GetMillis()
|
||||
data["node_id"] = ""
|
||||
|
||||
return data, nil
|
||||
}
|
||||
63
server/channels/wsapi/user.go
Обычный файл
63
server/channels/wsapi/user.go
Обычный файл
@@ -0,0 +1,63 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/server/channels/app/request"
|
||||
)
|
||||
|
||||
func (api *API) InitUser() {
|
||||
api.Router.Handle("user_typing", api.APIWebSocketHandler(api.userTyping))
|
||||
api.Router.Handle("user_update_active_status", api.APIWebSocketHandler(api.userUpdateActiveStatus))
|
||||
}
|
||||
|
||||
func (api *API) userTyping(req *model.WebSocketRequest) (map[string]any, *model.AppError) {
|
||||
api.App.ExtendSessionExpiryIfNeeded(&req.Session)
|
||||
|
||||
if api.App.Srv().Platform().Busy.IsBusy() {
|
||||
// this is considered a non-critical service and will be disabled when server busy.
|
||||
return nil, NewServerBusyWebSocketError(req.Action)
|
||||
}
|
||||
|
||||
var ok bool
|
||||
var channelId string
|
||||
if channelId, ok = req.Data["channel_id"].(string); !ok || !model.IsValidId(channelId) {
|
||||
return nil, NewInvalidWebSocketParamError(req.Action, "channel_id")
|
||||
}
|
||||
|
||||
if !api.App.SessionHasPermissionToChannel(request.EmptyContext(api.App.Log()), req.Session, channelId, model.PermissionCreatePost) {
|
||||
return nil, NewInvalidWebSocketParamError(req.Action, "channel_id")
|
||||
}
|
||||
|
||||
var parentId string
|
||||
if parentId, ok = req.Data["parent_id"].(string); !ok {
|
||||
parentId = ""
|
||||
}
|
||||
|
||||
appErr := api.App.PublishUserTyping(req.Session.UserId, channelId, parentId)
|
||||
|
||||
return nil, appErr
|
||||
}
|
||||
|
||||
func (api *API) userUpdateActiveStatus(req *model.WebSocketRequest) (map[string]any, *model.AppError) {
|
||||
var ok bool
|
||||
var userIsActive bool
|
||||
if userIsActive, ok = req.Data["user_is_active"].(bool); !ok {
|
||||
return nil, NewInvalidWebSocketParamError(req.Action, "user_is_active")
|
||||
}
|
||||
|
||||
var manual bool
|
||||
if manual, ok = req.Data["manual"].(bool); !ok {
|
||||
manual = false
|
||||
}
|
||||
|
||||
if userIsActive {
|
||||
api.App.SetStatusOnline(req.Session.UserId, manual)
|
||||
} else {
|
||||
api.App.SetStatusAwayIfNeeded(req.Session.UserId, manual)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
82
server/channels/wsapi/websocket_handler.go
Обычный файл
82
server/channels/wsapi/websocket_handler.go
Обычный файл
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package wsapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/server/channels/app"
|
||||
"github.com/mattermost/mattermost-server/v6/server/channels/app/platform"
|
||||
"github.com/mattermost/mattermost-server/v6/server/platform/shared/i18n"
|
||||
"github.com/mattermost/mattermost-server/v6/server/platform/shared/mlog"
|
||||
)
|
||||
|
||||
func (api *API) APIWebSocketHandler(wh func(*model.WebSocketRequest) (map[string]any, *model.AppError)) webSocketHandler {
|
||||
return webSocketHandler{api.App, wh}
|
||||
}
|
||||
|
||||
type webSocketHandler struct {
|
||||
app *app.App
|
||||
handlerFunc func(*model.WebSocketRequest) (map[string]any, *model.AppError)
|
||||
}
|
||||
|
||||
func (wh webSocketHandler) ServeWebSocket(conn *platform.WebConn, r *model.WebSocketRequest) {
|
||||
mlog.Debug("Websocket request", mlog.String("action", r.Action))
|
||||
|
||||
hub := wh.app.Srv().Platform().GetHubForUserId(conn.UserId)
|
||||
if hub == nil {
|
||||
return
|
||||
}
|
||||
session, sessionErr := wh.app.GetSession(conn.GetSessionToken())
|
||||
defer wh.app.ReturnSessionToPool(session)
|
||||
|
||||
if sessionErr != nil {
|
||||
mlog.Error(
|
||||
"websocket session error",
|
||||
mlog.String("action", r.Action),
|
||||
mlog.Int64("seq", r.Seq),
|
||||
mlog.String("user_id", conn.UserId),
|
||||
mlog.String("error_message", sessionErr.SystemMessage(i18n.T)),
|
||||
mlog.Err(sessionErr),
|
||||
)
|
||||
sessionErr.DetailedError = ""
|
||||
errResp := model.NewWebSocketError(r.Seq, sessionErr)
|
||||
hub.SendMessage(conn, errResp)
|
||||
return
|
||||
}
|
||||
|
||||
r.Session = *session
|
||||
r.T = conn.T
|
||||
r.Locale = conn.Locale
|
||||
|
||||
var data map[string]any
|
||||
var err *model.AppError
|
||||
|
||||
if data, err = wh.handlerFunc(r); err != nil {
|
||||
mlog.Error(
|
||||
"websocket request handling error",
|
||||
mlog.String("action", r.Action),
|
||||
mlog.Int64("seq", r.Seq),
|
||||
mlog.String("user_id", conn.UserId),
|
||||
mlog.String("error_message", err.SystemMessage(i18n.T)),
|
||||
mlog.Err(err),
|
||||
)
|
||||
err.DetailedError = ""
|
||||
errResp := model.NewWebSocketError(r.Seq, err)
|
||||
hub.SendMessage(conn, errResp)
|
||||
return
|
||||
}
|
||||
|
||||
resp := model.NewWebSocketResponse(model.StatusOk, r.Seq, data)
|
||||
hub.SendMessage(conn, resp)
|
||||
}
|
||||
|
||||
func NewInvalidWebSocketParamError(action string, name string) *model.AppError {
|
||||
return model.NewAppError("websocket: "+action, "api.websocket_handler.invalid_param.app_error", map[string]any{"Name": name}, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func NewServerBusyWebSocketError(action string) *model.AppError {
|
||||
return model.NewAppError("websocket: "+action, "api.websocket_handler.server_busy.app_error", nil, "", http.StatusServiceUnavailable)
|
||||
}
|
||||
Ссылка в новой задаче
Block a user