Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
larkox committed Apr 13, 2020
1 parent 0af3903 commit ffca8e1
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 53 deletions.
2 changes: 1 addition & 1 deletion server/mscalendar/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (c *mscalendar) ClearSettingsPosts(userID string) {
c.SettingsPanel.Clear(userID)
}

func NewSettingsPanel(bot bot.Bot, panelStore settingspanel.PanelStore, settingStore settingspanel.SettingStore, settingsHandler, pluginURL string, getTimezone func(userID string) string) settingspanel.Panel {
func NewSettingsPanel(bot bot.Bot, panelStore settingspanel.PanelStore, settingStore settingspanel.SettingStore, settingsHandler, pluginURL string, getTimezone func(userID string) (string, error)) settingspanel.Panel {
settings := []settingspanel.Setting{}
settings = append(settings, settingspanel.NewBoolSetting(
store.UpdateStatusSettingID,
Expand Down
53 changes: 31 additions & 22 deletions server/mscalendar/settings_daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ type dailySummarySetting struct {
optionsM []string
optionsAPM []string
store settingspanel.SettingStore
getTimezone func(userID string) string
getTimezone func(userID string) (string, error)
}

func NewDailySummarySetting(inStore settingspanel.SettingStore, getTimezone func(userID string) string) settingspanel.Setting {
func NewDailySummarySetting(inStore settingspanel.SettingStore, getTimezone func(userID string) (string, error)) settingspanel.Setting {
os := &dailySummarySetting{
title: "Daily Summary",
description: "When do you want to receive the daily summary.\n If you update this setting, it will automatically update to your the timezone currently set on your calendar.",
description: "When do you want to receive the daily summary?\n If you update this setting, it will automatically update to your the timezone currently set on your calendar.",
id: store.DailySummarySettingID,
dependsOn: "",
store: inStore,
Expand All @@ -47,6 +47,10 @@ func NewDailySummarySetting(inStore settingspanel.SettingStore, getTimezone func
}

func (s *dailySummarySetting) Set(userID string, value interface{}) error {
_, ok := value.(string)
if !ok {
return errors.New("trying to set Daily Summary Setting without a string value")
}
err := s.store.SetSetting(userID, s.id, value)
if err != nil {
return err
Expand Down Expand Up @@ -118,7 +122,14 @@ func (s *dailySummarySetting) GetSlackAttachments(userID, settingHandler string,
currentTextValue = fmt.Sprintf("%s (%s) (%s)", dsum.PostTime, dsum.Timezone, enableText)
}

timezone := s.getTimezone(userID)
timezone, err := s.getTimezone(userID)
if err != nil {
if dsum != nil {
timezone = dsum.Timezone
} else {
timezone = "UTC"
}
}
fullTime = fullTime + " " + timezone

currentValueMessage = fmt.Sprintf("Current value: %s", currentTextValue)
Expand Down Expand Up @@ -164,26 +175,24 @@ func (s *dailySummarySetting) GetSlackAttachments(userID, settingHandler string,

actions = []*model.PostAction{&actionOptionsH, &actionOptionsM, &actionOptionsAPM}

if currentTextValue != "Not set" {
buttonText := "Enable"
enable := "true"
if currentEnable {
buttonText = "Disable"
enable = "false"
}
actionToggle := model.PostAction{
Name: buttonText,
Integration: &model.PostActionIntegration{
URL: settingHandler,
Context: map[string]interface{}{
settingspanel.ContextIDKey: s.id,
settingspanel.ContextButtonValueKey: enable + " " + timezone,
},
buttonText := "Enable"
enable := "true"
if currentEnable {
buttonText = "Disable"
enable = "false"
}
actionToggle := model.PostAction{
Name: buttonText,
Integration: &model.PostActionIntegration{
URL: settingHandler,
Context: map[string]interface{}{
settingspanel.ContextIDKey: s.id,
settingspanel.ContextButtonValueKey: enable + " " + timezone,
},
}

actions = append(actions, &actionToggle)
},
}

actions = append(actions, &actionToggle)
}

text := fmt.Sprintf("%s\n%s", s.description, currentValueMessage)
Expand Down
5 changes: 2 additions & 3 deletions server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ func (p *Plugin) OnConfigurationChange() (err error) {
e.Dependencies.Store,
"/settings",
pluginURL,
func(userID string) string {
timezone, _ := mscalendar.New(e.Env, userID).GetTimezone(mscalendar.NewUser(userID))
return timezone
func(userID string) (string, error) {
return mscalendar.New(e.Env, userID).GetTimezone(mscalendar.NewUser(userID))
},
)

Expand Down
26 changes: 4 additions & 22 deletions server/store/setting_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package store
import (
"fmt"
"strings"

"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/kvstore"
)

const (
Expand Down Expand Up @@ -58,27 +56,12 @@ func (s *pluginStore) GetSetting(userID, settingID string) (interface{}, error)
case GetConfirmationSettingID:
return user.Settings.GetConfirmation, nil
case DailySummarySettingID:
return s.getDailySummarySettingForUser(userID)
return s.LoadDailySummaryUserSettings(userID)
default:
return nil, fmt.Errorf("setting %s not found", settingID)
}
}

func (s *pluginStore) getDailySummarySettingForUser(userID string) (*DailySummaryUserSettings, error) {
dsumIndex, err := s.LoadDailySummaryIndex()
if err != nil {
return nil, err
}

for _, dsum := range dsumIndex {
if dsum.MattermostUserID == userID {
return dsum, nil
}
}

return nil, nil
}

func (s *pluginStore) updateDailySummarySettingForUser(userID string, value interface{}) error {
dsum, err := s.LoadDailySummaryUserSettings(userID)
if err != nil {
Expand Down Expand Up @@ -122,21 +105,20 @@ func (s *pluginStore) updateDailySummarySettingForUser(userID string, value inte
}

func (s *pluginStore) SetPanelPostID(userID string, postID string) error {
err := kvstore.StoreJSON(s.settingsPanelKV, userID, postID)
err := s.settingsPanelKV.Store(userID, []byte(postID))
if err != nil {
return err
}
return nil
}

func (s *pluginStore) GetPanelPostID(userID string) (string, error) {
var postID string
err := kvstore.LoadJSON(s.settingsPanelKV, userID, &postID)
postID, err := s.settingsPanelKV.Load(userID)
if err != nil {
return "", err
}

return postID, nil
return string(postID), nil
}

func (s *pluginStore) DeletePanelPostID(userID string) error {
Expand Down
2 changes: 1 addition & 1 deletion server/utils/settingspanel/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (sh *handler) handleAction(w http.ResponseWriter, r *http.Request) {
sh.panel.Set(mattermostUserID, idString, value)

response := model.PostActionIntegrationResponse{}
post, err := sh.panel.GetUpdatePost(mattermostUserID)
post, err := sh.panel.ToPost(mattermostUserID)
if err == nil {
response.Update = post
}
Expand Down
2 changes: 1 addition & 1 deletion server/utils/settingspanel/read_only_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *readOnlySetting) Get(userID string) (interface{}, error) {
}
stringValue, ok := value.(string)
if !ok {
return "", errors.New("current value is not a bool")
return "", errors.New("current value is not a string")
}

return stringValue, nil
Expand Down
5 changes: 2 additions & 3 deletions server/utils/settingspanel/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Setting interface {
type Panel interface {
Set(userID, settingID string, value interface{}) error
Print(userID string)
GetUpdatePost(userID string) (*model.Post, error)
ToPost(userID string) (*model.Post, error)
Clear(userID string) error
URL() string
GetSettingIDs() []string
Expand Down Expand Up @@ -103,7 +103,6 @@ func (p *panel) Print(userID string) {
continue
}
sas = append(sas, sa)

}
postID, err := p.poster.DMWithAttachments(userID, sas...)
if err != nil {
Expand All @@ -117,7 +116,7 @@ func (p *panel) Print(userID string) {
}
}

func (p *panel) GetUpdatePost(userID string) (*model.Post, error) {
func (p *panel) ToPost(userID string) (*model.Post, error) {
post := &model.Post{}

sas := []*model.SlackAttachment{}
Expand Down

0 comments on commit ffca8e1

Please sign in to comment.