Skip to content

Commit 339b6ec

Browse files
authored
installation update add and remove (#2687)
1 parent 14636d9 commit 339b6ec

File tree

3 files changed

+146
-1
lines changed

3 files changed

+146
-1
lines changed

libraries/botbuilder-core/src/activityHandler.ts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,36 @@ export class ActivityHandler extends ActivityHandlerBase {
302302
return this.on('InstallationUpdate', handler);
303303
}
304304

305+
/**
306+
* Registers an activity event handler for the _installationupdate add_ activity.
307+
*
308+
* @param handler The event handler.
309+
*
310+
* @remarks
311+
* Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
312+
*
313+
* To handle a InstallationUpdateAdd event, use the
314+
* [onInstallationUpdateAdd](xref:botbuilder-core.ActivityHandler.onInstallationUpdateAdd) type-specific event handler.
315+
*/
316+
public onInstallationUpdateAdd(handler: BotHandler): this {
317+
return this.on('InstallationUpdateAdd', handler);
318+
}
319+
320+
/**
321+
* Registers an activity event handler for the _installationupdate remove_ activity.
322+
*
323+
* @param handler The event handler.
324+
*
325+
* @remarks
326+
* Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
327+
*
328+
* To handle a InstallationUpdateRemove event, use the
329+
* [onInstallationUpdateRemove](xref:botbuilder-core.ActivityHandler.onInstallationUpdateRemove) type-specific event handler.
330+
*/
331+
public onInstallationUpdateRemove(handler: BotHandler): this {
332+
return this.on('InstallationUpdateRemove', handler);
333+
}
334+
305335
/**
306336
* Registers an activity event handler for the _tokens-response_ event, emitted for any incoming
307337
* `tokens/response` event activity. These are generated as part of the OAuth authentication flow.
@@ -526,9 +556,65 @@ export class ActivityHandler extends ActivityHandlerBase {
526556
* and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
527557
*/
528558
protected async onInstallationUpdateActivity(context: TurnContext): Promise<void> {
529-
await this.handle(context, 'InstallationUpdate', this.defaultNextEvent(context));
559+
await this.handle(context, 'InstallationUpdate', async () => {
560+
await this.dispatchInstallationUpdateActivity(context);
561+
});
530562
}
531563

564+
/**
565+
* Runs the _installation update_ sub-type handlers, as appropriate, and then continues the event emission process.
566+
*
567+
* @param context The context object for the current turn.
568+
*
569+
* @remarks
570+
* Overwrite this method to support channel-specific behavior across multiple channels or to add
571+
* custom conversation update sub-type events.
572+
*
573+
* The default logic is:
574+
* - If any members were added, call handlers registered via [onMembersAdded](xref:botbuilder-core.ActivityHandler.onMembersAdded).
575+
* - If any members were removed, call handlers registered via [onMembersRemoved](xref:botbuilder-core.ActivityHandler.onMembersRemoved).
576+
* - Continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
577+
*/
578+
protected async dispatchInstallationUpdateActivity(context: TurnContext): Promise<void> {
579+
if (context.activity.action == 'add' || context.activity.action == 'remove') {
580+
await super.onInstallationUpdateActivity(context);
581+
} else {
582+
await this.defaultNextEvent(context)();
583+
}
584+
}
585+
586+
/**
587+
* Runs all registered _installation update add_ handlers and then continues the event emission process.
588+
*
589+
* @param context The context object for the current turn.
590+
*
591+
* @remarks
592+
* Overwrite this method to support channel-specific behavior across multiple channels.
593+
*
594+
* The default logic is to call any handlers registered via
595+
* [onInstallationUpdateAdd](xref:botbuilder-core.ActivityHandler.onInstallationUpdateAdd),
596+
* and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
597+
*/
598+
protected async onInstallationUpdateAddActivity(context: TurnContext): Promise<void> {
599+
await this.handle(context, 'InstallationUpdateAdd', this.defaultNextEvent(context));
600+
}
601+
602+
/**
603+
* Runs all registered _installation update remove_ handlers and then continues the event emission process.
604+
*
605+
* @param context The context object for the current turn.
606+
*
607+
* @remarks
608+
* Overwrite this method to support channel-specific behavior across multiple channels.
609+
*
610+
* The default logic is to call any handlers registered via
611+
* [onInstallationUpdateRemove](xref:botbuilder-core.ActivityHandler.onInstallationUpdateRemove),
612+
* and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
613+
*/
614+
protected async onInstallationUpdateRemoveActivity(context: TurnContext): Promise<void> {
615+
await this.handle(context, 'InstallationUpdateRemove', this.defaultNextEvent(context));
616+
}
617+
532618
/**
533619
* Runs all registered _unrecognized activity type_ handlers and then continues the event emission process.
534620
*

libraries/botbuilder-core/src/activityHandlerBase.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,39 @@ export class ActivityHandlerBase {
213213
* emission process.
214214
*/
215215
protected async onInstallationUpdateActivity(context: TurnContext): Promise<void> {
216+
switch (context.activity.action) {
217+
case 'add':
218+
await this.onInstallationUpdateAddActivity(context);
219+
return;
220+
case 'remove':
221+
await this.onInstallationUpdateRemoveActivity(context);
222+
return
223+
}
224+
}
225+
226+
/**
227+
* Provides a hook for emitting the _installationupdateadd_ event.
228+
*
229+
* @param context The context object for the current turn.
230+
*
231+
* @remarks
232+
* Overwrite this method to run registered _installationupdateadd_ handlers and then continue the event
233+
* emission process.
234+
*/
235+
protected async onInstallationUpdateAddActivity(context: TurnContext): Promise<void> {
236+
return;
237+
}
238+
239+
/**
240+
* Provides a hook for emitting the _installationupdateremove_ event.
241+
*
242+
* @param context The context object for the current turn.
243+
*
244+
* @remarks
245+
* Overwrite this method to run registered _installationupdateremove_ handlers and then continue the event
246+
* emission process.
247+
*/
248+
protected async onInstallationUpdateRemoveActivity(context: TurnContext): Promise<void> {
216249
return;
217250
}
218251

libraries/botbuilder-core/tests/ActivityHandler.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,32 @@ describe('ActivityHandler', function() {
236236
processActivity({type: ActivityTypes.InstallationUpdate}, bot, done);
237237
});
238238

239+
it(`should fire onInstallationUpdateAdd`, async function(done) {
240+
241+
const bot = new ActivityHandler();
242+
243+
bot.onInstallationUpdateAdd(async (context, next) => {
244+
assert(true, 'onInstallationUpdateAdd not called');
245+
done();
246+
await next();
247+
});
248+
249+
processActivity({type: ActivityTypes.InstallationUpdate, action: 'add'}, bot, done);
250+
});
251+
252+
it(`should fire onInstallationUpdateRemove`, async function(done) {
253+
254+
const bot = new ActivityHandler();
255+
256+
bot.onInstallationUpdateRemove(async (context, next) => {
257+
assert(true, 'onInstallationUpdateRemove not called');
258+
done();
259+
await next();
260+
});
261+
262+
processActivity({type: ActivityTypes.InstallationUpdate, action: 'remove'}, bot, done);
263+
});
264+
239265
it(`should fire onUnrecognizedActivityType`, async function(done) {
240266

241267
const bot = new ActivityHandler();

0 commit comments

Comments
 (0)