[OPB-32] fixing always exporting user ID in faq config when in reality it was bot ID

sorting faq commands and channel groups in export response alphabetically
This commit is contained in:
Sheldan
2021-07-25 16:30:56 +02:00
parent 81e58c3f7b
commit 4d569d4ec0

View File

@@ -1,6 +1,7 @@
package dev.sheldan.oneplus.bot.modules.faq.converter;
import com.google.gson.Gson;
import dev.sheldan.abstracto.core.service.BotService;
import dev.sheldan.oneplus.bot.modules.faq.models.command.config.*;
import dev.sheldan.oneplus.bot.modules.faq.models.database.FAQChannelGroupCommand;
import dev.sheldan.oneplus.bot.modules.faq.models.database.FAQCommand;
@@ -9,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@@ -18,9 +20,16 @@ public class FAQCommandConfigConverter {
@Autowired
private Gson gson;
@Autowired
private BotService botService;
public String serializeCommands(List<FAQCommand> asList) {
Long botId = botService.getInstance().getSelfUser().getIdLong();
asList.sort(Comparator.comparing(FAQCommand::getName));
List<FaqCommandConfig> config = asList.stream().map(faqCommand -> {
List<FaqCommandResponseConfig> commandResponseConfigs = convertGroupCommands(faqCommand.getGroupResponses());
List<FAQChannelGroupCommand> responses = faqCommand.getGroupResponses();
responses.sort(Comparator.comparing(groupCommand -> groupCommand.getChannelGroup().getChannelGroup().getGroupName()));
List<FaqCommandResponseConfig> commandResponseConfigs = convertGroupCommands(responses, botId);
List<String> aliases;
if(faqCommand.getAliases() != null) {
aliases = faqCommand.getAliases().stream().map(faqCommandAlias -> faqCommandAlias.getId().getAlias()).collect(Collectors.toList());
@@ -39,9 +48,9 @@ public class FAQCommandConfigConverter {
return gson.toJson(config);
}
private List<FaqCommandResponseConfig> convertGroupCommands(List<FAQChannelGroupCommand> responses) {
private List<FaqCommandResponseConfig> convertGroupCommands(List<FAQChannelGroupCommand> responses, Long botUserId) {
return responses.stream().map(faqChannelGroupCommand -> {
List<FaqCommandResponseMessageConfig> responseConfigs = convertCommandResponses(faqChannelGroupCommand.getResponses());
List<FaqCommandResponseMessageConfig> responseConfigs = convertCommandResponses(faqChannelGroupCommand.getResponses(), botUserId);
return FaqCommandResponseConfig
.builder()
.channelGroupName(faqChannelGroupCommand.getChannelGroup().getChannelGroup().getGroupName())
@@ -50,13 +59,14 @@ public class FAQCommandConfigConverter {
}).collect(Collectors.toList());
}
private List<FaqCommandResponseMessageConfig> convertCommandResponses(List<FAQCommandResponse> responses) {
return responses.stream().map(this::convertCommandResponse).collect(Collectors.toList());
private List<FaqCommandResponseMessageConfig> convertCommandResponses(List<FAQCommandResponse> responses, Long botUserId) {
return responses.stream().map(response -> convertCommandResponse(response, botUserId)).collect(Collectors.toList());
}
private FaqCommandResponseMessageConfig convertCommandResponse(FAQCommandResponse response) {
private FaqCommandResponseMessageConfig convertCommandResponse(FAQCommandResponse response, Long botUserId) {
FaqCommandResponseEmbedConfig embedConfig = null;
if(response.getDescription() != null) {
boolean useBot = response.getAuthorUserId().equals(botUserId);
FaqCommandResponseEmbedColorConfig colorConfig = FaqCommandResponseEmbedColorConfig
.builder()
.red(response.getRed())
@@ -65,8 +75,8 @@ public class FAQCommandConfigConverter {
.build();
FaqCommandResponseEmbedAuthorConfig authorConfig = FaqCommandResponseEmbedAuthorConfig
.builder()
.userId(response.getAuthorUserId())
.useBot(null)
.userId(useBot ? null : response.getAuthorUserId())
.useBot(useBot)
.build();
embedConfig = FaqCommandResponseEmbedConfig
.builder()