summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2018-07-16 15:49:26 -0400
committerGitHub <noreply@github.com>2018-07-16 15:49:26 -0400
commit275731578e72d2c6e12cfb2fc315d3446474faec (patch)
tree25df7525ed2244c7dec5be44495b45ffc0ae1023 /app
parent88eef609ab712097ff2b13a2ca45c31ea6fa7df2 (diff)
downloadchat-275731578e72d2c6e12cfb2fc315d3446474faec.tar.gz
chat-275731578e72d2c6e12cfb2fc315d3446474faec.tar.bz2
chat-275731578e72d2c6e12cfb2fc315d3446474faec.zip
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
Diffstat (limited to 'app')
-rw-r--r--app/apptestlib.go8
-rw-r--r--app/auto_responder.go2
-rw-r--r--app/auto_responder_test.go4
-rw-r--r--app/command_dnd.go2
-rw-r--r--app/command_online.go2
-rw-r--r--app/plugin_api.go26
-rw-r--r--app/plugin_api_test.go32
-rw-r--r--app/status.go2
-rw-r--r--app/web_conn.go2
-rw-r--r--app/websocket_router.go2
10 files changed, 74 insertions, 8 deletions
diff --git a/app/apptestlib.go b/app/apptestlib.go
index 12b01c5e5..48783f49c 100644
--- a/app/apptestlib.go
+++ b/app/apptestlib.go
@@ -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
}
diff --git a/app/auto_responder.go b/app/auto_responder.go
index aa7f243c4..a57a53f79 100644
--- a/app/auto_responder.go
+++ b/app/auto_responder.go
@@ -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)
}
}
diff --git a/app/auto_responder_test.go b/app/auto_responder_test.go
index 65b466c92..f78bbc669 100644
--- a/app/auto_responder_test.go
+++ b/app/auto_responder_test.go
@@ -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)
diff --git a/app/command_dnd.go b/app/command_dnd.go
index cd3764fdf..96135194d 100644
--- a/app/command_dnd.go
+++ b/app/command_dnd.go
@@ -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")}
}
}
diff --git a/app/command_online.go b/app/command_online.go
index 358bb8209..f5b14aff4 100644
--- a/app/command_online.go
+++ b/app/command_online.go
@@ -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")}
}
diff --git a/app/plugin_api.go b/app/plugin_api.go
index 4130fc4b2..d7b116c0f 100644
--- a/app/plugin_api.go
+++ b/app/plugin_api.go
@@ -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)
}
diff --git a/app/plugin_api_test.go b/app/plugin_api_test.go
new file mode 100644
index 000000000..56507a8f7
--- /dev/null
+++ b/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)
+}
diff --git a/app/status.go b/app/status.go
index 16c43160d..bfeb5c77e 100644
--- a/app/status.go
+++ b/app/status.go
@@ -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
}
diff --git a/app/web_conn.go b/app/web_conn.go
index dd01a8e31..47fae24c3 100644
--- a/app/web_conn.go
+++ b/app/web_conn.go
@@ -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)
})
}
diff --git a/app/websocket_router.go b/app/websocket_router.go
index 6c5e142a1..da5f03602 100644
--- a/app/websocket_router.go
+++ b/app/websocket_router.go
@@ -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)
})