mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-17 04:29:13 +00:00
[AB-268] adding button feature mode to suggestions which allows for hidden suggestion votes
moving gateway metric to separate service in case JDA is not ready yet
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package dev.sheldan.abstracto.suggestion.command;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.UtilityModuleDefinition;
|
||||
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
|
||||
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
|
||||
import dev.sheldan.abstracto.core.command.config.HelpInfo;
|
||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.command.config.ParameterValidator;
|
||||
import dev.sheldan.abstracto.core.command.config.validator.MinIntegerValueValidator;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.suggestion.config.SuggestionFeatureDefinition;
|
||||
import dev.sheldan.abstracto.suggestion.model.template.SuggestionInfoModel;
|
||||
import dev.sheldan.abstracto.suggestion.service.SuggestionService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class ShowSuggestion extends AbstractConditionableCommand {
|
||||
|
||||
public static final String SHOW_SUGGESTION_TEMPLATE_KEY = "suggestion_info_response";
|
||||
|
||||
@Autowired
|
||||
private SuggestionService suggestionService;
|
||||
|
||||
@Autowired
|
||||
private ChannelService channelService;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Long suggestionId = (Long) parameters.get(0);
|
||||
|
||||
SuggestionInfoModel suggestionInfoModel = suggestionService.getSuggestionInfo(commandContext.getGuild().getIdLong(), suggestionId);
|
||||
return FutureUtils.toSingleFutureGeneric(
|
||||
channelService.sendEmbedTemplateInTextChannelList(SHOW_SUGGESTION_TEMPLATE_KEY, suggestionInfoModel, commandContext.getChannel()))
|
||||
.thenApply(unused -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandConfiguration getConfiguration() {
|
||||
List<Parameter> parameters = new ArrayList<>();
|
||||
|
||||
List<ParameterValidator> suggestionIdValidator = Arrays.asList(MinIntegerValueValidator.min(1L));
|
||||
parameters.add(Parameter.builder().name("suggestionId").validators(suggestionIdValidator).type(Long.class).templated(true).build());
|
||||
HelpInfo helpInfo = HelpInfo.builder().templated(true).hasExample(false).build();
|
||||
return CommandConfiguration.builder()
|
||||
.name("showSuggestion")
|
||||
.module(UtilityModuleDefinition.UTILITY)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.parameters(parameters)
|
||||
.help(helpInfo)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FeatureDefinition getFeature() {
|
||||
return SuggestionFeatureDefinition.SUGGEST;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package dev.sheldan.abstracto.suggestion.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.config.ListenerPriority;
|
||||
import dev.sheldan.abstracto.core.interaction.InteractionService;
|
||||
import dev.sheldan.abstracto.core.listener.ButtonClickedListenerResult;
|
||||
import dev.sheldan.abstracto.core.listener.async.jda.ButtonClickedListener;
|
||||
import dev.sheldan.abstracto.core.models.listener.ButtonClickedListenerModel;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.suggestion.config.SuggestionFeatureDefinition;
|
||||
import dev.sheldan.abstracto.suggestion.model.template.SuggestionButtonPayload;
|
||||
import dev.sheldan.abstracto.suggestion.service.SuggestionServiceBean;
|
||||
import dev.sheldan.abstracto.suggestion.service.SuggestionVoteService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
|
||||
import net.dv8tion.jda.api.interactions.components.ButtonInteraction;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SuggestionButtonVoteClickedListener implements ButtonClickedListener {
|
||||
|
||||
@Autowired
|
||||
private SuggestionVoteService suggestionVoteService;
|
||||
|
||||
@Autowired
|
||||
private InteractionService interactionService;
|
||||
|
||||
public static final String VOTE_REMOVED_TEMPLATE_KEY = "suggestion_vote_removed_notification";
|
||||
public static final String VOTE_CAST_TEMPLATE_KEY = "suggestion_vote_cast_notification";
|
||||
|
||||
@Override
|
||||
public ButtonClickedListenerResult execute(ButtonClickedListenerModel model) {
|
||||
ButtonClickEvent event = model.getEvent();
|
||||
SuggestionButtonPayload payload = (SuggestionButtonPayload) model.getDeserializedPayload();
|
||||
suggestionVoteService.upsertSuggestionVote(event.getMember(), payload.getDecision(), payload.getSuggestionId());
|
||||
ButtonInteraction buttonInteraction = model.getEvent().getInteraction();
|
||||
String templateToUse;
|
||||
switch (payload.getDecision()) {
|
||||
case AGREE:
|
||||
case DISAGREE:
|
||||
templateToUse = VOTE_CAST_TEMPLATE_KEY;
|
||||
break;
|
||||
default:
|
||||
case REMOVE_VOTE:
|
||||
templateToUse = VOTE_REMOVED_TEMPLATE_KEY;
|
||||
}
|
||||
FutureUtils.toSingleFutureGeneric(interactionService.sendMessageToInteraction(templateToUse, new Object(), buttonInteraction.getHook()))
|
||||
.thenAccept(unused -> log.info("Notified user {} about vote action in suggestion {} in server {}.",
|
||||
model.getEvent().getMember().getIdLong(), payload.getSuggestionId(), payload.getServerId()));
|
||||
|
||||
return ButtonClickedListenerResult.ACKNOWLEDGED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean handlesEvent(ButtonClickedListenerModel model) {
|
||||
return model.getOrigin().equals(SuggestionServiceBean.SUGGESTION_VOTE_ORIGIN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FeatureDefinition getFeature() {
|
||||
return SuggestionFeatureDefinition.SUGGEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPriority() {
|
||||
return ListenerPriority.MEDIUM;
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,12 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface SuggestionRepository extends JpaRepository<Suggestion, ServerSpecificId> {
|
||||
List<Suggestion> findByUpdatedLessThanAndStateNot(Instant start, SuggestionState state);
|
||||
|
||||
Optional<Suggestion> findByMessageId(Long messageId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.suggestion.repository;
|
||||
|
||||
import dev.sheldan.abstracto.suggestion.model.database.SuggestionDecision;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.SuggestionVote;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.embed.SuggestionVoterId;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface SuggestionVoteRepository extends JpaRepository<SuggestionVote, SuggestionVoterId> {
|
||||
Long countByDecisionAndSuggestionVoteId_SuggestionIdAndSuggestionVoteId_ServerId(SuggestionDecision decision, Long suggestionId, Long serverId);
|
||||
void deleteBySuggestionVoteId_SuggestionIdAndSuggestionVoteId_ServerId(Long suggestionId, Long serverId);
|
||||
}
|
||||
@@ -5,7 +5,9 @@ import dev.sheldan.abstracto.core.models.ServerSpecificId;
|
||||
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.models.template.button.ButtonConfigModel;
|
||||
import dev.sheldan.abstracto.core.service.*;
|
||||
import dev.sheldan.abstracto.core.service.management.ComponentPayloadManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
@@ -18,10 +20,11 @@ import dev.sheldan.abstracto.suggestion.config.SuggestionFeatureMode;
|
||||
import dev.sheldan.abstracto.suggestion.config.SuggestionPostTarget;
|
||||
import dev.sheldan.abstracto.suggestion.exception.UnSuggestNotPossibleException;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.Suggestion;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.SuggestionDecision;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.SuggestionState;
|
||||
import dev.sheldan.abstracto.suggestion.model.template.SuggestionLog;
|
||||
import dev.sheldan.abstracto.suggestion.model.template.SuggestionReminderModel;
|
||||
import dev.sheldan.abstracto.suggestion.model.template.*;
|
||||
import dev.sheldan.abstracto.suggestion.service.management.SuggestionManagementService;
|
||||
import dev.sheldan.abstracto.suggestion.service.management.SuggestionVoteManagementService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -47,6 +50,7 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
public static final String SUGGESTION_NO_EMOTE = "suggestionNo";
|
||||
public static final String SUGGESTION_COUNTER_KEY = "suggestion";
|
||||
public static final String SUGGESTION_REMINDER_TEMPLATE_KEY = "suggest_suggestion_reminder";
|
||||
public static final String SUGGESTION_VOTE_ORIGIN = "suggestionVote";
|
||||
|
||||
@Autowired
|
||||
private SuggestionManagementService suggestionManagementService;
|
||||
@@ -93,6 +97,15 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
||||
@Autowired
|
||||
private ComponentService componentService;
|
||||
|
||||
@Autowired
|
||||
private ComponentPayloadManagementService componentPayloadManagementService;
|
||||
|
||||
@Autowired
|
||||
private SuggestionVoteManagementService suggestionVoteManagementService;
|
||||
|
||||
@Value("${abstracto.feature.suggestion.removalMaxAge}")
|
||||
private Long removalMaxAgeSeconds;
|
||||
|
||||
@@ -106,6 +119,7 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
AUserInAServer userSuggester = userInServerManagementService.loadOrCreateUser(suggester);
|
||||
Long newSuggestionId = counterService.getNextCounterValue(server, SUGGESTION_COUNTER_KEY);
|
||||
Boolean useButtons = featureModeService.featureModeActive(SuggestionFeatureDefinition.SUGGEST, serverId, SuggestionFeatureMode.SUGGESTION_BUTTONS);
|
||||
SuggestionLog model = SuggestionLog
|
||||
.builder()
|
||||
.suggestionId(newSuggestionId)
|
||||
@@ -114,14 +128,35 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
.message(commandMessage)
|
||||
.member(commandMessage.getMember())
|
||||
.suggesterUser(userSuggester)
|
||||
.useButtons(useButtons)
|
||||
.suggester(suggester.getUser())
|
||||
.text(text)
|
||||
.build();
|
||||
if(useButtons) {
|
||||
setupButtonIds(model);
|
||||
}
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(SUGGESTION_CREATION_TEMPLATE, model, serverId);
|
||||
log.info("Creating suggestion with id {} in server {} from member {}.", newSuggestionId, serverId, suggester.getIdLong());
|
||||
List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(messageToSend, SuggestionPostTarget.SUGGESTION, serverId);
|
||||
return FutureUtils.toSingleFutureGeneric(completableFutures).thenCompose(aVoid -> {
|
||||
Message message = completableFutures.get(0).join();
|
||||
return FutureUtils.toSingleFutureGeneric(completableFutures)
|
||||
.thenCompose(aVoid -> self.addDeletionPossibility(commandMessage, text, suggester, serverId, newSuggestionId, completableFutures, model));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CompletableFuture<Void> addDeletionPossibility(Message commandMessage, String text, Member suggester, Long serverId,
|
||||
Long newSuggestionId, List<CompletableFuture<Message>> completableFutures, SuggestionLog model) {
|
||||
Message message = completableFutures.get(0).join();
|
||||
if(model.getUseButtons()) {
|
||||
configureDecisionButtonPayload(serverId, newSuggestionId, model.getAgreeButtonModel(), SuggestionDecision.AGREE);
|
||||
configureDecisionButtonPayload(serverId, newSuggestionId, model.getDisAgreeButtonModel(), SuggestionDecision.DISAGREE);
|
||||
configureDecisionButtonPayload(serverId, newSuggestionId, model.getRemoveVoteButtonModel(), SuggestionDecision.REMOVE_VOTE);
|
||||
AServer server = serverManagementService.loadServer(serverId);
|
||||
componentPayloadManagementService.createPayload(model.getAgreeButtonModel(), server);
|
||||
componentPayloadManagementService.createPayload(model.getDisAgreeButtonModel(), server);
|
||||
componentPayloadManagementService.createPayload(model.getRemoveVoteButtonModel(), server);
|
||||
self.persistSuggestionInDatabase(suggester, text, message, newSuggestionId, commandMessage);
|
||||
return CompletableFuture.completedFuture(null);
|
||||
} else {
|
||||
log.debug("Posted message, adding reaction for suggestion {} to message {}.", newSuggestionId, message.getId());
|
||||
CompletableFuture<Void> firstReaction = reactionService.addReactionToMessageAsync(SUGGESTION_YES_EMOTE, serverId, message);
|
||||
CompletableFuture<Void> secondReaction = reactionService.addReactionToMessageAsync(SUGGESTION_NO_EMOTE, serverId, message);
|
||||
@@ -129,7 +164,25 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
log.debug("Reaction added to message {} for suggestion {}.", message.getId(), newSuggestionId);
|
||||
self.persistSuggestionInDatabase(suggester, text, message, newSuggestionId, commandMessage);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void configureDecisionButtonPayload(Long serverId, Long newSuggestionId, ButtonConfigModel model, SuggestionDecision decision) {
|
||||
SuggestionButtonPayload agreePayload = SuggestionButtonPayload
|
||||
.builder()
|
||||
.suggestionId(newSuggestionId)
|
||||
.serverId(serverId)
|
||||
.decision(decision)
|
||||
.build();
|
||||
model.setButtonPayload(agreePayload);
|
||||
model.setOrigin(SUGGESTION_VOTE_ORIGIN);
|
||||
model.setPayloadType(SuggestionButtonPayload.class);
|
||||
}
|
||||
|
||||
private void setupButtonIds(SuggestionLog suggestionLog) {
|
||||
suggestionLog.setAgreeButtonModel(componentService.createButtonConfigModel());
|
||||
suggestionLog.setDisAgreeButtonModel(componentService.createButtonConfigModel());
|
||||
suggestionLog.setRemoveVoteButtonModel(componentService.createButtonConfigModel());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -179,26 +232,32 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
Long serverId = suggestion.getServer().getId();
|
||||
Long channelId = suggestion.getChannel().getId();
|
||||
Long originalMessageId = suggestion.getMessageId();
|
||||
SuggestionLog model = SuggestionLog
|
||||
Long agreements = suggestionVoteManagementService.getDecisionsForSuggestion(suggestion, SuggestionDecision.AGREE);
|
||||
Long disagreements = suggestionVoteManagementService.getDecisionsForSuggestion(suggestion, SuggestionDecision.DISAGREE);
|
||||
Long suggestionId = suggestion.getSuggestionId().getId();
|
||||
SuggestionUpdateModel model = SuggestionUpdateModel
|
||||
.builder()
|
||||
.suggestionId(suggestion.getSuggestionId().getId())
|
||||
.suggestionId(suggestionId)
|
||||
.state(suggestion.getState())
|
||||
.suggesterUser(suggestion.getSuggester())
|
||||
.serverId(serverId)
|
||||
.member(memberExecutingCommand)
|
||||
.agreeVotes(agreements)
|
||||
.disAgreeVotes(disagreements)
|
||||
.originalMessageId(originalMessageId)
|
||||
.text(suggestion.getSuggestionText())
|
||||
.originalChannelId(channelId)
|
||||
.reason(reason)
|
||||
.build();
|
||||
log.info("Updated posted suggestion {} in server {}.", suggestion.getSuggestionId().getId(), suggestion.getServer().getId());
|
||||
log.info("Updated posted suggestion {} in server {}.", suggestionId, suggestion.getServer().getId());
|
||||
CompletableFuture<User> memberById = userService.retrieveUserForId(suggestion.getSuggester().getUserReference().getId());
|
||||
CompletableFuture<Void> finalFuture = new CompletableFuture<>();
|
||||
memberById.whenComplete((user, throwable) -> {
|
||||
if(throwable == null) {
|
||||
model.setSuggester(user);
|
||||
}
|
||||
self.updateSuggestionMessageText(reason, model).thenAccept(unused -> finalFuture.complete(null)).exceptionally(throwable1 -> {
|
||||
self.updateSuggestionMessageText(reason, model).thenAccept(unused -> finalFuture.complete(null))
|
||||
.thenAccept(unused -> self.removeSuggestionButtons(serverId, channelId, originalMessageId, suggestionId))
|
||||
.exceptionally(throwable1 -> {
|
||||
finalFuture.completeExceptionally(throwable1);
|
||||
return null;
|
||||
});
|
||||
@@ -208,11 +267,23 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
});
|
||||
|
||||
return finalFuture;
|
||||
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CompletableFuture<Void> updateSuggestionMessageText(String text, SuggestionLog suggestionLog) {
|
||||
public CompletableFuture<Void> removeSuggestionButtons(Long serverId, Long channelId, Long messageId, Long suggestionId) {
|
||||
Boolean useButtons = featureModeService.featureModeActive(SuggestionFeatureDefinition.SUGGEST, serverId, SuggestionFeatureMode.SUGGESTION_BUTTONS);
|
||||
if(useButtons) {
|
||||
return messageService.loadMessage(serverId, channelId, messageId).thenCompose(message -> {
|
||||
log.info("Clearing buttons from suggestion {} in with message {} in channel {} in server {}.", suggestionId, message, channelId, serverId);
|
||||
return componentService.clearButtons(message);
|
||||
});
|
||||
} else {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CompletableFuture<Void> updateSuggestionMessageText(String text, SuggestionUpdateModel suggestionLog) {
|
||||
suggestionLog.setReason(text);
|
||||
Long serverId = suggestionLog.getServerId();
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(SUGGESTION_UPDATE_TEMPLATE, suggestionLog, serverId);
|
||||
@@ -253,8 +324,11 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
Instant pointInTime = Instant.now().minus(Duration.ofDays(autoRemovalMaxDays)).truncatedTo(ChronoUnit.DAYS);
|
||||
List<Suggestion> suggestionsToRemove = suggestionManagementService.getSuggestionsUpdatedBeforeNotNew(pointInTime);
|
||||
log.info("Removing {} suggestions older than {}.", suggestionsToRemove.size(), pointInTime);
|
||||
suggestionsToRemove.forEach(suggestion -> log.info("Deleting suggestion {} in server {}.",
|
||||
suggestion.getSuggestionId().getId(), suggestion.getSuggestionId().getServerId()));
|
||||
suggestionsToRemove.forEach(suggestion -> {
|
||||
suggestionVoteManagementService.deleteSuggestionVotes(suggestion);
|
||||
log.info("Deleting suggestion {} in server {}.",
|
||||
suggestion.getSuggestionId().getId(), suggestion.getSuggestionId().getServerId());
|
||||
});
|
||||
suggestionManagementService.deleteSuggestion(suggestionsToRemove);
|
||||
}
|
||||
|
||||
@@ -298,6 +372,18 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SuggestionInfoModel getSuggestionInfo(Long serverId, Long suggestionId) {
|
||||
Suggestion suggestion = suggestionManagementService.getSuggestion(serverId, suggestionId);
|
||||
Long agreements = suggestionVoteManagementService.getDecisionsForSuggestion(suggestion, SuggestionDecision.AGREE);
|
||||
Long disagreements = suggestionVoteManagementService.getDecisionsForSuggestion(suggestion, SuggestionDecision.DISAGREE);
|
||||
return SuggestionInfoModel
|
||||
.builder()
|
||||
.agreements(agreements)
|
||||
.disagreements(disagreements)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteSuggestion(Long suggestionId, Long serverId) {
|
||||
Suggestion suggestion = suggestionManagementService.getSuggestion(serverId, suggestionId);
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package dev.sheldan.abstracto.suggestion.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.Suggestion;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.SuggestionDecision;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.SuggestionVote;
|
||||
import dev.sheldan.abstracto.suggestion.service.management.SuggestionManagementService;
|
||||
import dev.sheldan.abstracto.suggestion.service.management.SuggestionVoteManagementService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SuggestionVoteServiceBean implements SuggestionVoteService {
|
||||
|
||||
@Autowired
|
||||
private SuggestionVoteManagementService suggestionVoteManagementService;
|
||||
|
||||
@Autowired
|
||||
private UserInServerManagementService userInServerManagementService;
|
||||
|
||||
@Autowired
|
||||
private SuggestionManagementService suggestionManagementService;
|
||||
|
||||
@Override
|
||||
public SuggestionVote upsertSuggestionVote(Member votingMember, SuggestionDecision decision, Long suggestionId) {
|
||||
Long serverId = votingMember.getGuild().getIdLong();
|
||||
Suggestion suggestion = suggestionManagementService.getSuggestion(serverId, suggestionId);
|
||||
return upsertSuggestionVote(votingMember, decision, suggestion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SuggestionVote upsertSuggestionVote(Member votingMember, SuggestionDecision decision, Suggestion suggestion) {
|
||||
AUserInAServer votingUser = userInServerManagementService.loadOrCreateUser(votingMember);
|
||||
Optional<SuggestionVote> suggestionVoteOptional = suggestionVoteManagementService.getSuggestionVote(votingUser, suggestion);
|
||||
if(decision.equals(SuggestionDecision.REMOVE_VOTE)) {
|
||||
deleteSuggestionVote(votingMember, suggestion);
|
||||
return null;
|
||||
}
|
||||
if(suggestionVoteOptional.isPresent()) {
|
||||
log.info("Updating suggestion decision of user {} on suggestion {} in server {} to {}.", votingMember.getIdLong(),
|
||||
suggestion.getSuggestionId().getId(), suggestion.getServer().getId(), decision);
|
||||
SuggestionVote updatedVote = suggestionVoteOptional.get();
|
||||
updatedVote.setDecision(decision);
|
||||
return updatedVote;
|
||||
} else {
|
||||
return suggestionVoteManagementService.createSuggestionVote(votingUser, suggestion, decision);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteSuggestionVote(Member votingMember, Long suggestionId) {
|
||||
Suggestion suggestion = suggestionManagementService.getSuggestion(votingMember.getGuild().getIdLong(), suggestionId);
|
||||
deleteSuggestionVote(votingMember, suggestion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteSuggestionVote(Member votingMember, Suggestion suggestion) {
|
||||
AUserInAServer votingUser = userInServerManagementService.loadOrCreateUser(votingMember);
|
||||
log.info("Removing suggestion vote from user {} on suggestion {} in server {}.",
|
||||
votingMember.getIdLong(), suggestion.getSuggestionId().getId(), suggestion.getServer().getId());
|
||||
suggestionVoteManagementService.deleteSuggestionVote(votingUser, suggestion);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -101,4 +101,9 @@ public class SuggestionManagementServiceBean implements SuggestionManagementServ
|
||||
public List<Suggestion> getSuggestionsUpdatedBeforeNotNew(Instant date) {
|
||||
return suggestionRepository.findByUpdatedLessThanAndStateNot(date, SuggestionState.NEW);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Suggestion> findSuggestionByMessageId(Long messageId) {
|
||||
return suggestionRepository.findByMessageId(messageId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package dev.sheldan.abstracto.suggestion.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.Suggestion;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.SuggestionDecision;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.SuggestionVote;
|
||||
import dev.sheldan.abstracto.suggestion.model.database.embed.SuggestionVoterId;
|
||||
import dev.sheldan.abstracto.suggestion.repository.SuggestionVoteRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SuggestionVoteManagementServiceBean implements SuggestionVoteManagementService {
|
||||
|
||||
@Autowired
|
||||
private SuggestionVoteRepository suggestionVoteRepository;
|
||||
|
||||
@Override
|
||||
public Optional<SuggestionVote> getSuggestionVote(AUserInAServer aUserInAServer, Suggestion suggestion) {
|
||||
SuggestionVoterId suggestionVoteId = SuggestionVoterId
|
||||
.builder()
|
||||
.suggestionId(suggestion.getSuggestionId().getId())
|
||||
.serverId(suggestion.getServer().getId())
|
||||
.voterId(aUserInAServer.getUserInServerId())
|
||||
.build();
|
||||
return suggestionVoteRepository.findById(suggestionVoteId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteSuggestionVote(AUserInAServer aUserInAServer, Suggestion suggestion) {
|
||||
Optional<SuggestionVote> voteOptional = getSuggestionVote(aUserInAServer, suggestion);
|
||||
voteOptional.ifPresent(suggestionVote -> suggestionVoteRepository.delete(suggestionVote));
|
||||
|
||||
if(!voteOptional.isPresent()) {
|
||||
log.warn("User {} in server {} did not have a vote for suggestion {}.",
|
||||
aUserInAServer.getUserReference().getId(), aUserInAServer.getServerReference().getId(), suggestion.getSuggestionId().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SuggestionVote createSuggestionVote(AUserInAServer aUserInAServer, Suggestion suggestion, SuggestionDecision decision) {
|
||||
SuggestionVoterId suggestionVoteId = SuggestionVoterId
|
||||
.builder()
|
||||
.suggestionId(suggestion.getSuggestionId().getId())
|
||||
.serverId(suggestion.getServer().getId())
|
||||
.voterId(aUserInAServer.getUserInServerId())
|
||||
.build();
|
||||
SuggestionVote vote = SuggestionVote
|
||||
.builder()
|
||||
.suggestionVoteId(suggestionVoteId)
|
||||
.voter(aUserInAServer)
|
||||
.decision(decision)
|
||||
.suggestion(suggestion)
|
||||
.build();
|
||||
log.info("Creating suggestion decision of user {} on suggestion {} in server {} to {}.", aUserInAServer.getUserReference().getId(),
|
||||
suggestion.getSuggestionId().getId(), suggestion.getServer().getId(), decision);
|
||||
return suggestionVoteRepository.save(vote);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getDecisionsForSuggestion(Suggestion suggestion, SuggestionDecision decision) {
|
||||
return suggestionVoteRepository.countByDecisionAndSuggestionVoteId_SuggestionIdAndSuggestionVoteId_ServerId(decision, suggestion.getSuggestionId().getId(), suggestion.getSuggestionId().getServerId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteSuggestionVotes(Suggestion suggestion) {
|
||||
suggestionVoteRepository.deleteBySuggestionVoteId_SuggestionIdAndSuggestionVoteId_ServerId(suggestion.getSuggestionId().getId(), suggestion.getSuggestionId().getServerId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
|
||||
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
|
||||
<include file="tables/tables.xml" relativeToChangelogFile="true"/>
|
||||
<include file="seedData/data.xml" relativeToChangelogFile="true"/>
|
||||
</databaseChangeLog>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
|
||||
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
|
||||
<property name="utilityModule" value="(SELECT id FROM module WHERE name = 'utility')"/>
|
||||
<property name="suggestionFeature" value="(SELECT id FROM feature WHERE key = 'suggestion')"/>
|
||||
|
||||
<changeSet author="Sheldan" id="suggestionInfo-commands">
|
||||
<insert tableName="command">
|
||||
<column name="name" value="showSuggestion"/>
|
||||
<column name="module_id" valueComputed="${utilityModule}"/>
|
||||
<column name="feature_id" valueComputed="${suggestionFeature}"/>
|
||||
</insert>
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
|
||||
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
|
||||
<include file="command.xml" relativeToChangelogFile="true"/>
|
||||
</databaseChangeLog>
|
||||
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
|
||||
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
|
||||
<changeSet author="Sheldan" id="suggestion_vote-table">
|
||||
<createTable tableName="suggestion_vote">
|
||||
<column name="voter_user_in_server_id" type="BIGINT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="suggestion_id" type="BIGINT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="server_id" type="BIGINT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="decision" type="VARCHAR(255)">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
|
||||
</createTable>
|
||||
<addPrimaryKey columnNames="voter_user_in_server_id, suggestion_id, server_id" tableName="suggestion_vote" constraintName="pk_suggestion_vote" validate="false"/>
|
||||
<addForeignKeyConstraint baseColumnNames="suggestion_id, server_id" baseTableName="suggestion_vote" constraintName="fk_suggestion_vote_suggestion"
|
||||
deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
|
||||
referencedColumnNames="id, server_id" referencedTableName="suggestion" validate="false"/>
|
||||
<addForeignKeyConstraint baseColumnNames="voter_user_in_server_id" baseTableName="suggestion_vote" constraintName="fk_suggestion_vote_voter"
|
||||
deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
|
||||
referencedColumnNames="user_in_server_id" referencedTableName="user_in_server" validate="false"/>
|
||||
<sql>
|
||||
DROP TRIGGER IF EXISTS suggestion_vote_update_trigger ON suggestion_vote;
|
||||
CREATE TRIGGER suggestion_vote_update_trigger BEFORE UPDATE ON suggestion_vote FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
|
||||
</sql>
|
||||
<sql>
|
||||
DROP TRIGGER IF EXISTS suggestion_vote_insert_trigger ON suggestion_vote;
|
||||
CREATE TRIGGER suggestion_vote_insert_trigger BEFORE INSERT ON suggestion_vote FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
|
||||
</sql>
|
||||
<sql>
|
||||
ALTER TABLE suggestion_vote ADD CONSTRAINT check_suggestion_vote_state CHECK (decision IN ('AGREE','DISAGREE'));
|
||||
</sql>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
|
||||
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
|
||||
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
|
||||
<include file="suggestion_vote.xml" relativeToChangelogFile="true"/>
|
||||
</databaseChangeLog>
|
||||
@@ -9,4 +9,5 @@
|
||||
<include file="1.0-suggestion/collection.xml" relativeToChangelogFile="true"/>
|
||||
<include file="1.2.12/collection.xml" relativeToChangelogFile="true"/>
|
||||
<include file="1.2.13/collection.xml" relativeToChangelogFile="true"/>
|
||||
<include file="1.3.8/collection.xml" relativeToChangelogFile="true"/>
|
||||
</databaseChangeLog>
|
||||
@@ -12,4 +12,8 @@ abstracto.featureModes.suggestionReminder.mode=suggestionReminder
|
||||
abstracto.featureModes.suggestionReminder.enabled=false
|
||||
|
||||
abstracto.systemConfigs.suggestionReminderDays.name=suggestionReminderDays
|
||||
abstracto.systemConfigs.suggestionReminderDays.longValue=7
|
||||
abstracto.systemConfigs.suggestionReminderDays.longValue=7
|
||||
|
||||
abstracto.featureModes.suggestionButton.featureName=suggestion
|
||||
abstracto.featureModes.suggestionButton.mode=suggestionButton
|
||||
abstracto.featureModes.suggestionButton.enabled=true
|
||||
Reference in New Issue
Block a user