6
6
7
7
import { Chat } from './chat' ;
8
8
import { ChatType } from './chat-type' ;
9
- import { AttachmentContent , ChatContent } from './content' ;
9
+ import { AttachmentContent , ChatContent , TextContent } from './content' ;
10
10
11
11
/**
12
12
* Build Chat object from existing chat or create new.
13
13
*/
14
14
export class ChatBuilder {
15
- private _contents : ( string | ChatContent ) [ ] ;
16
-
17
- /**
18
- * Set chat options (shout, inapp)
19
- * This can be overridden by ChatContents.
20
- */
21
- public options : Record < string , unknown > ;
15
+ private _contents : ChatContent [ ] ;
22
16
23
17
constructor ( ) {
24
18
this . _contents = [ ] ;
25
- this . options = { } ;
19
+ }
20
+
21
+ /**
22
+ * Append text.
23
+ * this is equivalent of calling builder.append(new TextContent(text));
24
+ *
25
+ * @param {string } text
26
+ * @return {this }
27
+ */
28
+ text ( text : string ) : this {
29
+ return this . append ( new TextContent ( text ) ) ;
26
30
}
27
31
28
32
/**
@@ -37,12 +41,12 @@ export class ChatBuilder {
37
41
}
38
42
39
43
/**
40
- * Append text or chat content.
44
+ * Append chat content.
41
45
*
42
- * @param {string | ChatContent } content
46
+ * @param {ChatContent } content
43
47
* @return {this }
44
48
*/
45
- append ( content : string | ChatContent ) : this {
49
+ append ( content : ChatContent ) : this {
46
50
this . _contents . push ( content ) ;
47
51
return this ;
48
52
}
@@ -55,8 +59,7 @@ export class ChatBuilder {
55
59
* @return {this }
56
60
*/
57
61
shout ( flag : boolean ) : this {
58
- this . options [ 'shout' ] = flag ;
59
- return this ;
62
+ return this . attachment ( { shout : flag } ) ;
60
63
}
61
64
62
65
/**
@@ -81,14 +84,8 @@ export class ChatBuilder {
81
84
82
85
if ( ! chat . attachment ) chat . attachment = { } ;
83
86
84
- chat . attachment = Object . assign ( chat . attachment , this . options ) ;
85
-
86
87
for ( const content of this . _contents ) {
87
- if ( typeof content === 'string' ) {
88
- chat . text += content ;
89
- } else {
90
- content . append ( chat ) ;
91
- }
88
+ content . append ( chat ) ;
92
89
}
93
90
94
91
return chat ;
0 commit comments