[AB-60] improving java doc for assignable role module

This commit is contained in:
Sheldan
2021-03-03 23:51:26 +01:00
parent 373dfac001
commit 9c6333281b
65 changed files with 1357 additions and 210 deletions

View File

@@ -17,6 +17,9 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to activate an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class ActivateAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -23,6 +23,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to add an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole assignableRole}
* to an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class AddRoleToAssignableRolePost extends AbstractConditionableCommand {

View File

@@ -20,6 +20,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to change one attribute of an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
@Slf4j
public class ChangeAssignableRolePlaceConfig extends AbstractConditionableCommand {

View File

@@ -13,7 +13,6 @@ import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import net.dv8tion.jda.api.entities.TextChannel;
@@ -42,8 +41,7 @@ public class CreateAssignableRolePost extends AbstractConditionableCommand {
TextChannel channel = (TextChannel) parameters.get(1);
String text = (String) parameters.get(2);
AChannel chosenChannel = channelManagementService.loadChannel(channel.getIdLong());
AServer server = serverManagementService.loadServer(commandContext.getGuild());
service.createAssignableRolePlace(server, name, chosenChannel, text);
service.createAssignableRolePlace(name, chosenChannel, text);
return CommandResult.fromSuccess();
}

View File

@@ -17,6 +17,9 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to deactive an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class DeactivateAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -18,6 +18,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to delete an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class DeleteAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -21,7 +21,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to change the text of an {@link AssignableRolePlace place}
*/
@Component
public class EditAssignableRolePlaceText extends AbstractConditionableCommand {
@@ -37,7 +39,7 @@ public class EditAssignableRolePlaceText extends AbstractConditionableCommand {
String name = (String) parameters.get(0);
String newText = (String) parameters.get(1);
AServer server = serverManagementService.loadServer(commandContext.getGuild());
return service.changeText(server, name, newText)
return service.changeTextAsync(server, name, newText)
.thenApply(aVoid -> CommandResult.fromSuccess());
}

View File

@@ -9,7 +9,6 @@ import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +17,10 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to move an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
* to another {@link dev.sheldan.abstracto.core.models.database.AChannel channel}
*/
@Component
public class MoveAssignableRolePlace extends AbstractConditionableCommand {
@@ -32,8 +35,7 @@ public class MoveAssignableRolePlace extends AbstractConditionableCommand {
List<Object> parameters = commandContext.getParameters().getParameters();
String name = (String) parameters.get(0);
TextChannel newChannel = (TextChannel) parameters.get(1);
AServer server = serverManagementService.loadServer(commandContext.getGuild());
placeManagementService.moveAssignableRolePlace(server, name, newChannel);
placeManagementService.moveAssignableRolePlace(name, newChannel);
return CommandResult.fromSuccess();
}

View File

@@ -19,6 +19,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to remove a {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole role}
* from an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class RemoveRoleFromAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -18,6 +18,10 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to set the position of one individual {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole assignableRole}
* within an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class SetAssignableRolePosition extends AbstractConditionableCommand {

View File

@@ -20,6 +20,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to render the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
* and create the {@link net.dv8tion.jda.api.entities.Message messages} used to add {@link net.dv8tion.jda.api.entities.MessageReaction reactions} to
*/
@Component
@Slf4j
public class SetupAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -16,7 +16,11 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to show the configuration of an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class ShowAssignableRolePlaceConfig extends AbstractConditionableCommand {
@@ -28,13 +32,13 @@ public class ShowAssignableRolePlaceConfig extends AbstractConditionableCommand
private ServerManagementService serverManagementService;
@Override
public CommandResult execute(CommandContext commandContext) {
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
String name = (String) parameters.get(0);
AServer server = serverManagementService.loadServer(commandContext.getGuild());
// TODO refactor to return something to be posted in this command here instead of relying it to be posted somewhere else
service.showAssignablePlaceConfig(server, name, commandContext.getChannel());
return CommandResult.fromIgnored();
return service.showAssignablePlaceConfig(server, name, commandContext.getChannel())
.thenApply(unused -> CommandResult.fromIgnored());
}
@Override
@@ -46,6 +50,7 @@ public class ShowAssignableRolePlaceConfig extends AbstractConditionableCommand
.name("showAssignableRolePlaceConfig")
.module(AssignableRoleModule.ASSIGNABLE_ROLES)
.templated(true)
.async(true)
.causesReaction(true)
.supportsEmbedException(true)
.parameters(parameters)

View File

@@ -15,7 +15,10 @@ import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;
/**
* Command used to show all {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace places}
* within the current {@link AServer server}
*/
@Component
public class ShowAssignableRolePlaces extends AbstractConditionableCommand {

View File

@@ -20,6 +20,10 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to swap the positions of two {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole roles}
* within one {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class SwapAssignableRolePosition extends AbstractConditionableCommand {

View File

@@ -18,6 +18,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to show how an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
* would look like in the current {@link net.dv8tion.jda.api.entities.TextChannel channel}
*/
@Component
public class TestAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -20,6 +20,10 @@ public class AssignablePostDeletedListener implements AsyncMessageDeletedListene
@Autowired
private AssignableRolePlacePostManagementService service;
/**
* This method deletes one individual {@link AssignableRolePlacePost post}, because its message has been deleted
* @param messageBefore The {@link CachedMessage message} which was deleted
*/
@Override
public void execute(CachedMessage messageBefore) {
Optional<AssignableRolePlacePost> messageOptional = service.findByMessageIdOptional(messageBefore.getMessageId());

View File

@@ -69,6 +69,16 @@ public class AssignablePostReactionAdded implements AsyncReactionAddedListener {
@Autowired
private MemberService memberService;
/**
* Determines if the {@link net.dv8tion.jda.api.entities.Message message} a reaction was added to, belongs to a
* {@link AssignableRolePlacePost post}.
* If the {@link AssignableRolePlacePost post} belong to an inactive {@link AssignableRolePlace place} this method
* will automatically remove the reaction, self reactions are ignored. Otherwise the logic according to the configuration
* of the {@link AssignableRolePlace place} will be executed.
* @param message The {@link CachedMessage message} on which a reaction was added
* @param cachedReaction The {@link CachedReaction reaction} which was added
* @param serverUser The {@link ServerUser serverUser} who added the reaction
*/
@Override
public void executeReactionAdded(CachedMessage message, CachedReactions cachedReaction, ServerUser serverUser) {
Optional<AssignableRolePlacePost> messageOptional = service.findByMessageIdOptional(message.getMessageId());
@@ -79,13 +89,13 @@ public class AssignablePostReactionAdded implements AsyncReactionAddedListener {
return;
}
CachedReaction specificReaction = cachedReaction.getReactionForSpecificUser(serverUser);
Long assignableRolePlaceId = assignablePlacePost.getId();
Long assignableRolePlacePostId = assignablePlacePost.getId();
if(assignablePlacePost.getAssignablePlace().getActive()) {
log.info("User {} added reaction to assignable role place {} in server {}. Handling added event.", serverUser.getUserId(), assignablePlacePost.getId(), serverUser.getServerId());
addAppropriateRoles(specificReaction, assignablePlacePost, serverUser, message);
} else {
reactionService.removeReactionFromMessage(specificReaction, message).exceptionally(throwable -> {
log.error("Failed to remove reaction on place {} because place is inactive.", assignableRolePlaceId, throwable);
log.error("Failed to remove reaction on place post {} because place is inactive.", assignableRolePlacePostId, throwable);
return null;
});
log.trace("Reaction for assignable place {} in sever {} was added, but place is inactive.", assignablePlacePost.getAssignablePlace().getKey(), serverUser.getServerId());
@@ -93,6 +103,19 @@ public class AssignablePostReactionAdded implements AsyncReactionAddedListener {
}
}
/**
* Iterates over all {@link AssignableRole assignableRoles} of the post and checks which {@link AssignableRole assignableRole}
* is identified by the added {@link CachedReaction reaction}. If there is no valid reaction, the {@link net.dv8tion.jda.api.entities.MessageReaction reaction}
* will be removed again. In case the {@link AssignableRolePlace place} is configured to have unique roles, this will remove the existing
* {@link net.dv8tion.jda.api.entities.MessageReaction reaction} and the assigned {@link net.dv8tion.jda.api.entities.Role role}.
* Afterwards the appropriate {@link net.dv8tion.jda.api.entities.Role role} will be added and the update
* will be stored in the database.
* @param cachedReaction The {@link CachedReaction reaction} which was added
* @param assignablePlacePost The {@link AssignableRolePlacePost post} onto which the {@link CachedReaction reaction} was added to
* @param serverUser The {@link ServerUser serverUser} who added the {@link CachedReaction reaction}
* @param message The {@link CachedMessage message} onto which the {@link net.dv8tion.jda.api.entities.MessageReaction reaction}
* was added
*/
private void addAppropriateRoles(CachedReaction cachedReaction, AssignableRolePlacePost assignablePlacePost, ServerUser serverUser, CachedMessage message) {
boolean validReaction = false;
AssignableRolePlace assignableRolePlace = assignablePlacePost.getAssignablePlace();
@@ -128,6 +151,12 @@ public class AssignablePostReactionAdded implements AsyncReactionAddedListener {
});
}
/**
* Persists the {@link AssignableRole role} changes for the user who added a reaction in the database
* @param assignableRolePlaceId The ID of the {@link AssignableRolePlace place}
* @param serverUser The {@link ServerUser serverUser} who added the {@link net.dv8tion.jda.api.entities.MessageReaction reaction}
* @param cachedReaction The {@link CachedReaction reaction} wich was added
*/
@Transactional
public void updateStoredAssignableRoles(Long assignableRolePlaceId, ServerUser serverUser, CachedReaction cachedReaction) {
AssignableRolePlace place = assignableRolePlaceManagementService.findByPlaceId(assignableRolePlaceId);

View File

@@ -1,6 +1,7 @@
package dev.sheldan.abstracto.assignableroles.listener;
import dev.sheldan.abstracto.assignableroles.config.features.AssignableRoleFeature;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost;
import dev.sheldan.abstracto.assignableroles.service.AssignableRoleService;
import dev.sheldan.abstracto.assignableroles.service.management.AssignableRolePlacePostManagementService;
@@ -38,6 +39,16 @@ public class AssignablePostReactionRemoved implements AsyncReactionRemovedListen
return AssignableRoleFeature.ASSIGNABLE_ROLES;
}
/**
* Determines if the {@link net.dv8tion.jda.api.entities.Message message} a reaction was removed from, belongs to a
* {@link AssignableRolePlacePost post}.
* If the {@link AssignableRolePlacePost post} belong to an inactive {@link AssignableRolePlace place} this method ignores the removal.
* Otherwise the logic according to the configuration
* of the {@link AssignableRolePlace place} will be executed.
* @param message The {@link CachedMessage message} on which a reaction was added
* @param reactions All the reactions which are currently known to be on the {@link CachedMessage message}
* @param userRemoving The {@link ServerUser serverUser} which removed a {@link net.dv8tion.jda.api.entities.MessageReaction reaction}
*/
@Override
public void executeReactionRemoved(CachedMessage message, CachedReactions reactions, ServerUser userRemoving) {
Optional<AssignableRolePlacePost> messageOptional = service.findByMessageIdOptional(message.getMessageId());

View File

@@ -1,16 +1,12 @@
package dev.sheldan.abstracto.assignableroles.repository;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/**
* Repository to manage the access to the table managed by {@link AssignableRolePlacePost posts}
*/
@Repository
public interface AssignableRolePlacePostRepository extends JpaRepository<AssignableRolePlacePost, Long> {
@NotNull
@Override
Optional<AssignableRolePlacePost> findById(@NonNull Long aLong);
}

View File

@@ -2,24 +2,40 @@ package dev.sheldan.abstracto.assignableroles.repository;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
import dev.sheldan.abstracto.core.models.database.AServer;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
/**
* Repository to manage the access to the table managed by {@link AssignableRolePlace place}
*/
@Repository
public interface AssignableRolePlaceRepository extends JpaRepository<AssignableRolePlace, Long> {
/**
* Whether or not an {@link AssignableRolePlace place} exists in a {@link AServer server} with the given key
* @param server The {@link AServer server} to search in
* @param key The key to search for
* @return Whether or not a {@link AssignableRolePlace place} exists in the {@link AServer server} with the key
*/
boolean existsByServerAndKey(AServer server, String key);
/**
* Finds an {@link AssignableRolePlace place} in a {@link AServer server} with the given key,
* returns an empty {@link Optional optional} otherwise
* @param server The {@link AServer server} to search in
* @param key The key to search for
* @return An {@link Optional optional} containing the {@link AssignableRolePlace place} if it exists, empty otherwise.
*/
Optional<AssignableRolePlace> findByServerAndKey(AServer server, String key);
/**
* Finds all {@link AssignableRolePlace places} in the given {@link AServer server}
* @param server The {@link AServer server} to retrieve {@link AssignableRolePlace places} for
* @return A list of {@link AssignableRolePlace places} which were found in the given {@link AServer server}
*/
List<AssignableRolePlace> findByServer(AServer server);
@NotNull
@Override
Optional<AssignableRolePlace> findById(@NonNull Long aLong);
}

View File

@@ -1,16 +1,12 @@
package dev.sheldan.abstracto.assignableroles.repository;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRole;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/**
* Repository to manage the access to the table managed by {@link AssignableRole assignableRole}
*/
@Repository
public interface AssignableRoleRepository extends JpaRepository<AssignableRole, Long> {
@NotNull
@Override
Optional<AssignableRole> findById(@NonNull Long aLong);
}

View File

@@ -1,19 +1,12 @@
package dev.sheldan.abstracto.assignableroles.repository;
import dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/**
* Repository to manage the access to the table managed by {@link AssignedRoleUser assignedRoleUser}
*/
@Repository
public interface AssignedRoleUserRepository extends JpaRepository<AssignedRoleUser, Long> {
@NotNull
@Override
Optional<AssignedRoleUser> findById(@NonNull Long aLong);
@Override
boolean existsById(@NonNull Long aLong);
}

View File

@@ -2,7 +2,7 @@ package dev.sheldan.abstracto.assignableroles.service;
import dev.sheldan.abstracto.assignableroles.config.AssignableRolePlaceParameterKey;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceAlreadyExistsException;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceChannelDoesNotExist;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceChannelDoesNotExistException;
import dev.sheldan.abstracto.assignableroles.exceptions.EmoteNotInAssignableRolePlaceException;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRole;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
@@ -94,17 +94,17 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
private AssignableRolePlacePostManagementServiceBean assignableRolePlacePostManagementServiceBean;
@Override
public void createAssignableRolePlace(AServer server, String name, AChannel channel, String text) {
if(rolePlaceManagementService.doesPlaceExist(server, name)) {
public void createAssignableRolePlace(String name, AChannel channel, String text) {
if(rolePlaceManagementService.doesPlaceExist(channel.getServer(), name)) {
throw new AssignableRolePlaceAlreadyExistsException(name);
}
rolePlaceManagementService.createPlace(server, name, channel, text);
rolePlaceManagementService.createPlace(name, channel, text);
}
@Override
public boolean hasAssignableRolePlaceEmote(AServer server, String placeName, AEmote emote) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByServerAndKey(server, placeName);
return hasAssignableRolePlaceEmote(assignableRolePlace, emote);
return hasAssignableRolePlaceEmote(assignableRolePlace, emote);
}
@Override
@@ -186,7 +186,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
} else {
log.trace("Adding new post to assignable role place {} in channel {} in server {}.",
placeId, assignableRolePlace.getChannel().getId(), server.getId());
return addNewMessageToAssignableRolePlace(placeId, fakeEmote, description, roleId, serverId, messageToSend, textChannel);
return addNewMessageToAssignableRolePlace(placeId, fakeEmote, description, roleId, messageToSend, textChannel);
}
} else {
throw new ChannelNotInGuildException(latestPost.getUsedChannel().getId());
@@ -201,6 +201,19 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
return CompletableFuture.completedFuture(null);
}
/**
* Adds the reaction from {@link FullEmote} to the last {@link AssignableRolePlacePost post} of its {@link AssignableRolePlace place}.
* It will also update the message representing the last {@link AssignableRolePlacePost post} with the given {@link MessageToSend messageToSend}
* in the given {@link TextChannel channel}. Afterwards the added {@link AssignableRole assignableRole} will be persisted in the database
* @param fakeEmote The *fake* {@link FullEmote emote} which represents the reaction which should be added and identifies the {@link AssignableRole role}
* @param description The description the {@link AssignableRole role} should have
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param latestPost The last {@link AssignableRolePlacePost post} in the list of posts
* @param messageToSend The {@link MessageToSend messageToSend} which should be used to update the latest {@link AssignableRolePlacePost post} with. This
* will be done in place.
* @param textChannel The {@link TextChannel channel} in which the {@link AssignableRolePlacePost post} is
* @return A {@link CompletableFuture future} which will complete when the reaction was added, the message was edited and the database has been updated
*/
private CompletableFuture<Void> addReactionToExistingAssignableRolePlacePost(FullEmote fakeEmote, String description, Long roleId, AssignableRolePlacePost latestPost, MessageToSend messageToSend, TextChannel textChannel) {
// TODO maybe refactor to use the same message object, so we dont need to retrieve it twice and do in parallel
Long serverId = latestPost.getAssignablePlace().getServer().getId();
@@ -220,20 +233,43 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
);
}
private CompletableFuture<Void> addNewMessageToAssignableRolePlace(Long placeId, FullEmote fakeEmote, String description, Long roleId, Long serverId, MessageToSend messageToSend, TextChannel textChannel) {
/**
* Sends a new {@link Message message}, defined by the given {@link MessageToSend messageToSend}, to the channel the {@link AssignableRolePlace place}
* is configured for, adds a reaction and stores the update in the database.
* @param placeId The ID of the {@link AssignableRolePlace place} to add a post to
* @param fakeEmote The *fake* {@link FullEmote emote} which represents the reaction which should be added and identifies the {@link AssignableRole role}
* @param description The description the {@link AssignableRole role} should have
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param messageToSend The {@link MessageToSend messageToSend} which should be used to create the new {@link AssignableRolePlacePost post}
* @param textChannel The The {@link TextChannel channel} in which the {@link AssignableRolePlacePost post} should be in
* @return A {@link CompletableFuture future} which competes when the {@link MessageToSend messageToSend} has been sent, a {@link MessageReaction reaction}
* has been added and the changes have been persisted in the database.
*/
private CompletableFuture<Void> addNewMessageToAssignableRolePlace(Long placeId, FullEmote fakeEmote, String description, Long roleId, MessageToSend messageToSend, TextChannel textChannel) {
MessageEmbed embedToUse = messageToSend.getEmbeds().get(messageToSend.getEmbeds().size() - 1);
Long serverId = textChannel.getGuild().getIdLong();
return channelService.sendEmbedToChannel(embedToUse, textChannel)
.thenCompose(message -> {
log.trace("Adding reaction for role {} to newly created message {} for assignable role place {} in server {}.", roleId, message.getId(), placeId, serverId);
return reactionService.addReactionToMessageAsync(fakeEmote.getFakeEmote(), serverId, message)
.thenAccept(aVoid ->
self.addNewlyCreatedAssignablePlacePost(placeId, description, roleId, serverId, message, fakeEmote)
self.addNewlyCreatedAssignablePlacePost(placeId, description, roleId, message, fakeEmote)
);
});
}
/**
* Persisted the newly created {@link AssignableRolePlacePost post} and adds a {@link AssignableRole assignableRolePlace} to this
* @param placeId The ID of the {@link AssignableRolePlace place} to add a post to
* @param description The description the {@link AssignableRole role} should have
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param message The {@link Message message} which was created when sending the {@link MessageToSend messageToSend} representing this
* {@link AssignableRolePlacePost post}
* @param fakeEmote The *fake* {@link FullEmote emote} which is used to identify the {@link AssignableRole role}
*/
@Transactional
public void addNewlyCreatedAssignablePlacePost(Long placeId, String description,Long roleId, Long serverId, Message message, FullEmote fakeEmote) {
public void addNewlyCreatedAssignablePlacePost(Long placeId, String description, Long roleId, Message message, FullEmote fakeEmote) {
Long serverId = message.getGuild().getIdLong();
log.info("Storing newly created assignable role place post {} for place {} in server {}.", message.getId(), placeId, serverId);
ARole role = roleManagementService.findRole(roleId);
AssignableRolePlace loadedPlace = rolePlaceManagementService.findByPlaceId(placeId);
@@ -245,6 +281,18 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
assignableRoleManagementServiceBean.addRoleToPlace(loadedPlace, emote, role, description, newPost);
}
/**
* Actually creates the {@link AEmote emote} defined by the given {@link FullEmote emote} and adds a new instance
* of an {@link AssignableRole assignableRole} to the {@link AssignableRolePlace place}. This is used in case
* there already exists an {@link AssignableRolePlacePost post}
* @param messageId The ID of the {@link Message message} which identifies the {@link AssignableRolePlacePost post}
* at which this {@link AssignableRole role} is available via a {@link MessageReaction reaction}
* @param placeId The ID of the {@link AssignableRolePlace place} to add the {@link AssignableRole assignableRole} to
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param description The description the {@link AssignableRole role} should have
* @param fakeEmote The *fake* {@link FullEmote emote} which is used to identify the {@link AssignableRole role}
* @param serverId The ID of the {@link AServer server} this should be persisted for
*/
@Transactional
public void addAssignableRoleInstanceWithPost(Long messageId, Long placeId, Long roleId, String description, FullEmote fakeEmote, Long serverId) {
log.info("Storing newly created assignable role {} to post {} to assignable role place {} in server {}.", roleId, messageId, placeId, serverId);
@@ -254,6 +302,16 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
assignableRoleManagementServiceBean.addRoleToPlace(placeId, emote.getId(), roleId, description, messageId);
}
/**
* Actually creates the {@link AEmote emote} defined by the given {@link FullEmote emote} and adds a new instance
* of an {@link AssignableRole assignableRole} to the {@link AssignableRolePlace place}. This is used in case
* there doesnt exists an {@link AssignableRolePlacePost post}
* @param placeId The ID of the {@link AssignableRolePlace place} to add the {@link AssignableRole assignableRole} to
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param description The description the {@link AssignableRole role} should have
* @param fakeEmote The *fake* {@link FullEmote emote} which is used to identify the {@link AssignableRole role}
* @param serverId The ID of the {@link AServer server} this should be persisted for
*/
@Transactional
public void addAssignableRoleInstanceWithoutPost(Long placeId, Long roleId, FullEmote fakeEmote, String description, Long serverId) {
log.info("Storing newly created assignable role {} without post to assignable role place {} in server {}.", roleId, placeId, serverId);
@@ -279,6 +337,12 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
return CompletableFuture.completedFuture(null);
}
/**
* This method deletes the {@link AssignableRole assignableRole} from the given {@link AssignableRolePlace place}
* in the database
* @param placeId The ID of the {@link AssignableRolePlace place} to remove the {@link AssignableRole assignableRole} from
* @param assignableRoleId The ID of the {@link AssignableRole assignableRole} to remove from the {@link AssignableRolePlace place}
*/
@Transactional
public void deleteAssignableRoleFromPlace(Long placeId, Long assignableRoleId) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByPlaceId(placeId);
@@ -292,22 +356,31 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
});
}
private CompletableFuture<Void> removeRoleFromAssignablePlace(AssignableRole role, AssignableRolePlace assignableRolePlace) {
AssignableRolePlacePost post = role.getAssignableRolePlacePost();
/**
* The removes the {@link AssignableRole assignableRole} from the given {@link AssignableRolePlace place}. If
* there already is a {@link AssignableRolePlacePost post}, this removes the field from the post
* and also clears all {@link MessageReaction reactions}
* @param assignableRole The {@link AssignableRole role} to remove
* @param assignableRolePlace The {@link AssignableRolePlace place} to remove the role from
* @return A {@link CompletableFuture future} which completes when the message was updated and the reaction was removed, if
* no post was present it completes immediately.
*/
private CompletableFuture<Void> removeRoleFromAssignablePlace(AssignableRole assignableRole, AssignableRolePlace assignableRolePlace) {
AssignableRolePlacePost post = assignableRole.getAssignableRolePlacePost();
if(post != null) {
AServer server = assignableRolePlace.getServer();
TextChannel textChannel = channelService.getTextChannelFromServer(server.getId(), post.getUsedChannel().getId());
List<AssignableRole> assignableRoles = assignableRolePlace.getAssignableRoles();
assignableRoles.sort(Comparator.comparing(AssignableRole::getPosition));
Long messageId = post.getId();
log.trace("Removing field describing assignable role {} in assignable role place {} from post {}.", role.getId(), assignableRolePlace.getId(), messageId);
CompletableFuture<Message> fieldEditing = channelService.removeFieldFromMessage(textChannel, messageId, assignableRoles.indexOf(role));
log.trace("Clearing reaction for emote {} on assignable role post {} in assignable role place {}.", role.getEmote().getId(), messageId, assignableRolePlace.getId());
CompletableFuture<Void> reactionRemoval = reactionService.clearReactionFromMessageWithFuture(role.getEmote(), assignableRolePlace.getServer().getId(), role.getAssignableRolePlacePost().getUsedChannel().getId(), role.getAssignableRolePlacePost().getId());
log.trace("Removing field describing assignable role {} in assignable role place {} from post {}.", assignableRole.getId(), assignableRolePlace.getId(), messageId);
CompletableFuture<Message> fieldEditing = channelService.removeFieldFromMessage(textChannel, messageId, assignableRoles.indexOf(assignableRole));
log.trace("Clearing reaction for emote {} on assignable role post {} in assignable role place {}.", assignableRole.getEmote().getId(), messageId, assignableRolePlace.getId());
CompletableFuture<Void> reactionRemoval = reactionService.clearReactionFromMessageWithFuture(assignableRole.getEmote(), assignableRolePlace.getServer().getId(), assignableRole.getAssignableRolePlacePost().getUsedChannel().getId(), assignableRole.getAssignableRolePlacePost().getId());
return CompletableFuture.allOf(fieldEditing, reactionRemoval);
} else {
// this case comes from the situation in which, the emote was deleted and he initial post setup failed
log.warn("Reaction {} to remove does not have a post attached. The post needs to be setup again, it is most likely not functioning currently anyway.", role.getEmote().getEmoteId());
log.warn("Reaction {} to remove does not have a post attached. The post needs to be setup again, it is most likely not functioning currently anyway.", assignableRole.getEmote().getEmoteId());
return CompletableFuture.completedFuture(null);
}
}
@@ -360,7 +433,8 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
CompletableFuture<Message> messageCompletableFuture = channelService.editEmbedMessageInAChannel(iterator.next(), textChannel, post.getId());
futures.add(messageCompletableFuture);
});
} else {
throw new ChannelNotInGuildException(latestPost.getUsedChannel().getId());
}
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
}
@@ -378,8 +452,9 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
if(channelOptional.isPresent()) {
log.info("Refreshing text for assignable role place {} in channel {} in post {}.", place.getId(), channelId, firstPost.getId());
return channelService.editEmbedMessageInAChannel(renderedMessage.getEmbeds().get(0), channelOptional.get(), firstPost.getId()).thenCompose(message -> CompletableFuture.completedFuture(null));
} else {
throw new ChannelNotInGuildException(channelId);
}
throw new ChannelNotInGuildException(channelId);
}
return CompletableFuture.completedFuture(null);
}
@@ -394,6 +469,12 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
}
/**
* Delete all {@link Message messages} which are stored as {@link AssignableRolePlacePost posts} from the given
* {@link AssignableRolePlace place}
* @param assignableRolePlace The {@link AssignableRolePlace place} to delete the posts of
* @return A list of {@link CompletableFuture futures} each representing one deleted {@link Message message}
*/
private List<CompletableFuture<Void>> deleteExistingMessagePostsForPlace(AssignableRolePlace assignableRolePlace) {
List<CompletableFuture<Void>> oldPostDeletionFutures = new ArrayList<>();
assignableRolePlace.getMessagePosts().forEach(assignableRolePlacePost -> {
@@ -556,9 +637,9 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
@Override
public void showAssignablePlaceConfig(AServer server, String name, TextChannel channel) {
public CompletableFuture<Void> showAssignablePlaceConfig(AServer server, String name, TextChannel channel) {
AssignableRolePlace place = rolePlaceManagementService.findByServerAndKey(server, name);
List<AssignablePostConfigRole> roles = new ArrayList<>();
List<AssignableRolePlaceConfigRole> roles = new ArrayList<>();
Guild guild = guildService.getGuildById(server.getId());
log.info("Showing assignable role place config for place {} in channel {} on server {}.", place.getId(), channel.getId(), server.getId());
List<AssignableRole> assignableRoles = place.getAssignableRoles().stream().sorted(Comparator.comparingInt(AssignableRole::getPosition)).collect(Collectors.toList());
@@ -566,7 +647,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
AEmote emoteForRole = role.getEmote();
Emote jdaEmoteForRole = emoteService.getEmote(emoteForRole).orElse(null);
Role jdaRole = guild.getRoleById(role.getRole().getId());
AssignablePostConfigRole postRole = AssignablePostConfigRole
AssignableRolePlaceConfigRole postRole = AssignableRolePlaceConfigRole
.builder()
.description(role.getDescription())
.emote(FullEmote.builder().fakeEmote(emoteForRole).emote(jdaEmoteForRole).build())
@@ -581,31 +662,44 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
.roles(roles)
.place(place)
.build();
channelService.sendEmbedTemplateInTextChannelList(ASSIGNABLE_ROLES_CONFIG_POST_TEMPLATE_KEY, configModel, channel);
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInTextChannelList(ASSIGNABLE_ROLES_CONFIG_POST_TEMPLATE_KEY, configModel, channel));
}
@Override
public void moveAssignableRolePlace(AServer server, String name, TextChannel newChannel) {
public void moveAssignableRolePlace(String name, TextChannel newChannel) {
AChannel channel = channelManagementService.loadChannel(newChannel.getIdLong());
rolePlaceManagementService.moveAssignableRolePlace(server, name, channel);
rolePlaceManagementService.moveAssignableRolePlace(name, channel);
}
@Override
public void changeAssignablePlaceDescription(AServer server, String name, String newDescription) {
rolePlaceManagementService.changeAssignableRolePlaceDescription(server, name, newDescription);
public void changeText(AServer server, String name, String newText) {
rolePlaceManagementService.changeAssignableRolePlaceDescription(server, name, newText);
}
@Override
public CompletableFuture<Void> deleteAssignableRolePlace(AServer server, String name) {
AssignableRolePlace place = rolePlaceManagementService.findByServerAndKey(server, name);
Long placeId = place.getId();
List<CompletableFuture<Void>> deleteFutures = deleteExistingMessagePostsForPlace(place);
return CompletableFuture.allOf(deleteFutures.toArray(new CompletableFuture[0]))
.thenAccept(unused -> self.deleteAssignableRolePlaceInDatabase(placeId));
}
/**
* Deletes the {@link AssignableRolePlace place} in the database and all the
* {@link AEmote emotes} which were used to identify the {@link AssignableRole roles}
* @param placeId The ID of the {@link AssignableRolePlace place} to delete
*/
@Transactional
public void deleteAssignableRolePlaceInDatabase(Long placeId) {
AssignableRolePlace place = rolePlaceManagementService.findByPlaceId(placeId);
rolePlaceManagementService.deleteAssignablePlace(place);
deleteEmotesFromAssignableRolePlace(place);
List<CompletableFuture<Void>> deleteFutures = deleteExistingMessagePostsForPlace(place);
return CompletableFuture.allOf(deleteFutures.toArray(new CompletableFuture[0]));
}
@Override
public CompletableFuture<Void> changeText(AServer server, String name, String newText) {
public CompletableFuture<Void> changeTextAsync(AServer server, String name, String newText) {
AssignableRolePlace place = rolePlaceManagementService.findByServerAndKey(server, name);
log.info("Changing text of assignable role place {} in server {}.", place.getId(), server.getId());
place.setText(newText);
@@ -632,7 +726,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
public CompletableFuture<Void> changeConfiguration(AServer server, String name, AssignableRolePlaceParameterKey keyToChange, Object newValue) {
Boolean booleanValue = BooleanUtils.toBooleanObject(newValue.toString());
if(booleanValue == null) {
throwBooleanParameterKeyException();
throw new CommandParameterKeyValueWrongTypeException(Arrays.asList("yes", "no", "true", "false", "on", "off"));
}
switch (keyToChange) {
case INLINE:
@@ -660,10 +754,10 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
return CompletableFuture.allOf(promises.toArray(new CompletableFuture[0]));
}
private void throwBooleanParameterKeyException() {
throw new CommandParameterKeyValueWrongTypeException(Arrays.asList("yes", "no", "true", "false", "on", "off"));
}
/**
* Deletes the {@link AEmote emotes} for each {@link AssignableRole assignableRole} from a {@link AssignableRolePlace place}
* @param place The {@link AssignableRolePlace place} to delete the {@link AEmote emotes} of
*/
private void deleteEmotesFromAssignableRolePlace(AssignableRolePlace place) {
log.info("Deleting all emotes associated with assignable role place {} in server {}.", place.getId(), place.getServer().getId());
place.getAssignableRoles().forEach(role ->
@@ -671,16 +765,35 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
);
}
/**
* Renders the {@link AssignableRolePlace place} to a {@link MessageToSend messageToSend}, and
* sends it in the given {@link TextChannel channel}
* @param place The {@link AssignableRolePlace place} to render
* @param channel The {@link TextChannel channel} in which the {@link MessageToSend messageToSend} should be sent to
* @return A list of {@link CompletableFuture futures} which represent each message which was sent
*/
private List<CompletableFuture<Message>> sendAssignablePostMessages(AssignableRolePlace place, TextChannel channel) {
MessageToSend messageToSend = renderAssignablePlacePosts(place);
return channelService.sendMessageToSendToChannel(messageToSend, channel);
}
/**
* Renders the {@link AssignableRolePlace place} with the appropriate template and returns a {@link MessageToSend messageToSend}
* @param place The {@link AssignableRolePlace place} to render
* @return A {@link MessageToSend messageToSend} which can be sent, containing the individual posts
*/
private MessageToSend renderAssignablePlacePosts(AssignableRolePlace place) {
AssignablePostMessage model = prepareAssignablePostMessageModel(place);
return templateService.renderEmbedTemplate(ASSIGNABLE_ROLES_POST_TEMPLATE_KEY, model);
}
/**
* Converts individual parts of the given {@link AssignableRolePlace place} into a model which is usable
* in the template responsible to render the {@link AssignableRolePlace place}. This requires to load
* the {@link Emote emotes} and ordering according to their position
* @param place The {@link AssignableRolePlace place} which should be made available for rendering
* @return The {@link AssignablePostMessage model} which holds the necessary values to render the template
*/
private AssignablePostMessage prepareAssignablePostMessageModel(AssignableRolePlace place) {
List<AssignablePostRole> roles = new ArrayList<>();
List<AssignableRole> rolesToAdd = place.getAssignableRoles().stream().sorted(Comparator.comparingInt(AssignableRole::getPosition)).collect(Collectors.toList());
@@ -692,6 +805,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
AssignablePostRole lastAddedRole = null;
for (int position = 0; position < maxPosition + 1; position++) {
boolean legitEntry = current.getPosition().equals(position);
// TODO is this still required?
boolean startOfNewMessage = position > 0 && (position % 21) == 0;
if(legitEntry) {
AEmote emoteForRole = current.getEmote();
@@ -723,6 +837,15 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
.build();
}
/**
* Creates the {@link Message messages} for the posts required for the {@link AssignableRolePlace place} and
* adds the reactions afterwards.
* @param serverId The ID of the {@link AServer server} in which the {@link AssignableRolePlace place} posts should
* be created in
* @param assignablePlaceId The ID of the {@link AssignableRolePlace place} which should have its {@link Message messages} posted
* @return A {@link CompletableFuture future} which completes when the {@link Message messages} were posted and the {@link MessageReaction reactions}
* added
*/
@Transactional
public CompletableFuture<Void> createAssignableRolePlacePosts(Long serverId, Long assignablePlaceId) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByPlaceId(assignablePlaceId);
@@ -735,15 +858,26 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
.thenCompose(aVoid -> self.addEmotes(messageFutures, assignablePlaceId));
} else {
log.warn("Channel to create assignable role post in does not exist.");
throw new AssignableRolePlaceChannelDoesNotExist(assignableRolePlace.getChannel().getId(), assignableRolePlace.getKey());
throw new AssignableRolePlaceChannelDoesNotExistException(assignableRolePlace.getChannel().getId(), assignableRolePlace.getKey());
}
}
/**
* Adds the appropriate {@link MessageReaction reactions} defined by the {@link AssignableRole assignableRoles} of the
* {@link AssignableRolePlace place} to the {@link Message messages} which are provided via the {@link CompletableFuture futures}.
* The mapping of reaction to assignable role is done via the index of the fields. Depending on the amount of fields
* which were created in the {@link Message messages}. Afterwards it will store the created
* {@link AssignableRolePlacePost posts}
* @param assignablePlacePostsMessageFutures A list of {@link CompletableFuture futures} which represent the individual
* {@link Message messages} which were posted when setting up
* the {@link AssignableRolePlace place}
* @param assignablePlaceId The ID of the {@link AssignableRolePlace place} which was setup
* @return A {@link CompletableFuture future} which completes when all reactions were added and the database
* was updated.
*/
@Transactional
public CompletableFuture<Void> addEmotes(List<CompletableFuture<Message>> assignablePlacePostsMessageFutures, Long assignablePlaceId) {
Message firstMessage = assignablePlacePostsMessageFutures.get(0).join();
Long serverId = firstMessage.getGuild().getIdLong();
AssignableRolePlace innerRolePlace = rolePlaceManagementService.findByPlaceId(assignablePlaceId);
log.info("Adding emotes to assignable role place {}.", innerRolePlace);
@@ -760,12 +894,12 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
usedEmotes += embed.getFields().size();
log.trace("Adding {} emotes to message {} for place {}. In total {} were added.", embed.getFields().size(), sentMessage.getId(), innerRolePlace.getId(), usedEmotes);
List<Integer> usedEmoteIds = firstRoles.stream().map(assignableRole -> assignableRole.getEmote().getId()).collect(Collectors.toList());
CompletableFuture<Void> firstMessageFuture = addingReactionsToAssignableRolePlacePost(sentMessage, serverId, usedEmoteIds);
CompletableFuture<Void> firstMessageFuture = addingReactionsToAssignableRolePlacePost(sentMessage, usedEmoteIds);
reactionFutures.add(firstMessageFuture);
}
return CompletableFuture.allOf(reactionFutures.toArray(new CompletableFuture[0])).thenCompose(aVoid -> {
try {
self.storeCreatedAssignableRolePlacePosts(assignablePlaceId, serverId, assignablePlacePostsMessageFutures);
self.storeCreatedAssignableRolePlacePosts(assignablePlaceId, assignablePlacePostsMessageFutures);
} catch (Exception e) {
log.error("Failed to persist assignable role place posts. ", e);
}
@@ -773,10 +907,19 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
});
}
/**
* Creates the {@link AssignableRolePlacePost posts} for the created {@link Message messages} and stores them in the
* database. This also uses the fields in the {@link MessageEmbed embed} in order to determine which
* {@link AssignableRole role} is in which {@link AssignableRolePlacePost post}
* @param assignablePlaceId The ID of the {@link AssignableRolePlace place} to which the {@link AssignableRolePlacePost posts}
* should be added to
* @param futures A list of {@link CompletableFuture futures} which holds the {@link Message messages} which were created
* when setting up the {@link AssignableRolePlace place}
*/
@Transactional
public void storeCreatedAssignableRolePlacePosts(Long assignablePlaceId, Long serverId, List<CompletableFuture<Message>> futures) {
public void storeCreatedAssignableRolePlacePosts(Long assignablePlaceId, List<CompletableFuture<Message>> futures) {
AssignableRolePlace updatedPlace = rolePlaceManagementService.findByPlaceId(assignablePlaceId);
log.info("Storing {} messages for assignable role place {} in server {}.", futures.size(), assignablePlaceId, serverId);
log.info("Storing {} messages for assignable role place {} in server {}.", futures.size(), assignablePlaceId, updatedPlace.getServer().getId());
List<AssignableRole> rolesToAdd = updatedPlace.getAssignableRoles().stream().sorted(Comparator.comparingInt(AssignableRole::getPosition)).collect(Collectors.toList());
int usedEmotes = 0;
for (int i = 0; i < futures.size(); i++) {
@@ -794,14 +937,21 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
}
/**
* Adds the given {@link AEmote emotes} identified by the ID to the given Message
* @param message The {@link Message message} on which the {@link MessageReaction reactions} should be added on
* @param emotesToAdd A list of {@link Integer integers} which contains the ID of the {@link AEmote emotes} which should be added
* @return A {@link CompletableFuture future} which complets when all reactions have been added
*/
@Transactional
public CompletableFuture<Void> addingReactionsToAssignableRolePlacePost(Message message, Long server, List<Integer> emotesToAdd) {
public CompletableFuture<Void> addingReactionsToAssignableRolePlacePost(Message message, List<Integer> emotesToAdd) {
// TODO might need to guarantee the order
List<CompletableFuture<Void>> futures = new ArrayList<>();
log.info("Adding {} emotes to assignable role place post {} in server {}.", emotesToAdd.size(), message.getId(), server);
Long serverId = message.getGuild().getIdLong();
log.info("Adding {} emotes to assignable role place post {} in server {}.", emotesToAdd.size(), message.getId(), serverId);
emotesToAdd.forEach(emotesToUse -> {
AEmote emoteToUseObject = emoteManagementService.loadEmote(emotesToUse);
futures.add(reactionService.addReactionToMessageAsync(emoteToUseObject, server, message));
futures.add(reactionService.addReactionToMessageAsync(emoteToUseObject, serverId, message));
});
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
}

View File

@@ -69,11 +69,11 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
.build();
@Override
public CompletableFuture<Void> assignAssignableRoleToUser(Long assignableRoleId, Member toAdd) {
public CompletableFuture<Void> assignAssignableRoleToUser(Long assignableRoleId, Member member) {
metricService.incrementCounter(ASSIGNABLE_ROLES_ASSIGNED);
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
log.info("Assigning role {} to member {} in server {}.", assignableRoleId, toAdd.getId(), toAdd.getGuild().getId());
return roleService.addRoleToMemberFuture(toAdd, role.getRole());
log.info("Assigning role {} to member {} in server {}.", assignableRoleId, member.getId(), member.getGuild().getId());
return roleService.addRoleToMemberFuture(member, role.getRole());
}
@Override
@@ -133,6 +133,7 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
}
@Transactional
@Override
public void addRoleToUser(Long assignableRoleId, AUserInAServer aUserInAServer) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
addRoleToUser(role, aUserInAServer);
@@ -152,6 +153,11 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
assignedRoleUserManagementServiceBean.removeAssignedRoleFromUser(assignableRole, aUserInAServer);
}
/**
* Adds the given {@link AssignableRole assignableRole} identified by the ID to the given {@link Member member}
* @param assignableRoleId The ID of the {@link AssignableRole} to be added to the {@link Member member}
* @param member The {@link Member member} to add the {@link AssignableRole role} to
*/
@Transactional
public void addRoleToUser(Long assignableRoleId, Member member) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
@@ -159,12 +165,22 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
addRoleToUser(role, aUserInAServer);
}
/**
* Removes the given {@link AssignableRole assignableRole} identified by the ID from the given {@link AUserInAServer user}
* @param assignableRoleId The ID of the {@link AssignableRole} to be removed from the {@link Member member}
* @param aUserInAServer The {@link AUserInAServer user} to remove the {@link AssignableRole role} from
*/
@Transactional
public void removeRoleFromUser(Long assignableRoleId, AUserInAServer aUserInAServer) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
removeRoleFromUser(role, aUserInAServer);
}
/**
* Stores the removal of an {@link AssignableRole assignableRole} from a {@link Member member} in the database
* @param assignableRoleId The ID of the {@link AssignableRole} to be removed from the {@link Member member}
* @param member The {@link Member member} which should get the {@link AssignableRole role} removed
*/
@Transactional
public void persistRoleRemovalFromUser(Long assignableRoleId, Member member) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);

View File

@@ -13,7 +13,6 @@ import dev.sheldan.abstracto.core.service.EmoteService;
import dev.sheldan.abstracto.core.service.management.EmoteManagementService;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.MessageReaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -85,18 +84,6 @@ public class AssignableRoleManagementServiceBean implements AssignableRoleManage
return repository.findById(assignableRoleId).orElseThrow(() -> new AbstractoRunTimeException("Assignable role not found"));
}
@Override
public AssignableRole getRoleForReactionEmote(MessageReaction.ReactionEmote emote, AssignableRolePlace assignableRolePlace) {
for (AssignableRolePlacePost post : assignableRolePlace.getMessagePosts()) {
for (AssignableRole assignableRole : post.getAssignableRoles()) {
if (emoteService.isReactionEmoteAEmote(emote, assignableRole.getEmote())) {
return assignableRole;
}
}
}
throw new AbstractoRunTimeException("Role for reaction was not found.");
}
@Override
public AssignableRole getRoleForReactionEmote(CachedEmote cachedEmote, AssignableRolePlace assignableRolePlace) {
for (AssignableRolePlacePost post : assignableRolePlace.getMessagePosts()) {

View File

@@ -20,16 +20,16 @@ public class AssignableRolePlaceManagementServiceBean implements AssignableRoleP
private AssignableRolePlaceRepository repository;
@Override
public AssignableRolePlace createPlace(AServer server, String name, AChannel channel, String text) {
public AssignableRolePlace createPlace(String name, AChannel channel, String text) {
AssignableRolePlace place = AssignableRolePlace
.builder()
.channel(channel)
.server(server)
.server(channel.getServer())
.text(text)
.key(name)
.build();
repository.save(place);
log.info("Creating assignable role place in channel {} on server {}.", channel.getId(), server.getId());
log.info("Creating assignable role place in channel {} on server {}.", channel.getId(), channel.getServer().getId());
return place;
}
@@ -55,10 +55,10 @@ public class AssignableRolePlaceManagementServiceBean implements AssignableRoleP
}
@Override
public void moveAssignableRolePlace(AServer server, String name, AChannel newChannel) {
AssignableRolePlace assignablePlaceToChange = findByServerAndKey(server, name);
public void moveAssignableRolePlace(String name, AChannel newChannel) {
AssignableRolePlace assignablePlaceToChange = findByServerAndKey(newChannel.getServer(), name);
log.info("Moving assignable role place {} in server {} from channel {} to channel {}.",
assignablePlaceToChange.getId(), server.getId(), assignablePlaceToChange.getChannel().getId(), newChannel.getId());
assignablePlaceToChange.getId(), newChannel.getServer().getId(), assignablePlaceToChange.getChannel().getId(), newChannel.getId());
assignablePlaceToChange.setChannel(newChannel);
}

View File

@@ -1,6 +1,6 @@
package dev.sheldan.abstracto.assignableroles.service.management;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignablePlacePostNotFoundException;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlacePostNotFoundException;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost;
import dev.sheldan.abstracto.assignableroles.repository.AssignableRolePlacePostRepository;
@@ -22,7 +22,7 @@ public class AssignableRolePlacePostManagementServiceBean implements AssignableR
@Override
public AssignableRolePlacePost findByMessageId(Long messageId) {
return findByMessageIdOptional(messageId).orElseThrow(() -> new AssignablePlacePostNotFoundException(messageId));
return findByMessageIdOptional(messageId).orElseThrow(() -> new AssignableRolePlacePostNotFoundException(messageId));
}
@Override

View File

@@ -43,6 +43,7 @@ public class AssignedRoleUserManagementServiceBean implements AssignedRoleUserMa
removeAssignedRoleFromUser(assignableRole, user);
}
@Override
public void removeAssignedRoleFromUser(AssignableRole assignableRole, AssignedRoleUser user) {
assignableRole.getAssignedUsers().remove(user);
user.getRoles().remove(assignableRole);
@@ -55,21 +56,6 @@ public class AssignedRoleUserManagementServiceBean implements AssignedRoleUserMa
return repository.save(newUser);
}
@Override
public void clearAllAssignedRolesOfUser(AUserInAServer userInAServer) {
AssignedRoleUser user = findByUserInServer(userInAServer);
log.info("Clearing all assignable roles for user {} in server {}.", userInAServer.getUserReference().getId(), userInAServer.getServerReference().getId());
user.getRoles().forEach(assignableRole ->
assignableRole.getAssignedUsers().remove(user)
);
user.getRoles().clear();
}
@Override
public boolean doesAssignedRoleUserExist(AUserInAServer aUserInAServer) {
return repository.existsById(aUserInAServer.getUserInServerId());
}
@Override
public Optional<AssignedRoleUser> findByUserInServerOptional(AUserInAServer aUserInAServer) {
return repository.findById(aUserInAServer.getUserInServerId());