1616import java .util .concurrent .BlockingQueue ;
1717import java .util .concurrent .PriorityBlockingQueue ;
1818import java .util .concurrent .atomic .AtomicLong ;
19+ import java .util .function .Supplier ;
1920import java .util .regex .Pattern ;
2021
2122import org .jsoup .Jsoup ;
@@ -579,7 +580,7 @@ public boolean isCondensableOrEphemeral() {
579580 public boolean isEphemeral () {
580581 return ephemeral ;
581582 }
582-
583+
583584 /**
584585 * Gets the ID of the message that this was a reply to.
585586 * @return the parent ID or 0 if it's not a reply
@@ -689,7 +690,10 @@ public void complete() {
689690 }
690691
691692 private void handleMessage (ChatMessage message ) {
692- if (timeout && !isAdminUser (message .userId ())) {
693+ var userId = message .userId ();
694+ var isAdminUser = isAdminUser (userId );
695+
696+ if (timeout && !isAdminUser ) {
693697 //bot is in timeout, ignore
694698 return ;
695699 }
@@ -699,23 +703,26 @@ private void handleMessage(ChatMessage message) {
699703 return ;
700704 }
701705
702- if (!allowedUsers .isEmpty () && !allowedUsers .contains (message .userId ())) {
706+ var hasAllowedUsersList = !allowedUsers .isEmpty ();
707+ var userIsAllowed = allowedUsers .contains (userId );
708+ if (hasAllowedUsersList && !userIsAllowed ) {
703709 //message was posted by a user who is not in the green list, ignore
704710 return ;
705711 }
706712
707- if (bannedUsers .contains (message .userId ())) {
713+ var userIsBanned = bannedUsers .contains (userId );
714+ if (userIsBanned ) {
708715 //message was posted by a banned user, ignore
709716 return ;
710717 }
711718
712- var room = connection .getRoom (message .roomId ());
713- if (room == null ) {
719+ var isInRoom = connection .isInRoom (message .roomId ());
720+ if (! isInRoom ) {
714721 //the bot is no longer in the room
715722 return ;
716723 }
717724
718- if (message . userId () == userId ) {
725+ if (userId == Bot . this . userId ) {
719726 //message was posted by this bot
720727 handleBotMessage (message );
721728 return ;
@@ -797,34 +804,39 @@ private void handleActions(ChatMessage message, ChatActions actions) {
797804 var queue = new LinkedList <>(actions .getActions ());
798805 while (!queue .isEmpty ()) {
799806 var action = queue .removeFirst ();
807+ processAction (action , message , queue );
808+ }
809+ }
800810
801- if (action instanceof PostMessage pm ) {
802- handlePostMessageAction (pm , message );
803- continue ;
804- }
811+ private void processAction (ChatAction action , ChatMessage message , LinkedList <ChatAction > queue ) {
812+ if (action instanceof PostMessage pm ) {
813+ handlePostMessageAction (pm , message );
814+ return ;
815+ }
805816
806- if (action instanceof DeleteMessage dm ) {
807- var response = handleDeleteMessageAction (dm , message );
808- queue .addAll (response .getActions ());
809- continue ;
810- }
817+ if (action instanceof DeleteMessage dm ) {
818+ var response = handleDeleteMessageAction (dm , message );
819+ queue .addAll (response .getActions ());
820+ return ;
821+ }
811822
812- if (action instanceof JoinRoom jr ) {
813- var response = handleJoinRoomAction (jr );
814- queue .addAll (response .getActions ());
815- continue ;
816- }
823+ if (action instanceof JoinRoom jr ) {
824+ var response = handleJoinRoomAction (jr );
825+ queue .addAll (response .getActions ());
826+ return ;
827+ }
817828
818- if (action instanceof LeaveRoom lr ) {
819- handleLeaveRoomAction (lr );
820- continue ;
821- }
829+ if (action instanceof LeaveRoom lr ) {
830+ handleLeaveRoomAction (lr );
831+ return ;
832+ }
822833
823- if (action instanceof Shutdown ) {
824- stop ();
825- continue ;
826- }
834+ if (action instanceof Shutdown ) {
835+ stop ();
836+ return ;
827837 }
838+
839+ logger .atWarn ().log (() -> "Unknown action type: " + action .getClass ().getName ());
828840 }
829841
830842 private void handlePostMessageAction (PostMessage action , ChatMessage message ) {
@@ -865,19 +877,11 @@ private ChatActions handleJoinRoomAction(JoinRoom action) {
865877 return action .onSuccess ().get ();
866878 }
867879
868- try {
869- leave (action .roomId ());
870- } catch (Exception e ) {
871- logger .atError ().setCause (e ).log (() -> "Problem leaving room " + action .roomId () + " after it was found that the bot can't post messages to it." );
872- }
880+ leaveRoomSafely (action .roomId (), () -> "Problem leaving room " + action .roomId () + " after it was found that the bot can't post messages to it." );
873881
874882 return action .ifLackingPermissionToPost ().get ();
875883 } catch (PrivateRoomException | RoomPermissionException e ) {
876- try {
877- leave (action .roomId ());
878- } catch (Exception e2 ) {
879- logger .atError ().setCause (e2 ).log (() -> "Problem leaving room " + action .roomId () + " after it was found that the bot can't join or post messages to it." );
880- }
884+ leaveRoomSafely (action .roomId (), () -> "Problem leaving room " + action .roomId () + " after it was found that the bot can't join or post messages to it." );
881885
882886 return action .ifLackingPermissionToPost ().get ();
883887 } catch (RoomNotFoundException e ) {
@@ -887,6 +891,19 @@ private ChatActions handleJoinRoomAction(JoinRoom action) {
887891 }
888892 }
889893
894+ /**
895+ * Attempts to leave a room and logs any errors that occur.
896+ * @param roomId the room ID to leave
897+ * @param logMessage the log message
898+ **/
899+ private void leaveRoomSafely (int roomId , Supplier <String > logMessage ) {
900+ try {
901+ leave (roomId );
902+ } catch (Exception e ) {
903+ logger .atError ().setCause (e ).log (logMessage );
904+ }
905+ }
906+
890907 private void handleLeaveRoomAction (LeaveRoom action ) {
891908 try {
892909 leave (action .roomId ());
0 commit comments