Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rawLocalTimestamp to new interface #1924

Merged
merged 8 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions libraries/botbuilder/src/activityValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @module botbuilder
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import { Activity, ActivityTimestamps } from 'botbuilder-core';

export function validateActivity(activity: Activity): Activity {
if (typeof activity !== 'object') { throw new Error(`validateActivity(): invalid request body.`); }
if (typeof activity.type !== 'string') { throw new Error(`validateActivity(): missing activity type.`); }
if (typeof activity.timestamp === 'string') {
(activity as ActivityTimestamps).rawTimestamp = activity.timestamp;
EricDahlvang marked this conversation as resolved.
Show resolved Hide resolved
activity.timestamp = new Date(activity.timestamp);
}
if (typeof activity.expiration === 'string') {
(activity as ActivityTimestamps).rawExpiration = activity.expiration;
activity.expiration = new Date(activity.expiration);
}
if (typeof activity.localTimestamp === 'string') {
(activity as ActivityTimestamps).rawLocalTimestamp = activity.localTimestamp;
activity.localTimestamp = new Date(activity.localTimestamp);
}
return activity;
};
17 changes: 6 additions & 11 deletions libraries/botbuilder/src/botFrameworkAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { INodeBuffer, INodeSocket, IReceiveRequest, ISocket, IStreamingTransport
import { WebRequest, WebResponse } from './interfaces';
import { defaultPipeName, GET, POST, MESSAGES_PATH, StreamingHttpClient, TokenResolver, VERSION_PATH } from './streaming';

import { validateActivity } from './activityValidator';

/**
* Contains settings used to configure a [BotFrameworkAdapter](xref:botbuilder.BotFrameworkAdapter) instance.
*/
Expand Down Expand Up @@ -1441,18 +1443,10 @@ export class BotFrameworkAdapter extends BotAdapter implements ExtendedUserToken
*/
function parseRequest(req: WebRequest): Promise<Activity> {
return new Promise((resolve: any, reject: any): void => {
function returnActivity(activity: Activity): void {
if (typeof activity !== 'object') { throw new Error(`BotFrameworkAdapter.parseRequest(): invalid request body.`); }
if (typeof activity.type !== 'string') { throw new Error(`BotFrameworkAdapter.parseRequest(): missing activity type.`); }
if (typeof activity.timestamp === 'string') { activity.timestamp = new Date(activity.timestamp); }
if (typeof activity.localTimestamp === 'string') { activity.localTimestamp = new Date(activity.localTimestamp); }
if (typeof activity.expiration === 'string') { activity.expiration = new Date(activity.expiration); }
resolve(activity);
}

if (req.body) {
try {
returnActivity(req.body);
const activity = validateActivity(req.body);
resolve(activity);
} catch (err) {
reject(err);
}
Expand All @@ -1464,7 +1458,8 @@ function parseRequest(req: WebRequest): Promise<Activity> {
req.on('end', (): void => {
try {
req.body = JSON.parse(requestData);
returnActivity(req.body);
const activity = validateActivity(req.body);
resolve(activity);
} catch (err) {
reject(err);
}
Expand Down
17 changes: 6 additions & 11 deletions libraries/botbuilder/src/channelServiceRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { WebRequest, WebResponse } from './interfaces';

export type RouteHandler = (request: WebRequest, response: WebResponse) => void;

import { validateActivity } from './activityValidator';

/**
* Interface representing an Express Application or a Restify Server.
*/
Expand Down Expand Up @@ -240,18 +242,10 @@ export class ChannelServiceRoutes {

private static readActivity(req: WebRequest): Promise<Activity> {
return new Promise((resolve, reject) => {
function returnActivity(activity) {
if (typeof activity !== 'object') { throw new Error(`Invalid request body.`); }
if (typeof activity.type !== 'string') { throw new Error(`Missing activity type.`); }
if (typeof activity.timestamp === 'string') { activity.timestamp = new Date(activity.timestamp); }
if (typeof activity.localTimestamp === 'string') { activity.localTimestamp = new Date(activity.localTimestamp); }
if (typeof activity.expiration === 'string') { activity.expiration = new Date(activity.expiration); }
resolve(activity);
}

if (req.body) {
try {
returnActivity(req.body);
const activity = validateActivity(req.body);
resolve(activity);
} catch (err) {
reject(new StatusCodeError(StatusCodes.BAD_REQUEST, err.message));
}
Expand All @@ -263,7 +257,8 @@ export class ChannelServiceRoutes {
req.on('end', () => {
try {
const body = JSON.parse(requestData);
returnActivity(body);
const activity = validateActivity(body);
resolve(activity);
} catch (err) {
reject(new StatusCodeError(StatusCodes.BAD_REQUEST, err.message));
}
Expand Down
38 changes: 38 additions & 0 deletions libraries/botbuilder/tests/activityValidator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const assert = require('assert');
const { validateActivity } = require('../lib/activityValidator');

describe(`activityValidator`, function() {
this.timeout(5000);

const timestamp = '2020-03-17T14:42:39.3692591-07:00';

it(`should preserve original localTimestamp in rawLocalTimestamp.`, () => {
const activity = validateActivity({ type: 'message', localTimestamp: timestamp });
assert.strictEqual(activity.rawLocalTimestamp, timestamp);
});

it(`should preserve original expiration in rawExpiration.`, () => {
const activity = validateActivity({ type: 'message', expiration: timestamp });
assert.strictEqual(activity.rawExpiration, timestamp);
});

it(`should preserve original timestamp in rawTimestamp.`, () => {
const activity = validateActivity({ type: 'message', timestamp: timestamp });
assert.strictEqual(activity.rawTimestamp, timestamp);
});

it(`should not fail when missing localTimestamp.`, () => {
const activity = validateActivity({ type: 'message' });
assert.strictEqual(typeof(activity.rawLocalTimestamp), 'undefined');
});

it(`should not fail when missing expiration.`, () => {
const activity = validateActivity({ type: 'message' });
assert.strictEqual(typeof(activity.rawExpiration), 'undefined');
});

it(`should not fail when missing timestamp.`, () => {
const activity = validateActivity({ type: 'message' });
assert.strictEqual(typeof(activity.rawTimestamp), 'undefined');
});
});
11 changes: 11 additions & 0 deletions libraries/botframework-schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,17 @@ export interface Activity {
semanticAction?: SemanticAction;
}

/**
* This interface is used to preserve the original string values of dates on Activities.
* When an Activity is received, timestamps are converted to Dates. Due to how Javascript
* Date objects are UTC, timezone offset values are lost.
*/
export interface ActivityTimestamps extends Activity {
rawTimestamp?: string;
rawExpiration?: string;
rawLocalTimestamp?: string;
}

/**
* Parameters for creating a new conversation
*/
Expand Down