Skip to content

Commit af0a819

Browse files
authored
Merge pull request #224 from Deep-Blue-2013/master
Update UI dialogs by conversation.
2 parents 5bd15f7 + e71fdc7 commit af0a819

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private string GenerateJwtToken(User user)
9494
return tokenHandler.WriteToken(token);
9595
}
9696

97+
[MemoryCache(10 * 60)]
9798
public async Task<User> GetMyProfile()
9899
{
99100
var db = _services.GetRequiredService<IBotSharpRepository>();

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public override async Task OnConversationInitialized(Conversation conversation)
6161
var user = await userService.GetUser(conv.User.Id);
6262
conv.User = UserViewModel.FromUser(user);
6363

64-
await _chatHub.Clients.All.SendAsync("OnConversationInitFromClient", conv);
64+
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationInitFromClient", conv);
6565

6666
await base.OnConversationInitialized(conversation);
6767
}
@@ -73,7 +73,7 @@ public override async Task OnMessageReceived(RoleDialogModel message)
7373
var sender = await userService.GetMyProfile();
7474

7575
// Update console conversation UI for CSR
76-
await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromClient", new ChatResponseModel()
76+
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromClient", new ChatResponseModel()
7777
{
7878
ConversationId = conv.ConversationId,
7979
MessageId = message.MessageId,

src/web-live-chat/src/lib/helpers/typedefs.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@
4949
* @property {Date} created_at - The message sent time.
5050
*/
5151

52+
/**
53+
* Invoked when a new conersation is created.
54+
* This callback type is called `requestCallback` and is displayed as a global symbol.
55+
*
56+
* @callback OnConversationInitialized
57+
* @param {ConversationModel} conversation
58+
*/
59+
60+
/**
61+
* Invoked when message is received form chatHub.
62+
* This callback type is called `requestCallback` and is displayed as a global symbol.
63+
*
64+
* @callback OnMessageReceived
65+
* @param {ChatResponseModel} message
66+
*/
67+
5268
// having to export an empty object here is annoying,
5369
// but required for vscode to pass on your types.
5470
export default {};

src/web-live-chat/src/lib/services/signalr-service.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ let connection;
99
// create a SignalR service object that exposes methods to interact with the hub
1010
export const signalr = {
1111

12-
/** @type {function} */
12+
/** @type {import('$typedefs').OnConversationInitialized} */
1313
onConversationInitFromClient: () => {},
1414

15-
/** @type {function} */
15+
/** @type {import('$typedefs').OnMessageReceived} */
1616
onMessageReceivedFromClient: () => {},
1717

18-
/** @type {function} */
18+
/** @type {import('$typedefs').OnMessageReceived} */
1919
onMessageReceivedFromCsr: () => {},
2020

21-
/** @type {function} */
21+
/** @type {import('$typedefs').OnMessageReceived} */
2222
onMessageReceivedFromAssistant: () => {},
2323

2424
// start the connection
@@ -43,27 +43,35 @@ export const signalr = {
4343
// register handlers for the hub methods
4444
connection.on('OnConversationInitFromClient', (conversation) => {
4545
// do something when receiving a message, such as updating the UI or showing a notification
46-
console.log(`[OnConversationInitFromClient] ${conversation.id}: ${conversation.title}`);
47-
this.onConversationInitFromClient(conversation);
46+
if (conversationId === conversation.id) {
47+
console.log(`[OnConversationInitFromClient] ${conversation.id}: ${conversation.title}`);
48+
this.onConversationInitFromClient(conversation);
49+
}
4850
});
4951

5052
// register handlers for the hub methods
5153
connection.on('OnMessageReceivedFromClient', (message) => {
5254
// do something when receiving a message, such as updating the UI or showing a notification
53-
console.log(`[OnMessageReceivedFromClient] ${message.sender.role}: ${message.text}`);
54-
this.onMessageReceivedFromClient(message);
55+
if (conversationId === message.conversation_id) {
56+
console.log(`[OnMessageReceivedFromClient] ${message.sender.role}: ${message.text}`);
57+
this.onMessageReceivedFromClient(message);
58+
}
5559
});
5660

5761
connection.on('OnMessageReceivedFromCsr', (message) => {
5862
// do something when receiving a message, such as updating the UI or showing a notification
59-
console.log(`[OnMessageReceivedFromCsr] ${message.role}: ${message.content}`);
60-
this.onMessageReceivedFromCsr(message);
63+
if (conversationId === message.conversation_id) {
64+
console.log(`[OnMessageReceivedFromCsr] ${message.role}: ${message.content}`);
65+
this.onMessageReceivedFromCsr(message);
66+
}
6167
});
6268

6369
connection.on('OnMessageReceivedFromAssistant', (message) => {
6470
// do something when receiving a message, such as updating the UI or showing a notification
65-
console.log(`[OnMessageReceivedFromAssistant] ${message.sender.role}: ${message.text}`);
66-
this.onMessageReceivedFromAssistant(message);
71+
if (conversationId === message.conversation_id) {
72+
console.log(`[OnMessageReceivedFromAssistant] ${message.sender.role}: ${message.text}`);
73+
this.onMessageReceivedFromAssistant(message);
74+
}
6775
});
6876
},
6977

0 commit comments

Comments
 (0)