31
31
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
32
//
33
33
34
- import storage = require( './Storage ' ) ;
34
+ import storage = require( './BotStorage ' ) ;
35
35
import request = require( 'request' ) ;
36
36
37
37
export interface IBotConnectorStorageOptions {
@@ -40,53 +40,104 @@ export interface IBotConnectorStorageOptions {
40
40
appSecret : string ;
41
41
}
42
42
43
- export class BotConnectorStorage implements storage . IStorage {
43
+ export interface IBotConnectorStorageData extends storage . IBotStorageData {
44
+ userDataHash ?: string ;
45
+ conversationDataHash ?: string ;
46
+ }
47
+
48
+ export class BotConnectorStorage implements storage . IBotStorage {
44
49
constructor ( private options : IBotConnectorStorageOptions ) {
45
50
46
51
}
47
52
48
- public get ( id : string , callback : ( err : Error , data : any ) => void ) : void {
53
+ public get ( address : storage . IBotStorageAddress , callback : ( err : Error , data : storage . IBotStorageData ) => void ) : void {
54
+ var ops = 2 ;
49
55
var settings = this . options ;
50
- var options : request . Options = {
51
- url : settings . endpoint + '/bot/v1.0/bots' + id
52
- } ;
53
- if ( settings . appId && settings . appSecret ) {
54
- options . auth = {
55
- username : settings . appId ,
56
- password : settings . appSecret
57
- } ;
58
- options . headers = {
59
- 'Ocp-Apim-Subscription-Key' : settings . appSecret
60
- } ;
61
- }
62
- request . get ( options , ( err , response , body ) => {
63
- try {
64
- var data : any ;
65
- if ( ! err && typeof body === 'string' ) {
66
- data = JSON . parse ( body ) ;
56
+ var data : storage . IBotStorageData = { } ;
57
+ function read ( path : string , field : string ) {
58
+ if ( path ) {
59
+ var options : request . Options = {
60
+ url : settings . endpoint + '/bot/v1.0/bots' + path
61
+ } ;
62
+ if ( settings . appId && settings . appSecret ) {
63
+ options . auth = {
64
+ username : settings . appId ,
65
+ password : settings . appSecret
66
+ } ;
67
+ options . headers = {
68
+ 'Ocp-Apim-Subscription-Key' : settings . appSecret
69
+ } ;
67
70
}
68
- callback ( err , data ) ;
69
- } catch ( e ) {
70
- callback ( e instanceof Error ? e : new Error ( e . toString ( ) ) , null ) ;
71
+ request . get ( options , ( err , response , body ) => {
72
+ if ( ! err ) {
73
+ try {
74
+ ( < any > data ) [ field + 'Hash' ] = body ;
75
+ ( < any > data ) [ field ] = typeof body === 'string' ? JSON . parse ( body ) : null ;
76
+ } catch ( e ) {
77
+ err = e instanceof Error ? e : new Error ( e . toString ( ) ) ;
78
+ }
79
+ }
80
+ if ( callback && ( err || -- ops == 0 ) ) {
81
+ callback ( err , data ) ;
82
+ callback = null ;
83
+ }
84
+ } ) ;
85
+ } else if ( callback && -- ops == 0 ) {
86
+ callback ( null , data ) ;
71
87
}
72
- } ) ;
88
+ }
89
+ var userPath = address . userId ? '/users/' + address . userId : null ;
90
+ var convoPath = address . conversationId ? '/conversations/' + address . conversationId + userPath : null ;
91
+ read ( userPath , 'userData' ) ;
92
+ read ( convoPath , 'conversationData' ) ;
73
93
}
74
94
75
- public save ( id : string , data : any , callback ?: ( err : Error ) => void ) : void {
95
+ public save ( address : storage . IBotStorageAddress , data : storage . IBotStorageData , callback ?: ( err : Error ) => void ) : void {
96
+ var ops = 2 ;
76
97
var settings = this . options ;
77
- var options : request . Options = {
78
- url : settings . endpoint + '/bot/v1.0/bots' + id ,
79
- body : data
80
- } ;
81
- if ( settings . appId && settings . appSecret ) {
82
- options . auth = {
83
- username : settings . appId ,
84
- password : settings . appSecret
85
- } ;
86
- options . headers = {
87
- 'Ocp-Apim-Subscription-Key' : settings . appSecret
88
- } ;
98
+ function write ( path : string , field : string ) {
99
+ if ( path ) {
100
+ var err : Error ;
101
+ var body : string ;
102
+ var hashField = field + 'Hash' ;
103
+ try {
104
+ body = JSON . stringify ( ( < any > data ) [ field ] ) ;
105
+ } catch ( e ) {
106
+ err = e instanceof Error ? e : new Error ( e . toString ( ) ) ;
107
+ }
108
+ if ( ! err && ( ! ( < any > data ) [ hashField ] || body !== ( < any > data ) [ hashField ] ) ) {
109
+ ( < any > data ) [ hashField ] = body ;
110
+ var options : request . Options = {
111
+ url : settings . endpoint + '/bot/v1.0/bots' + path ,
112
+ body : body
113
+ } ;
114
+ if ( settings . appId && settings . appSecret ) {
115
+ options . auth = {
116
+ username : settings . appId ,
117
+ password : settings . appSecret
118
+ } ;
119
+ options . headers = {
120
+ 'Ocp-Apim-Subscription-Key' : settings . appSecret
121
+ } ;
122
+ }
123
+ request . post ( options , ( err ) => {
124
+ if ( callback && ( err || -- ops == 0 ) ) {
125
+ callback ( err ) ;
126
+ callback = null ;
127
+ }
128
+ } ) ;
129
+ } else if ( callback && ( err || -- ops == 0 ) ) {
130
+ callback ( err ) ;
131
+ callback = null ;
132
+ }
133
+ } else if ( callback && -- ops == 0 ) {
134
+ callback ( null ) ;
135
+ }
89
136
}
90
- request . post ( options , callback ) ;
137
+ var userPath = address . userId ? '/users/' + address . userId : null ;
138
+ var convoPath = address . conversationId ? '/conversations/' + address . conversationId + userPath : null ;
139
+ write ( userPath , 'userData' ) ;
140
+ write ( convoPath , 'conversationData' ) ;
141
+
91
142
}
92
143
}
0 commit comments