Skip to content

Commit

Permalink
Fix panic when Daily Summary is nil for a user (#199)
Browse files Browse the repository at this point in the history
* Fix panic when Daily Summary is nil for a user

* Add default daily summary setting into a function
  • Loading branch information
larkox authored Dec 2, 2020
1 parent 50beb11 commit a488054
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
18 changes: 17 additions & 1 deletion server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (m *mscalendar) SetDailySummaryPostTime(user *User, timeStr string) (*store
return nil, err
}

if user.Settings.DailySummary == nil {
user.Settings.DailySummary = store.DefaultDailySummaryUserSettings()
}

dsum := user.Settings.DailySummary
dsum.PostTime = timeStr
dsum.Timezone = timezone
Expand All @@ -75,6 +79,10 @@ func (m *mscalendar) SetDailySummaryEnabled(user *User, enable bool) (*store.Dai
return nil, err
}

if user.Settings.DailySummary == nil {
user.Settings.DailySummary = store.DefaultDailySummaryUserSettings()
}

dsum := user.Settings.DailySummary
dsum.Enable = enable

Expand Down Expand Up @@ -110,6 +118,10 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
byRemoteID[storeUser.Remote.ID] = storeUser

dsum := storeUser.Settings.DailySummary
if dsum == nil {
continue
}

shouldPost, shouldPostErr := shouldPostDailySummary(dsum, now)
if shouldPostErr != nil {
m.Logger.Warnf("Error posting daily summary for user %s. err=%v", user.MattermostUserID, shouldPostErr)
Expand Down Expand Up @@ -139,6 +151,10 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
m.Logger.Warnf("Error rendering user %s calendar. err=%s %s", user.MattermostUserID, res.Error.Code, res.Error.Message)
}
dsum := user.Settings.DailySummary
if dsum == nil {
// Should never reach this point
continue
}
postStr, err := views.RenderCalendarView(res.Events, dsum.Timezone)
if err != nil {
m.Logger.Warnf("Error rendering user %s calendar. err=%v", user.MattermostUserID, err)
Expand Down Expand Up @@ -172,7 +188,7 @@ func (m *mscalendar) GetDailySummaryForUser(user *User) (string, error) {
}

func shouldPostDailySummary(dsum *store.DailySummaryUserSettings, now time.Time) (bool, error) {
if !dsum.Enable {
if dsum == nil || !dsum.Enable {
return false, nil
}

Expand Down
11 changes: 11 additions & 0 deletions server/store/setting_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,18 @@ func (s *pluginStore) GetSetting(userID, settingID string) (interface{}, error)
}
}

func DefaultDailySummaryUserSettings() *DailySummaryUserSettings {
return &DailySummaryUserSettings{
PostTime: "8:00AM",
Timezone: "Eastern Standard Time",
Enable: false,
}
}
func (s *pluginStore) updateDailySummarySettingForUser(user *User, value interface{}) error {
if user.Settings.DailySummary == nil {
user.Settings.DailySummary = DefaultDailySummaryUserSettings()
}

dsum := user.Settings.DailySummary

stringValue := value.(string)
Expand Down

0 comments on commit a488054

Please sign in to comment.