aboutsummaryrefslogtreecommitdiffstats
path: root/src/web
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-12-29 16:47:43 -0600
committerGitHub <noreply@github.com>2022-12-29 16:47:43 -0600
commit49bc255026aa5ef2c4df0727134b7627808270fc (patch)
tree89349c7aeb4beac343e0e70388dce7063e243388 /src/web
parent5276699801f25eec692e48a39c73f45424a22d27 (diff)
parent8352d28eb6eae848530aa2d6b4a4196d53f4ac88 (diff)
downloadslskd-49bc255026aa5ef2c4df0727134b7627808270fc.tar.gz
Merge pull request #809 from slskd/chat-logs
Save chat logs in a database (prevent message loss when restarting)
Diffstat (limited to 'src/web')
-rw-r--r--src/web/src/components/App.js8
-rw-r--r--src/web/src/components/Chat/Chat.js74
-rw-r--r--src/web/src/components/Chat/ChatMenu.js10
-rw-r--r--src/web/src/lib/chat.js4
4 files changed, 62 insertions, 34 deletions
diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js
index df65bc20..70d154af 100644
--- a/src/web/src/components/App.js
+++ b/src/web/src/components/App.js
@@ -334,8 +334,12 @@ class App extends Component {
</div>)}
/>
<Route path={`${urlBase}/browse`} render={(props) => this.withTokenCheck(<Browse {...props}/>)}/>
- <Route path={`${urlBase}/users`} render={(props) => this.withTokenCheck(<Users {...props}/>)}/>
- <Route path={`${urlBase}/chat`} render={(props) => this.withTokenCheck(<Chat {...props}/>)}/>
+ <Route path={`${urlBase}/users`} render={(props) =>
+ this.withTokenCheck(<Users {...props}/>)}
+ />
+ <Route path={`${urlBase}/chat`} render={(props) =>
+ this.withTokenCheck(<Chat {...props} state={applicationState}/>)}
+ />
<Route path={`${urlBase}/rooms`} render={(props) => this.withTokenCheck(<Rooms {...props}/>)}/>
<Route path={`${urlBase}/uploads`} render={
(props) =>
diff --git a/src/web/src/components/Chat/Chat.js b/src/web/src/components/Chat/Chat.js
index 12840e4a..e9ff0be9 100644
--- a/src/web/src/components/Chat/Chat.js
+++ b/src/web/src/components/Chat/Chat.js
@@ -43,41 +43,55 @@ class Chat extends Component {
};
fetchConversations = async () => {
- const { active } = this.state;
+ // fetch all of the active conversations (but no messages)
let conversations = await chat.getAll();
-
- const unAckedActiveMessages = (conversations[active] || [])
- .filter(message => !message.acknowledged);
-
- if (unAckedActiveMessages.length > 0) {
- await this.acknowledgeMessages(active, { force: true });
+
+ // turn into a map, keyed by username
+ // if there are no active conversations, set to an empty map
+ if (conversations.length === 0) {
+ conversations = {};
+ } else {
+ conversations = conversations.reduce((map, current) => {
+ map[current.username] = current;
+ return map;
+ }, {});
+ }
+
+ const { active } = this.state;
+ const activeConversation = conversations[active];
+
+ // check to see if the active chat is still active
+ // this will happen whenever a chat is closed/removed
+ if (activeConversation) {
+ console.log('active?', activeConversation);
+ // *before* fetching messages, ack any unacked messages
+ // for the active chat
+ if (activeConversation.hasUnAcknowledgedMessages === true) {
+ await this.acknowledgeMessages(active);
+ }
+
conversations = {
- ...conversations,
- [active]: conversations[active].map(message => ({...message, acknowledged: true })),
+ ...conversations,
+ [active]: await chat.get({ username: active }),
};
}
this.setState({ conversations }, () => {
+ // if a chat isn't active or the active chat is closed,
+ // select the first conversation, if there is one
if (!this.state.conversations[this.state.active]) {
this.selectConversation(this.getFirstConversation());
- } else {
- this.acknowledgeMessages(this.state.active);
}
});
};
- acknowledgeMessages = async (username, { force = false } = {}) => {
- if (!username) return;
-
- const unAckedMessages = (this.state.conversations[username] || [])
- .filter(message => !message.acknowledged);
-
- if (!force && unAckedMessages.length === 0) return;
-
+ acknowledgeMessages = async (username) => {
+ if (!username || username === '') return;
await chat.acknowledge({ username });
};
sendMessage = async (username, message) => {
+ if (!username || !message || username === '') return;
await chat.send({ username, message });
};
@@ -91,6 +105,10 @@ class Chat extends Component {
await this.sendMessage(active, message);
this.messageRef.current.value = '';
+
+ // force a refresh to append the message
+ // we could probably do this in the browser but we can be lazy
+ this.fetchConversations();
};
validInput = () =>
@@ -118,11 +136,17 @@ class Chat extends Component {
active: username,
loading: true,
}, async () => {
- const { active } = this.state;
+ const { active, conversations } = this.state;
sessionStorage.setItem(activeChatKey, active);
- this.setState({ loading: false }, () => {
+ this.setState({
+ loading: false,
+ conversations: active === '' ? conversations : {
+ ...conversations,
+ [active]: await chat.get({ username: active }),
+ },
+ }, () => {
try {
this.listRef.current.lastChild.scrollIntoView();
} catch {
@@ -146,7 +170,8 @@ class Chat extends Component {
render = () => {
const { conversations = [], active, loading } = this.state;
- const messages = conversations[active] || [];
+ const messages = conversations[active]?.messages || [];
+ const { user } = this.props.state;
return (
<div className='chats'>
@@ -183,10 +208,11 @@ class Chat extends Component {
{messages.map((message, index) =>
<List.Content
key={index}
- className={`chat-message ${message.username !== active ? 'chat-message-self' : ''}`}
+ className={`chat-message ${message.direction === 'Out' ? 'chat-message-self' : ''}`}
>
<span className='chat-message-time'>{this.formatTimestamp(message.timestamp)}</span>
- <span className='chat-message-name'>{message.username}: </span>
+ <span className='chat-message-name'>
+ {message.direction === 'Out' ? user.username : message.username}: </span>
<span className='chat-message-message'>{message.message}</span>
</List.Content>
)}
diff --git a/src/web/src/components/Chat/ChatMenu.js b/src/web/src/components/Chat/ChatMenu.js
index 971646f4..3dd91763 100644
--- a/src/web/src/components/Chat/ChatMenu.js
+++ b/src/web/src/components/Chat/ChatMenu.js
@@ -8,12 +8,6 @@ import SendMessageModal from './SendMessageModal';
const ChatMenu = ({ conversations, active, onConversationChange, ...rest }) => {
const names = Object.keys(conversations);
-
- const unread = Object.entries(conversations).reduce((acc, [name, messages]) => {
- acc[name] = messages.filter(message => !message.acknowledged);
- return acc;
- }, {});
-
const isActive = (name) => active === name;
return (
@@ -28,8 +22,8 @@ const ChatMenu = ({ conversations, active, onConversationChange, ...rest }) => {
>
<Icon name='circle' size='tiny' color='green'/>
{name}
- {(unread[name] || []).length === 0 ? '' :
- <Label size='tiny' color='red'>{(unread[name] || []).length}</Label>
+ {conversations[name].hasUnAcknowledgedMessages &&
+ <Label size='tiny' color='red'>{conversations[name].unAcknowledgedMessageCount}</Label>
}
</Menu.Item>
)}
diff --git a/src/web/src/lib/chat.js b/src/web/src/lib/chat.js
index 2b31bcd8..31edcc8b 100644
--- a/src/web/src/lib/chat.js
+++ b/src/web/src/lib/chat.js
@@ -4,6 +4,10 @@ export const getAll = async () => {
return (await api.get('/conversations')).data;
};
+export const get = async ({ username }) => {
+ return (await api.get(`/conversations/${encodeURIComponent(username)}`)).data;
+};
+
export const acknowledge = ({ username }) => {
return api.put(`/conversations/${encodeURIComponent(username)}`);
};