Skip to content

Commit

Permalink
[GH-103] Add event reminder subscriptions to settings panel (#120)
Browse files Browse the repository at this point in the history
* 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 <mattermod@users.noreply.github.com>
  • Loading branch information
3 people authored May 28, 2020
1 parent c252ee0 commit 95ad6b1
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 4 deletions.
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,
}
}

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"
}
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

0 comments on commit 95ad6b1

Please sign in to comment.