From 16b845c0d77535ea306339f7a8bd22fc72f8a3c5 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Wed, 25 Oct 2017 08:17:17 -0400 Subject: Differentiate between installed and activated states for plugins (#7706) --- model/client4.go | 26 ++++++++++++++++++++++++-- model/config.go | 13 +++++++++++-- model/plugins_response.go | 31 +++++++++++++++++++++++++++++++ model/plugins_response_test.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 model/plugins_response.go create mode 100644 model/plugins_response_test.go (limited to 'model') diff --git a/model/client4.go b/model/client4.go index dc5a25bec..1c20954d8 100644 --- a/model/client4.go +++ b/model/client4.go @@ -3150,12 +3150,12 @@ func (c *Client4) UploadPlugin(file io.Reader) (*Manifest, *Response) { // GetPlugins will return a list of plugin manifests for currently active plugins. // WARNING: PLUGINS ARE STILL EXPERIMENTAL. THIS FUNCTION IS SUBJECT TO CHANGE. -func (c *Client4) GetPlugins() ([]*Manifest, *Response) { +func (c *Client4) GetPlugins() (*PluginsResponse, *Response) { if r, err := c.DoApiGet(c.GetPluginsRoute(), ""); err != nil { return nil, BuildErrorResponse(r, err) } else { defer closeBody(r) - return ManifestListFromJson(r.Body), BuildResponse(r) + return PluginsResponseFromJson(r.Body), BuildResponse(r) } } @@ -3180,3 +3180,25 @@ func (c *Client4) GetWebappPlugins() ([]*Manifest, *Response) { return ManifestListFromJson(r.Body), BuildResponse(r) } } + +// ActivatePlugin will activate an plugin installed. +// WARNING: PLUGINS ARE STILL EXPERIMENTAL. THIS FUNCTION IS SUBJECT TO CHANGE. +func (c *Client4) ActivatePlugin(id string) (bool, *Response) { + if r, err := c.DoApiPost(c.GetPluginRoute(id)+"/activate", ""); err != nil { + return false, BuildErrorResponse(r, err) + } else { + defer closeBody(r) + return CheckStatusOK(r), BuildResponse(r) + } +} + +// DeactivatePlugin will deactivate an active plugin. +// WARNING: PLUGINS ARE STILL EXPERIMENTAL. THIS FUNCTION IS SUBJECT TO CHANGE. +func (c *Client4) DeactivatePlugin(id string) (bool, *Response) { + if r, err := c.DoApiPost(c.GetPluginRoute(id)+"/deactivate", ""); err != nil { + return false, BuildErrorResponse(r, err) + } else { + defer closeBody(r) + return CheckStatusOK(r), BuildResponse(r) + } +} diff --git a/model/config.go b/model/config.go index d3446f533..a93defa7f 100644 --- a/model/config.go +++ b/model/config.go @@ -504,9 +504,14 @@ type JobSettings struct { RunScheduler *bool } +type PluginState struct { + Enable bool +} + type PluginSettings struct { - Enable *bool - Plugins map[string]interface{} + Enable *bool + Plugins map[string]interface{} + PluginStates map[string]*PluginState } type Config struct { @@ -1454,6 +1459,10 @@ func (o *Config) SetDefaults() { o.PluginSettings.Plugins = make(map[string]interface{}) } + if o.PluginSettings.PluginStates == nil { + o.PluginSettings.PluginStates = make(map[string]*PluginState) + } + o.defaultWebrtcSettings() } diff --git a/model/plugins_response.go b/model/plugins_response.go new file mode 100644 index 000000000..d726e491e --- /dev/null +++ b/model/plugins_response.go @@ -0,0 +1,31 @@ +package model + +import ( + "encoding/json" + "io" +) + +type PluginsResponse struct { + Active []*Manifest `json:"active"` + Inactive []*Manifest `json:"inactive"` +} + +func (m *PluginsResponse) ToJson() string { + b, err := json.Marshal(m) + if err != nil { + return "" + } else { + return string(b) + } +} + +func PluginsResponseFromJson(data io.Reader) *PluginsResponse { + decoder := json.NewDecoder(data) + var m PluginsResponse + err := decoder.Decode(&m) + if err == nil { + return &m + } else { + return nil + } +} diff --git a/model/plugins_response_test.go b/model/plugins_response_test.go new file mode 100644 index 000000000..9129c68f7 --- /dev/null +++ b/model/plugins_response_test.go @@ -0,0 +1,31 @@ +package model + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPluginsResponseJson(t *testing.T) { + manifest := &Manifest{ + Id: "theid", + Backend: &ManifestBackend{ + Executable: "theexecutable", + }, + Webapp: &ManifestWebapp{ + BundlePath: "thebundlepath", + }, + } + + response := &PluginsResponse{ + Active: []*Manifest{manifest}, + Inactive: []*Manifest{}, + } + + json := response.ToJson() + newResponse := PluginsResponseFromJson(strings.NewReader(json)) + assert.Equal(t, newResponse, response) + assert.Equal(t, newResponse.ToJson(), json) + assert.Equal(t, PluginsResponseFromJson(strings.NewReader("junk")), (*PluginsResponse)(nil)) +} -- cgit v1.2.3-1-g7c22