mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-05 08:54:34 +00:00
[AB-85] adding feature mode to automatically create a thread for suggestions
reworking post target service to function with optionals fixing trying to add reactions for suggestions if there was no message created not showing votes in case the buttons feature mode is active
This commit is contained in:
@@ -29,6 +29,8 @@ import dev.sheldan.abstracto.suggestion.service.management.SuggestionManagementS
|
|||||||
import dev.sheldan.abstracto.suggestion.service.management.SuggestionVoteManagementService;
|
import dev.sheldan.abstracto.suggestion.service.management.SuggestionVoteManagementService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.dv8tion.jda.api.entities.*;
|
import net.dv8tion.jda.api.entities.*;
|
||||||
|
import net.dv8tion.jda.api.entities.channel.attribute.IThreadContainer;
|
||||||
|
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -37,9 +39,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Date;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@@ -131,6 +131,7 @@ public class SuggestionServiceBean implements SuggestionService {
|
|||||||
if(autoEvaluationEnabled) {
|
if(autoEvaluationEnabled) {
|
||||||
autoEvaluateDays = configService.getLongValueOrConfigDefault(SUGGESTION_AUTO_EVALUATE_DAYS_CONFIG_KEY, serverId);
|
autoEvaluateDays = configService.getLongValueOrConfigDefault(SUGGESTION_AUTO_EVALUATE_DAYS_CONFIG_KEY, serverId);
|
||||||
}
|
}
|
||||||
|
Instant autoEvaluationTargetDate = autoEvaluationEnabled ? Instant.now().plus(autoEvaluateDays, ChronoUnit.DAYS) : null;
|
||||||
SuggestionLog model = SuggestionLog
|
SuggestionLog model = SuggestionLog
|
||||||
.builder()
|
.builder()
|
||||||
.suggestionId(newSuggestionId)
|
.suggestionId(newSuggestionId)
|
||||||
@@ -143,7 +144,7 @@ public class SuggestionServiceBean implements SuggestionService {
|
|||||||
.suggester(suggester.getUser())
|
.suggester(suggester.getUser())
|
||||||
.text(text)
|
.text(text)
|
||||||
.autoEvaluationEnabled(autoEvaluationEnabled)
|
.autoEvaluationEnabled(autoEvaluationEnabled)
|
||||||
.autoEvaluationTargetDate(autoEvaluationEnabled ? Instant.now().plus(autoEvaluateDays, ChronoUnit.DAYS) : null)
|
.autoEvaluationTargetDate(autoEvaluationTargetDate)
|
||||||
.build();
|
.build();
|
||||||
if(useButtons) {
|
if(useButtons) {
|
||||||
setupButtonIds(model);
|
setupButtonIds(model);
|
||||||
@@ -151,32 +152,73 @@ public class SuggestionServiceBean implements SuggestionService {
|
|||||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(SUGGESTION_CREATION_TEMPLATE, model, serverId);
|
MessageToSend messageToSend = templateService.renderEmbedTemplate(SUGGESTION_CREATION_TEMPLATE, model, serverId);
|
||||||
log.info("Creating suggestion with id {} in server {} from member {}.", newSuggestionId, serverId, suggester.getIdLong());
|
log.info("Creating suggestion with id {} in server {} from member {}.", newSuggestionId, serverId, suggester.getIdLong());
|
||||||
List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(messageToSend, SuggestionPostTarget.SUGGESTION, serverId);
|
List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(messageToSend, SuggestionPostTarget.SUGGESTION, serverId);
|
||||||
|
List<ButtonConfigModel> buttonConfigModels = Arrays.asList(model.getAgreeButtonModel(), model.getDisAgreeButtonModel(), model.getRemoveVoteButtonModel());
|
||||||
return FutureUtils.toSingleFutureGeneric(completableFutures)
|
return FutureUtils.toSingleFutureGeneric(completableFutures)
|
||||||
.thenCompose(aVoid -> self.addDeletionPossibility(suggestionChannelId, suggestionMessageId, text, suggester, serverId, newSuggestionId, completableFutures, model));
|
.thenCompose(aVoid -> self.addVotingPossibility(suggestionChannelId, suggestionMessageId, text, suggester, serverId, newSuggestionId, completableFutures, buttonConfigModels, useButtons))
|
||||||
|
.thenCompose(aVoid -> self.createSuggestionThread(serverId, newSuggestionId, suggester, text, autoEvaluationEnabled, autoEvaluationTargetDate));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public CompletableFuture<Void> addDeletionPossibility(Long suggestionChannelId, Long suggestionMessageId, String text, Member suggester, Long serverId,
|
public CompletableFuture<Void> createSuggestionThread(Long serverId, Long suggestionId, Member suggester, String suggestionText,
|
||||||
Long newSuggestionId, List<CompletableFuture<Message>> completableFutures, SuggestionLog model) {
|
Boolean autoEvaluationEnabled, Instant autoEvaluationTargetDate) {
|
||||||
Message message = completableFutures.get(0).join();
|
if(featureModeService.featureModeActive(SuggestionFeatureDefinition.SUGGEST, serverId, SuggestionFeatureMode.SUGGESTION_THREAD)) {
|
||||||
if(model.getUseButtons()) {
|
Optional<GuildMessageChannel> suggestionTargetChannelOptional = postTargetService.getPostTargetChannel(SuggestionPostTarget.SUGGESTION, serverId);
|
||||||
configureDecisionButtonPayload(serverId, newSuggestionId, model.getAgreeButtonModel(), SuggestionDecision.AGREE);
|
log.info("Trying to create thread for suggestion {} in server {}.", suggestionId, serverId);
|
||||||
configureDecisionButtonPayload(serverId, newSuggestionId, model.getDisAgreeButtonModel(), SuggestionDecision.DISAGREE);
|
if (suggestionTargetChannelOptional.isPresent()) {
|
||||||
configureDecisionButtonPayload(serverId, newSuggestionId, model.getRemoveVoteButtonModel(), SuggestionDecision.REMOVE_VOTE);
|
GuildMessageChannel messageChannel = suggestionTargetChannelOptional.get();
|
||||||
AServer server = serverManagementService.loadServer(serverId);
|
if(messageChannel instanceof IThreadContainer) {
|
||||||
componentPayloadManagementService.createButtonPayload(model.getAgreeButtonModel(), server);
|
SuggestionThreadModel model = SuggestionThreadModel
|
||||||
componentPayloadManagementService.createButtonPayload(model.getDisAgreeButtonModel(), server);
|
.builder()
|
||||||
componentPayloadManagementService.createButtonPayload(model.getRemoveVoteButtonModel(), server);
|
.suggestionId(suggestionId)
|
||||||
self.persistSuggestionInDatabase(suggester, text, message, newSuggestionId, suggestionChannelId, suggestionMessageId);
|
.suggester(suggester.getUser())
|
||||||
return CompletableFuture.completedFuture(null);
|
.member(suggester)
|
||||||
|
.autoEvaluationEnabled(autoEvaluationEnabled)
|
||||||
|
.text(suggestionText)
|
||||||
|
.autoEvaluationTargetDate(autoEvaluationTargetDate)
|
||||||
|
.build();
|
||||||
|
IThreadContainer threadContainer = (IThreadContainer) messageChannel;
|
||||||
|
String threadName = templateService.renderTemplate("suggestion_thread_name", model, serverId);
|
||||||
|
return threadContainer.createThreadChannel(threadName).submit()
|
||||||
|
.thenAccept(threadChannel -> log.info("Created thread for suggestion {} in server {}.", suggestionId, serverId));
|
||||||
|
} else {
|
||||||
|
log.info("Suggestion thread was not created - post target for suggestions does not allow to create threads");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.info("Suggestion thread was not created - post target did not evaluate to a channel or post target is disabled.");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.debug("Posted message, adding reaction for suggestion {} to message {}.", newSuggestionId, message.getId());
|
log.info("Suggestion thread feature disabled - not creating thread for suggestion {} in server {}.", suggestionId, serverId);
|
||||||
CompletableFuture<Void> firstReaction = reactionService.addReactionToMessageAsync(SUGGESTION_YES_EMOTE, serverId, message);
|
}
|
||||||
CompletableFuture<Void> secondReaction = reactionService.addReactionToMessageAsync(SUGGESTION_NO_EMOTE, serverId, message);
|
return CompletableFuture.completedFuture(null);
|
||||||
return CompletableFuture.allOf(firstReaction, secondReaction).thenAccept(aVoid1 -> {
|
}
|
||||||
log.debug("Reaction added to message {} for suggestion {}.", message.getId(), newSuggestionId);
|
|
||||||
|
@Transactional
|
||||||
|
public CompletableFuture<Void> addVotingPossibility(Long suggestionChannelId, Long suggestionMessageId, String text, Member suggester, Long serverId,
|
||||||
|
Long newSuggestionId, List<CompletableFuture<Message>> completableFutures, List<ButtonConfigModel> buttonConfigModels,
|
||||||
|
Boolean useButtons) {
|
||||||
|
if(!completableFutures.isEmpty()) {
|
||||||
|
Message message = completableFutures.get(0).join();
|
||||||
|
if(useButtons) {
|
||||||
|
configureDecisionButtonPayload(serverId, newSuggestionId, buttonConfigModels.get(0), SuggestionDecision.AGREE);
|
||||||
|
configureDecisionButtonPayload(serverId, newSuggestionId, buttonConfigModels.get(1), SuggestionDecision.DISAGREE);
|
||||||
|
configureDecisionButtonPayload(serverId, newSuggestionId, buttonConfigModels.get(2), SuggestionDecision.REMOVE_VOTE);
|
||||||
|
AServer server = serverManagementService.loadServer(serverId);
|
||||||
|
componentPayloadManagementService.createButtonPayload(buttonConfigModels.get(0), server);
|
||||||
|
componentPayloadManagementService.createButtonPayload(buttonConfigModels.get(1), server);
|
||||||
|
componentPayloadManagementService.createButtonPayload(buttonConfigModels.get(2), server);
|
||||||
self.persistSuggestionInDatabase(suggester, text, message, newSuggestionId, suggestionChannelId, suggestionMessageId);
|
self.persistSuggestionInDatabase(suggester, text, message, newSuggestionId, suggestionChannelId, suggestionMessageId);
|
||||||
});
|
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);
|
||||||
|
return CompletableFuture.allOf(firstReaction, secondReaction).thenAccept(aVoid1 -> {
|
||||||
|
log.debug("Reaction added to message {} for suggestion {}.", message.getId(), newSuggestionId);
|
||||||
|
self.persistSuggestionInDatabase(suggester, text, message, newSuggestionId, suggestionChannelId, suggestionMessageId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return CompletableFuture.completedFuture(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,6 +317,7 @@ public class SuggestionServiceBean implements SuggestionService {
|
|||||||
Long serverId = suggestion.getServer().getId();
|
Long serverId = suggestion.getServer().getId();
|
||||||
Long channelId = suggestion.getChannel().getId();
|
Long channelId = suggestion.getChannel().getId();
|
||||||
Long originalMessageId = suggestion.getMessageId();
|
Long originalMessageId = suggestion.getMessageId();
|
||||||
|
boolean buttonsActive = featureModeService.featureModeActive(SuggestionFeatureDefinition.SUGGEST, serverId, SuggestionFeatureMode.SUGGESTION_BUTTONS);
|
||||||
Long agreements = suggestionVoteManagementService.getDecisionsForSuggestion(suggestion, SuggestionDecision.AGREE);
|
Long agreements = suggestionVoteManagementService.getDecisionsForSuggestion(suggestion, SuggestionDecision.AGREE);
|
||||||
Long disagreements = suggestionVoteManagementService.getDecisionsForSuggestion(suggestion, SuggestionDecision.DISAGREE);
|
Long disagreements = suggestionVoteManagementService.getDecisionsForSuggestion(suggestion, SuggestionDecision.DISAGREE);
|
||||||
Long suggestionId = suggestion.getSuggestionId().getId();
|
Long suggestionId = suggestion.getSuggestionId().getId();
|
||||||
@@ -290,6 +333,7 @@ public class SuggestionServiceBean implements SuggestionService {
|
|||||||
.suggestionId(suggestionId)
|
.suggestionId(suggestionId)
|
||||||
.state(suggestion.getState())
|
.state(suggestion.getState())
|
||||||
.serverId(serverId)
|
.serverId(serverId)
|
||||||
|
.buttonsActive(buttonsActive)
|
||||||
.member(memberExecutingCommand)
|
.member(memberExecutingCommand)
|
||||||
.agreeVotes(agreements)
|
.agreeVotes(agreements)
|
||||||
.disAgreeVotes(disagreements)
|
.disAgreeVotes(disagreements)
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ abstracto.featureModes.suggestionReminder.featureName=suggestion
|
|||||||
abstracto.featureModes.suggestionReminder.mode=suggestionReminder
|
abstracto.featureModes.suggestionReminder.mode=suggestionReminder
|
||||||
abstracto.featureModes.suggestionReminder.enabled=false
|
abstracto.featureModes.suggestionReminder.enabled=false
|
||||||
|
|
||||||
|
abstracto.featureModes.suggestionThread.featureName=suggestion
|
||||||
|
abstracto.featureModes.suggestionThread.mode=suggestionThread
|
||||||
|
abstracto.featureModes.suggestionThread.enabled=false
|
||||||
|
|
||||||
abstracto.systemConfigs.suggestionReminderDays.name=suggestionReminderDays
|
abstracto.systemConfigs.suggestionReminderDays.name=suggestionReminderDays
|
||||||
abstracto.systemConfigs.suggestionReminderDays.longValue=7
|
abstracto.systemConfigs.suggestionReminderDays.longValue=7
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,8 @@ public class SuggestionFeatureConfig implements FeatureConfig {
|
|||||||
return Arrays.asList(
|
return Arrays.asList(
|
||||||
SuggestionFeatureMode.SUGGESTION_REMINDER,
|
SuggestionFeatureMode.SUGGESTION_REMINDER,
|
||||||
SuggestionFeatureMode.SUGGESTION_BUTTONS,
|
SuggestionFeatureMode.SUGGESTION_BUTTONS,
|
||||||
SuggestionFeatureMode.SUGGESTION_AUTO_EVALUATE);
|
SuggestionFeatureMode.SUGGESTION_AUTO_EVALUATE,
|
||||||
|
SuggestionFeatureMode.SUGGESTION_THREAD);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ import lombok.Getter;
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public enum SuggestionFeatureMode implements FeatureMode {
|
public enum SuggestionFeatureMode implements FeatureMode {
|
||||||
SUGGESTION_REMINDER("suggestionReminder"), SUGGESTION_BUTTONS("suggestionButton"), SUGGESTION_AUTO_EVALUATE("suggestionAutoEvaluate");
|
SUGGESTION_REMINDER("suggestionReminder"),
|
||||||
|
SUGGESTION_BUTTONS("suggestionButton"),
|
||||||
|
SUGGESTION_AUTO_EVALUATE("suggestionAutoEvaluate"),
|
||||||
|
SUGGESTION_THREAD("suggestionThread");
|
||||||
|
|
||||||
private final String key;
|
private final String key;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package dev.sheldan.abstracto.suggestion.model.template;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public class SuggestionThreadModel {
|
||||||
|
private Long suggestionId;
|
||||||
|
private User suggester;
|
||||||
|
private Member member;
|
||||||
|
private String text;
|
||||||
|
private Long serverId;
|
||||||
|
private Instant autoEvaluationTargetDate;
|
||||||
|
private Boolean autoEvaluationEnabled;
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ import net.dv8tion.jda.api.entities.User;
|
|||||||
public class SuggestionUpdateModel {
|
public class SuggestionUpdateModel {
|
||||||
private Long suggestionId;
|
private Long suggestionId;
|
||||||
private SuggestionState state;
|
private SuggestionState state;
|
||||||
|
private Boolean buttonsActive;
|
||||||
private User suggester;
|
private User suggester;
|
||||||
private Member member;
|
private Member member;
|
||||||
private String text;
|
private String text;
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
private MessageService messageService;
|
private MessageService messageService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Message> sendTextInPostTarget(String text, PostTarget target) {
|
public CompletableFuture<Message> sendTextInPostTarget(String text, PostTarget target) {
|
||||||
if(target.getDisabled()) {
|
if (target.getDisabled()) {
|
||||||
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
} else {
|
} else {
|
||||||
@@ -63,46 +63,47 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Message> sendEmbedInPostTarget(MessageEmbed embed, PostTarget target) {
|
public CompletableFuture<Message> sendEmbedInPostTarget(MessageEmbed embed, PostTarget target) {
|
||||||
if(target.getDisabled()) {
|
if (target.getDisabled()) {
|
||||||
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
} else {
|
} else {
|
||||||
GuildMessageChannel messageChannelForPostTarget = getMessageChannelForPostTarget(target);
|
|
||||||
log.debug("Sending message embed to post target {}.", target.getName());
|
log.debug("Sending message embed to post target {}.", target.getName());
|
||||||
return channelService.sendEmbedToChannel(embed, messageChannelForPostTarget);
|
return getMessageChannelForPostTarget(target)
|
||||||
|
.map(channel -> channelService.sendEmbedToChannel(embed, channel))
|
||||||
|
.orElse(CompletableFuture.completedFuture(null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private GuildMessageChannel getMessageChannelForPostTarget(PostTarget target) {
|
private Optional<GuildMessageChannel> getMessageChannelForPostTarget(PostTarget target) {
|
||||||
Guild guild = botService.getInstance().getGuildById(target.getServerReference().getId());
|
Guild guild = botService.getInstance().getGuildById(target.getServerReference().getId());
|
||||||
if(guild != null) {
|
if (guild != null) {
|
||||||
GuildChannel guildChannelById = guild.getGuildChannelById(target.getChannelReference().getId());
|
GuildChannel guildChannelById = guild.getGuildChannelById(target.getChannelReference().getId());
|
||||||
if(guildChannelById instanceof GuildMessageChannel) {
|
if (guildChannelById instanceof GuildMessageChannel) {
|
||||||
return (GuildMessageChannel) guildChannelById;
|
return Optional.of((GuildMessageChannel) guildChannelById);
|
||||||
} else {
|
} else {
|
||||||
log.error("Incorrect post target configuration (it is not a message channel): {} points to {} on server {}", target.getName(),
|
log.error("Incorrect post target configuration (it is not a message channel): {} points to {} on server {}", target.getName(),
|
||||||
target.getChannelReference().getId(), target.getServerReference().getId());
|
target.getChannelReference().getId(), target.getServerReference().getId());
|
||||||
throw new ChannelNotInGuildException(target.getChannelReference().getId());
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new GuildNotFoundException(target.getServerReference().getId());
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Message> sendTextInPostTarget(String text, PostTargetEnum postTargetEnum, Long serverId) {
|
public CompletableFuture<Message> sendTextInPostTarget(String text, PostTargetEnum postTargetEnum, Long serverId) {
|
||||||
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetEnum, serverId);
|
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetEnum, serverId);
|
||||||
if(!postTargetOptional.isPresent()) {
|
if (!postTargetOptional.isPresent()) {
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
}
|
}
|
||||||
return this.sendTextInPostTarget(text, postTargetOptional.get());
|
return this.sendTextInPostTarget(text, postTargetOptional.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Message> sendEmbedInPostTarget(MessageEmbed embed, PostTargetEnum postTargetName, Long serverId) {
|
public CompletableFuture<Message> sendEmbedInPostTarget(MessageEmbed embed, PostTargetEnum postTargetName, Long serverId) {
|
||||||
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
||||||
if(!postTargetOptional.isPresent()) {
|
if (!postTargetOptional.isPresent()) {
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
}
|
}
|
||||||
return this.sendEmbedInPostTarget(embed, postTargetOptional.get());
|
return this.sendEmbedInPostTarget(embed, postTargetOptional.get());
|
||||||
@@ -111,7 +112,7 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Message> sendMessageInPostTarget(Message message, PostTargetEnum postTargetName, Long serverId) {
|
public CompletableFuture<Message> sendMessageInPostTarget(Message message, PostTargetEnum postTargetName, Long serverId) {
|
||||||
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
||||||
if(!postTargetOptional.isPresent()) {
|
if (!postTargetOptional.isPresent()) {
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
}
|
}
|
||||||
return sendMessageInPostTarget(message, postTargetOptional.get());
|
return sendMessageInPostTarget(message, postTargetOptional.get());
|
||||||
@@ -119,7 +120,7 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Message> sendMessageInPostTarget(Message message, PostTarget target) {
|
public CompletableFuture<Message> sendMessageInPostTarget(Message message, PostTarget target) {
|
||||||
if(target.getDisabled()) {
|
if (target.getDisabled()) {
|
||||||
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
} else {
|
} else {
|
||||||
@@ -129,121 +130,123 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CompletableFuture<Message>> sendEmbedInPostTarget(MessageToSend message, PostTargetEnum postTargetName, Long serverId) {
|
public List<CompletableFuture<Message>> sendEmbedInPostTarget(MessageToSend message, PostTargetEnum postTargetName, Long serverId) {
|
||||||
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
||||||
if(!postTargetOptional.isPresent()) {
|
if (!postTargetOptional.isPresent()) {
|
||||||
return Arrays.asList(CompletableFuture.completedFuture(null));
|
return Arrays.asList(CompletableFuture.completedFuture(null));
|
||||||
}
|
}
|
||||||
return this.sendEmbedInPostTarget(message, postTargetOptional.get());
|
return this.sendEmbedInPostTarget(message, postTargetOptional.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CompletableFuture<Message>> sendEmbedInPostTarget(MessageToSend message, PostTarget target) {
|
public List<CompletableFuture<Message>> sendEmbedInPostTarget(MessageToSend message, PostTarget target) {
|
||||||
if(target.getDisabled()) {
|
if (target.getDisabled()) {
|
||||||
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
||||||
return Arrays.asList(CompletableFuture.completedFuture(null));
|
return Arrays.asList(CompletableFuture.completedFuture(null));
|
||||||
} else {
|
} else {
|
||||||
GuildMessageChannel guildMessageChannel = getMessageChannelForPostTarget(target);
|
|
||||||
log.debug("Send messageToSend towards post target {}.", target.getName());
|
log.debug("Send messageToSend towards post target {}.", target.getName());
|
||||||
return channelService.sendMessageToSendToChannel(message, guildMessageChannel);
|
return getMessageChannelForPostTarget(target)
|
||||||
|
.map(channel -> channelService.sendMessageToSendToChannel(message, channel))
|
||||||
|
.orElse(Arrays.asList(CompletableFuture.completedFuture(null)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, PostTarget target) {
|
public List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, PostTarget target) {
|
||||||
if(target.getDisabled()) {
|
if (target.getDisabled()) {
|
||||||
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
||||||
return Arrays.asList(CompletableFuture.completedFuture(null));
|
return Arrays.asList(CompletableFuture.completedFuture(null));
|
||||||
} else {
|
} else {
|
||||||
GuildMessageChannel guildMessageChannel = getMessageChannelForPostTarget(target);
|
return getMessageChannelForPostTarget(target).map(messageChannel -> {
|
||||||
// always takes the first one, only applicable for this scenario
|
// always takes the first one, only applicable for this scenario
|
||||||
String messageText = message.getMessages().get(0);
|
String messageText = message.getMessages().get(0);
|
||||||
if(StringUtils.isBlank(messageText)) {
|
if (StringUtils.isBlank(messageText)) {
|
||||||
log.debug("Editing embeds of message {} in post target {}.", messageId, target.getName());
|
log.debug("Editing embeds of message {} in post target {}.", messageId, target.getName());
|
||||||
return Arrays.asList(channelService.editEmbedMessageInAChannel(message.getEmbeds().get(0), guildMessageChannel, messageId));
|
return Arrays.asList(channelService.editEmbedMessageInAChannel(message.getEmbeds().get(0), messageChannel, messageId));
|
||||||
} else {
|
} else {
|
||||||
log.debug("Editing message text and potentially text for message {} in post target {}.", messageId, target.getName());
|
log.debug("Editing message text and potentially text for message {} in post target {}.", messageId, target.getName());
|
||||||
return Arrays.asList(channelService.editTextMessageInAChannel(messageText, message.getEmbeds().get(0), guildMessageChannel, messageId));
|
return Arrays.asList(channelService.editTextMessageInAChannel(messageText, message.getEmbeds().get(0), messageChannel, messageId));
|
||||||
}
|
}
|
||||||
|
}).orElse(Arrays.asList(CompletableFuture.completedFuture(null)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTarget target) {
|
public List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTarget target) {
|
||||||
if(target.getDisabled()) {
|
if (target.getDisabled()) {
|
||||||
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
log.info("Post target {} has been disabled in server {} - not sending message.", target.getName(), target.getServerReference().getId());
|
||||||
return Arrays.asList(CompletableFuture.completedFuture(null));
|
return Arrays.asList(CompletableFuture.completedFuture(null));
|
||||||
} else {
|
} else {
|
||||||
List<CompletableFuture<Message>> futures = new ArrayList<>();
|
List<CompletableFuture<Message>> futures = new ArrayList<>();
|
||||||
GuildMessageChannel guildMessageChannel = getMessageChannelForPostTarget(target);
|
return getMessageChannelForPostTarget(target).map(messageChannel -> {
|
||||||
CompletableFuture<Message> messageEditFuture = new CompletableFuture<>();
|
CompletableFuture<Message> messageEditFuture = new CompletableFuture<>();
|
||||||
futures.add(messageEditFuture);
|
futures.add(messageEditFuture);
|
||||||
if (StringUtils.isBlank(messageToSend.getMessages().get(0).trim())) {
|
if (StringUtils.isBlank(messageToSend.getMessages().get(0).trim())) {
|
||||||
channelService.retrieveMessageInChannel(guildMessageChannel, messageId).thenAccept(message -> {
|
channelService.retrieveMessageInChannel(messageChannel, messageId).thenAccept(message -> {
|
||||||
log.debug("Editing existing message {} when upserting message embeds in channel {} in server {}.",
|
log.debug("Editing existing message {} when upserting message embeds in channel {} in server {}.",
|
||||||
messageId, guildMessageChannel.getIdLong(), guildMessageChannel.getGuild().getId());
|
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||||
messageService.editMessage(message, messageToSend.getEmbeds().get(0))
|
messageService.editMessage(message, messageToSend.getEmbeds().get(0))
|
||||||
.queue(messageEditFuture::complete, messageEditFuture::completeExceptionally);
|
.queue(messageEditFuture::complete, messageEditFuture::completeExceptionally);
|
||||||
}).exceptionally(throwable -> {
|
}).exceptionally(throwable -> {
|
||||||
log.debug("Creating new message when upserting message embeds for message {} in channel {} in server {}.",
|
log.debug("Creating new message when upserting message embeds for message {} in channel {} in server {}.",
|
||||||
messageId, guildMessageChannel.getIdLong(), guildMessageChannel.getGuild().getId());
|
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||||
sendEmbedInPostTarget(messageToSend, target).get(0)
|
sendEmbedInPostTarget(messageToSend, target).get(0)
|
||||||
.thenAccept(messageEditFuture::complete).exceptionally(innerThrowable -> {
|
.thenAccept(messageEditFuture::complete).exceptionally(innerThrowable -> {
|
||||||
log.error("Failed to send message to create a message.", innerThrowable);
|
log.error("Failed to send message to create a message.", innerThrowable);
|
||||||
messageEditFuture.completeExceptionally(innerThrowable);
|
messageEditFuture.completeExceptionally(innerThrowable);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
return null;
|
} else {
|
||||||
});
|
channelService.retrieveMessageInChannel(messageChannel, messageId).thenAccept(message -> {
|
||||||
} else {
|
log.debug("Editing existing message {} when upserting message in channel {} in server {}.",
|
||||||
channelService.retrieveMessageInChannel(guildMessageChannel, messageId).thenAccept(message -> {
|
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||||
log.debug("Editing existing message {} when upserting message in channel {} in server {}.",
|
messageService.editMessage(message, messageToSend.getMessages().get(0), messageToSend.getEmbeds().get(0))
|
||||||
messageId, guildMessageChannel.getIdLong(), guildMessageChannel.getGuild().getId());
|
.queue(messageEditFuture::complete, messageEditFuture::completeExceptionally);
|
||||||
messageService.editMessage(message, messageToSend.getMessages().get(0), messageToSend.getEmbeds().get(0))
|
}).exceptionally(throwable -> {
|
||||||
.queue(messageEditFuture::complete, messageEditFuture::completeExceptionally);
|
log.debug("Creating new message when trying to upsert a message {} in channel {} in server {}.",
|
||||||
}).exceptionally(throwable -> {
|
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||||
log.debug("Creating new message when trying to upsert a message {} in channel {} in server {}.",
|
sendEmbedInPostTarget(messageToSend, target).get(0)
|
||||||
messageId, guildMessageChannel.getIdLong(), guildMessageChannel.getGuild().getId());
|
.thenAccept(messageEditFuture::complete).exceptionally(innerThrowable -> {
|
||||||
sendEmbedInPostTarget(messageToSend, target).get(0)
|
log.error("Failed to send message to create a message.", innerThrowable);
|
||||||
.thenAccept(messageEditFuture::complete).exceptionally(innerThrowable -> {
|
messageEditFuture.completeExceptionally(innerThrowable);
|
||||||
log.error("Failed to send message to create a message.", innerThrowable);
|
return null;
|
||||||
messageEditFuture.completeExceptionally(innerThrowable);
|
});
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
return null;
|
}
|
||||||
});
|
return futures;
|
||||||
}
|
}).orElse(Arrays.asList(CompletableFuture.completedFuture(null)));
|
||||||
|
|
||||||
return futures;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTargetEnum postTargetName, Long serverId) {
|
public List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTargetEnum postTargetName, Long serverId) {
|
||||||
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
||||||
if(!postTargetOptional.isPresent()) {
|
if (!postTargetOptional.isPresent()) {
|
||||||
return Arrays.asList(CompletableFuture.completedFuture(null));
|
return Arrays.asList(CompletableFuture.completedFuture(null));
|
||||||
}
|
}
|
||||||
return this.editOrCreatedInPostTarget(messageId, messageToSend, postTargetOptional.get());
|
return this.editOrCreatedInPostTarget(messageId, messageToSend, postTargetOptional.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void throwIfPostTargetIsNotDefined(PostTargetEnum name, Long serverId) {
|
public void throwIfPostTargetIsNotDefined(PostTargetEnum targetEnum, Long serverId) {
|
||||||
Optional<PostTarget> postTargetOptional = getPostTarget(name, serverId);
|
Optional<PostTarget> postTargetOptional = getPostTarget(targetEnum, serverId);
|
||||||
if(!postTargetOptional.isPresent()) {
|
if (!postTargetOptional.isPresent()) {
|
||||||
throw new PostTargetNotValidException(name.getKey(), defaultPostTargetManagementService.getDefaultPostTargetKeys());
|
throw new PostTargetNotValidException(targetEnum.getKey(), defaultPostTargetManagementService.getDefaultPostTargetKeys());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean postTargetDefinedInServer(PostTargetEnum name, Long serverId) {
|
public boolean postTargetDefinedInServer(PostTargetEnum targetEnum, Long serverId) {
|
||||||
return postTargetManagement.postTargetExists(name.getKey(), serverId);
|
return postTargetManagement.postTargetExists(targetEnum.getKey(), serverId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, PostTargetEnum postTargetName, Long serverId) {
|
public List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, PostTargetEnum postTargetName, Long serverId) {
|
||||||
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
Optional<PostTarget> postTargetOptional = getPostTarget(postTargetName, serverId);
|
||||||
if(!postTargetOptional.isPresent()) {
|
if (!postTargetOptional.isPresent()) {
|
||||||
return Arrays.asList(CompletableFuture.completedFuture(null));
|
return Arrays.asList(CompletableFuture.completedFuture(null));
|
||||||
}
|
}
|
||||||
return editEmbedInPostTarget(messageId, message, postTargetOptional.get());
|
return editEmbedInPostTarget(messageId, message, postTargetOptional.get());
|
||||||
@@ -260,15 +263,15 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validatePostTarget(PostTargetEnum name, Long serverId) {
|
public void validatePostTarget(PostTargetEnum targetEnum, Long serverId) {
|
||||||
if(!postTargetUsableInServer(name, serverId)) {
|
if (!postTargetUsableInServer(targetEnum, serverId)) {
|
||||||
throw new PostTargetNotUsableException(name.getKey());
|
throw new PostTargetNotUsableException(targetEnum.getKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean postTargetUsableInServer(PostTargetEnum name, Long serverId) {
|
public boolean postTargetUsableInServer(PostTargetEnum targetEnum, Long serverId) {
|
||||||
Optional<PostTarget> postTargetOptional = getPostTarget(name, serverId);
|
Optional<PostTarget> postTargetOptional = getPostTarget(targetEnum, serverId);
|
||||||
return postTargetOptional.isPresent() && !postTargetOptional.get().getDisabled();
|
return postTargetOptional.isPresent() && !postTargetOptional.get().getDisabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,7 +290,7 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
List<String> postTargets = new ArrayList<>();
|
List<String> postTargets = new ArrayList<>();
|
||||||
List<FeatureConfig> allFeatureConfigs = featureConfigService.getAllFeatureConfigs();
|
List<FeatureConfig> allFeatureConfigs = featureConfigService.getAllFeatureConfigs();
|
||||||
allFeatureConfigs.forEach(featureConfig -> {
|
allFeatureConfigs.forEach(featureConfig -> {
|
||||||
if(featureFlagService.isFeatureEnabled(featureConfig, server)) {
|
if (featureFlagService.isFeatureEnabled(featureConfig, server)) {
|
||||||
featureConfig.getRequiredPostTargets().forEach(postTargetEnum -> postTargets.add(postTargetEnum.getKey()));
|
featureConfig.getRequiredPostTargets().forEach(postTargetEnum -> postTargets.add(postTargetEnum.getKey()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -305,4 +308,16 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
PostTarget postTarget = postTargetManagement.getPostTarget(name, serverId);
|
PostTarget postTarget = postTargetManagement.getPostTarget(name, serverId);
|
||||||
postTarget.setDisabled(false);
|
postTarget.setDisabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<GuildMessageChannel> getPostTargetChannel(PostTargetEnum postTargetEnum, Long serverId) {
|
||||||
|
Optional<PostTarget> postTarget = getPostTarget(postTargetEnum, serverId);
|
||||||
|
return postTarget.flatMap(target -> {
|
||||||
|
if (target.getDisabled()) {
|
||||||
|
return Optional.empty();
|
||||||
|
} else {
|
||||||
|
return getMessageChannelForPostTarget(target);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ import dev.sheldan.abstracto.core.models.database.PostTarget;
|
|||||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||||
import net.dv8tion.jda.api.entities.Message;
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
|
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public interface PostTargetService {
|
public interface PostTargetService {
|
||||||
@@ -23,14 +25,15 @@ public interface PostTargetService {
|
|||||||
List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, PostTargetEnum postTargetName, Long serverId);
|
List<CompletableFuture<Message>> editEmbedInPostTarget(Long messageId, MessageToSend message, PostTargetEnum postTargetName, Long serverId);
|
||||||
List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTarget target);
|
List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTarget target);
|
||||||
List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTargetEnum postTarget, Long serverId);
|
List<CompletableFuture<Message>> editOrCreatedInPostTarget(Long messageId, MessageToSend messageToSend, PostTargetEnum postTarget, Long serverId);
|
||||||
void throwIfPostTargetIsNotDefined(PostTargetEnum name, Long serverId);
|
void throwIfPostTargetIsNotDefined(PostTargetEnum targetEnum, Long serverId);
|
||||||
boolean postTargetDefinedInServer(PostTargetEnum name, Long serverId);
|
boolean postTargetDefinedInServer(PostTargetEnum targetEnum, Long serverId);
|
||||||
boolean validPostTarget(String name);
|
boolean validPostTarget(String name);
|
||||||
void validatePostTarget(PostTargetEnum name, Long serverId);
|
void validatePostTarget(PostTargetEnum targetEnum, Long serverId);
|
||||||
boolean postTargetUsableInServer(PostTargetEnum name, Long serverId);
|
boolean postTargetUsableInServer(PostTargetEnum targetEnum, Long serverId);
|
||||||
List<PostTarget> getPostTargets(AServer server);
|
List<PostTarget> getPostTargets(AServer server);
|
||||||
List<String> getAvailablePostTargets();
|
List<String> getAvailablePostTargets();
|
||||||
List<String> getPostTargetsOfEnabledFeatures(AServer server);
|
List<String> getPostTargetsOfEnabledFeatures(AServer server);
|
||||||
void disablePostTarget(String name, Long serverId);
|
void disablePostTarget(String name, Long serverId);
|
||||||
void enablePostTarget(String name, Long serverId);
|
void enablePostTarget(String name, Long serverId);
|
||||||
|
Optional<GuildMessageChannel> getPostTargetChannel(PostTargetEnum postTargetEnum, Long serverId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user