Skip to content
This repository was archived by the owner on Sep 25, 2023. It is now read-only.

Commit 9587bd2

Browse files
committed
Added basic client visualization for bans, added log for (un)bans
1 parent 56bc7fc commit 9587bd2

File tree

5 files changed

+138
-76
lines changed

5 files changed

+138
-76
lines changed

public/styles/chat.css

+34-8
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,6 @@ header {
137137
flex: 1;
138138
}
139139

140-
#chatInputContainer.authorized > div {
141-
display: flex;
142-
}
143-
144-
/* Reversed visibility for unauthenticated */
145140
#chatInputContainer > #unauthenticated {
146141
display: flex;
147142
justify-content: space-around;
@@ -181,14 +176,19 @@ header {
181176
cursor: not-allowed;
182177
}
183178

184-
#chatInputContainer.authorized > #unauthenticated {
179+
#chatInputContainer.authorized > #unauthenticated,
180+
#chatInputContainer.banned > #unauthenticated {
185181
display: none;
186182
}
187183

188184
#authenticated {
189-
display: flex;
185+
display: none;
190186
flex-direction: column;
191187
}
188+
#chatInputContainer.authorized > #authenticated {
189+
display: flex;
190+
}
191+
192192
#chatUserAndInputContainer {
193193
display: flex;
194194
margin: 2px 10px;
@@ -269,7 +269,8 @@ header {
269269
margin-right: 4px;
270270
}
271271

272-
#chatButtons > *.minimal {
272+
#chatButtons > *.minimal,
273+
button.minimal {
273274
margin-left: 0;
274275
color: #999;
275276
font-size:.9em;
@@ -282,6 +283,31 @@ header {
282283
text-decoration: none;
283284
}
284285

286+
#banned {
287+
display: none;
288+
text-align: center;
289+
flex-direction: column;
290+
background: #06060699;
291+
}
292+
#banned a,
293+
#banned a:visited {
294+
color: #09f;
295+
}
296+
297+
#banned a:hover {
298+
color: #3df;
299+
}
300+
301+
#chatInputContainer.banned > #banned {
302+
display: flex;
303+
}
304+
305+
#banTitle {
306+
padding-top: 4px;
307+
font-size: 2em;
308+
color: #e44;
309+
}
310+
285311
input[type=number] {
286312
max-width: 50px;
287313
min-width: 30px;

src/client/chat/chat.js

+70-61
Original file line numberDiff line numberDiff line change
@@ -46,51 +46,7 @@ domReady.then(() => {
4646

4747
// Check for former authentication
4848
cookieData = Cookies.getJSON("user_data");
49-
50-
if (
51-
// If there is former data, check if it is not outdated.
52-
cookieData
53-
54-
// See if current date is later than origin date + expiration period
55-
&& Date.now() < cookieData.access_granted + cookieData.expires_in * 1000
56-
) {
57-
// Request a fresh token
58-
var xhr = new XMLHttpRequest();
59-
xhr.onreadystatechange = function() {
60-
if (this.readyState === 4 && this.status === 200) {
61-
let response = JSON.parse(xhr.responseText);
62-
63-
if (response.authorized && response.refreshed && response.tokenBody) {
64-
response.tokenBody.id = cookieData.id;
65-
response.tokenBody.username = cookieData.username;
66-
response.tokenBody.discriminator = cookieData.discriminator;
67-
response.tokenBody.avatar = cookieData.avatar;
68-
let days = (response.tokenBody.expires_in / 62400) - 0.1; // seconds to days minus some slack
69-
Cookies.set("user_data", response.tokenBody, {
70-
expires: days,
71-
path: "/",
72-
domain: window.location.hostname,
73-
secure: config.ssl
74-
});
75-
cookieData = response.tokenBody;
76-
}
77-
78-
// Redundant check as non-authorized requests are returned as a 400
79-
if (response.authorized) {
80-
onAuthorization(cookieData);
81-
}
82-
}
83-
};
84-
xhr.open("POST", "/chat", true);
85-
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
86-
xhr.send(
87-
JSON.stringify({
88-
"type": "refresh_token",
89-
"id": cookieData.id,
90-
"access_token": cookieData.access_token
91-
})
92-
);
93-
}
49+
checkAuthentication();
9450

9551
let lastMessageSent = Date.now();
9652
let sendMessage = function(message) {
@@ -189,24 +145,25 @@ domReady.then(() => {
189145
}, false);
190146

191147
// Make log out button functional
192-
let chatButtonLogOut = document.getElementById("buttonLogOut");
193-
chatButtonLogOut.addEventListener("click", function() {
194-
if (confirm("Do you really wish to log out?")) {
195-
Cookies.remove("user_data",
196-
{
197-
path: "/",
198-
domain: window.location.hostname
148+
for (let chatButtonLogOut of document.getElementsByClassName("buttonLogOut")) {
149+
chatButtonLogOut.addEventListener("click", function() {
150+
if (confirm("Do you really wish to log out?")) {
151+
Cookies.remove("user_data",
152+
{
153+
path: "/",
154+
domain: window.location.hostname
155+
}
156+
);
157+
158+
// Send to parent if applicable
159+
if (inIframe()) {
160+
window.top.postMessage(userState.AUTH_CHANGED, `${window.location.origin}/client`);
199161
}
200-
);
201162

202-
// Send to parent if applicable
203-
if (inIframe()) {
204-
window.top.postMessage(userState.AUTH_CHANGED, `${window.location.origin}/client`);
163+
window.location.reload(true);
205164
}
206-
207-
window.location.reload(true);
208-
}
209-
}, false);
165+
}, false);
166+
}
210167
});
211168

212169
let authWindow;
@@ -224,9 +181,9 @@ function authenticationWindow() {
224181
window.addEventListener("message", receiveMessage, false);
225182
function receiveMessage(event) {
226183
if (event.data && event.data.success && event.origin === window.location.origin) {
227-
onAuthorization(event.data.response);
228184
cookieData = Cookies.getJSON("user_data");
229185
authWindow.close();
186+
checkAuthentication();
230187

231188
// Send to parent if applicable
232189
if (inIframe()) {
@@ -240,3 +197,55 @@ function onAuthorization(data) {
240197
document.getElementById("userName").innerText = `${data.username}#${data.discriminator}`;
241198
document.getElementById("chatInputContainer").className = "authorized";
242199
}
200+
201+
function onBanned() {
202+
document.getElementById("chatInputContainer").className = "banned";
203+
}
204+
205+
function checkAuthentication() {
206+
if (
207+
// If there is former data, check if it is not outdated.
208+
cookieData
209+
210+
// See if current date is later than origin date + expiration period
211+
&& Date.now() < cookieData.access_granted + cookieData.expires_in * 1000
212+
) {
213+
// Request a fresh token
214+
var xhr = new XMLHttpRequest();
215+
xhr.onreadystatechange = function() {
216+
if (this.readyState === 4 && this.status === 200) {
217+
let response = JSON.parse(xhr.responseText);
218+
219+
if (response.authorized && response.refreshed && response.tokenBody) {
220+
response.tokenBody.id = cookieData.id;
221+
response.tokenBody.username = cookieData.username;
222+
response.tokenBody.discriminator = cookieData.discriminator;
223+
response.tokenBody.avatar = cookieData.avatar;
224+
let days = (response.tokenBody.expires_in / 62400) - 0.1; // seconds to days minus some slack
225+
Cookies.set("user_data", response.tokenBody, {
226+
expires: days,
227+
path: "/",
228+
domain: window.location.hostname,
229+
secure: config.ssl
230+
});
231+
cookieData = response.tokenBody;
232+
}
233+
234+
if (response.authorized) {
235+
onAuthorization(cookieData);
236+
} else if (response.banned) {
237+
onBanned();
238+
}
239+
}
240+
};
241+
xhr.open("POST", "/chat", true);
242+
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
243+
xhr.send(
244+
JSON.stringify({
245+
"type": "refresh_token",
246+
"id": cookieData.id,
247+
"access_token": cookieData.access_token
248+
})
249+
);
250+
}
251+
}

src/server/chat/discord-manager.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ const discordManager = function() {
5151
this.client.on("error", console.error, console.error);
5252

5353
this.client.on("guildBanAdd", function(guild, user) {
54+
log.info(`DISCORD: ${"Banned user".red} ${user.username}#${user.discriminator} (${user.id})`);
5455
db.user.setBanState(true, user.id);
5556
}, console.error);
5657

5758
this.client.on("guildBanRemove", function(guild, user) {
59+
log.info(`DISCORD: ${"Unbanned user".green} ${user.username}#${user.discriminator} (${user.id})`);
5860
db.user.setBanState(false, user.id);
5961
}, console.error);
6062

@@ -161,21 +163,25 @@ const discordManager = function() {
161163
let response = {
162164
authorized: true,
163165
refreshed: true,
166+
banned: false,
164167
tokenBody
165168
};
166169

167170
res.send(response);
168171
},
169172
() => {
170-
res.status(400).send({ authorized: false, refreshed: false });
173+
res.status(400).send({ authorized: false, refreshed: false, banned: false });
171174
});
172175
} else if (this.db.user.idIsAuthenticated(req.body.id, req.body.access_token)) {
173-
res.send({ authorized: true, refreshed: false });
176+
res.send({ authorized: true, refreshed: false, banned: false });
177+
return;
178+
} else if (this.db.user.idIsBanned(req.body.id)) {
179+
res.send({ authorized: false, refreshed: false, banned: true });
174180
return;
175181
}
176182
}
177183

178-
res.status(400).send({ authorized: false, refreshed: false });
184+
res.status(400).send({ authorized: false, refreshed: false, banned: false });
179185
}
180186
};
181187
}();

src/server/database/users.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@ module.exports = function(db, common) {
1414
if (
1515
row
1616
&& row.access_token == access_token
17-
&& row.is_banned != 1
17+
&& row.is_banned !== 1
1818
) {
1919
return true;
2020
}
2121
}
2222
return false;
2323
},
2424

25+
_idIsBanned: db.prepare("SELECT is_banned FROM users WHERE id = ?"),
26+
27+
idIsBanned(id) {
28+
if (this.idExists(id)) {
29+
let row = this._idIsBanned.get(id);
30+
if (row && row.is_banned === 1) {
31+
return true;
32+
}
33+
}
34+
return false;
35+
},
36+
2537
_getUsernameById: db.prepare("SELECT username FROM users WHERE id = ?"),
2638

2739
getUsernameById(id) {
@@ -117,6 +129,7 @@ module.exports = function(db, common) {
117129
timestamp_refresh_last,
118130
time_refresh_expire,
119131
scope,
132+
is_banned,
120133
stat_points_earned,
121134
stat_rounds_entered,
122135
stat_rounds_finished,
@@ -126,7 +139,7 @@ module.exports = function(db, common) {
126139
stat_marbles_not_finished,
127140
stat_unique_levels_played,
128141
timestamp_first_login
129-
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
142+
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
130143
),
131144

132145
insertNewUserEmbed(token_body, user_body, scope) {
@@ -148,6 +161,7 @@ module.exports = function(db, common) {
148161
0,
149162
0,
150163
0,
164+
0,
151165
Date.now()
152166
]);
153167
},
@@ -159,6 +173,7 @@ module.exports = function(db, common) {
159173
username,
160174
discriminator,
161175
avatar,
176+
is_banned,
162177
stat_points_earned,
163178
stat_rounds_entered,
164179
stat_rounds_finished,
@@ -168,7 +183,7 @@ module.exports = function(db, common) {
168183
stat_marbles_not_finished,
169184
stat_unique_levels_played,
170185
timestamp_first_login
171-
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)`
186+
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
172187
),
173188

174189
insertNewUserDiscord(user) {
@@ -185,6 +200,7 @@ module.exports = function(db, common) {
185200
0,
186201
0,
187202
0,
203+
0,
188204
Date.now()
189205
]);
190206
},

templates/chat.mustache

+6-1
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@
6464
<i class="icon-right-big" id="buttonSend"></i>
6565
</div>
6666
<div id="chatButtons">
67-
<button id="buttonLogOut" class="minimal"><i class="icon-logout"></i> log out</button>
67+
<button class="buttonLogOut minimal"><i class="icon-logout"></i> log out</button>
6868
{{#invitelink}}<a href="{{invitelink}}" target="_blank" class="minimal"><i class="icon-link-ext"></i> join server</a>{{/invitelink}}
6969
<div class="flexSpacer">&nbsp;</div>
7070
<button id="buttonMarble">!marble</button>
7171
</div>
7272
</div>
73+
<div id="banned">
74+
<div id="banTitle">You are banned</div>
75+
<p>If you wish to appeal this decision, <a href="/contact">contact us</a>.</p>
76+
<button class="buttonLogOut minimal"><i class="icon-logout"></i> log out</button>
77+
</div>
7378
</div>
7479
</body>
7580
</html>

0 commit comments

Comments
 (0)