Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
[Calendar] add auth steps before call api (#2382)
Browse files Browse the repository at this point in the history
* add auth steps before call api

* fix merge issue

* fix test
  • Loading branch information
KayMKM authored and darrenj committed Oct 21, 2019
1 parent 30e106d commit 908f137
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 38 deletions.
61 changes: 45 additions & 16 deletions skills/csharp/calendarskill/Dialogs/ChangeEventStatusDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public ChangeEventStatusDialog(
AfterGetAuthToken,
CheckFocusedEvent,
ConfirmBeforeAction,
AfterConfirmBeforeAction,
GetAuthToken,
AfterGetAuthToken,
ChangeEventStatus
};

Expand Down Expand Up @@ -102,40 +105,66 @@ public ChangeEventStatusDialog(
}
}

private async Task<DialogTurnResult> ChangeEventStatus(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
private async Task<DialogTurnResult> AfterConfirmBeforeAction(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
var state = await Accessor.GetAsync(sc.Context);
var options = (ChangeEventStatusDialogOptions)sc.Options;

var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
var confirmResult = (bool)sc.Result;
if (confirmResult)
{
var deleteEvent = state.ShowMeetingInfor.FocusedEvents[0];
if (options.NewEventStatus == EventStatus.Cancelled)
return await sc.NextAsync();
}
else
{
if (options.SubFlowMode)
{
if (deleteEvent.IsOrganizer)
{
await calendarService.DeleteEventByIdAsync(deleteEvent.Id);
}
else
{
await calendarService.DeclineEventByIdAsync(deleteEvent.Id);
}
state.MeetingInfor.ClearTimes();
state.MeetingInfor.ClearTitle();
}
else
{
state.Clear();
}

await sc.Context.SendActivityAsync(ResponseManager.GetResponse(ChangeEventStatusResponses.EventDeleted));
return await sc.EndDialogAsync(true);
}
}
catch (Exception ex)
{
await HandleDialogExceptions(sc, ex);
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
}
}

private async Task<DialogTurnResult> ChangeEventStatus(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
var state = await Accessor.GetAsync(sc.Context);
var options = (ChangeEventStatusDialogOptions)sc.Options;

var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
var deleteEvent = state.ShowMeetingInfor.FocusedEvents[0];
if (options.NewEventStatus == EventStatus.Cancelled)
{
if (deleteEvent.IsOrganizer)
{
await calendarService.DeleteEventByIdAsync(deleteEvent.Id);
}
else
{
await calendarService.AcceptEventByIdAsync(deleteEvent.Id);
await sc.Context.SendActivityAsync(ResponseManager.GetResponse(ChangeEventStatusResponses.EventAccepted));
await calendarService.DeclineEventByIdAsync(deleteEvent.Id);
}

await sc.Context.SendActivityAsync(ResponseManager.GetResponse(ChangeEventStatusResponses.EventDeleted));
}
else
{
await sc.Context.SendActivityAsync(ResponseManager.GetResponse(CalendarSharedResponses.ActionEnded));
await calendarService.AcceptEventByIdAsync(deleteEvent.Id);
await sc.Context.SendActivityAsync(ResponseManager.GetResponse(ChangeEventStatusResponses.EventAccepted));
}

if (options.SubFlowMode)
Expand Down
2 changes: 2 additions & 0 deletions skills/csharp/calendarskill/Dialogs/CreateEventDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public CreateEventDialog(
ShowEventInfo,
ConfirmBeforeCreatePrompt,
AfterConfirmBeforeCreatePrompt,
GetAuthToken,
AfterGetAuthToken,
CreateEvent,
};

Expand Down
23 changes: 21 additions & 2 deletions skills/csharp/calendarskill/Dialogs/FindContactDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public FindContactDialog(
// if got multiple persons, call selectPerson. use replace
// if got no person, replace/restart this flow.
AfterUpdateUserName,
GetAuthToken,
AfterGetAuthToken,
GetUserFromUserName
};

// select person, called bt updateName with replace.
Expand Down Expand Up @@ -451,8 +454,24 @@ public FindContactDialog(
return await sc.EndDialogAsync();
}

var currentRecipientName = string.IsNullOrEmpty(userInput) ? state.MeetingInfor.ContactInfor.CurrentContactName : userInput;
state.MeetingInfor.ContactInfor.CurrentContactName = currentRecipientName;
state.MeetingInfor.ContactInfor.CurrentContactName = string.IsNullOrEmpty(userInput) ? state.MeetingInfor.ContactInfor.CurrentContactName : userInput;

return await sc.NextAsync();
}
catch (Exception ex)
{
await HandleDialogExceptions(sc, ex);
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
}
}

private async Task<DialogTurnResult> GetUserFromUserName(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
var state = await Accessor.GetAsync(sc.Context);
var options = (FindContactDialogOptions)sc.Options;
var currentRecipientName = state.MeetingInfor.ContactInfor.CurrentContactName;

// if it's an email, add to attendee and kepp the state.MeetingInfor.ContactInfor.ConfirmedContact null
if (!string.IsNullOrEmpty(currentRecipientName) && IsEmail(currentRecipientName))
Expand Down
67 changes: 48 additions & 19 deletions skills/csharp/calendarskill/Dialogs/UpdateEventDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public UpdateEventDialog(
CheckFocusedEvent,
GetNewEventTime,
ConfirmBeforeUpdate,
AfterConfirmBeforeUpdate,
GetAuthToken,
AfterGetAuthToken,
UpdateEventTime
};

Expand Down Expand Up @@ -152,7 +155,7 @@ public UpdateEventDialog(
}
}

private async Task<DialogTurnResult> UpdateEventTime(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
private async Task<DialogTurnResult> AfterConfirmBeforeUpdate(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
Expand All @@ -161,32 +164,58 @@ public UpdateEventDialog(
var confirmResult = (bool)sc.Result;
if (confirmResult)
{
var newStartTime = (DateTime)state.UpdateMeetingInfor.NewStartDateTime;
var origin = state.ShowMeetingInfor.FocusedEvents[0];
var updateEvent = new EventModel(origin.Source);
var last = origin.EndTime - origin.StartTime;
updateEvent.StartTime = newStartTime;
updateEvent.EndTime = (newStartTime + last).AddSeconds(1);
updateEvent.TimeZone = TimeZoneInfo.Utc;
updateEvent.Id = origin.Id;

if (!string.IsNullOrEmpty(state.UpdateMeetingInfor.RecurrencePattern) && !string.IsNullOrEmpty(origin.RecurringId))
return await sc.NextAsync();
}
else
{
await sc.Context.SendActivityAsync(ResponseManager.GetResponse(CalendarSharedResponses.ActionEnded));
if (options.SubFlowMode)
{
state.UpdateMeetingInfor.Clear();
}
else
{
updateEvent.Id = origin.RecurringId;
state.Clear();
}

var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
var newEvent = await calendarService.UpdateEventByIdAsync(updateEvent);
return await sc.EndDialogAsync(true);
}
}
catch (Exception ex)
{
await HandleDialogExceptions(sc, ex);
return new DialogTurnResult(DialogTurnStatus.Cancelled, CommonUtil.DialogTurnResultCancelAllDialogs);
}
}

var replyMessage = await GetDetailMeetingResponseAsync(sc, newEvent, UpdateEventResponses.EventUpdated);
private async Task<DialogTurnResult> UpdateEventTime(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
var state = await Accessor.GetAsync(sc.Context);
var options = (CalendarSkillDialogOptions)sc.Options;

await sc.Context.SendActivityAsync(replyMessage);
}
else
var newStartTime = (DateTime)state.UpdateMeetingInfor.NewStartDateTime;
var origin = state.ShowMeetingInfor.FocusedEvents[0];
var updateEvent = new EventModel(origin.Source);
var last = origin.EndTime - origin.StartTime;
updateEvent.StartTime = newStartTime;
updateEvent.EndTime = (newStartTime + last).AddSeconds(1);
updateEvent.TimeZone = TimeZoneInfo.Utc;
updateEvent.Id = origin.Id;

if (!string.IsNullOrEmpty(state.UpdateMeetingInfor.RecurrencePattern) && !string.IsNullOrEmpty(origin.RecurringId))
{
await sc.Context.SendActivityAsync(ResponseManager.GetResponse(CalendarSharedResponses.ActionEnded));
updateEvent.Id = origin.RecurringId;
}

var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
var newEvent = await calendarService.UpdateEventByIdAsync(updateEvent);

var replyMessage = await GetDetailMeetingResponseAsync(sc, newEvent, UpdateEventResponses.EventUpdated);

await sc.Context.SendActivityAsync(replyMessage);

if (options.SubFlowMode)
{
state.UpdateMeetingInfor.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ await this.GetTestFlow()
.Send(this.GetAuthResponse())
.AssertReply(this.ShowCalendarList())
.Send(Strings.Strings.ConfirmYes)
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReplyOneOf(this.DeleteEventPrompt())
.AssertReply(this.ActionEndMessage())
.StartTestAsync();
Expand All @@ -54,6 +56,8 @@ await this.GetTestFlow()
.Send(this.GetAuthResponse())
.AssertReply(this.ShowCalendarList())
.Send(Strings.Strings.ConfirmYes)
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReplyOneOf(this.DeleteEventPrompt())
.AssertReply(this.ActionEndMessage())
.StartTestAsync();
Expand All @@ -72,6 +76,8 @@ await this.GetTestFlow()
.Send(GeneralTestUtterances.ChooseOne)
.AssertReply(this.ShowCalendarList())
.Send(Strings.Strings.ConfirmYes)
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReplyOneOf(this.DeleteEventPrompt())
.AssertReply(this.ActionEndMessage())
.StartTestAsync();
Expand All @@ -86,6 +92,8 @@ await this.GetTestFlow()
.Send(this.GetAuthResponse())
.AssertReply(this.ShowCalendarList())
.Send(Strings.Strings.ConfirmYes)
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReplyOneOf(this.AcceptEventPrompt())
.AssertReply(this.ActionEndMessage())
.StartTestAsync();
Expand Down
Loading

0 comments on commit 908f137

Please sign in to comment.