Compare commits

...

5 Commits

Author SHA1 Message Date
9f1a3f737b
Fix blanck string in db 2022-06-18 01:19:04 +02:00
bfcba4eb30
🔨 Add setting post 2022-06-17 23:04:20 +02:00
f8b134df34
🚑 Fix setting type 2022-06-17 19:12:22 +02:00
3fa9f8f7a1
🚑 Fix swagger url 2022-06-17 18:19:58 +02:00
0364dabb89
🚑 Fix auto_voice_channel_title type 2022-06-17 18:04:23 +02:00
10 changed files with 345 additions and 187 deletions

View File

@ -3,6 +3,8 @@ package net.Broken.Api.Controllers;
import net.Broken.Api.Data.Settings.SettingGroup;
import net.Broken.Api.Data.Settings.Value;
import net.Broken.Api.Services.SettingService;
import net.Broken.DB.Entity.GuildPreferenceEntity;
import net.Broken.Tools.Settings.SettingValueBuilder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@ -28,4 +30,11 @@ public class SettingController {
public List<Value> getSettingValues(@PathVariable String guildId){
return settingService.getValues(guildId);
}
@PostMapping("/{guildId}/values")
@PreAuthorize("isInGuild(#guildId) && canManageGuild(#guildId)")
public List<Value> getSettingValues(@PathVariable String guildId, @RequestBody List<Value> values){
GuildPreferenceEntity pref = settingService.saveValue(guildId, values);
return new SettingValueBuilder(pref).build();
}
}

View File

@ -1,4 +1,4 @@
package net.Broken.Api.Data.Settings;
public record Value(String id, String value) {
public record Value(String id, Object value) {
}

View File

@ -1,33 +1,35 @@
package net.Broken.Api.OpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI customOpenAPI() {
final String securitySchemeName = "JWT";
@Bean
public OpenAPI customOpenAPI() {
final String securitySchemeName = "JWT";
// final String apiTitle = String.format("%s API", StringUtils.capitalize(moduleName));
return new OpenAPI()
.addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
.components(
new Components()
.addSecuritySchemes(securitySchemeName,
new SecurityScheme()
.name(securitySchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
// final String apiTitle = String.format("%s API", StringUtils.capitalize(moduleName));
return new OpenAPI()
.addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
.components(
new Components()
.addSecuritySchemes(securitySchemeName,
new SecurityScheme()
.name(securitySchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
)
)
);
}
.addServersItem(new Server().url("/").description("Default"));
}
}

View File

@ -6,6 +6,9 @@ import net.Broken.Api.Data.Settings.SettingGroup;
import net.Broken.Api.Data.Settings.Value;
import net.Broken.DB.Entity.GuildPreferenceEntity;
import net.Broken.DB.Repository.GuildPreferenceRepository;
import net.Broken.Tools.Settings.SettingDescriptionBuilder;
import net.Broken.Tools.Settings.SettingSaver;
import net.Broken.Tools.Settings.SettingValueBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
@ -25,106 +28,7 @@ public class SettingService {
}
public List<SettingGroup> getSettingDescription() {
List<SettingGroup> toReturn = new ArrayList<>();
toReturn.add(getWelcomeGroup());
toReturn.add(getDefaultRoleGroup());
toReturn.add(getDailyGroup());
toReturn.add(getAutoVoiceChannelGroup());
return toReturn;
}
private SettingGroup getWelcomeGroup() {
SettingDescriber mainField = new SettingDescriber(
"welcome_enable",
"Enable Welcome Message",
null,
SettingDescriber.TYPE.BOOL
);
List<SettingDescriber> fields = new ArrayList<>();
fields.add(new SettingDescriber(
"welcome_chanel_id",
"Welcome Message chanel",
null,
SettingDescriber.TYPE.TEXT_CHANNEL
));
fields.add(new SettingDescriber(
"welcome_message",
"Welcome Message",
null,
SettingDescriber.TYPE.STRING
));
return new SettingGroup(
"Welcome Message",
mainField,
fields);
}
private SettingGroup getDefaultRoleGroup() {
SettingDescriber mainField = new SettingDescriber(
"default_role",
"Enable Default Role",
null,
SettingDescriber.TYPE.BOOL
);
List<SettingDescriber> fields = new ArrayList<>();
fields.add(new SettingDescriber(
"default_role_id",
"Default Role",
null,
SettingDescriber.TYPE.ROLE
));
return new SettingGroup(
"Default Role",
mainField,
fields);
}
private SettingGroup getDailyGroup() {
List<SettingDescriber> fields = new ArrayList<>();
fields.add(new SettingDescriber(
"daily_madame",
"[NSFW] Enable Daily Madame Message",
null,
SettingDescriber.TYPE.BOOL
));
return new SettingGroup(
"Daily",
null,
fields);
}
private SettingGroup getAutoVoiceChannelGroup() {
SettingDescriber mainField = new SettingDescriber(
"auto_voice",
"Enable Auto Create Voice Chanel",
"Auto create voice channel on join.",
SettingDescriber.TYPE.BOOL
);
List<SettingDescriber> fields = new ArrayList<>();
fields.add(new SettingDescriber(
"auto_voice_base_channel",
"Base Voice Channel For Auto Create",
"If someone joint this channel, a new voice channel will be created with the same settings.",
SettingDescriber.TYPE.VOICE_CHANNEL
));
fields.add(new SettingDescriber(
"auto_voice_channel_title",
"Auto Created Voice Channel title",
"Auto created voice channel will use this title, @count will be replaced by the channel count.",
SettingDescriber.TYPE.VOICE_CHANNEL
));
return new SettingGroup(
"Auto Voice Channel",
mainField,
fields);
return new SettingDescriptionBuilder().build();
}
@ -133,74 +37,15 @@ public class SettingService {
logger.info("[API] : Generate default guild pref");
return preferenceRepository.save(GuildPreferenceEntity.getDefault(guildId));
});
List<Value> values = new ArrayList<>(getWelcomeValues(pref));
values.addAll(getDefaultRoleValues(pref));
values.addAll(getDailyValues(pref));
values.addAll(getAutoVoiceChannelValues(pref));
return values;
return new SettingValueBuilder(pref).build();
}
private List<Value> getWelcomeValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"welcome_enable",
String.valueOf(pref.isWelcome())
)
);
toReturn.add(new Value(
"welcome_chanel_id",
pref.getWelcomeChanelID()
)
);
toReturn.add(new Value(
"welcome_message",
pref.getWelcomeMessage()
)
);
return toReturn;
}
private List<Value> getDefaultRoleValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"default_role",
String.valueOf(pref.isDefaultRole())
)
);
toReturn.add(new Value(
"default_role_id",
pref.getDefaultRoleId()
)
);
return toReturn;
}
private List<Value> getDailyValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"daily_madame",
String.valueOf(pref.isDefaultRole())
)
);
return toReturn;
public GuildPreferenceEntity saveValue(String guildId, List<Value> values){
GuildPreferenceEntity pref = preferenceRepository.findByGuildId(guildId).orElseGet(() -> {
logger.info("[API] : Generate default guild pref");
return preferenceRepository.save(GuildPreferenceEntity.getDefault(guildId));
});
return new SettingSaver(preferenceRepository, pref).save(values);
}
private List<Value> getAutoVoiceChannelValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"auto_voice",
String.valueOf(pref.isAutoVoice())
)
);
toReturn.add(new Value(
"auto_voice_base_channel",
pref.getAutoVoiceChannelID()
)
);
toReturn.add(new Value(
"auto_voice_channel_title",
pref.getAutoVoiceChannelTitle()
)
);
return toReturn;
}
}

View File

@ -60,7 +60,7 @@ public class GuildPreferenceEntity {
public static GuildPreferenceEntity getDefault(String guildId) {
return new GuildPreferenceEntity(guildId, false, "Welcome to this awesome server @name! ", " ", false, " ", true, false, " ", " ");
return new GuildPreferenceEntity(guildId, false, "Welcome to this awesome server @name! ", null, false, null, true, false, null, null);
}
public Integer getId() {

View File

@ -1,6 +1,8 @@
package net.Broken;
import net.Broken.DB.Entity.GuildPreferenceEntity;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.GuildPreferenceRepository;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.RestApi.ApiCommandLoader;
import net.Broken.Tools.Command.SlashCommandLoader;
@ -17,6 +19,7 @@ import net.dv8tion.jda.api.utils.MemberCachePolicy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.thymeleaf.spring5.processor.SpringOptionFieldTagProcessor;
import javax.security.auth.login.LoginException;
import java.util.List;
@ -82,12 +85,47 @@ public class Init {
List<UserEntity> users = (List<UserEntity>) userRepository.findAll();
UserStatsUtils userStatsUtils = UserStatsUtils.getINSTANCE();
logger.debug("Stats...");
// for (UserEntity userEntity : users) {
// logger.debug("..." + userEntity.getName());
// userStatsUtils.getUserStats(userEntity);
//
// }
logger.debug("Guild Prefs...");
GuildPreferenceRepository guildPreference = context.getBean(GuildPreferenceRepository.class);
for(GuildPreferenceEntity pref :guildPreference.findAll()){
boolean save = false;
if(pref.getWelcomeMessage() != null && pref.getWelcomeMessage().equals(" ")){
pref.setWelcomeMessage(null);
save = true;
}
if(pref.getWelcomeChanelID() != null && pref.getWelcomeChanelID().equals(" ")){
pref.setWelcomeChanelID(null);
save = true;
}
if(pref.getWelcomeChanelID() != null && pref.getWelcomeChanelID().equals(" ")){
pref.setWelcomeChanelID(null);
save = true;
}
if(pref.getDefaultRoleId() != null && pref.getDefaultRoleId().equals(" ")){
pref.setDefaultRoleId(null);
save = true;
}
if(pref.getAutoVoiceChannelID() != null && pref.getAutoVoiceChannelID().equals(" ")){
pref.setAutoVoiceChannelID(null);
save = true;
}
if(pref.getAutoVoiceChannelTitle() != null && pref.getAutoVoiceChannelTitle().equals(" ")){
pref.setAutoVoiceChannelTitle(null);
save = true;
}
if(save){
guildPreference.save(pref);
}
}
}
}

View File

@ -0,0 +1,111 @@
package net.Broken.Tools.Settings;
import net.Broken.Api.Data.Settings.SettingDescriber;
import net.Broken.Api.Data.Settings.SettingGroup;
import java.util.ArrayList;
import java.util.List;
public class SettingDescriptionBuilder {
public List<SettingGroup> build(){
List<SettingGroup> toReturn = new ArrayList<>();
toReturn.add(getWelcomeGroup());
toReturn.add(getDefaultRoleGroup());
toReturn.add(getAutoVoiceChannelGroup());
toReturn.add(getDailyGroup());
return toReturn;
}
private SettingGroup getWelcomeGroup() {
SettingDescriber mainField = new SettingDescriber(
"welcome_enable",
"Enable Welcome Message",
null,
SettingDescriber.TYPE.BOOL
);
List<SettingDescriber> fields = new ArrayList<>();
fields.add(new SettingDescriber(
"welcome_chanel_id",
"Welcome Message chanel",
null,
SettingDescriber.TYPE.TEXT_CHANNEL
));
fields.add(new SettingDescriber(
"welcome_message",
"Welcome Message",
null,
SettingDescriber.TYPE.STRING
));
return new SettingGroup(
"Welcome Message",
mainField,
fields);
}
private SettingGroup getDefaultRoleGroup() {
SettingDescriber mainField = new SettingDescriber(
"default_role",
"Enable Default Role",
null,
SettingDescriber.TYPE.BOOL
);
List<SettingDescriber> fields = new ArrayList<>();
fields.add(new SettingDescriber(
"default_role_id",
"Default Role",
null,
SettingDescriber.TYPE.ROLE
));
return new SettingGroup(
"Default Role",
mainField,
fields);
}
private SettingGroup getDailyGroup() {
List<SettingDescriber> fields = new ArrayList<>();
fields.add(new SettingDescriber(
"daily_madame",
"[NSFW] Enable Daily Madame Message",
null,
SettingDescriber.TYPE.BOOL
));
return new SettingGroup(
"Daily",
null,
fields);
}
private SettingGroup getAutoVoiceChannelGroup() {
SettingDescriber mainField = new SettingDescriber(
"auto_voice",
"Enable Auto Create Voice Chanel",
null,
SettingDescriber.TYPE.BOOL
);
List<SettingDescriber> fields = new ArrayList<>();
fields.add(new SettingDescriber(
"auto_voice_base_channel",
"Base Voice Channel For Auto Create",
"If someone joint this channel, a new voice channel will be created with the same settings.",
SettingDescriber.TYPE.VOICE_CHANNEL
));
fields.add(new SettingDescriber(
"auto_voice_channel_title",
"Auto Created Voice Channel title",
"Auto created voice channel will use this title, @count will be replaced by the channel count.",
SettingDescriber.TYPE.STRING
));
return new SettingGroup(
"Auto Voice Channel",
mainField,
fields);
}
}

View File

@ -0,0 +1,47 @@
package net.Broken.Tools.Settings;
import net.Broken.Api.Data.Settings.Value;
import net.Broken.DB.Entity.GuildPreferenceEntity;
import net.Broken.DB.Repository.GuildPreferenceRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
public class SettingSaver {
private final GuildPreferenceRepository guildPreferenceRepository;
private final GuildPreferenceEntity guildPreference;
private final Logger logger = LogManager.getLogger();
public SettingSaver(GuildPreferenceRepository guildPreferenceRepository, GuildPreferenceEntity guildPreference) {
this.guildPreferenceRepository = guildPreferenceRepository;
this.guildPreference = guildPreference;
}
public GuildPreferenceEntity save(List<Value> values) {
for (Value value : values) {
setValue(value);
}
return guildPreferenceRepository.save(guildPreference);
}
private void setValue(Value value) {
switch (value.id()) {
// WELCOME
case "welcome_enable" -> guildPreference.setWelcome((Boolean) value.value());
case "welcome_chanel_id" -> guildPreference.setWelcomeChanelID((String) value.value());
case "welcome_message" -> guildPreference.setWelcomeMessage((String) value.value());
// DEFAULT ROLE
case "default_role" -> guildPreference.setDefaultRole((Boolean) value.value());
case "default_role_id" -> guildPreference.setDefaultRoleId((String) value.value());
// DAILY
case "daily_madame" -> guildPreference.setDailyMadame((Boolean) value.value());
// AUTO VOICE CHAN
case "auto_voice" -> guildPreference.setAutoVoice((Boolean) value.value());
case "auto_voice_base_channel" -> guildPreference.setAutoVoiceChannelID((String) value.value());
case "auto_voice_channel_title" -> guildPreference.setAutoVoiceChannelTitle((String) value.value());
default -> logger.warn("Invalid setting received: " + value.id());
}
}
}

View File

@ -0,0 +1,89 @@
package net.Broken.Tools.Settings;
import net.Broken.Api.Data.Settings.Value;
import net.Broken.DB.Entity.GuildPreferenceEntity;
import java.util.ArrayList;
import java.util.List;
public class SettingValueBuilder {
private final GuildPreferenceEntity guildPreference;
public SettingValueBuilder(GuildPreferenceEntity guildPreference) {
this.guildPreference = guildPreference;
}
public List<Value> build() {
List<Value> values = new ArrayList<>(getWelcomeValues(guildPreference));
values.addAll(getDefaultRoleValues(guildPreference));
values.addAll(getDailyValues(guildPreference));
values.addAll(getAutoVoiceChannelValues(guildPreference));
return values;
}
private List<Value> getWelcomeValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"welcome_enable",
pref.isWelcome()
)
);
toReturn.add(new Value(
"welcome_chanel_id",
pref.getWelcomeChanelID()
)
);
toReturn.add(new Value(
"welcome_message",
pref.getWelcomeMessage()
)
);
return toReturn;
}
private List<Value> getDefaultRoleValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"default_role",
pref.isDefaultRole()
)
);
toReturn.add(new Value(
"default_role_id",
pref.getDefaultRoleId()
)
);
return toReturn;
}
private List<Value> getDailyValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"daily_madame",
pref.isDailyMadame()
)
);
return toReturn;
}
private List<Value> getAutoVoiceChannelValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"auto_voice",
pref.isAutoVoice()
)
);
toReturn.add(new Value(
"auto_voice_base_channel",
pref.getAutoVoiceChannelID()
)
);
toReturn.add(new Value(
"auto_voice_channel_title",
pref.getAutoVoiceChannelTitle()
)
);
return toReturn;
}
}

View File

@ -716,3 +716,20 @@ databaseChangeLog:
columnName: welcome_message
tableName: guild_preference_entity
# 12/06/2022
- changeSet:
id: 1655026344809-1
author: seb65 (generated)
changes:
- addUniqueConstraint:
columnNames: guild_id
constraintName: UC_GUILD_PREFERENCE_ENTITYGUILD_ID_COL
tableName: guild_preference_entity
- changeSet:
id: 1655026344809-2
author: seb65 (generated)
changes:
- dropColumn:
columnName: anti_spam
tableName: guild_preference_entity