Skip to content

Commit

Permalink
Make notification system more generic internally
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Oct 25, 2024
1 parent 56a3803 commit 2a19373
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import com.rtm516.mcxboxbroadcast.core.Logger;
import com.rtm516.mcxboxbroadcast.core.SessionInfo;
import com.rtm516.mcxboxbroadcast.core.SessionManager;
import com.rtm516.mcxboxbroadcast.core.SlackNotificationManager;
import com.rtm516.mcxboxbroadcast.core.notifications.NotificationManager;
import com.rtm516.mcxboxbroadcast.core.notifications.SlackNotificationManager;
import com.rtm516.mcxboxbroadcast.core.configs.ExtensionConfig;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
Expand All @@ -30,6 +31,7 @@

public class MCXboxBroadcastExtension implements Extension {
Logger logger;
NotificationManager notificationManager;
SessionManager sessionManager;
SessionInfo sessionInfo;
ExtensionConfig config;
Expand Down Expand Up @@ -114,7 +116,8 @@ public void onCommandDefine(GeyserDefineCommandsEvent event) {
private void restart() {
sessionManager.shutdown();

sessionManager = new SessionManager(new FileStorageManager(this.dataFolder().toString()), new SlackNotificationManager(logger, config.slackWebhook()), logger);
// Create a new session manager, but reuse the notification manager as config hasn't been reloaded
sessionManager = new SessionManager(new FileStorageManager(this.dataFolder().toString()), notificationManager, logger);

// Pull onto another thread so we don't hang the main thread
sessionManager.scheduledThread().execute(this::createSession);
Expand All @@ -126,11 +129,15 @@ public void onPostInitialize(GeyserPostInitializeEvent event) {

logger.info("Starting MCXboxBroadcast Extension " + BuildData.VERSION);

sessionManager = new SessionManager(new FileStorageManager(this.dataFolder().toString()), new SlackNotificationManager(logger, config.slackWebhook()), logger);

// Load the config file
config = ConfigLoader.load(this, MCXboxBroadcastExtension.class, ExtensionConfig.class);

// TODO Support multiple notification types
notificationManager = new SlackNotificationManager(logger, config.slackWebhook());

// Create the session manager
sessionManager = new SessionManager(new FileStorageManager(this.dataFolder().toString()), notificationManager, logger);

// Pull onto another thread so we don't hang the main thread
sessionManager.scheduledThread().execute(() -> {
// Get the ip to broadcast
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/geyser/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ slack-webhook:
# The message to send when a friend has restrictions in place that prevent them from be friend with our account
friend-restriction-message: |
%s (%s) has restrictions in place that prevent them from be friend with our account.
%s (%s) has restrictions in place that prevent them from be friend with our account.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.rtm516.mcxboxbroadcast.core.BuildData;
import com.rtm516.mcxboxbroadcast.core.SessionInfo;
import com.rtm516.mcxboxbroadcast.core.SessionManager;
import com.rtm516.mcxboxbroadcast.core.SlackNotificationManager;
import com.rtm516.mcxboxbroadcast.core.notifications.NotificationManager;
import com.rtm516.mcxboxbroadcast.core.notifications.SlackNotificationManager;
import com.rtm516.mcxboxbroadcast.core.configs.StandaloneConfig;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
Expand All @@ -26,6 +27,7 @@ public class StandaloneMain {
private static StandaloneConfig config;
private static StandaloneLoggerImpl logger;
private static SessionInfo sessionInfo;
private static NotificationManager notificationManager;

public static SessionManager sessionManager;

Expand Down Expand Up @@ -65,7 +67,10 @@ public static void main(String[] args) throws Exception {

logger.setDebug(config.debugLog());

sessionManager = new SessionManager(new FileStorageManager("./cache"), new SlackNotificationManager(logger, config.slackWebhook()), logger);
// TODO Support multiple notification types
notificationManager = new SlackNotificationManager(logger, config.slackWebhook());

sessionManager = new SessionManager(new FileStorageManager("./cache"), notificationManager, logger);

sessionInfo = config.session().sessionInfo();

Expand All @@ -83,7 +88,8 @@ public static void restart() {
try {
sessionManager.shutdown();

sessionManager = new SessionManager(new FileStorageManager("./cache"), new SlackNotificationManager(logger, config.slackWebhook()), logger);
// Create a new session manager, but reuse the notification manager as config hasn't been reloaded
sessionManager = new SessionManager(new FileStorageManager("./cache"), notificationManager, logger);

createSession();
} catch (SessionCreationException | SessionUpdateException e) {
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/standalone/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ slack-webhook:
debug-log: false

# Suppresses "Updated session!" log into debug
suppress-session-update-info: false
suppress-session-update-info: false
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.rtm516.mcxboxbroadcast.core.models.auth.SisuAuthorizeBody;
import com.rtm516.mcxboxbroadcast.core.models.auth.XboxTokenInfo;
import com.rtm516.mcxboxbroadcast.core.models.auth.XstsAuthData;
import com.rtm516.mcxboxbroadcast.core.notifications.NotificationManager;
import com.rtm516.mcxboxbroadcast.core.storage.StorageManager;
import java.io.IOException;

Expand All @@ -31,7 +32,7 @@
import net.raphimc.minecraftauth.util.logging.ILogger;

public class AuthManager {
private final SlackNotificationManager slackNotificationManager;
private final NotificationManager notificationManager;
private final StorageManager storageManager;
private final Logger logger;

Expand All @@ -42,12 +43,12 @@ public class AuthManager {
/**
* Create an instance of AuthManager
*
* @param slackNotificationManager The Slack notification manager to use for sending messages
* @param notificationManager The notification manager to use for sending messages
* @param storageManager The storage manager to use for storing data
* @param logger The logger to use for outputting messages
*/
public AuthManager(SlackNotificationManager slackNotificationManager, StorageManager storageManager, Logger logger) {
this.slackNotificationManager = slackNotificationManager;
public AuthManager(NotificationManager notificationManager, StorageManager storageManager, Logger logger) {
this.notificationManager = notificationManager;
this.storageManager = storageManager;
this.logger = logger.prefixed("Auth");

Expand Down Expand Up @@ -113,7 +114,7 @@ private void initialise() {
if (xboxToken == null) {
xboxToken = MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.getFromInput(logger, httpClient, new StepMsaDeviceCode.MsaDeviceCodeCallback(msaDeviceCode -> {
logger.info("To sign in, use a web browser to open the page " + msaDeviceCode.getVerificationUri() + " and enter the code " + msaDeviceCode.getUserCode() + " to authenticate.");
slackNotificationManager.sendSessionExpiredNotification(msaDeviceCode.getVerificationUri(), msaDeviceCode.getUserCode());
notificationManager.sendSessionExpiredNotification(msaDeviceCode.getVerificationUri(), msaDeviceCode.getUserCode());
}));
} else if (xboxToken.isExpired()) {
xboxToken = MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.refresh(logger, httpClient, xboxToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private void internalProcess() {
forceUnfollow(entry.getKey());

logger.warn("Removed " + entry.getValue() + " (" + entry.getKey() + ") as a friend due to restrictions on their account");
sessionManager.slackNotificationManager().sendFriendRestrictionNotification(entry.getValue(), entry.getKey());
sessionManager.notificationManager().sendFriendRestrictionNotification(entry.getValue(), entry.getKey());
} else {
logger.warn("Failed to add " + entry.getValue() + " (" + entry.getKey() + ") as a friend: (" + response.statusCode() + ") " + response.body());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
import com.rtm516.mcxboxbroadcast.core.models.session.CreateSessionRequest;
import com.rtm516.mcxboxbroadcast.core.models.session.CreateSessionResponse;
import com.rtm516.mcxboxbroadcast.core.notifications.NotificationManager;
import com.rtm516.mcxboxbroadcast.core.storage.StorageManager;
import org.java_websocket.util.NamedThreadFactory;

Expand Down Expand Up @@ -35,11 +36,11 @@ public class SessionManager extends SessionManagerCore {
* Create an instance of SessionManager
*
* @param storageManager The storage manager to use for storing data
* @param slackNotificationManager The Slack notification manager to use for sending messages
* @param notificationManager The notification manager to use for sending messages
* @param logger The logger to use for outputting messages
*/
public SessionManager(StorageManager storageManager, SlackNotificationManager slackNotificationManager, Logger logger) {
super(storageManager, slackNotificationManager, logger.prefixed("Primary Session"));
public SessionManager(StorageManager storageManager, NotificationManager notificationManager, Logger logger) {
super(storageManager, notificationManager, logger.prefixed("Primary Session"));
this.scheduledThreadPool = Executors.newScheduledThreadPool(5, new NamedThreadFactory("MCXboxBroadcast Thread"));
this.subSessionManagers = new HashMap<>();
}
Expand Down Expand Up @@ -100,7 +101,7 @@ public void init(SessionInfo sessionInfo, FriendSyncConfig friendSyncConfig) thr
// Create the sub-session manager for each sub-session
for (String subSession : finalSubSessions) {
try {
SubSessionManager subSessionManager = new SubSessionManager(subSession, this, storageManager.subSession(subSession), slackNotificationManager, logger);
SubSessionManager subSessionManager = new SubSessionManager(subSession, this, storageManager.subSession(subSession), notificationManager, logger);
subSessionManager.init();
subSessionManager.friendManager().initAutoFriend(this.friendSyncConfig);
subSessionManagers.put(subSession, subSessionManager);
Expand Down Expand Up @@ -204,7 +205,7 @@ public void addSubSession(String id) {

// Create the sub-session manager
try {
SubSessionManager subSessionManager = new SubSessionManager(id, this, storageManager.subSession(id), slackNotificationManager,logger);
SubSessionManager subSessionManager = new SubSessionManager(id, this, storageManager.subSession(id), notificationManager, logger);
subSessionManager.init();
subSessionManager.friendManager().initAutoFriend(friendSyncConfig);
subSessionManagers.put(id, subSessionManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.rtm516.mcxboxbroadcast.core.models.session.SessionRef;
import com.rtm516.mcxboxbroadcast.core.models.session.SocialSummaryResponse;
import com.rtm516.mcxboxbroadcast.core.models.auth.XboxTokenInfo;
import com.rtm516.mcxboxbroadcast.core.notifications.NotificationManager;
import com.rtm516.mcxboxbroadcast.core.storage.StorageManager;

import com.rtm516.mcxboxbroadcast.core.webrtc.RtcWebsocketClient;
Expand Down Expand Up @@ -40,7 +41,7 @@ public abstract class SessionManagerCore {
protected final Logger logger;
protected final Logger coreLogger;
protected final StorageManager storageManager;
protected final SlackNotificationManager slackNotificationManager;
protected final NotificationManager notificationManager;

protected RtaWebsocketClient rtaWebsocket;
protected ExpandedSessionInfo sessionInfo;
Expand All @@ -53,10 +54,10 @@ public abstract class SessionManagerCore {
* Create an instance of SessionManager
*
* @param storageManager The storage manager to use for storing data
* @param slackNotificationManager The Slack notification manager to use for sending messages
* @param notificationManager The notification manager to use for sending messages
* @param logger The logger to use for outputting messages
*/
public SessionManagerCore(StorageManager storageManager, SlackNotificationManager slackNotificationManager, Logger logger) {
public SessionManagerCore(StorageManager storageManager, NotificationManager notificationManager, Logger logger) {
this.httpClient = Methanol.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.NORMAL)
Expand All @@ -66,9 +67,9 @@ public SessionManagerCore(StorageManager storageManager, SlackNotificationManage
this.logger = logger;
this.coreLogger = logger.prefixed("");
this.storageManager = storageManager;
this.slackNotificationManager = slackNotificationManager;
this.notificationManager = notificationManager;

this.authManager = new AuthManager(slackNotificationManager, storageManager, logger);
this.authManager = new AuthManager(notificationManager, storageManager, logger);

this.friendManager = new FriendManager(httpClient, logger, this);

Expand All @@ -86,12 +87,12 @@ public FriendManager friendManager() {
}

/**
* Get the Slack notification manager for this session manager
* Get the notification manager for this session manager
*
* @return The Slack notification manager
* @return The notification manager
*/
public SlackNotificationManager slackNotificationManager() {
return slackNotificationManager;
public NotificationManager notificationManager() {
return notificationManager;
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
import com.rtm516.mcxboxbroadcast.core.models.session.JoinSessionRequest;
import com.rtm516.mcxboxbroadcast.core.notifications.NotificationManager;
import com.rtm516.mcxboxbroadcast.core.storage.StorageManager;

import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -18,11 +19,11 @@ public class SubSessionManager extends SessionManagerCore {
* @param id The id of the sub-session
* @param parent The parent session manager
* @param storageManager The storage manager to use for storing data
* @param slackNotificationManager The Slack notification manager to use for sending messages
* @param notificationManager The notification manager to use for sending messages
* @param logger The logger to use for outputting messages
*/
public SubSessionManager(String id, SessionManager parent, StorageManager storageManager, SlackNotificationManager slackNotificationManager, Logger logger) {
super(storageManager, slackNotificationManager, logger.prefixed("Sub-Session " + id));
public SubSessionManager(String id, SessionManager parent, StorageManager storageManager, NotificationManager notificationManager, Logger logger) {
super(storageManager, notificationManager, logger.prefixed("Sub-Session " + id));
this.parent = parent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public record ExtensionConfig(
@JsonProperty("remote-port") String remotePort,
@JsonProperty("update-interval") int updateInterval,
@JsonProperty("friend-sync") FriendSyncConfig friendSync,
@JsonProperty("slack-webhook") SlackWebhookConfig slackWebhook) {
@JsonProperty("slack-webhook") NotificationConfig slackWebhook) { // TODO Rename once we support multiple notification types
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;

public record SlackWebhookConfig(
public record NotificationConfig(
@JsonProperty("enabled") boolean enabled,
@JsonProperty("webhook-url") String webhookUrl,
@JsonProperty("session-expired-message") String sessionExpiredMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public record StandaloneConfig(
@JsonProperty("debug-log") boolean debugLog,
@JsonProperty("suppress-session-update-info") boolean suppressSessionUpdateInfo,
@JsonProperty("friend-sync") FriendSyncConfig friendSync,
@JsonProperty("slack-webhook") SlackWebhookConfig slackWebhook) {
@JsonProperty("slack-webhook") NotificationConfig slackWebhook) {
}
Loading

0 comments on commit 2a19373

Please sign in to comment.