Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
Postback when other user completes reminder (#147)
Browse files Browse the repository at this point in the history
* original user callback on complete logic

* post back to original user for complete reminder.  Fixes Issue #139
  • Loading branch information
scottleedavis authored Aug 6, 2019
1 parent 8e5c3c8 commit 30d69dc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
4 changes: 4 additions & 0 deletions assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}}\"."
Expand Down
49 changes: 39 additions & 10 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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())
Expand Down Expand Up @@ -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{})
}

Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,23 @@ func TestHandleComplete(t *testing.T) {
},
}
stringReminders, _ := json.Marshal(reminders)
channel := &model.Channel{
Id: model.NewId(),
}

setupAPI := func() *plugintest.API {
api := &plugintest.API{}
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("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
}
Expand All @@ -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(),
},
Expand Down Expand Up @@ -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(),
},
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion server/reminder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 30d69dc

Please sign in to comment.