Browse Source

Konfigurationen ausgelagert, Erwähnungen implementiert

Bugfix für Emoji-Patterns
Styling-Fixes
Alexander Vornam 2 years ago
parent
commit
1bf57ad847
6 changed files with 99 additions and 22 deletions
  1. 3 0
      config/admins.json
  2. 14 0
      config/config.json
  3. 29 10
      index.js
  4. 6 0
      www/css/app.css
  5. 14 5
      www/css/theme.css
  6. 33 7
      www/js/main.js

+ 3 - 0
config/admins.json

@@ -0,0 +1,3 @@
+{
+  "André": "passwort"
+}

+ 14 - 0
config/config.json

@@ -0,0 +1,14 @@
+{
+  "port": 443,
+  "historySize": 50,
+  "randomColors": [
+    "#c42020",
+    "#de801e",
+    "#f6c60b",
+    "#19ce19",
+    "#129696",
+    "#2c53e3",
+    "#6f23e3",
+    "#883f88"
+  ]
+}

+ 29 - 10
index.js

@@ -14,23 +14,27 @@ const options = {
 const server = https.createServer(options, app);
 const server = https.createServer(options, app);
 const io = require('socket.io')(server);
 const io = require('socket.io')(server);
 
 
-const PORT = 443;
+var port = 443;
 
 
-const admins = {
-    'André': 'passwort'
-}
+var admins = {}
+
+fs.readFile("config/admins.json", "utf8", (err, data) => {
+    if (!err && data) {
+        admins = JSON.parse(data);
+    }
+})
 
 
 var users = [];
 var users = [];
 
 
 var bannedIps = [];
 var bannedIps = [];
 
 
 fs.readFile("config/ban.json", "utf8", (err, data) => {
 fs.readFile("config/ban.json", "utf8", (err, data) => {
-    if (!err) {
+    if (!err && data) {
         bannedIps = JSON.parse(data);
         bannedIps = JSON.parse(data);
     }
     }
 })
 })
 
 
-const randomColors = [
+var randomColors = [
     '#c42020',
     '#c42020',
     '#de801e',
     '#de801e',
     '#f6c60b',
     '#f6c60b',
@@ -41,9 +45,18 @@ const randomColors = [
     '#883f88',
     '#883f88',
 ];
 ];
 
 
-const HISTORY_SIZE = 50;
+var historySize = 50;
 var history = [];
 var history = [];
 
 
+fs.readFile("config/config.json", "utf8", (err, data) => {
+    if (!err) {
+        const config = JSON.parse(data);
+        if (config.port) port = config.port;
+        if (config.historySize) historySize = config.historySize;
+        if (config.randomColors) randomColors = config.randomColors;
+    }
+})
+
 app.use(express.static(__dirname + '/www'));
 app.use(express.static(__dirname + '/www'));
 app.use('/node_modules', express.static(__dirname + '/node_modules'));
 app.use('/node_modules', express.static(__dirname + '/node_modules'));
 app.get('/img/ava/:username', (req, res) => {
 app.get('/img/ava/:username', (req, res) => {
@@ -165,8 +178,14 @@ io.on('connection', (socket) => {
                     return '<a href="' + url + '"><img class="favicon" src="' + faviconUrl + '" alt="" />' + url + '</a>';
                     return '<a href="' + url + '"><img class="favicon" src="' + faviconUrl + '" alt="" />' + url + '</a>';
                 });
                 });
 
 
+                for (const u of users) {
+                    while (msg.indexOf('@' + u.name) > -1) {
+                        msg = msg.replace('@' + u.name, '<span class="mention" style="color: ' + u.color + '">' + u.name + '</span>');
+                    }
+                }
+
                 log(user.name + ':', msg, '(' + user.ip + ')');
                 log(user.name + ':', msg, '(' + user.ip + ')');
-                if (history.length >= HISTORY_SIZE) {
+                if (history.length >= historySize) {
                     history = history.slice(1);
                     history = history.slice(1);
                 }
                 }
                 history.push({msg: msg, user: getCleanUser(user), timestamp: Date.now()});
                 history.push({msg: msg, user: getCleanUser(user), timestamp: Date.now()});
@@ -237,6 +256,6 @@ function log(...inputs) {
     });
     });
 }
 }
 
 
-server.listen(PORT, () => {
-    log('- Server up and running at port ' + PORT);
+server.listen(port, () => {
+    log('- Server up and running at port ' + port);
 });
 });

+ 6 - 0
www/css/app.css

@@ -116,10 +116,15 @@
     text-align: right;
     text-align: right;
 }
 }
 
 
+#chat-host .messages .userMsg .avatar {
+    cursor: pointer;
+}
+
 #chat-host .messages .userMsg .user {
 #chat-host .messages .userMsg .user {
     order: -1;
     order: -1;
     margin-right: 0.3em;
     margin-right: 0.3em;
     font-weight: bold;
     font-weight: bold;
+    cursor: pointer;
 }
 }
 
 
 #chat-host .messages .userMsg .time {
 #chat-host .messages .userMsg .time {
@@ -157,6 +162,7 @@
 
 
 #chat-host .userlistWrapper span {
 #chat-host .userlistWrapper span {
     font-weight: bold;
     font-weight: bold;
+    cursor: pointer;
 }
 }
 
 
 #chat-host .userlistWrapper button {
 #chat-host .userlistWrapper button {

+ 14 - 5
www/css/theme.css

@@ -34,11 +34,6 @@
     border-right-width: 1em;
     border-right-width: 1em;
 }
 }
 
 
-#chat-host .main .messages li.userMsg .user,
-#chat-host .main .userlistWrapper .userlist li span {
-    filter: brightness(0.7) saturate(1.2);
-}
-
 #chat-host .main .messages li.userMsg .avatar {
 #chat-host .main .messages li.userMsg .avatar {
     position: absolute;
     position: absolute;
     border-radius: 50%;
     border-radius: 50%;
@@ -54,6 +49,12 @@
     right: 0.75em;
     right: 0.75em;
 }
 }
 
 
+#chat-host .main .messages li.userMsg .user,
+#chat-host .main .messages li.userMsg .message .mention,
+#chat-host .main .userlistWrapper .userlist li span {
+    filter: brightness(0.7) saturate(1.2);
+}
+
 #chat-host .main .messages li.userMsg .message {
 #chat-host .main .messages li.userMsg .message {
     margin-top: 0.2em;
     margin-top: 0.2em;
 }
 }
@@ -74,4 +75,12 @@
     font-size: inherit;
     font-size: inherit;
     text-transform: uppercase;
     text-transform: uppercase;
     color: #4d4d4d;
     color: #4d4d4d;
+}
+
+#chat-host a {
+    color: #751b09;
+}
+
+#chat-host a:not(:hover) {
+    text-decoration: none;
 }
 }

+ 33 - 7
www/js/main.js

@@ -115,6 +115,7 @@ function onServerLogin(user, history) {
             const usernameEl = $('<span>');
             const usernameEl = $('<span>');
             usernameEl.text(userString);
             usernameEl.text(userString);
             usernameEl.css('color', listUser.color);
             usernameEl.css('color', listUser.color);
+            usernameEl.click(() => mentionUser(listUser.name));
             userListEl.append(usernameEl);
             userListEl.append(usernameEl);
 
 
             if (user.admin) {
             if (user.admin) {
@@ -171,13 +172,15 @@ function onServerLogin(user, history) {
             const valBefore = val.substring(0, cursorPos);
             const valBefore = val.substring(0, cursorPos);
             const valAfter = val.substring(cursorPos);
             const valAfter = val.substring(cursorPos);
 
 
-            for (const emojiPatternItem of emojiPatterns) {
-                if (valBefore.endsWith(emojiPatternItem.pattern)) {
-                    field.val(
-                        valBefore.substring(0, valBefore.length - emojiPatternItem.pattern.length) + emojiPatternItem.replacement + valAfter
-                    );
-                    field.prop('selectionStart', cursorPos);
-                    field.prop('selectionEnd', cursorPos);
+            if (!valBefore.endsWith('http:/') && !valBefore.endsWith('https:/')) {
+                for (const emojiPatternItem of emojiPatterns) {
+                    if (valBefore.endsWith(emojiPatternItem.pattern)) {
+                        field.val(
+                            valBefore.substring(0, valBefore.length - emojiPatternItem.pattern.length) + emojiPatternItem.replacement + valAfter
+                        );
+                        field.prop('selectionStart', cursorPos);
+                        field.prop('selectionEnd', cursorPos);
+                    }
                 }
                 }
             }
             }
         }
         }
@@ -236,6 +239,7 @@ function appendUserMessage(msg, msgUser, timestamp, user) {
     const avaEl = $('<img class="avatar" />');
     const avaEl = $('<img class="avatar" />');
     avaEl.prop('src', '/img/ava/' + msgUser.name);
     avaEl.prop('src', '/img/ava/' + msgUser.name);
     avaEl.css('backgroundColor', msgUser.color);
     avaEl.css('backgroundColor', msgUser.color);
+    avaEl.click(() => mentionUser(msgUser.name));
     messageWrapper.append(avaEl);
     messageWrapper.append(avaEl);
 
 
     const timeEl = $('<span class="time">');
     const timeEl = $('<span class="time">');
@@ -250,6 +254,7 @@ function appendUserMessage(msg, msgUser, timestamp, user) {
     }
     }
     userEl.text(userString);
     userEl.text(userString);
     userEl.css('color', msgUser.color);
     userEl.css('color', msgUser.color);
+    userEl.click(() => mentionUser(msgUser.name));
     messageWrapper.append(userEl);
     messageWrapper.append(userEl);
 
 
     const messageEl = $('<span class="message">');
     const messageEl = $('<span class="message">');
@@ -268,3 +273,24 @@ function onKickUser(user) {
 function onLiftBan(ip) {
 function onLiftBan(ip) {
     socket.emit('requestLiftBan', ip);
     socket.emit('requestLiftBan', ip);
 }
 }
+
+function mentionUser(username) {
+    const field = $("#chat-host .messageField");
+    if (field) {
+        const mention = '@' + username + ' ';
+
+        const val = field.val();
+        var cursorPos = +field.prop('selectionStart');
+        const valBefore = val.substring(0, cursorPos);
+        const valAfter = val.substring(cursorPos);
+
+        field.val(valBefore + mention + valAfter);
+
+        cursorPos += mention.length;
+
+        field.prop('selectionStart', cursorPos);
+        field.prop('selectionEnd', cursorPos);
+
+        field.focus();
+    }
+}