From 6c442d8bfa0a2a243b232a94598f11ba6c3ddb45 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Mon, 24 Sep 2018 18:43:19 +0300 Subject: [PATCH] Can now change setting on web page --- .../java/net/Broken/Commands/Settings.java | 19 +-- .../Data/Settings/ListPostSetting.java | 7 ++ .../Data/Settings/PostSetSettings.java | 6 + .../Broken/RestApi/SettingAPIController.java | 43 ++++++- .../java/net/Broken/Tools/SettingsUtils.java | 116 +++++++++++++++++- src/main/resources/static/js/settings.js | 61 +++++++++ src/main/resources/templates/settings.html | 74 ++++++++++- 7 files changed, 301 insertions(+), 25 deletions(-) create mode 100644 src/main/java/net/Broken/RestApi/Data/Settings/ListPostSetting.java create mode 100644 src/main/java/net/Broken/RestApi/Data/Settings/PostSetSettings.java diff --git a/src/main/java/net/Broken/Commands/Settings.java b/src/main/java/net/Broken/Commands/Settings.java index 15968dc..58b6034 100644 --- a/src/main/java/net/Broken/Commands/Settings.java +++ b/src/main/java/net/Broken/Commands/Settings.java @@ -7,6 +7,7 @@ import net.Broken.MainBot; import net.Broken.SpringContext; import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.MessageTimeOut; +import net.Broken.Tools.SettingsUtils; import net.Broken.audio.AudioM; import net.Broken.audio.NotConnectedException; import net.Broken.audio.NullMusicManager; @@ -37,7 +38,7 @@ public class Settings implements Commande { @Override public void action(String[] args, MessageReceivedEvent event) { if(args.length == 0){ - GuildPreferenceEntity guildPref = getPreference(event.getGuild()); + GuildPreferenceEntity guildPref = SettingsUtils.getInstance().getPreference(event.getGuild()); MessageEmbed message = EmbedMessageUtils.getPref(guildPref); event.getTextChannel().sendMessage(message).complete(); @@ -89,22 +90,8 @@ public class Settings implements Commande { } - private GuildPreferenceEntity getPreference(Guild guild){ - List guildPrefList = guildPreferenceRepository.findByGuildId(guild.getId()); - GuildPreferenceEntity guildPref; - if(guildPrefList.isEmpty()){ - logger.info("Generate default pref for " + guild.getName()); - guildPref = GuildPreferenceEntity.getDefault(guild); - guildPreferenceRepository.save(guildPref); - } - else - guildPref = guildPrefList.get(0); - return guildPref; - } - - private void set(MessageReceivedEvent event, String key, String value){ - GuildPreferenceEntity pref = getPreference(event.getGuild()); + GuildPreferenceEntity pref = SettingsUtils.getInstance().getPreference(event.getGuild()); switch (key){ case "anti_spam": value = value.replaceAll(" ", ""); diff --git a/src/main/java/net/Broken/RestApi/Data/Settings/ListPostSetting.java b/src/main/java/net/Broken/RestApi/Data/Settings/ListPostSetting.java new file mode 100644 index 0000000..78f2bc7 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/Settings/ListPostSetting.java @@ -0,0 +1,7 @@ +package net.Broken.RestApi.Data.Settings; + +import java.util.List; + +public class ListPostSetting { + public List settings; +} diff --git a/src/main/java/net/Broken/RestApi/Data/Settings/PostSetSettings.java b/src/main/java/net/Broken/RestApi/Data/Settings/PostSetSettings.java new file mode 100644 index 0000000..1bb362e --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/Settings/PostSetSettings.java @@ -0,0 +1,6 @@ +package net.Broken.RestApi.Data.Settings; + +public class PostSetSettings { + public String id; + public String val; +} diff --git a/src/main/java/net/Broken/RestApi/SettingAPIController.java b/src/main/java/net/Broken/RestApi/SettingAPIController.java index 2e62e04..91589a2 100644 --- a/src/main/java/net/Broken/RestApi/SettingAPIController.java +++ b/src/main/java/net/Broken/RestApi/SettingAPIController.java @@ -6,6 +6,8 @@ import net.Broken.DB.Repository.GuildPreferenceRepository; import net.Broken.DB.Repository.UserRepository; import net.Broken.MainBot; import net.Broken.RestApi.Data.Settings.GetSettingsData; +import net.Broken.RestApi.Data.Settings.ListPostSetting; +import net.Broken.RestApi.Data.Settings.PostSetSettings; import net.Broken.RestApi.Data.Settings.Value; import net.Broken.Tools.SettingsUtils; import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; @@ -21,10 +23,7 @@ import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.CookieValue; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.annotation.RegEx; import java.util.ArrayList; @@ -34,7 +33,13 @@ import java.util.List; @RequestMapping("/api") public class SettingAPIController { private Logger logger = LogManager.getLogger(); + final + UserRepository userRepository; + @Autowired + public SettingAPIController(UserRepository userRepository) { + this.userRepository = userRepository; + } @RequestMapping(value = "/settings", method = RequestMethod.GET) @@ -49,4 +54,34 @@ public class SettingAPIController { } } + @RequestMapping(value = "/settings", method = RequestMethod.POST) + public ResponseEntity setSetting(@CookieValue("token") String token, @CookieValue("guild") String guild, @RequestBody ListPostSetting settings){ + SettingsUtils settingUtils = SettingsUtils.getInstance(); + + if(settingUtils.checkPermission(token, guild)){ + Guild jdaGuild = MainBot.jda.getGuildById(guild); + try { + UserEntity user = UserUtils.getInstance().getUserWithApiToken(userRepository,token); + logger.info(user.getName() + " change config of " + jdaGuild.getName()); + } catch (UnknownTokenException e) { + e.printStackTrace(); + } + if(settingUtils.setSettings(jdaGuild, settings.settings)){ + return new ResponseEntity<>(HttpStatus.OK); + + }else{ + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + + } + + + } + else{ + logger.warn("Try to change setting, UNAUTHORIZED. TOKEN: " + token + " GUILD: " + guild); + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } + } + + + } diff --git a/src/main/java/net/Broken/Tools/SettingsUtils.java b/src/main/java/net/Broken/Tools/SettingsUtils.java index 7ba7b0c..02cb06d 100644 --- a/src/main/java/net/Broken/Tools/SettingsUtils.java +++ b/src/main/java/net/Broken/Tools/SettingsUtils.java @@ -7,6 +7,7 @@ import net.Broken.DB.Repository.PendingPwdResetRepository; import net.Broken.DB.Repository.UserRepository; import net.Broken.MainBot; import net.Broken.RestApi.Data.Settings.GetSettingsData; +import net.Broken.RestApi.Data.Settings.PostSetSettings; import net.Broken.RestApi.Data.Settings.Value; import net.Broken.SpringContext; import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; @@ -29,7 +30,7 @@ import java.util.List; public class SettingsUtils { private static SettingsUtils INSTANCE; - Logger logger = LogManager.getLogger(); + private Logger logger = LogManager.getLogger(); public static SettingsUtils getInstance(){ return (INSTANCE == null) ? new SettingsUtils() : INSTANCE; @@ -152,6 +153,106 @@ public class SettingsUtils { } + public boolean setSettings(Guild guild, List settings){ + GuildPreferenceEntity pref = getPreference(guild); + for (PostSetSettings setting : settings){ + String value = setting.val; + logger.debug(setting.id + " : " + value); + switch (setting.id) { + case "anti_spam": + if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) { + boolean result = Boolean.parseBoolean(value); + pref.setAntiSpam(result); + pref = guildPreferenceRepository.save(pref); + } else { + logger.error("anti_spam error. Key: " + setting.id + " Val: " + setting.val); + return false; + } + + break; + + + case "default_role": + if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) { + boolean result = Boolean.parseBoolean(value); + pref.setDefaultRole(result); + pref = guildPreferenceRepository.save(pref); + } else { + logger.error("default_role error. Key: " + setting.id + " Val: " + setting.val); + + return false; + } + + break; + case "default_role_id": + try { + Role role = guild.getRoleById(value); + if (role != null) { + pref.setDefaultRoleId(role.getId()); + + pref = guildPreferenceRepository.save(pref); + + } else { + throw new NumberFormatException(); + } + } catch (NumberFormatException e) { + logger.error("default_role_id error. Key: " + setting.id + " Val: " + setting.val); + + return false; + } + + break; + case "welcome": + if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) { + boolean result = Boolean.parseBoolean(value); + pref.setWelcome(result); + pref = guildPreferenceRepository.save(pref); + } else { + logger.error("welcome error. Key: " + setting.id + " Val: " + setting.val); + return false; + } + break; + case "welcome_chanel_id": + try { + TextChannel chanel = guild.getTextChannelById(value); + if (chanel != null) { + pref.setWelcomeChanelID(chanel.getId()); + + pref = guildPreferenceRepository.save(pref); + + } else { + throw new NumberFormatException(); + } + } catch (NumberFormatException e) { + logger.error("welcome_chanel_id error. Key: " + setting.id + " Val: " + setting.val); + + return false; + } + break; + case "welcome_message": + pref.setWelcomeMessage(value); + pref = guildPreferenceRepository.save(pref); + + break; + + case "daily_madame": + if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) { + boolean result = Boolean.parseBoolean(value); + pref.setDailyMadame(result); + pref = guildPreferenceRepository.save(pref); + + } else { + logger.error("daily_madame error. Key: " + setting.id + " Val: " + setting.val); + + return false; + } + break; + } + } + return true; + + } + private List getTextChannels(Guild guild){ List channels = new ArrayList<>(); @@ -168,4 +269,17 @@ public class SettingsUtils { } return roles; } + + public GuildPreferenceEntity getPreference(Guild guild){ + List guildPrefList = guildPreferenceRepository.findByGuildId(guild.getId()); + GuildPreferenceEntity guildPref; + if(guildPrefList.isEmpty()){ + logger.info("Generate default pref for " + guild.getName()); + guildPref = GuildPreferenceEntity.getDefault(guild); + guildPreferenceRepository.save(guildPref); + } + else + guildPref = guildPrefList.get(0); + return guildPref; + } } diff --git a/src/main/resources/static/js/settings.js b/src/main/resources/static/js/settings.js index e06132e..ba74276 100644 --- a/src/main/resources/static/js/settings.js +++ b/src/main/resources/static/js/settings.js @@ -1,3 +1,64 @@ +var post_json = {settings : []}; + + + $(document).ready(function(){ + $('select').formSelect(); + modal_loading = $('#modal_loading'); + modal_loading.modal({ + dismissible: false + }); + + $('#sendBtn').click(function () { + + var select = $('.collect-select'); + select.each(function(){ + var val = $(this).find("select").val(); + var id = $(this).attr("id"); + if(val != null){ + post_json["settings"].push({"id" : id, "val" : val}); + } + + }); + + var switch_collected = $('.collect-switch'); + switch_collected.each(function(){ + var val = $(this).is(':checked').toString(); + var id = $(this).attr("id"); + if(val != null){ + post_json["settings"].push({"id" : id, "val" : val}); + } + + }); + + var text = $('.collect-text'); + text.each(function(){ + var val = $(this).val(); + var id = $(this).attr("id"); + if(val != null){ + post_json["settings"].push({"id" : id, "val" : val}); + } + + }); + + modal_loading.modal('open'); + $.ajax({ + type: "POST", + contentType: 'application/json', + url: "/api/settings", + data: JSON.stringify(post_json) + + }).done(function (data) { + console.log("ok"); + M.toast({html: 'doneSave Successful ! ', classes: 'rounded green'}); + modal_loading.modal('close'); + }).fail(function (data) { + console.log(data); + modal_loading.modal('close'); + M.toast({html: 'reportSave Failed ! ', classes: 'rounded red'}); + }); + + + }) }); \ No newline at end of file diff --git a/src/main/resources/templates/settings.html b/src/main/resources/templates/settings.html index 2805dc5..2dc82a4 100644 --- a/src/main/resources/templates/settings.html +++ b/src/main/resources/templates/settings.html @@ -33,6 +33,7 @@
+

@@ -40,21 +41,21 @@
-
+
-
- +
+
@@ -66,7 +67,72 @@
+
+
+ Savesave +
+
+ +
+ +