2022-07-18 19:21:57 +02:00
|
|
|
package boards
|
2021-05-24 19:06:11 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// configuration captures the plugin's external configuration as exposed in the Mattermost server
|
|
|
|
// configuration, as well as values computed from the configuration. Any public fields will be
|
|
|
|
// deserialized from the Mattermost server configuration in OnConfigurationChange.
|
|
|
|
//
|
|
|
|
// As plugins are inherently concurrent (hooks being called asynchronously), and the plugin
|
|
|
|
// configuration can change at any time, access to the configuration must be synchronized. The
|
|
|
|
// strategy used in this plugin is to guard a pointer to the configuration, and clone the entire
|
|
|
|
// struct whenever it changes. You may replace this with whatever strategy you choose.
|
|
|
|
//
|
|
|
|
// If you add non-reference types to your configuration struct, be sure to rewrite Clone as a deep
|
|
|
|
// copy appropriate for your types.
|
|
|
|
type configuration struct {
|
2021-09-16 21:31:02 +02:00
|
|
|
EnablePublicSharedBoards bool
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clone shallow copies the configuration. Your implementation may require a deep copy if
|
|
|
|
// your configuration has reference types.
|
|
|
|
func (c *configuration) Clone() *configuration {
|
|
|
|
var clone = *c
|
|
|
|
return &clone
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConfiguration retrieves the active configuration under lock, making it safe to use
|
|
|
|
// concurrently. The active configuration may change underneath the client of this method, but
|
|
|
|
// the struct returned by this API call is considered immutable.
|
2022-07-18 19:21:57 +02:00
|
|
|
func (b *BoardsApp) getConfiguration() *configuration {
|
|
|
|
b.configurationLock.RLock()
|
|
|
|
defer b.configurationLock.RUnlock()
|
2021-05-24 19:06:11 +02:00
|
|
|
|
2022-07-18 19:21:57 +02:00
|
|
|
if b.configuration == nil {
|
2021-05-24 19:06:11 +02:00
|
|
|
return &configuration{}
|
|
|
|
}
|
|
|
|
|
2022-07-18 19:21:57 +02:00
|
|
|
return b.configuration
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// setConfiguration replaces the active configuration under lock.
|
|
|
|
//
|
|
|
|
// Do not call setConfiguration while holding the configurationLock, as sync.Mutex is not
|
|
|
|
// reentrant. In particular, avoid using the plugin API entirely, as this may in turn trigger a
|
|
|
|
// hook back into the plugin. If that hook attempts to acquire this lock, a deadlock may occur.
|
|
|
|
//
|
|
|
|
// This method panics if setConfiguration is called with the existing configuration. This almost
|
|
|
|
// certainly means that the configuration was modified without being cloned and may result in
|
|
|
|
// an unsafe access.
|
2022-07-18 19:21:57 +02:00
|
|
|
func (b *BoardsApp) setConfiguration(configuration *configuration) {
|
|
|
|
b.configurationLock.Lock()
|
|
|
|
defer b.configurationLock.Unlock()
|
2021-05-24 19:06:11 +02:00
|
|
|
|
2022-07-18 19:21:57 +02:00
|
|
|
if configuration != nil && b.configuration == configuration {
|
2021-05-24 19:06:11 +02:00
|
|
|
// Ignore assignment if the configuration struct is empty. Go will optimize the
|
|
|
|
// allocation for same to point at the same memory address, breaking the check
|
|
|
|
// above.
|
|
|
|
if reflect.ValueOf(*configuration).NumField() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("setConfiguration called with the existing configuration")
|
|
|
|
}
|
|
|
|
|
2022-07-18 19:21:57 +02:00
|
|
|
b.configuration = configuration
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnConfigurationChange is invoked when configuration changes may have been made.
|
2022-07-18 19:21:57 +02:00
|
|
|
func (b *BoardsApp) OnConfigurationChange() error {
|
2021-09-16 21:31:02 +02:00
|
|
|
// Have we been setup by OnActivate?
|
2022-07-18 19:21:57 +02:00
|
|
|
if b.server == nil {
|
2021-09-16 21:31:02 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-07-18 19:21:57 +02:00
|
|
|
mmconfig := b.servicesAPI.GetConfig()
|
2021-05-24 19:06:11 +02:00
|
|
|
|
2021-11-09 16:56:04 +02:00
|
|
|
// handle plugin configuration settings
|
|
|
|
enableShareBoards := false
|
2022-07-18 19:21:57 +02:00
|
|
|
if mmconfig.PluginSettings.Plugins[PluginName][SharedBoardsName] == true {
|
2021-11-09 16:56:04 +02:00
|
|
|
enableShareBoards = true
|
|
|
|
}
|
|
|
|
configuration := &configuration{
|
|
|
|
EnablePublicSharedBoards: enableShareBoards,
|
|
|
|
}
|
2022-07-18 19:21:57 +02:00
|
|
|
b.setConfiguration(configuration)
|
|
|
|
b.server.Config().EnablePublicSharedBoards = enableShareBoards
|
2021-05-24 19:06:11 +02:00
|
|
|
|
2021-11-09 16:56:04 +02:00
|
|
|
// handle feature flags
|
2022-07-18 19:21:57 +02:00
|
|
|
b.server.Config().FeatureFlags = parseFeatureFlags(mmconfig.FeatureFlags.ToMap())
|
2022-04-25 20:58:08 +02:00
|
|
|
|
|
|
|
// handle Data Retention settings
|
|
|
|
enableBoardsDeletion := false
|
|
|
|
if mmconfig.DataRetentionSettings.EnableBoardsDeletion != nil {
|
|
|
|
enableBoardsDeletion = true
|
|
|
|
}
|
2022-07-18 19:21:57 +02:00
|
|
|
b.server.Config().EnableDataRetention = enableBoardsDeletion
|
|
|
|
b.server.Config().DataRetentionDays = *mmconfig.DataRetentionSettings.BoardsRetentionDays
|
|
|
|
b.server.Config().TeammateNameDisplay = *mmconfig.TeamSettings.TeammateNameDisplay
|
2022-04-25 20:58:08 +02:00
|
|
|
|
2022-07-18 19:21:57 +02:00
|
|
|
b.server.UpdateAppConfig()
|
|
|
|
b.wsPluginAdapter.BroadcastConfigChange(*b.server.App().GetClientConfig())
|
2021-05-24 19:06:11 +02:00
|
|
|
return nil
|
|
|
|
}
|