From 95ad6b1e80d46cfbf2a7f76ba61edb6d74fb1480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Thu, 28 May 2020 11:52:46 +0200 Subject: [PATCH] [GH-103] Add event reminder subscriptions to settings panel (#120) * Add a way to handle subscriptions from the settings panel * Change notification setting id name, getCalendar function name to getCal, and return notification setting directly Co-authored-by: Lev <1187448+levb@users.noreply.github.com> Co-authored-by: mattermod --- server/mscalendar/settings.go | 5 +- server/mscalendar/settings_notifications.go | 135 ++++++++++++++++++++ server/plugin/plugin.go | 4 +- 3 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 server/mscalendar/settings_notifications.go diff --git a/server/mscalendar/settings.go b/server/mscalendar/settings.go index 8b98c6c4..65dc0c47 100644 --- a/server/mscalendar/settings.go +++ b/server/mscalendar/settings.go @@ -22,7 +22,7 @@ func (c *mscalendar) ClearSettingsPosts(userID string) { } } -func NewSettingsPanel(bot bot.Bot, panelStore settingspanel.PanelStore, settingStore settingspanel.SettingStore, settingsHandler, pluginURL string, getTimezone func(userID string) (string, error)) settingspanel.Panel { +func NewSettingsPanel(bot bot.Bot, panelStore settingspanel.PanelStore, settingStore settingspanel.SettingStore, settingsHandler, pluginURL string, getCal func(userID string) MSCalendar) settingspanel.Panel { settings := []settingspanel.Setting{} settings = append(settings, settingspanel.NewBoolSetting( store.UpdateStatusSettingID, @@ -52,9 +52,10 @@ func NewSettingsPanel(bot bot.Bot, panelStore settingspanel.PanelStore, settingS "", settingStore, )) + settings = append(settings, NewNotificationsSetting(getCal)) settings = append(settings, NewDailySummarySetting( settingStore, - getTimezone, + func(userID string) (string, error) { return getCal(userID).GetTimezone(NewUser(userID)) }, )) return settingspanel.NewSettingsPanel(settings, bot, bot, panelStore, settingsHandler, pluginURL) } diff --git a/server/mscalendar/settings_notifications.go b/server/mscalendar/settings_notifications.go new file mode 100644 index 00000000..b3c2d272 --- /dev/null +++ b/server/mscalendar/settings_notifications.go @@ -0,0 +1,135 @@ +package mscalendar + +import ( + "fmt" + + "github.com/mattermost/mattermost-plugin-mscalendar/server/utils/settingspanel" + "github.com/mattermost/mattermost-server/v5/model" +) + +type notificationSetting struct { + title string + description string + id string + dependsOn string + getCal func(string) MSCalendar +} + +func NewNotificationsSetting(getCal func(string) MSCalendar) settingspanel.Setting { + return ¬ificationSetting{ + title: "Receive notifications of new events", + description: "Do you want to subscribe to new events and receive a message when they are created?", + id: "new_or_updated_event_setting", + dependsOn: "", + getCal: getCal, + } +} + +func (s *notificationSetting) Set(userID string, value interface{}) error { + boolValue := false + if value == "true" { + boolValue = true + } + + cal := s.getCal(userID) + + if boolValue { + _, err := cal.LoadMyEventSubscription() + if err != nil { + _, err := cal.CreateMyEventSubscription() + if err != nil { + return err + } + } + + return nil + } + + _, err := cal.LoadMyEventSubscription() + if err == nil { + return cal.DeleteMyEventSubscription() + } + return nil +} + +func (s *notificationSetting) Get(userID string) (interface{}, error) { + cal := s.getCal(userID) + _, err := cal.LoadMyEventSubscription() + if err == nil { + return "true", nil + } + + return "false", nil +} + +func (s *notificationSetting) GetID() string { + return s.id +} + +func (s *notificationSetting) GetTitle() string { + return s.title +} + +func (s *notificationSetting) GetDescription() string { + return s.description +} + +func (s *notificationSetting) GetDependency() string { + return s.dependsOn +} + +func (s *notificationSetting) GetSlackAttachments(userID, settingHandler string, disabled bool) (*model.SlackAttachment, error) { + title := fmt.Sprintf("Setting: %s", s.title) + currentValueMessage := "Disabled" + + actions := []*model.PostAction{} + if !disabled { + currentValue, err := s.Get(userID) + if err != nil { + return nil, err + } + + currentTextValue := "No" + if currentValue == "true" { + currentTextValue = "Yes" + } + currentValueMessage = fmt.Sprintf("Current value: %s", currentTextValue) + + actionTrue := model.PostAction{ + Name: "Yes", + Integration: &model.PostActionIntegration{ + URL: settingHandler, + Context: map[string]interface{}{ + settingspanel.ContextIDKey: s.id, + settingspanel.ContextButtonValueKey: "true", + }, + }, + } + + actionFalse := model.PostAction{ + Name: "No", + Integration: &model.PostActionIntegration{ + URL: settingHandler, + Context: map[string]interface{}{ + settingspanel.ContextIDKey: s.id, + settingspanel.ContextButtonValueKey: "false", + }, + }, + } + actions = []*model.PostAction{&actionTrue, &actionFalse} + + } + + text := fmt.Sprintf("%s\n%s", s.description, currentValueMessage) + sa := model.SlackAttachment{ + Title: title, + Text: text, + Actions: actions, + } + + return &sa, nil +} + +func (s *notificationSetting) IsDisabled(foreignValue interface{}) bool { + return foreignValue == "false" +} diff --git a/server/plugin/plugin.go b/server/plugin/plugin.go index 660f7742..384399a3 100644 --- a/server/plugin/plugin.go +++ b/server/plugin/plugin.go @@ -142,8 +142,8 @@ func (p *Plugin) OnConfigurationChange() (err error) { e.Dependencies.Store, "/settings", pluginURL, - func(userID string) (string, error) { - return mscalendar.New(e.Env, userID).GetTimezone(mscalendar.NewUser(userID)) + func(userID string) mscalendar.MSCalendar { + return mscalendar.New(e.Env, userID) }, )