Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.javadiscord.javabot.api.exception.InternalServerException;
import net.javadiscord.javabot.api.exception.InvalidEntityIdException;
import net.javadiscord.javabot.api.routes.CaffeineCache;
Expand All @@ -15,9 +16,6 @@
import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository;
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
import net.javadiscord.javabot.systems.qotw.model.QOTWAccount;
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceService;
import net.javadiscord.javabot.systems.user_preferences.model.Preference;
import net.javadiscord.javabot.systems.user_preferences.model.UserPreference;
import net.javadiscord.javabot.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
Expand All @@ -30,8 +28,6 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.sql.DataSource;
Expand All @@ -43,7 +39,6 @@
public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserProfileData> {
private final JDA jda;
private final QOTWPointsService qotwPointsService;
private final UserPreferenceService preferenceService;
private final DataSource dataSource;
private final BotConfig botConfig;
private final HelpExperienceService helpExperienceService;
Expand All @@ -54,21 +49,19 @@ public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserP
*
* @param jda The {@link Autowired} {@link JDA} instance to use.
* @param qotwPointsService The {@link QOTWPointsService}
* @param preferenceService The {@link UserPreferenceService}
* @param botConfig The main configuration of the bot
* @param dataSource A factory for connections to the main database
* @param helpExperienceService Service object that handles Help Experience Transactions.
* @param warnRepository DAO for interacting with the set of {@link Warn} objects.
*/
@Autowired
public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService, UserPreferenceService preferenceService, BotConfig botConfig, DataSource dataSource, HelpExperienceService helpExperienceService, WarnRepository warnRepository) {
public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService, BotConfig botConfig, DataSource dataSource, HelpExperienceService helpExperienceService, WarnRepository warnRepository) {
super(Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.build()
);
this.jda = jda;
this.qotwPointsService = qotwPointsService;
this.preferenceService = preferenceService;
this.dataSource = dataSource;
this.botConfig = botConfig;
this.helpExperienceService = helpExperienceService;
Expand All @@ -91,9 +84,11 @@ public ResponseEntity<UserProfileData> getUserProfile(
if (guild == null) {
throw new InvalidEntityIdException(Guild.class, "You've provided an invalid guild id!");
}
User user = jda.retrieveUserById(userId).complete();
if (user == null) {
throw new InvalidEntityIdException(User.class, "You've provided an invalid user id!");
User user;
try{
user = jda.retrieveUserById(userId).complete();
}catch (ErrorResponseException e) {
throw new InvalidEntityIdException(User.class, "Cannot fetch user: " + e.getMeaning());
}
try (Connection con = dataSource.getConnection()) {
// Check Cache
Expand All @@ -110,9 +105,6 @@ public ResponseEntity<UserProfileData> getUserProfile(
// Help Account
HelpAccount helpAccount = helpExperienceService.getOrCreateAccount(user.getIdLong());
data.setHelpAccount(HelpAccountData.of(botConfig, helpAccount, guild));
// User Preferences
List<UserPreference> preferences = Arrays.stream(Preference.values()).map(p -> preferenceService.getOrCreate(user.getIdLong(), p)).toList();
data.setPreferences(preferences);
// User Warns
LocalDateTime cutoff = LocalDateTime.now().minusDays(botConfig.get(guild).getModerationConfig().getWarnTimeoutDays());
data.setWarns(warnRepository.getActiveWarnsByUserId(user.getIdLong(), cutoff));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.Data;
import net.javadiscord.javabot.systems.moderation.warn.model.Warn;
import net.javadiscord.javabot.systems.qotw.model.QOTWAccount;
import net.javadiscord.javabot.systems.user_preferences.model.UserPreference;

import java.util.List;

Expand All @@ -19,6 +18,5 @@ public class UserProfileData {
private String effectiveAvatarUrl;
private QOTWAccount qotwAccount;
private HelpAccountData helpAccount;
private List<UserPreference> preferences;
private List<Warn> warns;
}