Skip to content

Commit

Permalink
Only get user info if logged in (#226)
Browse files Browse the repository at this point in the history
* Only get user info if logged in

* Set user to null if guest

* PR Changes

* Fix null on dispatch

* Removed connectUser param
  • Loading branch information
Mitchdev authored Apr 28, 2023
1 parent 57f0061 commit 313b7ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
73 changes: 40 additions & 33 deletions assets/chat/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class Chat {
this.source.on('REFRESH', () => window.location.reload(false));
this.source.on('PING', (data) => this.source.send('PONG', data));
this.source.on('CONNECTING', (data) => this.onCONNECTING(data));
this.source.on('ME', (data) => this.onME(data));
this.source.on('OPEN', (data) => this.onOPEN(data));
this.source.on('DISPATCH', (data) => this.onDISPATCH(data));
this.source.on('CLOSE', (data) => this.onCLOSE(data));
Expand Down Expand Up @@ -575,18 +576,20 @@ class Chat {
this.source.connect(this.config.url);
}

async loadUserAndSettings() {
return fetch(`${this.config.api.base}/api/chat/me`, {
async loadSettings() {
fetch(`${this.config.api.base}/api/chat/me/settings`, {
credentials: 'include',
})
.then((res) => res.json())
.then((data) => {
this.setUser(data);
this.setSettings(new Map(data.settings));
// Set user settings.
this.setSettings(new Map(data));
this.getActiveWindow().update(true);
})
.catch(() => {
this.setUser(null);
// Set default settings.
this.setSettings();
this.getActiveWindow().update(true);
});
}

Expand Down Expand Up @@ -633,24 +636,22 @@ class Chat {
}

async loadWhispers() {
if (this.authenticated) {
fetch(`${this.config.api.base}/api/messages/unread`, {
credentials: 'include',
fetch(`${this.config.api.base}/api/messages/unread`, {
credentials: 'include',
})
.then((res) => res.json())
.then((d) => {
d.forEach((e) =>
this.whispers.set(e.username.toLowerCase(), {
id: e.messageid,
nick: e.username,
unread: Number(e.unread),
open: false,
})
);
})
.then((res) => res.json())
.then((d) => {
d.forEach((e) =>
this.whispers.set(e.username.toLowerCase(), {
id: e.messageid,
nick: e.username,
unread: Number(e.unread),
open: false,
})
);
})
.then(() => this.menus.get('whisper-users').redraw())
.catch(() => {});
}
.then(() => this.menus.get('whisper-users').redraw())
.catch(() => {});
}

setEmotes(emotes) {
Expand Down Expand Up @@ -1070,7 +1071,7 @@ class Chat {
*/

onDISPATCH({ data }) {
if (typeof data === 'object') {
if (data && typeof data === 'object') {
let users = [];
const now = Date.now();
if (Object.hasOwn(data, 'nick')) users.push(this.addUser(data));
Expand All @@ -1092,16 +1093,20 @@ class Chat {
}

onCONNECTING(url) {
if (this.authenticated) {
MessageBuilder.status(
`Connecting as ${this.user.username} to ${Chat.extractHostname(
url
)} ...`
).into(this);
MessageBuilder.status(
`Connecting to ${Chat.extractHostname(url)} ...`
).into(this);
}

onME(data) {
this.setUser(data);
if (data) {
// If is a logged in user.
this.loadSettings();
this.loadWhispers();
} else {
MessageBuilder.status(
`Connecting to ${Chat.extractHostname(url)} ...`
).into(this);
// If guest load default settings.
this.setSettings();
}
}

Expand All @@ -1111,7 +1116,9 @@ class Chat {

onNAMES(data) {
MessageBuilder.status(
`Connected. Serving ${data.connectioncount || 0} connections and ${
`Connected as ${
this.authenticated ? this.user.username : 'Guest'
}. Serving ${data.connectioncount || 0} connections and ${
data.users.length
} users.`
).into(this);
Expand Down
2 changes: 0 additions & 2 deletions assets/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ switch ((Chat.reqParam('t') || 'embed').toUpperCase()) {
default:
chat
.withGui(embedHtml)
.then(() => chat.loadUserAndSettings())
.then(() => chat.loadEmotesAndFlairs())
.then(() => chat.loadHistory())
.then(() => chat.loadWhispers())
.then(() => chat.connect());
break;
}

0 comments on commit 313b7ce

Please sign in to comment.