Skip to content

Commit 156ed94

Browse files
committed
store messages
1 parent d2922ea commit 156ed94

File tree

5 files changed

+82
-43
lines changed

5 files changed

+82
-43
lines changed

services/static-webserver/client/source/class/osparc/data/model/Conversation.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,22 @@ qx.Class.define("osparc.data.model.Conversation", {
100100
init: null,
101101
event: "changeExtraContent",
102102
},
103+
104+
messages: {
105+
check: "Array",
106+
nullable: false,
107+
init: null,
108+
},
103109
},
104110

105111
statics: {
106112
},
113+
114+
members: {
115+
addMessage: function(message) {
116+
const messages = this.getMessages() || [];
117+
messages.push(message);
118+
this.setMessages(messages);
119+
},
120+
},
107121
});

services/static-webserver/client/source/class/osparc/store/ConversationsSupport.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ qx.Class.define("osparc.store.ConversationsSupport", {
4646
};
4747
return osparc.data.Resources.fetch("conversationsSupport", "getConversationsPage", params)
4848
.then(conversationsData => {
49+
const conversations = [];
4950
if (conversationsData.length) {
5051
// Sort conversations by created date, oldest first (the new ones will be next to the plus button)
5152
conversationsData.sort((a, b) => new Date(a["created"]) - new Date(b["created"]));
5253
}
53-
conversationsData.forEach(conversationData => this.__addToCache(conversationData));
54-
return this.__conversationsCached;
54+
conversationsData.forEach(conversationData => {
55+
const conversation = new osparc.data.model.Conversation(conversationData);
56+
this.__addToCache(conversation);
57+
conversations.push(conversation);
58+
});
59+
return conversations;
5560
})
5661
.catch(err => osparc.FlashMessenger.logError(err));
5762
},
@@ -62,7 +67,12 @@ qx.Class.define("osparc.store.ConversationsSupport", {
6267
conversationId,
6368
}
6469
};
65-
return osparc.data.Resources.fetch("conversationsSupport", "getConversation", params);
70+
return osparc.data.Resources.fetch("conversationsSupport", "getConversation", params)
71+
.then(conversationData => {
72+
const conversation = new osparc.data.model.Conversation(conversationData);
73+
this.__addToCache(conversation);
74+
return conversation;
75+
});
6676
},
6777

6878
addConversation: function(extraContext = {}) {
@@ -127,7 +137,11 @@ qx.Class.define("osparc.store.ConversationsSupport", {
127137
limit: 1,
128138
}
129139
};
130-
return osparc.data.Resources.fetch("conversationsSupport", "getMessagesPage", params);
140+
return osparc.data.Resources.fetch("conversationsSupport", "getMessagesPage", params)
141+
.then(messagesData => {
142+
messagesData.forEach(messageData => this.__addMessageToCache(conversationId, messageData));
143+
return messagesData;
144+
});
131145
},
132146

133147
addMessage: function(conversationId, message) {
@@ -169,9 +183,12 @@ qx.Class.define("osparc.store.ConversationsSupport", {
169183
.catch(err => osparc.FlashMessenger.logError(err));
170184
},
171185

172-
__addToCache: function(conversationData) {
173-
const conversation = new osparc.data.model.Conversation(conversationData);
186+
__addToCache: function(conversation) {
174187
this.__conversationsCached[conversation.getConversationId()] = conversation;
175188
},
189+
190+
__addMessageToCache: function(conversationId, messageData) {
191+
this.__conversationsCached[conversationId].addMessage(messageData);
192+
},
176193
}
177194
});

services/static-webserver/client/source/class/osparc/support/ConversationListItem.js

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ qx.Class.define("osparc.support.ConversationListItem", {
2424
const layout = this._getLayout();
2525
layout.setSpacingX(10);
2626
layout.setSpacingY(0);
27+
28+
// decorate
29+
this.getChildControl("thumbnail").getContentElement().setStyles({
30+
"border-radius": "16px"
31+
});
32+
this.getChildControl("subtitle").set({
33+
textColor: "text-disabled",
34+
});
2735
},
2836

2937
properties: {
@@ -39,36 +47,39 @@ qx.Class.define("osparc.support.ConversationListItem", {
3947
members: {
4048
__applyConversation: function(conversation) {
4149
const conversationId = conversation.getConversationId();
42-
osparc.store.ConversationsSupport.getInstance().getLastMessage(conversationId)
43-
.then(lastMessages => {
44-
if (lastMessages && lastMessages.length) {
45-
// decorate
46-
this.getChildControl("thumbnail").getContentElement().setStyles({
47-
"border-radius": "16px"
48-
});
49-
this.getChildControl("subtitle").set({
50-
textColor: "text-disabled",
51-
});
52-
const lastMessage = lastMessages[0];
53-
const date = osparc.utils.Utils.formatDateAndTime(new Date(lastMessage.created));
54-
const name = conversation.getName();
55-
this.set({
56-
title: name && name !== "null" ? name : lastMessage.content,
57-
subtitle: date,
58-
});
50+
if (conversation.getMessages()) {
51+
this.__populateListItem();
52+
} else {
53+
osparc.store.ConversationsSupport.getInstance().getLastMessage(conversationId)
54+
.then(() => {
55+
this.__populateListItem();
56+
});
57+
}
58+
},
5959

60-
const userGroupId = lastMessage.userGroupId;
61-
osparc.store.Users.getInstance().getUser(userGroupId)
62-
.then(user => {
63-
if (user) {
64-
this.set({
65-
thumbnail: user.getThumbnail(),
66-
subtitle: user.getLabel() + " - " + date,
67-
});
68-
}
69-
});
70-
}
60+
__populateListItem: function() {
61+
const conversation = this.getConversation();
62+
const messages = conversation.getMessages();
63+
if (messages && messages.length) {
64+
const lastMessage = messages[0];
65+
const date = osparc.utils.Utils.formatDateAndTime(new Date(lastMessage.created));
66+
const name = conversation.getName();
67+
this.set({
68+
title: name && name !== "null" ? name : lastMessage.content,
69+
subtitle: date,
7170
});
71+
72+
const userGroupId = lastMessage.userGroupId;
73+
osparc.store.Users.getInstance().getUser(userGroupId)
74+
.then(user => {
75+
if (user) {
76+
this.set({
77+
thumbnail: user.getThumbnail(),
78+
subtitle: user.getLabel() + " - " + date,
79+
});
80+
}
81+
});
82+
}
7283
},
7384
}
7485
});

services/static-webserver/client/source/class/osparc/support/ConversationPage.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,10 @@ qx.Class.define("osparc.support.ConversationPage", {
9898
if (conversationId) {
9999
osparc.store.ConversationsSupport.getInstance().getConversation(conversationId)
100100
.then(conversation => {
101-
let titleText = "";
102-
if (conversation["name"] === "null") {
103-
titleText = this.tr("Conversation: {id}", { id: conversationId });
104-
} else {
105-
title.setValue(titleText);
106-
}
101+
const name = conversation.getName();
102+
title.set({
103+
value: name && name !== "null" ? name : lastMessage.content,
104+
});
107105
options.show();
108106
});
109107
} else {

services/static-webserver/client/source/class/osparc/support/Conversations.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ qx.Class.define("osparc.support.Conversations", {
7979

8080
osparc.store.ConversationsSupport.getInstance().getConversations()
8181
.then(conversations => {
82-
const conversationsList = Object.values(conversations);
83-
if (conversationsList.length) {
84-
conversationsList.forEach(conversation => this.__addConversation(conversation));
82+
if (conversations.length) {
83+
conversations.forEach(conversation => this.__addConversation(conversation));
8584
}
8685
})
8786
.finally(() => {

0 commit comments

Comments
 (0)