From 4d569d4ec0ded8dd19d5ff863785a5f98ef10ca7 Mon Sep 17 00:00:00 2001 From: Sheldan <5037282+Sheldan@users.noreply.github.com> Date: Sun, 25 Jul 2021 16:30:56 +0200 Subject: [PATCH] [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 --- .../converter/FAQCommandConfigConverter.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/application/oneplus-bot-modules/faq/src/main/java/dev/sheldan/oneplus/bot/modules/faq/converter/FAQCommandConfigConverter.java b/application/oneplus-bot-modules/faq/src/main/java/dev/sheldan/oneplus/bot/modules/faq/converter/FAQCommandConfigConverter.java index d8b81e2..591179e 100644 --- a/application/oneplus-bot-modules/faq/src/main/java/dev/sheldan/oneplus/bot/modules/faq/converter/FAQCommandConfigConverter.java +++ b/application/oneplus-bot-modules/faq/src/main/java/dev/sheldan/oneplus/bot/modules/faq/converter/FAQCommandConfigConverter.java @@ -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 asList) { + Long botId = botService.getInstance().getSelfUser().getIdLong(); + asList.sort(Comparator.comparing(FAQCommand::getName)); List config = asList.stream().map(faqCommand -> { - List commandResponseConfigs = convertGroupCommands(faqCommand.getGroupResponses()); + List responses = faqCommand.getGroupResponses(); + responses.sort(Comparator.comparing(groupCommand -> groupCommand.getChannelGroup().getChannelGroup().getGroupName())); + List commandResponseConfigs = convertGroupCommands(responses, botId); List 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 convertGroupCommands(List responses) { + private List convertGroupCommands(List responses, Long botUserId) { return responses.stream().map(faqChannelGroupCommand -> { - List responseConfigs = convertCommandResponses(faqChannelGroupCommand.getResponses()); + List 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 convertCommandResponses(List responses) { - return responses.stream().map(this::convertCommandResponse).collect(Collectors.toList()); + private List convertCommandResponses(List 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()