Skip to content

Commit 06ea3c0

Browse files
author
Bill Barnes
committed
Size & style cleanups
1 parent 58b9c1b commit 06ea3c0

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

src/directLine.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import 'rxjs/add/operator/delay';
1313
import 'rxjs/add/operator/do';
1414
import 'rxjs/add/operator/filter';
1515
import 'rxjs/add/operator/map';
16-
import 'rxjs/add/operator/mapTo';
1716
import 'rxjs/add/operator/mergeMap';
1817
import 'rxjs/add/operator/retryWhen';
1918
import 'rxjs/add/operator/take';
@@ -41,8 +40,10 @@ export interface Media {
4140
name?: string
4241
}
4342

43+
export type CardActionTypes = "openUrl" | "imBack" | "postBack" | "playAudio" | "playVideo" | "showImage" | "downloadFile" | "signin" | "call";
44+
4445
export interface CardAction {
45-
type: "openUrl" | "imBack" | "postBack" | "playAudio" | "playVideo" | "showImage" | "downloadFile" | "signin" | "call",
46+
type: CardActionTypes,
4647
title: string,
4748
value: string,
4849
image?: string
@@ -296,7 +297,7 @@ export class DirectLine implements IBotConnection {
296297
}, error => {
297298
this.connectionStatus$.next(ConnectionStatus.FailedToConnect);
298299
})
299-
.mapTo(connectionStatus);
300+
.map(_ => connectionStatus);
300301
} else {
301302
return Observable.of(connectionStatus);
302303
}
@@ -321,6 +322,12 @@ export class DirectLine implements IBotConnection {
321322
return once ? obs.take(1) : obs;
322323
}
323324

325+
private expiredToken() {
326+
const connectionStatus = this.connectionStatus$.getValue();
327+
if (connectionStatus != ConnectionStatus.Ended && connectionStatus != ConnectionStatus.FailedToConnect)
328+
this.connectionStatus$.next(ConnectionStatus.ExpiredToken);
329+
}
330+
324331
private startConversation() {
325332
return Observable.ajax({
326333
method: "POST",
@@ -370,7 +377,7 @@ export class DirectLine implements IBotConnection {
370377
.mergeMap(error => {
371378
if (error.status === 403) {
372379
// if the token is expired there's no reason to keep trying
373-
this.connectionStatus$.next(ConnectionStatus.ExpiredToken);
380+
this.expiredToken();
374381
return Observable.throw(error);
375382
}
376383
return Observable.of(error);
@@ -422,9 +429,8 @@ export class DirectLine implements IBotConnection {
422429
.catch(error => this.catchExpiredToken(error));
423430
}
424431

425-
private postMessageWithAttachments(message: Message) {
432+
private postMessageWithAttachments({ attachments, ... messageWithoutAttachments }: Message) {
426433
let formData: FormData;
427-
const { attachments, ... newMessage } = message;
428434

429435
// If we're not connected to the bot, get connected
430436
// Will throw an error if we are not connected
@@ -433,7 +439,7 @@ export class DirectLine implements IBotConnection {
433439
// To send this message to DirectLine we need to deconstruct it into a "template" activity
434440
// and one blob for each attachment.
435441
formData = new FormData();
436-
formData.append('activity', new Blob([JSON.stringify(newMessage)], { type: 'application/vnd.microsoft.activity' }));
442+
formData.append('activity', new Blob([JSON.stringify(messageWithoutAttachments)], { type: 'application/vnd.microsoft.activity' }));
437443

438444
return Observable.from(attachments || [])
439445
.flatMap((media: Media) =>
@@ -451,7 +457,7 @@ export class DirectLine implements IBotConnection {
451457
.flatMap(_ =>
452458
Observable.ajax({
453459
method: "POST",
454-
url: `${this.domain}/conversations/${this.conversationId}/upload?userId=${message.from.id}`,
460+
url: `${this.domain}/conversations/${this.conversationId}/upload?userId=${messageWithoutAttachments.from.id}`,
455461
body: formData,
456462
timeout,
457463
headers: {
@@ -467,7 +473,7 @@ export class DirectLine implements IBotConnection {
467473
private catchPostError(error: any) {
468474
if (error.status === 403)
469475
// token has expired (will fall through to return "retry")
470-
this.connectionStatus$.next(ConnectionStatus.ExpiredToken);
476+
this.expiredToken();
471477
else if (error.status >= 400 && error.status < 500)
472478
// more unrecoverable errors
473479
return Observable.throw(error);
@@ -500,7 +506,7 @@ export class DirectLine implements IBotConnection {
500506
// to immediately throw an error, which is caught by the catch() below and transformed into an empty
501507
// object. Then next() returns, and we emit an empty object. Which means one 403 is causing
502508
// two empty objects to be emitted. Which is harmless but, again, slightly ugly.
503-
this.connectionStatus$.next(ConnectionStatus.ExpiredToken);
509+
this.expiredToken();
504510
}
505511
return Observable.empty<AjaxResponse>();
506512
})
@@ -581,13 +587,13 @@ export class DirectLine implements IBotConnection {
581587
this.token = result.response.token;
582588
this.streamUrl = result.response.streamUrl;
583589
})
584-
.mapTo(null)
590+
.map(_ => null)
585591
.retryWhen(error$ => error$
586592
.mergeMap(error => {
587593
if (error.status === 403) {
588594
// token has expired. We can't recover from this here, but the embedding
589595
// website might eventually call reconnect() with a new token and streamUrl.
590-
this.connectionStatus$.next(ConnectionStatus.ExpiredToken);
596+
this.expiredToken();
591597
}
592598
return Observable.of(error);
593599
})

0 commit comments

Comments
 (0)