[MM-53968] Includes mattermost-plugin-api into the mono repo (#24235)

Include https://github.com/mattermost/mattermost-plugin-api into the mono repo

Co-authored-by: Jesse Hallam <jesse.hallam@gmail.com>
Co-authored-by: Michael Kochell <mjkochell@gmail.com>
Co-authored-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
Co-authored-by: Alex Dovenmuehle <alex.dovenmuehle@mattermost.com>
Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>
Co-authored-by: Christopher Poile <cpoile@gmail.com>
Co-authored-by: İlker Göktuğ Öztürk <ilkergoktugozturk@gmail.com>
Co-authored-by: Shota Gvinepadze <wineson@gmail.com>
Co-authored-by: Ali Farooq <ali.farooq0@pm.me>
Co-authored-by: Maria A Nunez <maria.nunez@mattermost.com>
Co-authored-by: Daniel Espino García <larkox@gmail.com>
Co-authored-by: Christopher Speller <crspeller@gmail.com>
Co-authored-by: Alex Dovenmuehle <adovenmuehle@gmail.com>
Co-authored-by: Szymon Gibała <szymongib@gmail.com>
Co-authored-by: Lev <1187448+levb@users.noreply.github.com>
Co-authored-by: Jason Frerich <jason.frerich@mattermost.com>
Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in>
Co-authored-by: Artur M. Wolff <artur.m.wolff@gmail.com>
Co-authored-by: Madhav Hugar <16546715+madhavhugar@users.noreply.github.com>
Co-authored-by: Joe <security.joe@pm.me>
Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>
Co-authored-by: José Peso <trilopin@users.noreply.github.com>
Этот коммит содержится в:
Ben Schumacher
2023-08-21 09:50:30 +02:00
коммит произвёл GitHub
родитель bc11b29807
Коммит 3ee5432664
117 изменённых файлов: 14912 добавлений и 5 удалений

Просмотреть файл

@@ -0,0 +1,70 @@
package panel
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/common"
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/panel/settings"
)
type handler struct {
panel Panel
}
func Init(r *mux.Router, panel Panel) {
sh := &handler{
panel: panel,
}
panelRouter := r.PathPrefix("/").Subrouter()
panelRouter.HandleFunc(panel.URL(), sh.handleAction).Methods(http.MethodPost)
}
func (sh *handler) handleAction(w http.ResponseWriter, r *http.Request) {
mattermostUserID := r.Header.Get("Mattermost-User-ID")
if mattermostUserID == "" {
common.SlackAttachmentError(w, errors.New("Not authorized"))
return
}
var request model.PostActionIntegrationRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
common.SlackAttachmentError(w, errors.New("invalid request"))
return
}
id, ok := request.Context[settings.ContextIDKey]
if !ok {
common.SlackAttachmentError(w, errors.New("missing setting id"))
return
}
value, ok := request.Context[settings.ContextButtonValueKey]
if !ok {
value, ok = request.Context[settings.ContextOptionValueKey]
if !ok {
common.SlackAttachmentError(w, errors.New("valid key not found"))
return
}
}
idString := id.(string)
err := sh.panel.Set(mattermostUserID, idString, value)
if err != nil {
common.SlackAttachmentError(w, errors.Wrap(err, "cannot save setting"))
return
}
response := model.PostActionIntegrationResponse{}
post, err := sh.panel.ToPost(mattermostUserID)
if err == nil {
response.Update = post
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(response)
}

Просмотреть файл

@@ -0,0 +1,118 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/mattermost/mattermost/server/public/pluginapi/experimental/panel (interfaces: Panel)
// Package mock_panel is a generated GoMock package.
package mock_panel
import (
reflect "reflect"
gomock "github.com/golang/mock/gomock"
model "github.com/mattermost/mattermost/server/public/model"
)
// MockPanel is a mock of Panel interface.
type MockPanel struct {
ctrl *gomock.Controller
recorder *MockPanelMockRecorder
}
// MockPanelMockRecorder is the mock recorder for MockPanel.
type MockPanelMockRecorder struct {
mock *MockPanel
}
// NewMockPanel creates a new mock instance.
func NewMockPanel(ctrl *gomock.Controller) *MockPanel {
mock := &MockPanel{ctrl: ctrl}
mock.recorder = &MockPanelMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockPanel) EXPECT() *MockPanelMockRecorder {
return m.recorder
}
// Clear mocks base method.
func (m *MockPanel) Clear(arg0 string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Clear", arg0)
ret0, _ := ret[0].(error)
return ret0
}
// Clear indicates an expected call of Clear.
func (mr *MockPanelMockRecorder) Clear(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Clear", reflect.TypeOf((*MockPanel)(nil).Clear), arg0)
}
// GetSettingIDs mocks base method.
func (m *MockPanel) GetSettingIDs() []string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetSettingIDs")
ret0, _ := ret[0].([]string)
return ret0
}
// GetSettingIDs indicates an expected call of GetSettingIDs.
func (mr *MockPanelMockRecorder) GetSettingIDs() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSettingIDs", reflect.TypeOf((*MockPanel)(nil).GetSettingIDs))
}
// Print mocks base method.
func (m *MockPanel) Print(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Print", arg0)
}
// Print indicates an expected call of Print.
func (mr *MockPanelMockRecorder) Print(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Print", reflect.TypeOf((*MockPanel)(nil).Print), arg0)
}
// Set mocks base method.
func (m *MockPanel) Set(arg0, arg1 string, arg2 interface{}) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Set", arg0, arg1, arg2)
ret0, _ := ret[0].(error)
return ret0
}
// Set indicates an expected call of Set.
func (mr *MockPanelMockRecorder) Set(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Set", reflect.TypeOf((*MockPanel)(nil).Set), arg0, arg1, arg2)
}
// ToPost mocks base method.
func (m *MockPanel) ToPost(arg0 string) (*model.Post, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ToPost", arg0)
ret0, _ := ret[0].(*model.Post)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// ToPost indicates an expected call of ToPost.
func (mr *MockPanelMockRecorder) ToPost(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ToPost", reflect.TypeOf((*MockPanel)(nil).ToPost), arg0)
}
// URL mocks base method.
func (m *MockPanel) URL() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "URL")
ret0, _ := ret[0].(string)
return ret0
}
// URL indicates an expected call of URL.
func (mr *MockPanelMockRecorder) URL() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "URL", reflect.TypeOf((*MockPanel)(nil).URL))
}

Просмотреть файл

@@ -0,0 +1,77 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/mattermost/mattermost/server/public/pluginapi/experimental/panel (interfaces: Store)
// Package mock_panel is a generated GoMock package.
package mock_panel
import (
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockStore is a mock of Store interface.
type MockStore struct {
ctrl *gomock.Controller
recorder *MockStoreMockRecorder
}
// MockStoreMockRecorder is the mock recorder for MockStore.
type MockStoreMockRecorder struct {
mock *MockStore
}
// NewMockStore creates a new mock instance.
func NewMockStore(ctrl *gomock.Controller) *MockStore {
mock := &MockStore{ctrl: ctrl}
mock.recorder = &MockStoreMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockStore) EXPECT() *MockStoreMockRecorder {
return m.recorder
}
// DeletePanelPostID mocks base method.
func (m *MockStore) DeletePanelPostID(arg0 string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "DeletePanelPostID", arg0)
ret0, _ := ret[0].(error)
return ret0
}
// DeletePanelPostID indicates an expected call of DeletePanelPostID.
func (mr *MockStoreMockRecorder) DeletePanelPostID(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeletePanelPostID", reflect.TypeOf((*MockStore)(nil).DeletePanelPostID), arg0)
}
// GetPanelPostID mocks base method.
func (m *MockStore) GetPanelPostID(arg0 string) (string, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetPanelPostID", arg0)
ret0, _ := ret[0].(string)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetPanelPostID indicates an expected call of GetPanelPostID.
func (mr *MockStoreMockRecorder) GetPanelPostID(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPanelPostID", reflect.TypeOf((*MockStore)(nil).GetPanelPostID), arg0)
}
// SetPanelPostID mocks base method.
func (m *MockStore) SetPanelPostID(arg0, arg1 string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SetPanelPostID", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// SetPanelPostID indicates an expected call of SetPanelPostID.
func (mr *MockStoreMockRecorder) SetPanelPostID(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetPanelPostID", reflect.TypeOf((*MockStore)(nil).SetPanelPostID), arg0, arg1)
}

Просмотреть файл

@@ -0,0 +1,149 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/mattermost/mattermost/server/public/pluginapi/experimental/panel/settings (interfaces: Setting)
// Package mock_panel is a generated GoMock package.
package mock_panel
import (
reflect "reflect"
gomock "github.com/golang/mock/gomock"
model "github.com/mattermost/mattermost/server/public/model"
)
// MockSetting is a mock of Setting interface.
type MockSetting struct {
ctrl *gomock.Controller
recorder *MockSettingMockRecorder
}
// MockSettingMockRecorder is the mock recorder for MockSetting.
type MockSettingMockRecorder struct {
mock *MockSetting
}
// NewMockSetting creates a new mock instance.
func NewMockSetting(ctrl *gomock.Controller) *MockSetting {
mock := &MockSetting{ctrl: ctrl}
mock.recorder = &MockSettingMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockSetting) EXPECT() *MockSettingMockRecorder {
return m.recorder
}
// Get mocks base method.
func (m *MockSetting) Get(arg0 string) (interface{}, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Get", arg0)
ret0, _ := ret[0].(interface{})
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Get indicates an expected call of Get.
func (mr *MockSettingMockRecorder) Get(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockSetting)(nil).Get), arg0)
}
// GetDependency mocks base method.
func (m *MockSetting) GetDependency() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetDependency")
ret0, _ := ret[0].(string)
return ret0
}
// GetDependency indicates an expected call of GetDependency.
func (mr *MockSettingMockRecorder) GetDependency() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDependency", reflect.TypeOf((*MockSetting)(nil).GetDependency))
}
// GetDescription mocks base method.
func (m *MockSetting) GetDescription() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetDescription")
ret0, _ := ret[0].(string)
return ret0
}
// GetDescription indicates an expected call of GetDescription.
func (mr *MockSettingMockRecorder) GetDescription() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDescription", reflect.TypeOf((*MockSetting)(nil).GetDescription))
}
// GetID mocks base method.
func (m *MockSetting) GetID() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetID")
ret0, _ := ret[0].(string)
return ret0
}
// GetID indicates an expected call of GetID.
func (mr *MockSettingMockRecorder) GetID() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetID", reflect.TypeOf((*MockSetting)(nil).GetID))
}
// GetSlackAttachments mocks base method.
func (m *MockSetting) GetSlackAttachments(arg0, arg1 string, arg2 bool) (*model.SlackAttachment, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetSlackAttachments", arg0, arg1, arg2)
ret0, _ := ret[0].(*model.SlackAttachment)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetSlackAttachments indicates an expected call of GetSlackAttachments.
func (mr *MockSettingMockRecorder) GetSlackAttachments(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSlackAttachments", reflect.TypeOf((*MockSetting)(nil).GetSlackAttachments), arg0, arg1, arg2)
}
// GetTitle mocks base method.
func (m *MockSetting) GetTitle() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetTitle")
ret0, _ := ret[0].(string)
return ret0
}
// GetTitle indicates an expected call of GetTitle.
func (mr *MockSettingMockRecorder) GetTitle() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTitle", reflect.TypeOf((*MockSetting)(nil).GetTitle))
}
// IsDisabled mocks base method.
func (m *MockSetting) IsDisabled(arg0 interface{}) bool {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "IsDisabled", arg0)
ret0, _ := ret[0].(bool)
return ret0
}
// IsDisabled indicates an expected call of IsDisabled.
func (mr *MockSettingMockRecorder) IsDisabled(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsDisabled", reflect.TypeOf((*MockSetting)(nil).IsDisabled), arg0)
}
// Set mocks base method.
func (m *MockSetting) Set(arg0 string, arg1 interface{}) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Set", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// Set indicates an expected call of Set.
func (mr *MockSettingMockRecorder) Set(arg0, arg1 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Set", reflect.TypeOf((*MockSetting)(nil).Set), arg0, arg1)
}

Просмотреть файл

@@ -0,0 +1,171 @@
package panel
import (
"errors"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/logger"
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/poster"
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/common"
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/panel/settings"
)
type Panel interface {
Set(userID, settingID string, value interface{}) error
Print(userID string)
ToPost(userID string) (*model.Post, error)
Clear(userID string) error
URL() string
GetSettingIDs() []string
}
type panel struct {
settings map[string]settings.Setting
settingKeys []string
poster poster.Poster
logger logger.Logger
store Store
settingHandler string
pluginURL string
}
func NewSettingsPanel(
settingList []settings.Setting,
p poster.Poster,
l logger.Logger,
store Store,
settingHandler,
pluginURL string,
) Panel {
settingsMap := make(map[string]settings.Setting)
settingKeys := []string{}
for _, s := range settingList {
settingsMap[s.GetID()] = s
settingKeys = append(settingKeys, s.GetID())
}
panel := &panel{
settings: settingsMap,
settingKeys: settingKeys,
poster: p,
logger: l,
store: store,
settingHandler: settingHandler,
pluginURL: pluginURL,
}
return panel
}
func (p *panel) Set(userID, settingID string, value interface{}) error {
s, ok := p.settings[settingID]
if !ok {
return errors.New("cannot find setting " + settingID)
}
err := s.Set(userID, value)
if err != nil {
return err
}
return nil
}
func (p *panel) GetSettingIDs() []string {
return p.settingKeys
}
func (p *panel) URL() string {
return p.settingHandler
}
func (p *panel) Print(userID string) {
err := p.cleanPreviousSettingsPosts(userID)
if err != nil {
p.logger.Errorf("could not clean previous setting post, " + err.Error())
}
sas := []*model.SlackAttachment{}
for _, key := range p.settingKeys {
s := p.settings[key]
sa, loopErr := s.GetSlackAttachments(userID, p.pluginURL+p.settingHandler, p.isSettingDisabled(userID, s))
if loopErr != nil {
p.logger.Errorf("error creating the slack attachment, err=" + loopErr.Error())
continue
}
sas = append(sas, sa)
}
postID, err := p.poster.DMWithAttachments(userID, sas...)
if err != nil {
p.logger.Errorf("error creating the message, err=", err.Error())
return
}
err = p.store.SetPanelPostID(userID, postID)
if err != nil {
p.logger.Errorf("could not set the post IDs, err=", err.Error())
}
}
func (p *panel) ToPost(userID string) (*model.Post, error) {
post := &model.Post{}
sas := []*model.SlackAttachment{}
for _, key := range p.settingKeys {
s := p.settings[key]
sa, err := s.GetSlackAttachments(userID, p.pluginURL+p.settingHandler, p.isSettingDisabled(userID, s))
if err != nil {
p.logger.Errorf("error creating the slack attachment for setting %s, err=%s", s.GetID(), err.Error())
continue
}
sas = append(sas, sa)
}
model.ParseSlackAttachment(post, sas)
return post, nil
}
func (p *panel) cleanPreviousSettingsPosts(userID string) error {
postID, err := p.store.GetPanelPostID(userID)
if err == common.ErrNotFound {
return nil
}
if err != nil {
return err
}
err = p.poster.DeletePost(postID)
if err != nil {
p.logger.Errorf("could not delete setting post, %s", err)
}
err = p.store.DeletePanelPostID(userID)
if err != nil {
return err
}
return nil
}
func (p *panel) Clear(userID string) error {
return p.cleanPreviousSettingsPosts(userID)
}
func (p *panel) isSettingDisabled(userID string, s settings.Setting) bool {
dependencyID := s.GetDependency()
if dependencyID == "" {
return false
}
dependency, ok := p.settings[dependencyID]
if !ok {
p.logger.Errorf("settings dependency %s not found", dependencyID)
return false
}
value, err := dependency.Get(userID)
if err != nil {
p.logger.Errorf("cannot get dependency %s value", dependencyID)
return false
}
return s.IsDisabled(value)
}

Просмотреть файл

@@ -0,0 +1,28 @@
package settings
type baseSetting struct {
title string
description string
id string
dependsOn string
}
func (s *baseSetting) GetID() string {
return s.id
}
func (s *baseSetting) GetTitle() string {
return s.title
}
func (s *baseSetting) GetDescription() string {
return s.description
}
func (s *baseSetting) GetDependency() string {
return s.dependsOn
}
func (s *baseSetting) IsDisabled(foreignValue interface{}) bool {
return false
}

Просмотреть файл

@@ -0,0 +1,114 @@
package settings
import (
"errors"
"fmt"
"github.com/mattermost/mattermost/server/public/model"
)
type boolSetting struct {
baseSetting
store SettingStore
}
// NewBoolSetting creates a new setting input for boolean values
func NewBoolSetting(id, title, description, dependsOn string, store SettingStore) Setting {
return &boolSetting{
baseSetting: baseSetting{
title: title,
description: description,
id: id,
dependsOn: dependsOn,
},
store: store,
}
}
func (s *boolSetting) Set(userID string, value interface{}) error {
boolValue := false
if value == TrueString {
boolValue = true
}
err := s.store.SetSetting(userID, s.id, boolValue)
if err != nil {
return err
}
return nil
}
func (s *boolSetting) Get(userID string) (interface{}, error) {
value, err := s.store.GetSetting(userID, s.id)
if err != nil {
return "", err
}
boolValue, ok := value.(bool)
if !ok {
return "", errors.New("current value is not a bool")
}
stringValue := FalseString
if boolValue {
stringValue = TrueString
}
return stringValue, nil
}
func (s *boolSetting) GetSlackAttachments(userID, settingHandler string, disabled bool) (*model.SlackAttachment, error) {
title := fmt.Sprintf("Setting: %s", s.title)
currentValueMessage := DisabledString
actions := []*model.PostAction{}
if !disabled {
currentValue, err := s.Get(userID)
if err != nil {
return nil, err
}
currentTextValue := "No"
if currentValue == TrueString {
currentTextValue = "Yes"
}
currentValueMessage = fmt.Sprintf("Current value: %s", currentTextValue)
actionTrue := model.PostAction{
Name: "Yes",
Integration: &model.PostActionIntegration{
URL: settingHandler,
Context: map[string]interface{}{
ContextIDKey: s.id,
ContextButtonValueKey: TrueString,
},
},
}
actionFalse := model.PostAction{
Name: "No",
Integration: &model.PostActionIntegration{
URL: settingHandler,
Context: map[string]interface{}{
ContextIDKey: s.id,
ContextButtonValueKey: FalseString,
},
},
}
actions = []*model.PostAction{&actionTrue, &actionFalse}
}
text := fmt.Sprintf("%s\n%s", s.description, currentValueMessage)
sa := model.SlackAttachment{
Title: title,
Text: text,
Fallback: fmt.Sprintf("%s: %s", title, text),
Actions: actions,
}
return &sa, nil
}
func (s *boolSetting) IsDisabled(foreignValue interface{}) bool {
return foreignValue == FalseString
}

Просмотреть файл

@@ -0,0 +1,41 @@
package settings
import (
"fmt"
"github.com/mattermost/mattermost/server/public/model"
)
type emptySetting struct {
baseSetting
}
// NewEmptySetting creates a new panel value with no setting attached
func NewEmptySetting(id, title, description string) Setting {
return &emptySetting{
baseSetting: baseSetting{
id: id,
title: title,
description: description,
},
}
}
func (s *emptySetting) GetSlackAttachments(userID, settingHandler string, disabled bool) (*model.SlackAttachment, error) {
title := fmt.Sprintf("Setting: %s", s.title)
sa := model.SlackAttachment{
Title: title,
Text: s.description,
Fallback: fmt.Sprintf("%s: %s", title, s.description),
}
return &sa, nil
}
func (s *emptySetting) Get(userID string) (interface{}, error) {
return nil, nil
}
func (s *emptySetting) Set(userID string, value interface{}) error {
return nil
}

Просмотреть файл

@@ -0,0 +1,91 @@
package settings
import (
"errors"
"fmt"
"github.com/mattermost/mattermost/server/public/model"
)
type optionSetting struct {
baseSetting
options []string
store SettingStore
}
// NewOptionSetting creates a new setting input to select from a dropdown
func NewOptionSetting(id, title, description, dependsOn string, options []string, store SettingStore) Setting {
return &optionSetting{
baseSetting: baseSetting{
title: title,
description: description,
id: id,
dependsOn: dependsOn,
},
options: options,
store: store,
}
}
func (s *optionSetting) Set(userID string, value interface{}) error {
err := s.store.SetSetting(userID, s.id, value)
if err != nil {
return err
}
return nil
}
func (s *optionSetting) Get(userID string) (interface{}, error) {
value, err := s.store.GetSetting(userID, s.id)
if err != nil {
return "", err
}
valueString, ok := value.(string)
if !ok {
return "", errors.New("current value is not a string")
}
return valueString, nil
}
func (s *optionSetting) GetSlackAttachments(userID, settingHandler string, disabled bool) (*model.SlackAttachment, error) {
title := fmt.Sprintf("Setting: %s", s.title)
currentValueMessage := DisabledString
actions := []*model.PostAction{}
if !disabled {
currentTextValue, err := s.Get(userID)
if err != nil {
return nil, err
}
currentValueMessage = fmt.Sprintf("Current value: %s", currentTextValue)
actionOptions := model.PostAction{
Name: "Select an option:",
Integration: &model.PostActionIntegration{
URL: settingHandler + "?" + s.id + "=true",
Context: map[string]interface{}{
ContextIDKey: s.id,
},
},
Type: "select",
Options: stringsToOptions(s.options),
}
actions = []*model.PostAction{&actionOptions}
}
text := fmt.Sprintf("%s\n%s", s.description, currentValueMessage)
sa := model.SlackAttachment{
Title: title,
Text: text,
Fallback: fmt.Sprintf("%s: %s", title, text),
Actions: actions,
}
return &sa, nil
}
func (s *optionSetting) IsDisabled(foreignValue interface{}) bool {
return foreignValue == FalseString
}

Просмотреть файл

@@ -0,0 +1,69 @@
package settings
import (
"errors"
"fmt"
"github.com/mattermost/mattermost/server/public/model"
)
type readOnlySetting struct {
baseSetting
store SettingStore
}
// NewReadOnlySetting creates a new panel value that only read from the setting
func NewReadOnlySetting(id, title, description, dependsOn string, store SettingStore) Setting {
return &readOnlySetting{
baseSetting: baseSetting{
title: title,
description: description,
id: id,
dependsOn: dependsOn,
},
store: store,
}
}
func (s *readOnlySetting) Get(userID string) (interface{}, error) {
value, err := s.store.GetSetting(userID, s.id)
if err != nil {
return "", err
}
stringValue, ok := value.(string)
if !ok {
return "", errors.New("current value is not a string")
}
return stringValue, nil
}
func (s *readOnlySetting) Set(userID string, value interface{}) error {
return nil
}
func (s *readOnlySetting) GetSlackAttachments(userID, settingHandler string, disabled bool) (*model.SlackAttachment, error) {
title := fmt.Sprintf("Setting: %s", s.title)
currentValueMessage := DisabledString
if !disabled {
currentValue, err := s.Get(userID)
if err != nil {
return nil, err
}
currentValueMessage = fmt.Sprintf("Current value: %s", currentValue)
}
text := fmt.Sprintf("%s\n%s", s.description, currentValueMessage)
sa := model.SlackAttachment{
Title: title,
Text: text,
Fallback: fmt.Sprintf("%s: %s", title, text),
}
return &sa, nil
}
func (s *readOnlySetting) IsDisabled(foreignValue interface{}) bool {
return foreignValue == FalseString
}

Просмотреть файл

@@ -0,0 +1,33 @@
package settings
import (
"github.com/mattermost/mattermost/server/public/model"
)
const (
// ContextIDKey defines the key used in the context to store the ID
ContextIDKey = "setting_id"
// ContextButtonValueKey defines the key used in the context to store a button value
ContextButtonValueKey = "button_value"
// ContextOptionValueKey defines the key used in the context to store a selected option value
ContextOptionValueKey = "selected_option"
// DisabledString defines the string used to show that a setting is disabled
DisabledString = "Disabled"
// TrueString codify the boolean true into a string
TrueString = "true"
// FalseString codify the boolean false into a string
FalseString = "false"
)
// Setting defines the behavior of each element a the panel
type Setting interface {
Set(userID string, value interface{}) error
Get(userID string) (interface{}, error)
GetID() string
GetDependency() string
IsDisabled(foreignValue interface{}) bool
GetTitle() string
GetDescription() string
GetSlackAttachments(userID, settingHandler string, disabled bool) (*model.SlackAttachment, error)
}

Просмотреть файл

@@ -0,0 +1,7 @@
package settings
// SettingStore defines the behavior needed to set and get settings
type SettingStore interface {
SetSetting(userID, settingID string, value interface{}) error
GetSetting(userID, settingID string) (interface{}, error)
}

Просмотреть файл

@@ -0,0 +1,16 @@
package settings
import (
"github.com/mattermost/mattermost/server/public/model"
)
func stringsToOptions(in []string) []*model.PostActionOptions {
out := make([]*model.PostActionOptions, len(in))
for i, o := range in {
out[i] = &model.PostActionOptions{
Text: o,
Value: o,
}
}
return out
}

Просмотреть файл

@@ -0,0 +1,53 @@
package panel
import (
"errors"
"github.com/mattermost/mattermost/server/public/pluginapi"
)
type Store interface {
SetPanelPostID(userID string, postID string) error
GetPanelPostID(userID string) (string, error)
DeletePanelPostID(userID string) error
}
type panelStore struct {
kv *pluginapi.KVService
keyPrefix string
}
func NewPanelStore(kv *pluginapi.KVService, keyPrefix string) Store {
return &panelStore{
kv: kv,
keyPrefix: keyPrefix,
}
}
func (ps *panelStore) SetPanelPostID(userID, postID string) error {
ok, err := ps.kv.Set(ps.getKey(userID), postID)
if err != nil {
return err
}
if !ok {
return errors.New("value not set without errors")
}
return nil
}
func (ps *panelStore) GetPanelPostID(userID string) (string, error) {
var postID string
err := ps.kv.Get(ps.getKey(userID), &postID)
if err != nil {
return "", err
}
return postID, nil
}
func (ps *panelStore) DeletePanelPostID(userID string) error {
return ps.kv.Delete(ps.getKey(userID))
}
func (ps *panelStore) getKey(userID string) string {
return ps.keyPrefix + "-" + userID
}