From cb0f2689c636e2725501ab072932aba78ed2e6f4 Mon Sep 17 00:00:00 2001 From: Andy Janata Date: Wed, 28 Feb 2018 11:19:34 -0800 Subject: [PATCH] Let global chat enablement be configured via the properties file. Can be adjusted on the fly, though clients are not notified of the change (which doesn't affect the client's operation anyway). --- WebContent/js/cah.ajax.handlers.js | 4 +- build.properties.example | 1 + .../filtered-resources/WEB-INF/pyx.properties | 1 + .../java/net/socialgamer/cah/CahModule.java | 14 +++++++ .../socialgamer/cah/handlers/ChatHandler.java | 18 ++++++--- .../cah/servlets/JavascriptConfigServlet.java | 38 ++++++++++++++++--- 6 files changed, 63 insertions(+), 13 deletions(-) diff --git a/WebContent/js/cah.ajax.handlers.js b/WebContent/js/cah.ajax.handlers.js index f1b1b30..169a0fa 100644 --- a/WebContent/js/cah.ajax.handlers.js +++ b/WebContent/js/cah.ajax.handlers.js @@ -96,7 +96,9 @@ cah.ajax.after_registered = function() { $("#bottom").removeClass("hide"); // TODO once there are channels, this needs to specify the global channel cah.Ajax.build(cah.$.AjaxOperation.NAMES).run(); - cah.log.error("IMPORTANT: Global chat has been disabled."); + if (!cah.GLOBAL_CHAT_ENABLED) { + cah.log.error("IMPORTANT: Global chat has been disabled."); + } cah.GameList.instance.show(); cah.GameList.instance.update(); cah.longpoll.longPoll(); diff --git a/build.properties.example b/build.properties.example index 4fc179b..e13d25d 100644 --- a/build.properties.example +++ b/build.properties.example @@ -3,6 +3,7 @@ pyx.max_users=100 pyx.max_games=25 pyx.include_inactive_cardsets=true pyx.broadcast_connects_and_disconnects=true +pyx.global_chat_enabled=true # for production use, use postgres #hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect diff --git a/src/main/filtered-resources/WEB-INF/pyx.properties b/src/main/filtered-resources/WEB-INF/pyx.properties index 42123b3..9708c21 100644 --- a/src/main/filtered-resources/WEB-INF/pyx.properties +++ b/src/main/filtered-resources/WEB-INF/pyx.properties @@ -3,6 +3,7 @@ pyx.server.include_inactive_cardsets=${pyx.include_inactive_cardsets} pyx.server.max_users=${pyx.max_users} pyx.server.max_games=${pyx.max_games} pyx.server.broadcast_connects_and_disconnects=${pyx.broadcast_connects_and_disconnects} +pyx.server.global_chat_enabled=${pyx.global_chat_enabled} pyx.build=${buildNumber} # this is NOT allowed to be changed during a reload, as metrics depend on previous events diff --git a/src/main/java/net/socialgamer/cah/CahModule.java b/src/main/java/net/socialgamer/cah/CahModule.java index 938c48d..bd509c7 100644 --- a/src/main/java/net/socialgamer/cah/CahModule.java +++ b/src/main/java/net/socialgamer/cah/CahModule.java @@ -159,6 +159,15 @@ public class CahModule extends AbstractModule { } } + @Provides + @GlobalChatEnabled + Boolean provideGlobalChatEnabled() { + synchronized (properties) { + return Boolean.valueOf(properties.getProperty( + "pyx.server.global_chat_enabled", "true")); + } + } + @Provides @CookieDomain String getCookieDomain() { @@ -204,6 +213,11 @@ public class CahModule extends AbstractModule { public @interface BroadcastConnectsAndDisconnects { } + @BindingAnnotation + @Retention(RetentionPolicy.RUNTIME) + public @interface GlobalChatEnabled { + } + @BindingAnnotation @Retention(RetentionPolicy.RUNTIME) public @interface CookieDomain { diff --git a/src/main/java/net/socialgamer/cah/handlers/ChatHandler.java b/src/main/java/net/socialgamer/cah/handlers/ChatHandler.java index 573c5ae..082c0c4 100644 --- a/src/main/java/net/socialgamer/cah/handlers/ChatHandler.java +++ b/src/main/java/net/socialgamer/cah/handlers/ChatHandler.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2012, Andy Janata + * Copyright (c) 2012-2018, Andy Janata * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted @@ -28,6 +28,9 @@ import java.util.Map; import javax.servlet.http.HttpSession; +import com.google.inject.Inject; + +import net.socialgamer.cah.CahModule.GlobalChatEnabled; import net.socialgamer.cah.Constants; import net.socialgamer.cah.Constants.AjaxOperation; import net.socialgamer.cah.Constants.AjaxRequest; @@ -41,8 +44,6 @@ import net.socialgamer.cah.data.ConnectedUsers; import net.socialgamer.cah.data.QueuedMessage.MessageType; import net.socialgamer.cah.data.User; -import com.google.inject.Inject; - /** * Handler for chat messages. @@ -54,10 +55,13 @@ public class ChatHandler extends Handler { public static final String OP = AjaxOperation.CHAT.toString(); private final ConnectedUsers users; + private final boolean globalChatEnabled; @Inject - public ChatHandler(final ConnectedUsers users) { + public ChatHandler(final ConnectedUsers users, + @GlobalChatEnabled final boolean globalChatEnabled) { this.users = users; + this.globalChatEnabled = globalChatEnabled; } @Override @@ -74,8 +78,10 @@ public class ChatHandler extends Handler { if (request.getParameter(AjaxRequest.MESSAGE) == null) { return error(ErrorCode.NO_MSG_SPECIFIED); - } else if (/* wall && */!user.isAdmin()) { - // Making global chat admin-only because it's hopeless + } else if (wall && !user.isAdmin()) { + return error(ErrorCode.NOT_ADMIN); + } else if (!globalChatEnabled && !user.isAdmin()) { + // global chat can be turned off in the properties file return error(ErrorCode.NOT_ADMIN); } else { final String message = request.getParameter(AjaxRequest.MESSAGE).trim(); diff --git a/src/main/java/net/socialgamer/cah/servlets/JavascriptConfigServlet.java b/src/main/java/net/socialgamer/cah/servlets/JavascriptConfigServlet.java index 8a95ccd..cb08738 100644 --- a/src/main/java/net/socialgamer/cah/servlets/JavascriptConfigServlet.java +++ b/src/main/java/net/socialgamer/cah/servlets/JavascriptConfigServlet.java @@ -1,3 +1,26 @@ +/** + * Copyright (c) 2012-2018, Andy Janata + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + package net.socialgamer.cah.servlets; import java.io.IOException; @@ -10,12 +33,13 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import net.socialgamer.cah.CahModule.CookieDomain; -import net.socialgamer.cah.StartupUtils; - import com.google.inject.Injector; import com.google.inject.Key; +import net.socialgamer.cah.CahModule.CookieDomain; +import net.socialgamer.cah.CahModule.GlobalChatEnabled; +import net.socialgamer.cah.StartupUtils; + @WebServlet("/js/cah.config.js") public class JavascriptConfigServlet extends HttpServlet { @@ -47,16 +71,18 @@ public class JavascriptConfigServlet extends HttpServlet { protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { - // We have to do this every time since it comes from the properties file and that can change... - final StringBuilder builder = new StringBuilder(256); + // We have to do this every time since these come from the properties file and that can change... + final StringBuilder builder = new StringBuilder(256).append(configString); // Ideally we'd figure out how to make this Servlet itself injectable but I don't have time. final Injector injector = (Injector) getServletContext().getAttribute(StartupUtils.INJECTOR); final String cookieDomain = injector.getInstance(Key.get(String.class, CookieDomain.class)); + final Boolean globalChatEnabled = injector.getInstance(Key.get(Boolean.class, GlobalChatEnabled.class)); builder.append(String.format("cah.COOKIE_DOMAIN = '%s';\n", cookieDomain)); + builder.append(String.format("cah.GLOBAL_CHAT_ENABLED = %b;\n", globalChatEnabled)); resp.setContentType("text/javascript"); final PrintWriter out = resp.getWriter(); - out.println(configString + builder.toString()); + out.println(builder.toString()); out.flush(); out.close(); }