Skip to content

Commit

Permalink
installation update add and remove (#2687)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnataylor committed Aug 12, 2020
1 parent 14636d9 commit 339b6ec
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 1 deletion.
88 changes: 87 additions & 1 deletion libraries/botbuilder-core/src/activityHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,36 @@ export class ActivityHandler extends ActivityHandlerBase {
return this.on('InstallationUpdate', handler);
}

/**
* Registers an activity event handler for the _installationupdate add_ activity.
*
* @param handler The event handler.
*
* @remarks
* Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
*
* To handle a InstallationUpdateAdd event, use the
* [onInstallationUpdateAdd](xref:botbuilder-core.ActivityHandler.onInstallationUpdateAdd) type-specific event handler.
*/
public onInstallationUpdateAdd(handler: BotHandler): this {
return this.on('InstallationUpdateAdd', handler);
}

/**
* Registers an activity event handler for the _installationupdate remove_ activity.
*
* @param handler The event handler.
*
* @remarks
* Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
*
* To handle a InstallationUpdateRemove event, use the
* [onInstallationUpdateRemove](xref:botbuilder-core.ActivityHandler.onInstallationUpdateRemove) type-specific event handler.
*/
public onInstallationUpdateRemove(handler: BotHandler): this {
return this.on('InstallationUpdateRemove', handler);
}

/**
* Registers an activity event handler for the _tokens-response_ event, emitted for any incoming
* `tokens/response` event activity. These are generated as part of the OAuth authentication flow.
Expand Down Expand Up @@ -526,9 +556,65 @@ export class ActivityHandler extends ActivityHandlerBase {
* and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
*/
protected async onInstallationUpdateActivity(context: TurnContext): Promise<void> {
await this.handle(context, 'InstallationUpdate', this.defaultNextEvent(context));
await this.handle(context, 'InstallationUpdate', async () => {
await this.dispatchInstallationUpdateActivity(context);
});
}

/**
* Runs the _installation update_ sub-type handlers, as appropriate, and then continues the event emission process.
*
* @param context The context object for the current turn.
*
* @remarks
* Overwrite this method to support channel-specific behavior across multiple channels or to add
* custom conversation update sub-type events.
*
* The default logic is:
* - If any members were added, call handlers registered via [onMembersAdded](xref:botbuilder-core.ActivityHandler.onMembersAdded).
* - If any members were removed, call handlers registered via [onMembersRemoved](xref:botbuilder-core.ActivityHandler.onMembersRemoved).
* - Continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
*/
protected async dispatchInstallationUpdateActivity(context: TurnContext): Promise<void> {
if (context.activity.action == 'add' || context.activity.action == 'remove') {
await super.onInstallationUpdateActivity(context);
} else {
await this.defaultNextEvent(context)();
}
}

/**
* Runs all registered _installation update add_ handlers and then continues the event emission process.
*
* @param context The context object for the current turn.
*
* @remarks
* Overwrite this method to support channel-specific behavior across multiple channels.
*
* The default logic is to call any handlers registered via
* [onInstallationUpdateAdd](xref:botbuilder-core.ActivityHandler.onInstallationUpdateAdd),
* and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
*/
protected async onInstallationUpdateAddActivity(context: TurnContext): Promise<void> {
await this.handle(context, 'InstallationUpdateAdd', this.defaultNextEvent(context));
}

/**
* Runs all registered _installation update remove_ handlers and then continues the event emission process.
*
* @param context The context object for the current turn.
*
* @remarks
* Overwrite this method to support channel-specific behavior across multiple channels.
*
* The default logic is to call any handlers registered via
* [onInstallationUpdateRemove](xref:botbuilder-core.ActivityHandler.onInstallationUpdateRemove),
* and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
*/
protected async onInstallationUpdateRemoveActivity(context: TurnContext): Promise<void> {
await this.handle(context, 'InstallationUpdateRemove', this.defaultNextEvent(context));
}

/**
* Runs all registered _unrecognized activity type_ handlers and then continues the event emission process.
*
Expand Down
33 changes: 33 additions & 0 deletions libraries/botbuilder-core/src/activityHandlerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,39 @@ export class ActivityHandlerBase {
* emission process.
*/
protected async onInstallationUpdateActivity(context: TurnContext): Promise<void> {
switch (context.activity.action) {
case 'add':
await this.onInstallationUpdateAddActivity(context);
return;
case 'remove':
await this.onInstallationUpdateRemoveActivity(context);
return
}
}

/**
* Provides a hook for emitting the _installationupdateadd_ event.
*
* @param context The context object for the current turn.
*
* @remarks
* Overwrite this method to run registered _installationupdateadd_ handlers and then continue the event
* emission process.
*/
protected async onInstallationUpdateAddActivity(context: TurnContext): Promise<void> {
return;
}

/**
* Provides a hook for emitting the _installationupdateremove_ event.
*
* @param context The context object for the current turn.
*
* @remarks
* Overwrite this method to run registered _installationupdateremove_ handlers and then continue the event
* emission process.
*/
protected async onInstallationUpdateRemoveActivity(context: TurnContext): Promise<void> {
return;
}

Expand Down
26 changes: 26 additions & 0 deletions libraries/botbuilder-core/tests/ActivityHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,32 @@ describe('ActivityHandler', function() {
processActivity({type: ActivityTypes.InstallationUpdate}, bot, done);
});

it(`should fire onInstallationUpdateAdd`, async function(done) {

const bot = new ActivityHandler();

bot.onInstallationUpdateAdd(async (context, next) => {
assert(true, 'onInstallationUpdateAdd not called');
done();
await next();
});

processActivity({type: ActivityTypes.InstallationUpdate, action: 'add'}, bot, done);
});

it(`should fire onInstallationUpdateRemove`, async function(done) {

const bot = new ActivityHandler();

bot.onInstallationUpdateRemove(async (context, next) => {
assert(true, 'onInstallationUpdateRemove not called');
done();
await next();
});

processActivity({type: ActivityTypes.InstallationUpdate, action: 'remove'}, bot, done);
});

it(`should fire onUnrecognizedActivityType`, async function(done) {

const bot = new ActivityHandler();
Expand Down

0 comments on commit 339b6ec

Please sign in to comment.