MM-10254 Add plugin APIs for getting/updating user statuses (#9101)
* Add plugin APIs for getting/updating user statuses * Add and update tests * Updates per feedback
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
88eef609ab
Коммит
275731578e
@@ -463,6 +463,14 @@ func (me *TestHelper) SetupChannelScheme() *model.Scheme {
|
||||
}
|
||||
}
|
||||
|
||||
func (me *TestHelper) SetupPluginAPI() *PluginAPI {
|
||||
manifest := &model.Manifest{
|
||||
Id: "pluginid",
|
||||
}
|
||||
|
||||
return NewPluginAPI(me.App, manifest)
|
||||
}
|
||||
|
||||
type FakeClusterInterface struct {
|
||||
clusterMessageHandler einterfaces.ClusterMessageHandler
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func (a *App) SetAutoResponderStatus(user *model.User, oldNotifyProps model.Stri
|
||||
if autoResponderEnabled {
|
||||
a.SetStatusOutOfOffice(user.Id)
|
||||
} else if autoResponderDisabled {
|
||||
a.SetStatusOnline(user.Id, "", true)
|
||||
a.SetStatusOnline(user.Id, true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestSetAutoResponderStatus(t *testing.T) {
|
||||
user := th.CreateUser()
|
||||
defer th.App.PermanentDeleteUser(user)
|
||||
|
||||
th.App.SetStatusOnline(user.Id, "", true)
|
||||
th.App.SetStatusOnline(user.Id, true)
|
||||
|
||||
patch := &model.UserPatch{}
|
||||
patch.NotifyProps = make(map[string]string)
|
||||
@@ -57,7 +57,7 @@ func TestDisableAutoResponder(t *testing.T) {
|
||||
user := th.CreateUser()
|
||||
defer th.App.PermanentDeleteUser(user)
|
||||
|
||||
th.App.SetStatusOnline(user.Id, "", true)
|
||||
th.App.SetStatusOnline(user.Id, true)
|
||||
|
||||
patch := &model.UserPatch{}
|
||||
patch.NotifyProps = make(map[string]string)
|
||||
|
||||
@@ -38,7 +38,7 @@ func (me *DndProvider) DoCommand(a *App, args *model.CommandArgs, message string
|
||||
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command_dnd.error")}
|
||||
} else {
|
||||
if status.Status == "dnd" {
|
||||
a.SetStatusOnline(args.UserId, args.Session.Id, true)
|
||||
a.SetStatusOnline(args.UserId, true)
|
||||
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command_dnd.disabled")}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func (me *OnlineProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Comm
|
||||
}
|
||||
|
||||
func (me *OnlineProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
|
||||
a.SetStatusOnline(args.UserId, args.Session.Id, true)
|
||||
a.SetStatusOnline(args.UserId, true)
|
||||
|
||||
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command_online.success")}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ package app
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost-server/mlog"
|
||||
@@ -144,6 +145,31 @@ func (api *PluginAPI) UpdateUser(user *model.User) (*model.User, *model.AppError
|
||||
return api.app.UpdateUser(user, true)
|
||||
}
|
||||
|
||||
func (api *PluginAPI) GetUserStatus(userId string) (*model.Status, *model.AppError) {
|
||||
return api.app.GetStatus(userId)
|
||||
}
|
||||
|
||||
func (api *PluginAPI) GetUserStatusesByIds(userIds []string) ([]*model.Status, *model.AppError) {
|
||||
return api.app.GetUserStatusesByIds(userIds)
|
||||
}
|
||||
|
||||
func (api *PluginAPI) UpdateUserStatus(userId, status string) (*model.Status, *model.AppError) {
|
||||
switch status {
|
||||
case model.STATUS_ONLINE:
|
||||
api.app.SetStatusOnline(userId, true)
|
||||
case model.STATUS_OFFLINE:
|
||||
api.app.SetStatusOffline(userId, true)
|
||||
case model.STATUS_AWAY:
|
||||
api.app.SetStatusAwayIfNeeded(userId, true)
|
||||
case model.STATUS_DND:
|
||||
api.app.SetStatusDoNotDisturb(userId)
|
||||
default:
|
||||
return nil, model.NewAppError("UpdateUserStatus", "plugin.api.update_user_status.bad_status", nil, "unrecognized status", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
return api.app.GetStatus(userId)
|
||||
}
|
||||
|
||||
func (api *PluginAPI) CreateChannel(channel *model.Channel) (*model.Channel, *model.AppError) {
|
||||
return api.app.CreateChannel(channel, false)
|
||||
}
|
||||
|
||||
32
app/plugin_api_test.go
Обычный файл
32
app/plugin_api_test.go
Обычный файл
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
func TestPluginAPIUpdateUserStatus(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
api := th.SetupPluginAPI()
|
||||
|
||||
statuses := []string{model.STATUS_ONLINE, model.STATUS_AWAY, model.STATUS_DND, model.STATUS_OFFLINE}
|
||||
|
||||
for _, s := range statuses {
|
||||
status, err := api.UpdateUserStatus(th.BasicUser.Id, s)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, status)
|
||||
assert.Equal(t, s, status.Status)
|
||||
}
|
||||
|
||||
status, err := api.UpdateUserStatus(th.BasicUser.Id, "notrealstatus")
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, status)
|
||||
}
|
||||
@@ -177,7 +177,7 @@ func (a *App) SetStatusLastActivityAt(userId string, activityAt int64) {
|
||||
a.SetStatusAwayIfNeeded(userId, false)
|
||||
}
|
||||
|
||||
func (a *App) SetStatusOnline(userId string, sessionId string, manual bool) {
|
||||
func (a *App) SetStatusOnline(userId string, manual bool) {
|
||||
if !*a.Config().ServiceSettings.EnableUserStatuses {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ type WebConn struct {
|
||||
func (a *App) NewWebConn(ws *websocket.Conn, session model.Session, t goi18n.TranslateFunc, locale string) *WebConn {
|
||||
if len(session.UserId) > 0 {
|
||||
a.Go(func() {
|
||||
a.SetStatusOnline(session.UserId, session.Id, false)
|
||||
a.SetStatusOnline(session.UserId, false)
|
||||
a.UpdateLastActivityAtIfNeeded(session)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func (wr *WebSocketRouter) ServeWebSocket(conn *WebConn, r *model.WebSocketReque
|
||||
conn.WebSocket.Close()
|
||||
} else {
|
||||
wr.app.Go(func() {
|
||||
wr.app.SetStatusOnline(session.UserId, session.Id, false)
|
||||
wr.app.SetStatusOnline(session.UserId, false)
|
||||
wr.app.UpdateLastActivityAtIfNeeded(*session)
|
||||
})
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user