Skip to content

Commit 70dfc9d

Browse files
committed
using the model
1 parent 448ca79 commit 70dfc9d

File tree

3 files changed

+28
-116
lines changed

3 files changed

+28
-116
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ qx.Class.define("osparc.store.ConversationsSupport", {
1919
extend: qx.core.Object,
2020
type: "singleton",
2121

22+
construct: function() {
23+
this.base(arguments);
24+
25+
this.__conversationsCached = {};
26+
},
27+
2228
events: {
2329
"conversationRenamed": "qx.event.type.Data",
2430
"conversationDeleted": "qx.event.type.Data",
@@ -39,12 +45,13 @@ qx.Class.define("osparc.store.ConversationsSupport", {
3945
}
4046
};
4147
return osparc.data.Resources.fetch("conversationsSupport", "getConversationsPage", params)
42-
.then(conversations => {
43-
if (conversations.length) {
48+
.then(conversationsData => {
49+
if (conversationsData.length) {
4450
// Sort conversations by created date, oldest first (the new ones will be next to the plus button)
45-
conversations.sort((a, b) => new Date(a["created"]) - new Date(b["created"]));
51+
conversationsData.sort((a, b) => new Date(a["created"]) - new Date(b["created"]));
4652
}
47-
return conversations;
53+
conversationsData.forEach(conversationData => this.__addToCache(conversationData));
54+
return this.__conversationsCached;
4855
})
4956
.catch(err => osparc.FlashMessenger.logError(err));
5057
},
@@ -161,5 +168,10 @@ qx.Class.define("osparc.store.ConversationsSupport", {
161168
return osparc.data.Resources.fetch("conversationsSupport", "deleteMessage", params)
162169
.catch(err => osparc.FlashMessenger.logError(err));
163170
},
171+
172+
__addToCache: function(conversationData) {
173+
const conversation = new osparc.data.model.Conversation(conversationData);
174+
this.__conversationsCached[conversation.getConversationId()] = conversation;
175+
},
164176
}
165177
});

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ qx.Class.define("osparc.support.ConversationListItem", {
2727
},
2828

2929
properties: {
30-
conversationId: {
31-
check: "String",
30+
conversation: {
31+
check: "osparc.data.model.Conversation",
3232
init: null,
3333
nullable: false,
34-
event: "changeConversationId",
35-
apply: "__applyConversationId",
34+
event: "changeConversation",
35+
apply: "__applyConversation",
3636
},
3737
},
3838

3939
members: {
40-
__applyConversationId: function(conversationId) {
40+
__applyConversation: function(conversation) {
41+
const conversationId = conversation.getConversationId();
4142
osparc.store.ConversationsSupport.getInstance().getLastMessage(conversationId)
4243
.then(lastMessages => {
4344
if (lastMessages && lastMessages.length) {

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

Lines changed: 6 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ qx.Class.define("osparc.support.Conversations", {
2828
this.__openConversationId = openConversationId;
2929

3030
this.__fetchConversations();
31-
this.__listenToConversationWS();
3231
},
3332

3433
statics: {
@@ -73,66 +72,6 @@ qx.Class.define("osparc.support.Conversations", {
7372
return control || this.base(arguments, id);
7473
},
7574

76-
__listenToConversationWS: function() {
77-
this.__wsHandlers = [];
78-
79-
const socket = osparc.wrapper.WebSocket.getInstance();
80-
81-
[
82-
this.self().CHANNELS.CONVERSATION_CREATED,
83-
this.self().CHANNELS.CONVERSATION_UPDATED,
84-
this.self().CHANNELS.CONVERSATION_DELETED,
85-
].forEach(eventName => {
86-
const eventHandler = conversation => {
87-
if (conversation) {
88-
switch (eventName) {
89-
case this.self().CHANNELS.CONVERSATION_CREATED:
90-
if (!conversation["projectId"]) {
91-
this.__addConversation(conversation);
92-
}
93-
break;
94-
case this.self().CHANNELS.CONVERSATION_UPDATED:
95-
this.__updateConversationName(conversation);
96-
break;
97-
case this.self().CHANNELS.CONVERSATION_DELETED:
98-
this.__removeConversationItem(conversation["conversationId"]);
99-
break;
100-
}
101-
}
102-
};
103-
socket.on(eventName, eventHandler, this);
104-
this.__wsHandlers.push({ eventName, handler: eventHandler });
105-
});
106-
107-
[
108-
this.self().CHANNELS.CONVERSATION_MESSAGE_CREATED,
109-
this.self().CHANNELS.CONVERSATION_MESSAGE_UPDATED,
110-
this.self().CHANNELS.CONVERSATION_MESSAGE_DELETED,
111-
].forEach(eventName => {
112-
const eventHandler = message => {
113-
if (message) {
114-
const conversationId = message["conversationId"];
115-
const conversationPage = this.__getConversationItem(conversationId);
116-
if (conversationPage) {
117-
switch (eventName) {
118-
case this.self().CHANNELS.CONVERSATION_MESSAGE_CREATED:
119-
conversationPage.addMessage(message);
120-
break;
121-
case this.self().CHANNELS.CONVERSATION_MESSAGE_UPDATED:
122-
conversationPage.updateMessage(message);
123-
break;
124-
case this.self().CHANNELS.CONVERSATION_MESSAGE_DELETED:
125-
conversationPage.deleteMessage(message);
126-
break;
127-
}
128-
}
129-
}
130-
};
131-
socket.on(eventName, eventHandler, this);
132-
this.__wsHandlers.push({ eventName, handler: eventHandler });
133-
});
134-
},
135-
13675
__getConversationItem: function(conversationId) {
13776
return this.__conversationListItems.find(conversation => conversation.getConversationId() === conversationId);
13877
},
@@ -143,8 +82,8 @@ qx.Class.define("osparc.support.Conversations", {
14382

14483
osparc.store.ConversationsSupport.getInstance().getConversations()
14584
.then(conversations => {
146-
if (conversations.length) {
147-
conversations.forEach(conversation => this.__addConversation(conversation));
85+
if (Object.values(conversations).length) {
86+
Object.values(conversations).forEach(conversation => this.__addConversation(conversation));
14887
if (this.__openConversationId) {
14988
const conversationsLayout = this.getChildControl("conversations-layout");
15089
const conversation = conversationsLayout.getSelectables().find(c => c.getConversationId() === this.__openConversationId);
@@ -161,17 +100,17 @@ qx.Class.define("osparc.support.Conversations", {
161100
});
162101
},
163102

164-
__addConversation: function(conversationData) {
103+
__addConversation: function(conversation) {
165104
// ignore it if it was already there
166-
const conversationId = conversationData["conversationId"];
105+
const conversationId = conversation.getConversationId();
167106
const conversationItemFound = this.__getConversationItem(conversationId);
168107
if (conversationItemFound) {
169108
return null;
170109
}
171110

172111
const conversationListItem = new osparc.support.ConversationListItem();
173-
conversationListItem.setConversationId(conversationData["conversationId"]);
174-
conversationListItem.addListener("tap", () => this.fireDataEvent("openConversation", conversationData["conversationId"]), this);
112+
conversationListItem.setConversation(conversation);
113+
conversationListItem.addListener("tap", () => this.fireDataEvent("openConversation", conversationId, this));
175114

176115
const conversationsLayout = this.getChildControl("conversations-layout");
177116
conversationsLayout.add(conversationListItem);
@@ -180,45 +119,5 @@ qx.Class.define("osparc.support.Conversations", {
180119

181120
return conversationListItem;
182121
},
183-
184-
__removeConversationItem: function(conversationId, changeSelection = false) {
185-
const conversationItem = this.__getConversationItem(conversationId);
186-
if (conversationItem) {
187-
const conversationsLayout = this.getChildControl("conversations-layout");
188-
if (conversationsLayout.indexOf(conversationItem) > -1) {
189-
conversationsLayout.remove(conversationItem);
190-
}
191-
this.__conversationListItems = this.__conversationListItems.filter(c => c !== conversationItem);
192-
const conversationPages = conversationsLayout.getSelectables();
193-
if (conversationPages.length) {
194-
if (changeSelection) {
195-
// change selection to the first conversation
196-
conversationsLayout.setSelection([conversationPages[0]]);
197-
}
198-
}
199-
}
200-
},
201-
202-
// it can only be renamed, not updated
203-
__updateConversationName: function(conversationData) {
204-
const conversationId = conversationData["conversationId"];
205-
const conversationPage = this.__getConversationItem(conversationId);
206-
if (conversationPage) {
207-
conversationPage.renameConversation(conversationData["name"]);
208-
}
209-
},
210-
211-
// overridden
212-
destroy: function() {
213-
const socket = osparc.wrapper.WebSocket.getInstance();
214-
if (this.__wsHandlers) {
215-
this.__wsHandlers.forEach(({ eventName }) => {
216-
socket.removeSlot(eventName);
217-
});
218-
this.__wsHandlers = null;
219-
}
220-
221-
this.base(arguments);
222-
},
223122
},
224123
});

0 commit comments

Comments
 (0)