-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathAsyncChatStreamHelper.java
More file actions
185 lines (165 loc) · 7.23 KB
/
AsyncChatStreamHelper.java
File metadata and controls
185 lines (165 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.slack.api.methods;
import com.slack.api.methods.request.chat.ChatAppendStreamRequest;
import com.slack.api.methods.request.chat.ChatStartStreamRequest;
import com.slack.api.methods.request.chat.ChatStopStreamRequest;
import com.slack.api.methods.response.chat.ChatAppendStreamResponse;
import com.slack.api.methods.response.chat.ChatStartStreamResponse;
import com.slack.api.methods.response.chat.ChatStopStreamResponse;
import com.slack.api.model.Message;
import com.slack.api.model.block.LayoutBlock;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Async variant of {@link ChatStreamHelper} for {@link AsyncMethodsClient}.
* <p>
* This helper buffers markdown text and flushes via chat.startStream / chat.appendStream, then finalizes via
* chat.stopStream.
* <p>
*
*/
@Data
@Slf4j
@Builder
public class AsyncChatStreamHelper {
private enum StreamState {
STARTING,
IN_PROGRESS,
COMPLETED
}
private final AsyncMethodsClient client;
private final String channel;
private final String threadTs;
private final String recipientTeamId;
private final String recipientUserId;
@Builder.Default
private final int bufferSize = 256;
@Builder.Default
private StringBuilder buffer = new StringBuilder();
@Builder.Default
private StreamState state = StreamState.STARTING;
private String streamTs;
/**
* Append text to the stream.
*
* @param markdownText markdown text to append
* @return a future that completes with a response if the buffer was flushed; completes with null if buffering
*/
public CompletableFuture<ChatAppendStreamResponse> append(String markdownText) {
if (state == StreamState.COMPLETED) {
CompletableFuture<ChatAppendStreamResponse> f = new CompletableFuture<>();
f.completeExceptionally(new SlackChatStreamException("Cannot append to stream: stream state is " + state));
return f;
}
buffer.append(markdownText);
if (buffer.length() >= bufferSize) {
return flushBuffer();
}
if (log.isDebugEnabled()) {
log.debug("AsyncChatStream appended to buffer: bufferLength={}, bufferSize={}, channel={}, " +
"recipientTeamId={}, recipientUserId={}, threadTs={}",
buffer.length(), bufferSize, channel, recipientTeamId, recipientUserId, threadTs);
}
return CompletableFuture.completedFuture(null);
}
public CompletableFuture<ChatStopStreamResponse> stop() {
return stop(null, null, null);
}
public CompletableFuture<ChatStopStreamResponse> stop(String markdownText) {
return stop(markdownText, null, null);
}
public CompletableFuture<ChatStopStreamResponse> stop(
String markdownText,
List<LayoutBlock> blocks,
Message.Metadata metadata
) {
if (state == StreamState.COMPLETED) {
CompletableFuture<ChatStopStreamResponse> f = new CompletableFuture<>();
f.completeExceptionally(new SlackChatStreamException("Cannot stop stream: stream state is " + state));
return f;
}
if (markdownText != null) {
buffer.append(markdownText);
}
CompletableFuture<Void> ensureStarted;
if (streamTs == null) {
ensureStarted = client.chatStartStream(ChatStartStreamRequest.builder()
.channel(channel)
.threadTs(threadTs)
.recipientTeamId(recipientTeamId)
.recipientUserId(recipientUserId)
.build())
.thenApply(startResponse -> {
if (!startResponse.isOk() || startResponse.getTs() == null) {
SlackChatStreamException ex = new SlackChatStreamException(
"Failed to stop stream: stream not started - " + startResponse.getError());
ex.setStartResponse(startResponse);
throw ex;
}
streamTs = startResponse.getTs();
state = StreamState.IN_PROGRESS;
return null;
});
} else {
ensureStarted = CompletableFuture.completedFuture(null);
}
return ensureStarted.thenCompose(ignored -> client.chatStopStream(ChatStopStreamRequest.builder()
.channel(channel)
.ts(streamTs)
.markdownText(buffer.toString())
.blocks(blocks)
.metadata(metadata)
.build())
.thenApply(resp -> {
state = StreamState.COMPLETED;
return resp;
}));
}
private CompletableFuture<ChatAppendStreamResponse> flushBuffer() {
if (streamTs == null) {
return client.chatStartStream(ChatStartStreamRequest.builder()
.channel(channel)
.threadTs(threadTs)
.recipientTeamId(recipientTeamId)
.recipientUserId(recipientUserId)
.markdownText(buffer.toString())
.build())
.thenApply(startResponse -> {
if (!startResponse.isOk()) {
SlackChatStreamException ex = new SlackChatStreamException(
"Failed to start stream: " + startResponse.getError());
ex.setStartResponse(startResponse);
throw ex;
}
streamTs = startResponse.getTs();
state = StreamState.IN_PROGRESS;
ChatAppendStreamResponse synth = new ChatAppendStreamResponse();
synth.setOk(startResponse.isOk());
synth.setChannel(startResponse.getChannel());
synth.setTs(startResponse.getTs());
synth.setWarning(startResponse.getWarning());
synth.setError(startResponse.getError());
buffer.setLength(0);
return synth;
});
} else {
return client.chatAppendStream(ChatAppendStreamRequest.builder()
.channel(channel)
.ts(streamTs)
.markdownText(buffer.toString())
.build())
.thenApply(resp -> {
if (!resp.isOk()) {
SlackChatStreamException ex = new SlackChatStreamException(
"Failed to append to stream: " + resp.getError());
ex.getAppendResponses().add(resp);
throw ex;
}
buffer.setLength(0);
return resp;
});
}
}
}