diff --git a/assets/i18n/en.json b/assets/i18n/en.json index 7bd1a34..70be513 100644 --- a/assets/i18n/en.json +++ b/assets/i18n/en.json @@ -103,6 +103,10 @@ "id": "action.complete", "translation": "I've marked \"{{.Message}}\" as complete." }, + { + "id": "action.complete.callback", + "translation": "{{.User}} marked their reminder \"{{.Message}}\" as complete." + }, { "id": "action.delete", "translation": "I’ve deleted the reminder \"{{.Message}}\"." diff --git a/server/http.go b/server/http.go index ee5a325..776a70e 100755 --- a/server/http.go +++ b/server/http.go @@ -188,10 +188,12 @@ func (p *Plugin) handleComplete(w http.ResponseWriter, r *http.Request) { request := model.PostActionIntegrationRequestFromJson(r.Body) - reminder := p.GetReminder(request.UserId, request.Context["reminder_id"].(string)) + reminder := p.GetReminder(request.Context["orig_user_id"].(string), request.Context["reminder_id"].(string)) user, uErr := p.API.GetUser(request.UserId) if uErr != nil { p.API.LogError(uErr.Error()) + writePostActionIntegrationResponseError(w, &model.PostActionIntegrationResponse{}) + return } T, _ := p.translation(user) @@ -200,7 +202,7 @@ func (p *Plugin) handleComplete(w http.ResponseWriter, r *http.Request) { } reminder.Completed = time.Now().UTC() - p.UpdateReminder(request.UserId, reminder) + p.UpdateReminder(request.Context["orig_user_id"].(string), reminder) if post, pErr := p.API.GetPost(request.PostId); pErr != nil { p.API.LogError("unable to get post " + pErr.Error()) @@ -231,6 +233,33 @@ func (p *Plugin) handleComplete(w http.ResponseWriter, r *http.Request) { post.Message = "~~" + T("reminder.message", messageParameters) + "~~\n" + T("action.complete", updateParameters) post.Props = model.StringInterface{} p.API.UpdatePost(post) + + if reminder.Username != user.Username { + if originalUser, uErr := p.API.GetUserByUsername(reminder.Username); uErr != nil { + p.API.LogError(uErr.Error()) + writePostActionIntegrationResponseError(w, &model.PostActionIntegrationResponse{}) + return + } else { + if channel, cErr := p.API.GetDirectChannel(p.remindUserId, originalUser.Id); cErr != nil { + p.API.LogError("failed to create channel " + cErr.Error()) + writePostActionIntegrationResponseError(w, &model.PostActionIntegrationResponse{}) + } else { + var postbackUpdateParameters = map[string]interface{}{ + "User": "@" + user.Username, + "Message": reminder.Message, + } + if _, pErr := p.API.CreatePost(&model.Post{ + ChannelId: channel.Id, + UserId: p.remindUserId, + Message: T("action.complete.callback", postbackUpdateParameters), + }); pErr != nil { + p.API.LogError(pErr.Error()) + writePostActionIntegrationResponseError(w, &model.PostActionIntegrationResponse{}) + } + } + } + } + writePostActionIntegrationResponseOk(w, &model.PostActionIntegrationResponse{}) } @@ -240,7 +269,7 @@ func (p *Plugin) handleDelete(w http.ResponseWriter, r *http.Request) { request := model.PostActionIntegrationRequestFromJson(r.Body) - reminder := p.GetReminder(request.UserId, request.Context["reminder_id"].(string)) + reminder := p.GetReminder(request.Context["orig_user_id"].(string), request.Context["reminder_id"].(string)) user, uErr := p.API.GetUser(request.UserId) if uErr != nil { p.API.LogError(uErr.Error()) @@ -254,7 +283,7 @@ func (p *Plugin) handleDelete(w http.ResponseWriter, r *http.Request) { } message := reminder.Message - p.DeleteReminder(request.UserId, reminder) + p.DeleteReminder(request.Context["orig_user_id"].(string), reminder) if post, pErr := p.API.GetPost(request.PostId); pErr != nil { p.API.LogError(pErr.Error()) @@ -308,7 +337,7 @@ func (p *Plugin) handleSnooze(w http.ResponseWriter, r *http.Request) { request := model.PostActionIntegrationRequestFromJson(r.Body) - reminder := p.GetReminder(request.UserId, request.Context["reminder_id"].(string)) + reminder := p.GetReminder(request.Context["orig_user_id"].(string), request.Context["reminder_id"].(string)) user, uErr := p.API.GetUser(request.UserId) if uErr != nil { p.API.LogError(uErr.Error()) @@ -337,7 +366,7 @@ func (p *Plugin) handleSnooze(w http.ResponseWriter, r *http.Request) { 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.UpdateReminder(request.Context["orig_user_id"].(string), reminder) p.upsertSnoozedOccurrence(&occurrence) post.Message = T("action.snooze.20min", snoozeParameters) break @@ -348,7 +377,7 @@ func (p *Plugin) handleSnooze(w http.ResponseWriter, r *http.Request) { 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.UpdateReminder(request.Context["orig_user_id"].(string), reminder) p.upsertSnoozedOccurrence(&occurrence) post.Message = T("action.snooze.1hr", snoozeParameters) break @@ -359,7 +388,7 @@ func (p *Plugin) handleSnooze(w http.ResponseWriter, r *http.Request) { 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.UpdateReminder(request.Context["orig_user_id"].(string), reminder) p.upsertSnoozedOccurrence(&occurrence) post.Message = T("action.snooze.3hr", snoozeParameters) break @@ -377,7 +406,7 @@ func (p *Plugin) handleSnooze(w http.ResponseWriter, r *http.Request) { 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.UpdateReminder(request.Context["orig_user_id"].(string), reminder) p.upsertSnoozedOccurrence(&occurrence) post.Message = T("action.snooze.tomorrow", snoozeParameters) break @@ -407,7 +436,7 @@ func (p *Plugin) handleSnooze(w http.ResponseWriter, r *http.Request) { 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.UpdateReminder(request.Context["orig_user_id"].(string), reminder) p.upsertSnoozedOccurrence(&occurrence) post.Message = T("action.snooze.nextweek", snoozeParameters) break diff --git a/server/http_test.go b/server/http_test.go index c710cf6..56c8daf 100755 --- a/server/http_test.go +++ b/server/http_test.go @@ -174,6 +174,9 @@ func TestHandleComplete(t *testing.T) { }, } stringReminders, _ := json.Marshal(reminders) + channel := &model.Channel{ + Id: model.NewId(), + } setupAPI := func() *plugintest.API { api := &plugintest.API{} @@ -181,11 +184,13 @@ func TestHandleComplete(t *testing.T) { 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("CreatePost", mock.Anything).Return(post, nil) api.On("UpdatePost", 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) api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + api.On("GetDirectChannel", mock.Anything, mock.Anything).Return(channel, nil) return api } @@ -204,6 +209,7 @@ func TestHandleComplete(t *testing.T) { UserId: "userID1", PostId: "postID1", Context: model.StringInterface{ + "orig_user_id": "foobar", "reminder_id": model.NewId(), "occurrence_id": model.NewId(), }, @@ -286,6 +292,7 @@ func TestHandleDelete(t *testing.T) { UserId: "userID1", PostId: "postID1", Context: model.StringInterface{ + "orig_user_id": "foobar", "reminder_id": model.NewId(), "occurrence_id": model.NewId(), }, @@ -467,6 +474,7 @@ func TestHandleSnooze(t *testing.T) { UserId: "userID1", PostId: "postID1", Context: model.StringInterface{ + "orig_user_id": "foobar", "reminder_id": model.NewId(), "occurrence_id": model.NewId(), "selected_option": test.SnoozeTime, diff --git a/server/reminder.go b/server/reminder.go index 1f8424c..351d255 100755 --- a/server/reminder.go +++ b/server/reminder.go @@ -141,6 +141,7 @@ func (p *Plugin) TriggerReminders() { Id: model.NewId(), Integration: &model.PostActionIntegration{ Context: model.StringInterface{ + "orig_user_id": user.Id, "reminder_id": reminder.Id, "occurrence_id": occurrence.Id, "action": "complete", @@ -153,6 +154,7 @@ func (p *Plugin) TriggerReminders() { { Integration: &model.PostActionIntegration{ Context: model.StringInterface{ + "orig_user_id": user.Id, "reminder_id": reminder.Id, "occurrence_id": occurrence.Id, "action": "delete", @@ -165,6 +167,7 @@ func (p *Plugin) TriggerReminders() { { Integration: &model.PostActionIntegration{ Context: model.StringInterface{ + "orig_user_id": user.Id, "reminder_id": reminder.Id, "occurrence_id": occurrence.Id, "action": "snooze", @@ -341,7 +344,7 @@ func (p *Plugin) GetReminders(username string) []Reminder { err := json.Unmarshal(bytes, &reminders) if err != nil { - p.API.LogError("new reminder " + user.Username) + p.API.LogError("no reminders for " + user.Username) } return reminders }