[AB-303] adding scheduled configurable activity updates

This commit is contained in:
Sheldan
2021-07-13 00:35:15 +02:00
parent c7514a6bad
commit ac605e4791
25 changed files with 1859 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.sheldan.abstracto.modules</groupId>
<artifactId>dynamic-activity</artifactId>
<version>1.3.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dynamic-activity-impl</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/liquibase.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>dev.sheldan.abstracto.modules</groupId>
<artifactId>dynamic-activity-int</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.sheldan.abstracto.scheduling</groupId>
<artifactId>scheduling-int</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.sheldan.abstracto.core</groupId>
<artifactId>core-int</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,18 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>liquibase</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<outputDirectory>.</outputDirectory>
<directory>${project.basedir}/src/main/resources/migrations</directory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>

View File

@@ -0,0 +1,27 @@
package dev.sheldan.abstracto.activity.job;
import dev.sheldan.abstracto.activity.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
@Slf4j
@DisallowConcurrentExecution
@Component
@PersistJobDataAfterExecution
public class DynamicActivityJob extends QuartzJobBean {
@Autowired
private ActivityService activityService;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("Switching activity.");
activityService.switchToOtherActivity();
}
}

View File

@@ -0,0 +1,18 @@
package dev.sheldan.abstracto.activity.listener;
import dev.sheldan.abstracto.activity.service.ActivityService;
import dev.sheldan.abstracto.core.listener.AsyncStartupListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ActivityStartupListener implements AsyncStartupListener {
@Autowired
private ActivityService activityService;
@Override
public void execute() {
activityService.switchToOtherActivity();
}
}

View File

@@ -0,0 +1,9 @@
package dev.sheldan.abstracto.activity.repository;
import dev.sheldan.abstracto.activity.models.CustomActivity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ActivityRepository extends JpaRepository<CustomActivity, Long> {
}

View File

@@ -0,0 +1,68 @@
package dev.sheldan.abstracto.activity.service;
import dev.sheldan.abstracto.activity.models.CustomActivity;
import dev.sheldan.abstracto.activity.service.management.ActivityManagementService;
import dev.sheldan.abstracto.core.service.BotService;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Activity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.security.SecureRandom;
import java.util.List;
@Component
@Slf4j
public class ActivityServiceBean implements ActivityService {
@Autowired
private ActivityManagementService activityManagementService;
@Autowired
private SecureRandom secureRandom;
@Autowired
private BotService botService;
@Autowired
private TemplateService templateService;
@Override
@Transactional
public void switchToOtherActivity() {
List<CustomActivity> activities = activityManagementService.getAllActivities();
if(!activities.isEmpty()) {
CustomActivity chosen = activities.get(secureRandom.nextInt(activities.size()));
log.info("Chosen {} activity.", chosen.getId());
switchToActivity(chosen);
} else {
log.info("No activities configured.");
}
}
@Override
public void switchToActivity(CustomActivity activity) {
JDA jda = botService.getInstance();
String text = templateService.renderSimpleTemplate("dynamic_activity_" + activity.getTemplateKey());
Activity jdaActivity = matchActivity(activity, text);
jda.getPresence().setActivity(jdaActivity);
}
@Override
public Activity matchActivity(CustomActivity activity, String text) {
switch (activity.getType()) {
case STREAMING:
return Activity.streaming(text, "https://twitch.tv/a");
case WATCHING:
return Activity.watching(text);
case LISTENING:
return Activity.listening(text);
case PLAYING:
default:
return Activity.playing(text);
}
}
}

View File

@@ -0,0 +1,20 @@
package dev.sheldan.abstracto.activity.service.management;
import dev.sheldan.abstracto.activity.repository.ActivityRepository;
import dev.sheldan.abstracto.activity.models.CustomActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class ActivityManagementServiceBean implements ActivityManagementService {
@Autowired
private ActivityRepository activityRepository;
@Override
public List<CustomActivity> getAllActivities() {
return activityRepository.findAll();
}
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<include file="tables/tables.xml" relativeToChangelogFile="true"/>
<include file="seedData/data.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,19 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<changeSet author="Sheldan" id="activity_job-insert">
<insert tableName="scheduler_job">
<column name="name" value="activityJob"/>
<column name="group_name" value="activity"/>
<column name="clazz" value="dev.sheldan.abstracto.activity.job.DynamicActivityJob"/>
<column name="active" value="true"/>
<column name="cron_expression" value="0 0/10 * 1/1 * ?"/>
<column name="recovery" value="false"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<include file="activity_job.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,29 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<changeSet author="Sheldan" id="activity-table">
<createTable tableName="activity">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true" primaryKeyName="activity_pkey"/>
</column>
<column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
<constraints nullable="false"/>
</column>
<column name="type" type="VARCHAR(100)">
<constraints nullable="false" />
</column>
<column name="template_key" type="VARCHAR(100)">
<constraints nullable="false" />
</column>
</createTable>
<sql>
DROP TRIGGER IF EXISTS activity_insert_trigger ON activity;
CREATE TRIGGER activity_insert_trigger BEFORE INSERT ON activity FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
</sql>
</changeSet>
</databaseChangeLog>

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<include file="activity.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<include file="1.3.1/collection.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dynamic-activity</artifactId>
<groupId>dev.sheldan.abstracto.modules</groupId>
<version>1.3.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dynamic-activity-int</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>dev.sheldan.abstracto.core</groupId>
<artifactId>core-int</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,5 @@
package dev.sheldan.abstracto.activity.models;
public enum ActivityType {
WATCHING, STREAMING, PLAYING, LISTENING
}

View File

@@ -0,0 +1,31 @@
package dev.sheldan.abstracto.activity.models;
import lombok.*;
import javax.persistence.*;
import java.time.Instant;
@Builder
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "activity")
@Getter
@Setter
@EqualsAndHashCode
public class CustomActivity {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Enumerated(EnumType.STRING)
@Column(name = "type")
private ActivityType type;
@Column(name = "template_key")
private String templateKey;
@Column(name = "created", nullable = false, insertable = false, updatable = false)
private Instant created;
}

View File

@@ -0,0 +1,9 @@
package dev.sheldan.abstracto.activity.service;
import dev.sheldan.abstracto.activity.models.CustomActivity;
public interface ActivityService {
void switchToOtherActivity();
void switchToActivity(CustomActivity activity);
net.dv8tion.jda.api.entities.Activity matchActivity(CustomActivity activity, String text);
}

View File

@@ -0,0 +1,9 @@
package dev.sheldan.abstracto.activity.service.management;
import dev.sheldan.abstracto.activity.models.CustomActivity;
import java.util.List;
public interface ActivityManagementService {
List<CustomActivity> getAllActivities();
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>abstracto-modules</artifactId>
<groupId>dev.sheldan.abstracto.modules</groupId>
<version>1.3.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dynamic-activity</artifactId>
<packaging>pom</packaging>
<modules>
<module>dynamic-activity-int</module>
<module>dynamic-activity-impl</module>
</modules>
</project>

View File

@@ -28,6 +28,7 @@
<module>invite-filter</module>
<module>profanity-filter</module>
<module>voice-channel-context</module>
<module>dynamic-activity</module>
</modules>
</project>

View File

@@ -6,6 +6,7 @@ import lombok.*;
import javax.persistence.*;
import java.io.Serializable;
import java.time.Instant;
@Entity
@Table(name="voice_channel_context")
@@ -29,4 +30,7 @@ public class VoiceChannelContext implements Serializable {
@JoinColumn(name = "role_id", nullable = false)
private ARole role;
@Column(name = "created", nullable = false, insertable = false, updatable = false)
private Instant created;
}

View File

@@ -180,6 +180,18 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.sheldan.abstracto.modules</groupId>
<artifactId>dynamic-activity-int</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.sheldan.abstracto.modules</groupId>
<artifactId>dynamic-activity-impl</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.sheldan.abstracto.modules</groupId>
<artifactId>starboard-impl</artifactId>

View File

@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.command.model.database.ACommand;
import dev.sheldan.abstracto.core.command.service.management.CommandInServerManagementService;
import dev.sheldan.abstracto.core.command.service.management.CommandManagementService;
import dev.sheldan.abstracto.core.listener.AsyncStartupListener;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AChannelType;
import dev.sheldan.abstracto.core.models.database.ARole;
@@ -25,6 +26,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.security.auth.login.LoginException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@Slf4j
@@ -37,6 +39,12 @@ public class StartupServiceBean implements Startup {
@Autowired
private List<? extends ListenerAdapter> listeners;
@Autowired(required = false)
private List<AsyncStartupListener> startupListeners;
@Autowired
private StartupServiceBean self;
@Autowired
private ServerManagementService serverManagementService;
@@ -67,9 +75,37 @@ public class StartupServiceBean implements Startup {
log.info("Synchronizing servers.");
synchronizeServers();
log.info("Done synchronizing servers");
executeStartUpListeners();
profanityService.reloadRegex();
}
private void executeStartUpListeners() {
if(startupListeners == null) {
return;
}
log.info("Executing {} startup listeners.", startupListeners.size());
startupListeners.forEach(asyncStartupListener ->
CompletableFuture.runAsync(() -> {
try {
log.info("Executing startup listener {}.", asyncStartupListener);
self.executeStartupListener(asyncStartupListener);
} catch (Exception e) {
log.error("Startup listener {} failed.", asyncStartupListener);
}
}).thenAccept(unused -> log.info("Startup listener {} finished.", asyncStartupListener))
.exceptionally(throwable -> {
log.error("Startup listener {} failed.", asyncStartupListener);
return null;
})
);
}
@Transactional
public void executeStartupListener(AsyncStartupListener startupListener) {
startupListener.execute();
}
private void synchronizeServers(){
JDA instance = service.getInstance();
List<Guild> onlineGuilds = instance.getGuilds();

View File

@@ -0,0 +1,5 @@
package dev.sheldan.abstracto.core.listener;
public interface AsyncStartupListener {
void execute();
}