From 468bbbcf74dec2fdd4a9b62313a5caf38dbc914a Mon Sep 17 00:00:00 2001 From: Scott Lee Davis Date: Mon, 19 Aug 2019 20:44:11 -0700 Subject: [PATCH] Snoozed reminders in list functioning again. Issue #154 --- server/http.go | 137 +++++++++++++++++++++----------------------- server/http_test.go | 1 - 2 files changed, 66 insertions(+), 72 deletions(-) diff --git a/server/http.go b/server/http.go index 584cf9b..8533643 100755 --- a/server/http.go +++ b/server/http.go @@ -513,93 +513,88 @@ func (p *Plugin) handleSnoozeList(w http.ResponseWriter, r *http.Request) { } } - if _, pErr := p.API.GetPost(request.PostId); pErr != nil { - p.API.LogError(pErr.Error()) - writePostActionIntegrationResponseError(w, &model.PostActionIntegrationResponse{}) - } else { - switch request.Context["selected_option"].(string) { - case "20min": - for i, occurrence := range reminder.Occurrences { - if occurrence.Id == request.Context["occurrence_id"].(string) { - occurrence.Snoozed = time.Now().UTC().Round(time.Second).Add(time.Minute * time.Duration(20)) - reminder.Occurrences[i] = occurrence - p.UpdateReminder(request.UserId, reminder) - p.upsertSnoozedOccurrence(&occurrence) - break - } + switch request.Context["selected_option"].(string) { + case "20min": + for i, occurrence := range reminder.Occurrences { + if occurrence.Id == request.Context["occurrence_id"].(string) { + occurrence.Snoozed = time.Now().UTC().Round(time.Second).Add(time.Minute * time.Duration(20)) + reminder.Occurrences[i] = occurrence + p.UpdateReminder(request.UserId, reminder) + p.upsertSnoozedOccurrence(&occurrence) + break } - case "1hr": - for i, occurrence := range reminder.Occurrences { - if occurrence.Id == request.Context["occurrence_id"].(string) { - occurrence.Snoozed = time.Now().UTC().Round(time.Second).Add(time.Hour * time.Duration(1)) - reminder.Occurrences[i] = occurrence - p.UpdateReminder(request.UserId, reminder) - p.upsertSnoozedOccurrence(&occurrence) - break - } + } + case "1hr": + for i, occurrence := range reminder.Occurrences { + if occurrence.Id == request.Context["occurrence_id"].(string) { + occurrence.Snoozed = time.Now().UTC().Round(time.Second).Add(time.Hour * time.Duration(1)) + reminder.Occurrences[i] = occurrence + p.UpdateReminder(request.UserId, reminder) + p.upsertSnoozedOccurrence(&occurrence) + break } - case "3hrs": - for i, occurrence := range reminder.Occurrences { - if occurrence.Id == request.Context["occurrence_id"].(string) { - occurrence.Snoozed = time.Now().UTC().Round(time.Second).Add(time.Hour * time.Duration(3)) + } + case "3hrs": + for i, occurrence := range reminder.Occurrences { + if occurrence.Id == request.Context["occurrence_id"].(string) { + occurrence.Snoozed = time.Now().UTC().Round(time.Second).Add(time.Hour * time.Duration(3)) + reminder.Occurrences[i] = occurrence + p.UpdateReminder(request.UserId, reminder) + p.upsertSnoozedOccurrence(&occurrence) + break + } + } + case "tomorrow": + for i, occurrence := range reminder.Occurrences { + if occurrence.Id == request.Context["occurrence_id"].(string) { + + if user, uErr := p.API.GetUser(request.UserId); uErr != nil { + p.API.LogError(uErr.Error()) + return + } else { + location := p.location(user) + tt := time.Now().In(location).Add(time.Hour * time.Duration(24)) + occurrence.Snoozed = time.Date(tt.Year(), tt.Month(), tt.Day(), 9, 0, 0, 0, location).UTC() reminder.Occurrences[i] = occurrence p.UpdateReminder(request.UserId, reminder) p.upsertSnoozedOccurrence(&occurrence) break } } - case "tomorrow": - for i, occurrence := range reminder.Occurrences { - if occurrence.Id == request.Context["occurrence_id"].(string) { - - if user, uErr := p.API.GetUser(request.UserId); uErr != nil { - p.API.LogError(uErr.Error()) - return - } else { - location := p.location(user) - tt := time.Now().In(location).Add(time.Hour * time.Duration(24)) - occurrence.Snoozed = time.Date(tt.Year(), tt.Month(), tt.Day(), 9, 0, 0, 0, location).UTC() - reminder.Occurrences[i] = occurrence - p.UpdateReminder(request.UserId, reminder) - p.upsertSnoozedOccurrence(&occurrence) - break - } - } - } - case "nextweek": - for i, occurrence := range reminder.Occurrences { - if occurrence.Id == request.Context["occurrence_id"].(string) { + } + case "nextweek": + for i, occurrence := range reminder.Occurrences { + if occurrence.Id == request.Context["occurrence_id"].(string) { - if user, uErr := p.API.GetUser(request.UserId); uErr != nil { - p.API.LogError(uErr.Error()) - return - } else { - location := p.location(user) + if user, uErr := p.API.GetUser(request.UserId); uErr != nil { + p.API.LogError(uErr.Error()) + return + } else { + location := p.location(user) - todayWeekDayNum := int(time.Now().In(location).Weekday()) - weekDayNum := 1 - day := 0 + todayWeekDayNum := int(time.Now().In(location).Weekday()) + weekDayNum := 1 + day := 0 - if weekDayNum < todayWeekDayNum { - day = 7 - (todayWeekDayNum - weekDayNum) - } else if weekDayNum >= todayWeekDayNum { - day = 7 + (weekDayNum - todayWeekDayNum) - } - - tt := time.Now().In(location).Add(time.Hour * time.Duration(24)) - occurrence.Snoozed = time.Date(tt.Year(), tt.Month(), tt.Day(), 9, 0, 0, 0, location).AddDate(0, 0, day).UTC() - reminder.Occurrences[i] = occurrence - p.UpdateReminder(request.UserId, reminder) - p.upsertSnoozedOccurrence(&occurrence) - break + if weekDayNum < todayWeekDayNum { + day = 7 - (todayWeekDayNum - weekDayNum) + } else if weekDayNum >= todayWeekDayNum { + day = 7 + (weekDayNum - todayWeekDayNum) } + + tt := time.Now().In(location).Add(time.Hour * time.Duration(24)) + occurrence.Snoozed = time.Date(tt.Year(), tt.Month(), tt.Day(), 9, 0, 0, 0, location).AddDate(0, 0, day).UTC() + reminder.Occurrences[i] = occurrence + p.UpdateReminder(request.UserId, reminder) + p.upsertSnoozedOccurrence(&occurrence) + break } } } - - p.UpdateListReminders(request.UserId, request.PostId, request.ChannelId, int(request.Context["offset"].(float64))) - writePostActionIntegrationResponseOk(w, &model.PostActionIntegrationResponse{}) } + + p.UpdateListReminders(request.UserId, request.PostId, request.ChannelId, int(request.Context["offset"].(float64))) + writePostActionIntegrationResponseOk(w, &model.PostActionIntegrationResponse{}) } func (p *Plugin) handleCloseList(w http.ResponseWriter, r *http.Request) { diff --git a/server/http_test.go b/server/http_test.go index 5b3cd93..d0d30bc 100755 --- a/server/http_test.go +++ b/server/http_test.go @@ -952,7 +952,6 @@ func TestHandleSnoozeList(t *testing.T) { api.On("LogDebug", mock.Anything, mock.Anything, mock.Anything).Maybe() api.On("LogError", mock.Anything, mock.Anything, mock.Anything).Maybe() api.On("LogInfo", mock.Anything).Maybe() - api.On("GetPost", mock.Anything).Return(post, nil) api.On("GetUser", mock.Anything).Return(user, nil) api.On("GetUserByUsername", mock.Anything).Return(user, nil) api.On("KVGet", user.Username).Return(stringReminders, nil)