Skip to content

Commit 24a9790

Browse files
authored
Merge pull request #453 from danthe1st/user-api
proper message instead of internal server error, remove unnecessary data
2 parents e63ef35 + ee211ac commit 24a9790

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

src/main/java/net/javadiscord/javabot/api/routes/user_profile/UserProfileController.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.dv8tion.jda.api.JDA;
55
import net.dv8tion.jda.api.entities.Guild;
66
import net.dv8tion.jda.api.entities.User;
7+
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
78
import net.javadiscord.javabot.api.exception.InternalServerException;
89
import net.javadiscord.javabot.api.exception.InvalidEntityIdException;
910
import net.javadiscord.javabot.api.routes.CaffeineCache;
@@ -15,9 +16,6 @@
1516
import net.javadiscord.javabot.systems.moderation.warn.dao.WarnRepository;
1617
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
1718
import net.javadiscord.javabot.systems.qotw.model.QOTWAccount;
18-
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceService;
19-
import net.javadiscord.javabot.systems.user_preferences.model.Preference;
20-
import net.javadiscord.javabot.systems.user_preferences.model.UserPreference;
2119
import net.javadiscord.javabot.util.Pair;
2220
import org.springframework.beans.factory.annotation.Autowired;
2321
import org.springframework.dao.DataAccessException;
@@ -30,8 +28,6 @@
3028
import java.sql.Connection;
3129
import java.sql.SQLException;
3230
import java.time.LocalDateTime;
33-
import java.util.Arrays;
34-
import java.util.List;
3531
import java.util.concurrent.TimeUnit;
3632

3733
import javax.sql.DataSource;
@@ -43,7 +39,6 @@
4339
public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserProfileData> {
4440
private final JDA jda;
4541
private final QOTWPointsService qotwPointsService;
46-
private final UserPreferenceService preferenceService;
4742
private final DataSource dataSource;
4843
private final BotConfig botConfig;
4944
private final HelpExperienceService helpExperienceService;
@@ -54,21 +49,19 @@ public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserP
5449
*
5550
* @param jda The {@link Autowired} {@link JDA} instance to use.
5651
* @param qotwPointsService The {@link QOTWPointsService}
57-
* @param preferenceService The {@link UserPreferenceService}
5852
* @param botConfig The main configuration of the bot
5953
* @param dataSource A factory for connections to the main database
6054
* @param helpExperienceService Service object that handles Help Experience Transactions.
6155
* @param warnRepository DAO for interacting with the set of {@link Warn} objects.
6256
*/
6357
@Autowired
64-
public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService, UserPreferenceService preferenceService, BotConfig botConfig, DataSource dataSource, HelpExperienceService helpExperienceService, WarnRepository warnRepository) {
58+
public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService, BotConfig botConfig, DataSource dataSource, HelpExperienceService helpExperienceService, WarnRepository warnRepository) {
6559
super(Caffeine.newBuilder()
6660
.expireAfterWrite(10, TimeUnit.MINUTES)
6761
.build()
6862
);
6963
this.jda = jda;
7064
this.qotwPointsService = qotwPointsService;
71-
this.preferenceService = preferenceService;
7265
this.dataSource = dataSource;
7366
this.botConfig = botConfig;
7467
this.helpExperienceService = helpExperienceService;
@@ -91,9 +84,11 @@ public ResponseEntity<UserProfileData> getUserProfile(
9184
if (guild == null) {
9285
throw new InvalidEntityIdException(Guild.class, "You've provided an invalid guild id!");
9386
}
94-
User user = jda.retrieveUserById(userId).complete();
95-
if (user == null) {
96-
throw new InvalidEntityIdException(User.class, "You've provided an invalid user id!");
87+
User user;
88+
try{
89+
user = jda.retrieveUserById(userId).complete();
90+
}catch (ErrorResponseException e) {
91+
throw new InvalidEntityIdException(User.class, "Cannot fetch user: " + e.getMeaning());
9792
}
9893
try (Connection con = dataSource.getConnection()) {
9994
// Check Cache
@@ -110,9 +105,6 @@ public ResponseEntity<UserProfileData> getUserProfile(
110105
// Help Account
111106
HelpAccount helpAccount = helpExperienceService.getOrCreateAccount(user.getIdLong());
112107
data.setHelpAccount(HelpAccountData.of(botConfig, helpAccount, guild));
113-
// User Preferences
114-
List<UserPreference> preferences = Arrays.stream(Preference.values()).map(p -> preferenceService.getOrCreate(user.getIdLong(), p)).toList();
115-
data.setPreferences(preferences);
116108
// User Warns
117109
LocalDateTime cutoff = LocalDateTime.now().minusDays(botConfig.get(guild).getModerationConfig().getWarnTimeoutDays());
118110
data.setWarns(warnRepository.getActiveWarnsByUserId(user.getIdLong(), cutoff));

src/main/java/net/javadiscord/javabot/api/routes/user_profile/model/UserProfileData.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import lombok.Data;
44
import net.javadiscord.javabot.systems.moderation.warn.model.Warn;
55
import net.javadiscord.javabot.systems.qotw.model.QOTWAccount;
6-
import net.javadiscord.javabot.systems.user_preferences.model.UserPreference;
76

87
import java.util.List;
98

@@ -19,6 +18,5 @@ public class UserProfileData {
1918
private String effectiveAvatarUrl;
2019
private QOTWAccount qotwAccount;
2120
private HelpAccountData helpAccount;
22-
private List<UserPreference> preferences;
2321
private List<Warn> warns;
2422
}

0 commit comments

Comments
 (0)