Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-103] Add event reminder subscriptions to settings panel #120

Merged
merged 7 commits into from
May 28, 2020
5 changes: 3 additions & 2 deletions server/mscalendar/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
135 changes: 135 additions & 0 deletions server/mscalendar/settings_notifications.go
Original file line number Diff line number Diff line change
@@ -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 &notificationSetting{
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,
mickmister marked this conversation as resolved.
Show resolved Hide resolved
}
}

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()
mickmister marked this conversation as resolved.
Show resolved Hide resolved
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe reference these as constants like BOOL_STRING_FALSE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am making in parallel some changes on how to handle slack attachment context, and therefore how to pass this value here. Long story short, marshaling and unmarshaling the values as jsons. You can see some of that implementation here.

I will leave this as it is, and change it in the future when those changes are in place.

}
4 changes: 2 additions & 2 deletions server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
)

Expand Down