From 187c46e1f241e8a06bd16ac85021ccd997ec1814 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Thu, 26 Nov 2020 18:35:26 +0100 Subject: [PATCH] Now when user unsubscribe, their timers are also cancelled Signed-off-by: Louis Vallat --- app/build.gradle | 1 - app/src/main/java/xyz/vallat/louis/App.java | 21 ++-------- .../vallat/louis/commands/Desinscription.java | 2 + .../managers/calendar/CalendarManager.java | 1 + .../managers/discord/DiscordManager.java | 11 +++++- .../xyz/vallat/louis/timer/TimerManager.java | 39 +++++++++++++++++++ .../tasks}/AutoReimport.java | 2 +- .../louis/timer/tasks/MessagePresence.java | 30 ++++++++++++++ 8 files changed, 86 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/xyz/vallat/louis/timer/TimerManager.java rename app/src/main/java/xyz/vallat/louis/{timertasks => timer/tasks}/AutoReimport.java (92%) create mode 100644 app/src/main/java/xyz/vallat/louis/timer/tasks/MessagePresence.java diff --git a/app/build.gradle b/app/build.gradle index 7d465a4..eb1d5dc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,7 +35,6 @@ jar { } dependencies { - testImplementation 'junit:junit:4.13' implementation 'com.discord4j:discord4j-core:3.1.1' implementation 'ch.qos.logback:logback-classic:1.2.3' implementation 'org.postgresql:postgresql:42.2.18.jre7' diff --git a/app/src/main/java/xyz/vallat/louis/App.java b/app/src/main/java/xyz/vallat/louis/App.java index 522585e..e360928 100644 --- a/app/src/main/java/xyz/vallat/louis/App.java +++ b/app/src/main/java/xyz/vallat/louis/App.java @@ -4,26 +4,22 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.managers.database.DBManager; import xyz.vallat.louis.managers.discord.DiscordManager; -import xyz.vallat.louis.timertasks.AutoReimport; - -import java.util.Calendar; -import java.util.Timer; -import java.util.concurrent.TimeUnit; +import xyz.vallat.louis.timer.TimerManager; public class App { - private static final Logger logger = LoggerFactory.getLogger(App.class.getCanonicalName()); public static final String PREFIX = "+"; public static final String NAME = "PrésencEirb"; public static final String DESCRIPTION = "Tu as pensé à pointer ?"; public static final String VERSION = "0.1-SNAPSHOT"; + private static final Logger logger = LoggerFactory.getLogger(App.class.getCanonicalName()); public static void main(String[] args) { logger.info("Starting {} ver. {}.", NAME, VERSION); DBManager.testConnection(); DBManager.initializeDatabase(); DiscordManager.login(); - setTimers(); + TimerManager.startAutoReimportTimer(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { logger.info("Received shut down signal. Bye!"); @@ -32,15 +28,4 @@ public class App { DiscordManager.onDisconnect(); } - - private static void setTimers() { - Calendar midday = Calendar.getInstance(); - midday.set(Calendar.HOUR_OF_DAY, 12); - midday.set(Calendar.MINUTE, 30); - midday.set(Calendar.SECOND, 0); - - Timer reimportCron = new Timer(); - reimportCron.schedule(new AutoReimport(), midday.getTime(), - TimeUnit.MILLISECONDS.convert(12, TimeUnit.HOURS)); - } } diff --git a/app/src/main/java/xyz/vallat/louis/commands/Desinscription.java b/app/src/main/java/xyz/vallat/louis/commands/Desinscription.java index 7b47a0a..b2eb6e6 100644 --- a/app/src/main/java/xyz/vallat/louis/commands/Desinscription.java +++ b/app/src/main/java/xyz/vallat/louis/commands/Desinscription.java @@ -3,6 +3,7 @@ package xyz.vallat.louis.commands; import discord4j.core.event.domain.message.MessageCreateEvent; import reactor.core.publisher.Mono; import xyz.vallat.louis.managers.database.StudentManager; +import xyz.vallat.louis.timer.TimerManager; public class Desinscription extends Command { @@ -16,6 +17,7 @@ public class Desinscription extends Command { if (event.getMessage().getAuthor().isEmpty() || !StudentManager.deleteFromSnowflake(event.getMessage().getAuthor().get().getId().asString())) return channel.createMessage("On dirait que je ne t'ai pas dans ma base de données !"); + TimerManager.cancelStudentTimer(event.getMessage().getAuthor().get().getId().asString()); return channel.createMessage("C'est bon, tu n'es plus dans ma base de données ! :thumbs_up:"); }).then().onErrorResume(t -> fatalError(event, t)); } diff --git a/app/src/main/java/xyz/vallat/louis/managers/calendar/CalendarManager.java b/app/src/main/java/xyz/vallat/louis/managers/calendar/CalendarManager.java index 31af988..c1c56d4 100644 --- a/app/src/main/java/xyz/vallat/louis/managers/calendar/CalendarManager.java +++ b/app/src/main/java/xyz/vallat/louis/managers/calendar/CalendarManager.java @@ -20,6 +20,7 @@ public final class CalendarManager { private static final Logger logger = LoggerFactory.getLogger(CalendarManager.class.getCanonicalName()); public static final Calendar TZ_UTC = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + public static final Calendar TZ = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); private CalendarManager() { } diff --git a/app/src/main/java/xyz/vallat/louis/managers/discord/DiscordManager.java b/app/src/main/java/xyz/vallat/louis/managers/discord/DiscordManager.java index be0adaf..5074bbd 100644 --- a/app/src/main/java/xyz/vallat/louis/managers/discord/DiscordManager.java +++ b/app/src/main/java/xyz/vallat/louis/managers/discord/DiscordManager.java @@ -1,5 +1,6 @@ package xyz.vallat.louis.managers.discord; +import discord4j.common.util.Snowflake; import discord4j.core.DiscordClientBuilder; import discord4j.core.GatewayDiscordClient; import discord4j.core.event.domain.lifecycle.ReadyEvent; @@ -26,7 +27,7 @@ public final class DiscordManager { static { commands.add(new Inscription(PREFIX + "inscription")); - commands.add(new Desinscription(PREFIX + "désincription")); + commands.add(new Desinscription(PREFIX + "désinscription")); commands.add(new Lien(PREFIX + "lien")); commands.add(new Version(PREFIX + "version")); commands.add(new Aide(PREFIX + "aide")); @@ -66,6 +67,14 @@ public final class DiscordManager { registerDiscordCommands(); } + public static Mono sendPrivateMessageToUser(String snowflake, String message) { + if (discordClient != null) { + Snowflake u = Snowflake.of(snowflake); + return discordClient.getUserById(u) + .flatMap(user -> user.getPrivateChannel().flatMap(privateChannel -> privateChannel.createMessage(message))).then(); + } else return Mono.empty(); + } + public static Mono getClientImage() { if (discordClient == null) return Mono.empty(); return discordClient.getSelf().map(User::getAvatarUrl); diff --git a/app/src/main/java/xyz/vallat/louis/timer/TimerManager.java b/app/src/main/java/xyz/vallat/louis/timer/TimerManager.java new file mode 100644 index 0000000..43b4659 --- /dev/null +++ b/app/src/main/java/xyz/vallat/louis/timer/TimerManager.java @@ -0,0 +1,39 @@ +package xyz.vallat.louis.timer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import xyz.vallat.louis.timer.tasks.AutoReimport; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +public final class TimerManager { + + private static final Logger logger = LoggerFactory.getLogger(TimerManager.class.getCanonicalName()); + private static final Map> studentsTimers = new HashMap<>(); + + private TimerManager() { + } + + public static void cancelStudentTimer(String snowflake) { + List timers = studentsTimers.remove(snowflake); + for (Timer t : timers) { + try { + t.cancel(); + } catch (Exception e) { + logger.warn("Got exception while cancelling student's timer:", e); + } + } + } + + public static void startAutoReimportTimer() { + Calendar midday = Calendar.getInstance(); + midday.set(Calendar.HOUR, 12); + midday.set(Calendar.MINUTE, 30); + midday.set(Calendar.SECOND, 0); + + Timer reimportCron = new Timer(); + reimportCron.schedule(new AutoReimport(), midday.getTime(), + TimeUnit.MILLISECONDS.convert(12, TimeUnit.HOURS)); + } +} diff --git a/app/src/main/java/xyz/vallat/louis/timertasks/AutoReimport.java b/app/src/main/java/xyz/vallat/louis/timer/tasks/AutoReimport.java similarity index 92% rename from app/src/main/java/xyz/vallat/louis/timertasks/AutoReimport.java rename to app/src/main/java/xyz/vallat/louis/timer/tasks/AutoReimport.java index d66ad5f..9080d84 100644 --- a/app/src/main/java/xyz/vallat/louis/timertasks/AutoReimport.java +++ b/app/src/main/java/xyz/vallat/louis/timer/tasks/AutoReimport.java @@ -1,4 +1,4 @@ -package xyz.vallat.louis.timertasks; +package xyz.vallat.louis.timer.tasks; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/app/src/main/java/xyz/vallat/louis/timer/tasks/MessagePresence.java b/app/src/main/java/xyz/vallat/louis/timer/tasks/MessagePresence.java new file mode 100644 index 0000000..e1b97a1 --- /dev/null +++ b/app/src/main/java/xyz/vallat/louis/timer/tasks/MessagePresence.java @@ -0,0 +1,30 @@ +package xyz.vallat.louis.timer.tasks; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import xyz.vallat.louis.environment.EnvironmentVariables; +import xyz.vallat.louis.managers.discord.DiscordManager; + +import java.util.TimerTask; + +public class MessagePresence extends TimerTask { + + private static final Logger logger = LoggerFactory.getLogger(MessagePresence.class.getCanonicalName()); + + private final String summary; + private final String snowflake; + + public MessagePresence(String summary, String snowflake) { + this.summary = summary; + this.snowflake = snowflake; + } + + @Override + public void run() { + logger.debug("Warning user {} for course '{}'.", snowflake, summary); + DiscordManager.sendPrivateMessageToUser(snowflake, + "Salut ! Tu as pensé à émarger pour ton cours de " + summary + " ? Le lien vers moodle est ici : " + + System.getenv(EnvironmentVariables.MOODLE_PRESENCE_LINK.getValue())).subscribe(); + } +}