Skip to content

Commit

Permalink
Fix NPE in im.turms.plugin.rasa.RasaResponser#notify #1175 + Trivia…
Browse files Browse the repository at this point in the history
…l code improvements
  • Loading branch information
JamesChenX committed Mar 13, 2023
1 parent d8b7930 commit dad0013
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Mono<RequestHandlerResult> notify(@NotNull RequestHandlerResult result,
@NotNull DeviceType requesterDevice) {
// 1. Validate
TurmsRequest request = result.dataForRecipients();
if (request.getKindCase() != TurmsRequest.KindCase.CREATE_MESSAGE_REQUEST) {
if (request == null || request.getKindCase() != TurmsRequest.KindCase.CREATE_MESSAGE_REQUEST) {
return Mono.just(result);
}
CreateMessageRequest createMessageRequest = request.getCreateMessageRequest();
Expand All @@ -122,25 +122,26 @@ public Mono<RequestHandlerResult> notify(@NotNull RequestHandlerResult result,
if (CollectionUtil.isEmpty(recipients)) {
return Mono.just(result);
}
Set<Long> currentChatbotUserIds = CollectionUtil
Set<Long> specifiedChatbotUserIds = CollectionUtil
.intersection(recipients, chatbotUserIds);
if (currentChatbotUserIds.isEmpty()) {
if (specifiedChatbotUserIds.isEmpty()) {
return Mono.just(result);
}
// 2. Send requests
boolean isGroupMessage = createMessageRequest.hasGroupId();
long targetId = isGroupMessage
? createMessageRequest.getGroupId()
: createMessageRequest.getRecipientId();
List<Mono<Void>> sendRequests = new ArrayList<>(currentChatbotUserIds.size());
for (Long chatbotUserId : currentChatbotUserIds) {
List<Mono<Void>> sendRequests = new ArrayList<>(specifiedChatbotUserIds.size());
for (Long chatbotUserId : specifiedChatbotUserIds) {
RasaClientInfo clientInfo = idToClientInfo.get(chatbotUserId);
Mono<Void> sendRequest = clientInfo.client
.sendRequest(new RasaRequest(requesterId, text))
.flatMap(responses -> {
if (responses.isEmpty()) {
return Mono.empty();
}
// 3. Send rasa responses to the requester
String responseText = formatResponse(clientInfo.properties.getResponse(), responses);
return messageService.authAndSaveAndSendMessage(true,
chatbotUserId,
Expand All @@ -166,8 +167,8 @@ public Mono<RequestHandlerResult> notify(@NotNull RequestHandlerResult result,
LOGGER.error("Caught an error while sending requests to Rasa servers", t);
}
});
// 3. Return final handler result
Set<Long> recipientIds = CollectionUtil.remove(recipients, currentChatbotUserIds);
// 4. Return final handler result
Set<Long> recipientIds = CollectionUtil.remove(recipients, specifiedChatbotUserIds);
return Mono.just(result.withRecipients(recipientIds));
});
}
Expand Down

0 comments on commit dad0013

Please sign in to comment.