[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,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
}