Skip to content

Commit 83a44c7

Browse files
committed
refactor
1 parent efede3c commit 83a44c7

File tree

3 files changed

+91
-102
lines changed

3 files changed

+91
-102
lines changed

services/static-webserver/client/source/class/osparc/conversation/AddMessage.js

Lines changed: 8 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ qx.Class.define("osparc.conversation.AddMessage", {
5353
},
5454

5555
events: {
56-
"messageAdded": "qx.event.type.Data",
56+
"addMessage": "qx.event.type.Data",
5757
"updateMessage": "qx.event.type.Data",
58+
"notifyUser": "qx.event.type.Data",
5859
},
5960

6061
members: {
@@ -171,53 +172,12 @@ qx.Class.define("osparc.conversation.AddMessage", {
171172
},
172173

173174
addComment: function() {
174-
const conversationId = this.getConversationId();
175-
if (conversationId) {
176-
return this.__postMessage();
177-
} else {
178-
// create new conversation first
179-
const studyData = this.getStudyData();
180-
let promise = null;
181-
if (studyData) {
182-
// project conversation
183-
promise = osparc.store.ConversationsProject.getInstance().postConversation(studyData["uuid"])
184-
} else {
185-
// support conversation
186-
const extraContext = {};
187-
const currentStudy = osparc.store.Store.getInstance().getCurrentStudy()
188-
if (currentStudy) {
189-
extraContext["projectId"] = currentStudy.getUuid();
190-
}
191-
promise = osparc.store.ConversationsSupport.getInstance().postConversation(extraContext);
192-
}
193-
return promise
194-
.then(data => {
195-
this.setConversationId(data["conversationId"]);
196-
return this.__postMessage();
197-
});
198-
}
199-
},
200-
201-
__postMessage: function() {
202175
const commentField = this.getChildControl("comment-field");
203176
const content = commentField.getChildControl("text-area").getValue();
204-
let promise = null;
205177
if (content) {
206-
const studyData = this.getStudyData();
207-
const conversationId = this.getConversationId();
208-
if (studyData) {
209-
promise = osparc.store.ConversationsProject.getInstance().postMessage(studyData["uuid"], conversationId, content);
210-
} else {
211-
promise = osparc.store.ConversationsSupport.getInstance().postMessage(conversationId, content);
212-
}
213-
return promise
214-
.then(data => {
215-
this.fireDataEvent("messageAdded", data);
216-
commentField.getChildControl("text-area").setValue("");
217-
return data;
218-
});
178+
this.fireDataEvent("addMessage", content);
179+
commentField.getChildControl("text-area").setValue("");
219180
}
220-
return Promise.reject();
221181
},
222182

223183
__editComment: function() {
@@ -263,7 +223,7 @@ qx.Class.define("osparc.conversation.AddMessage", {
263223
// This check only works if the project is directly shared with the user.
264224
// If it's shared through a group, it might be a bit confusing
265225
if (userGid in studyData["accessRights"]) {
266-
this.__addNotify(userGid);
226+
this.__doNotifyUser(userGid);
267227
} else {
268228
const msg = this.tr("This user has no access to the project. Do you want to share it?");
269229
const win = new osparc.ui.window.Confirmation(msg).set({
@@ -280,7 +240,7 @@ qx.Class.define("osparc.conversation.AddMessage", {
280240
};
281241
osparc.store.Study.getInstance().addCollaborators(studyData, newCollaborators)
282242
.then(() => {
283-
this.__addNotify(userGid);
243+
this.__doNotifyUser(userGid);
284244
const potentialCollaborators = osparc.store.Groups.getInstance().getPotentialCollaborators()
285245
if (userGid in potentialCollaborators && "getUserId" in potentialCollaborators[userGid]) {
286246
const uid = potentialCollaborators[userGid].getUserId();
@@ -293,47 +253,13 @@ qx.Class.define("osparc.conversation.AddMessage", {
293253
}
294254
},
295255

296-
__addNotify: function(userGid) {
297-
const studyData = this.getStudyData();
298-
if (!studyData) {
299-
return;
300-
}
301-
302-
const conversationId = this.getConversationId();
303-
if (conversationId) {
304-
this.__postNotify(userGid);
305-
} else {
306-
// create new conversation first
307-
osparc.store.ConversationsProject.getInstance().postConversation(studyData["uuid"])
308-
.then(data => {
309-
this.setConversationId(data["conversationId"]);
310-
this.__postNotify(userGid);
311-
});
312-
}
313-
},
314-
315-
__postNotify: function(userGid) {
256+
__doNotifyUser: function(userGid) {
316257
const studyData = this.getStudyData();
317258
if (!studyData) {
318259
return;
319260
}
320261

321-
if (userGid) {
322-
const conversationId = this.getConversationId();
323-
osparc.store.ConversationsProject.getInstance().notifyUser(studyData["uuid"], conversationId, userGid)
324-
.then(data => {
325-
this.fireDataEvent("messageAdded", data);
326-
const potentialCollaborators = osparc.store.Groups.getInstance().getPotentialCollaborators();
327-
if (userGid in potentialCollaborators) {
328-
if ("getUserId" in potentialCollaborators[userGid]) {
329-
const uid = potentialCollaborators[userGid].getUserId();
330-
osparc.notification.Notifications.pushConversationNotification(uid, studyData["uuid"]);
331-
}
332-
const msg = "getLabel" in potentialCollaborators[userGid] ? potentialCollaborators[userGid].getLabel() + this.tr(" was notified") : this.tr("Notification sent");
333-
osparc.FlashMessenger.logAs(msg, "INFO");
334-
}
335-
});
336-
}
262+
this.fireDataEvent("notifyUser", userGid);
337263
},
338264
/* NOTIFY USERS */
339265
}

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

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,26 +191,69 @@ qx.Class.define("osparc.study.Conversation", {
191191
this.__loadMoreMessages.addListener("execute", () => this.__reloadMessages(false));
192192
this._add(this.__loadMoreMessages);
193193

194-
const addMessages = new osparc.conversation.AddMessage().set({
194+
const addMessage = new osparc.conversation.AddMessage().set({
195195
studyData: this.__studyData,
196196
conversationId: this.getConversationId(),
197197
enabled: osparc.data.model.Study.canIWrite(this.__studyData["accessRights"]),
198198
paddingLeft: 10,
199199
});
200-
addMessages.addListener("messageAdded", e => {
201-
const data = e.getData();
202-
if (data["conversationId"] && this.getConversation() === null) {
203-
osparc.store.ConversationsProject.getInstance().getConversation(this.__studyData["uuid"], data["conversationId"])
204-
.then(conversationData => {
205-
const conversation = new osparc.data.model.Conversation(conversationData);
206-
this.setConversation(conversation);
200+
addMessage.addListener("addMessage", e => {
201+
const content = e.getData();
202+
const conversation = this.getConversation();
203+
if (conversation) {
204+
this.__postMessage(content);
205+
} else {
206+
// create new conversation first
207+
osparc.store.ConversationsProject.getInstance().postConversation(this.__studyData["uuid"])
208+
.then(data => {
209+
const newConversation = new osparc.data.model.Conversation(data);
210+
this.setConversation(newConversation);
211+
this.__postMessage(content);
207212
});
213+
}
214+
});
215+
addMessage.addListener("notifyUser", e => {
216+
const userGid = e.getData();
217+
const conversation = this.getConversation();
218+
if (conversation) {
219+
this.__postNotify(userGid);
208220
} else {
209-
this.getConversation().addMessage(data);
210-
this.addMessage(data);
221+
// create new conversation first
222+
osparc.store.ConversationsProject.getInstance().postConversation(this.__studyData["uuid"])
223+
.then(data => {
224+
const newConversation = new osparc.data.model.Conversation(data);
225+
this.setConversation(newConversation);
226+
this.__postNotify(userGid);
227+
});
211228
}
212229
});
213-
this._add(addMessages);
230+
this._add(addMessage);
231+
},
232+
233+
__postMessage: function(content) {
234+
const conversationId = this.getConversation().getConversationId();
235+
osparc.store.ConversationsProject.getInstance().postMessage(this.__studyData["uuid"], conversationId, content)
236+
.then(data => {
237+
this.fireDataEvent("messageAdded", data);
238+
return data;
239+
});
240+
},
241+
242+
__postNotify: function(userGid) {
243+
const conversationId = this.getConversation().getConversationId();
244+
osparc.store.ConversationsProject.getInstance().notifyUser(this.__studyData["uuid"], conversationId, userGid)
245+
.then(data => {
246+
this.fireDataEvent("messageAdded", data);
247+
const potentialCollaborators = osparc.store.Groups.getInstance().getPotentialCollaborators();
248+
if (userGid in potentialCollaborators) {
249+
if ("getUserId" in potentialCollaborators[userGid]) {
250+
const uid = potentialCollaborators[userGid].getUserId();
251+
osparc.notification.Notifications.pushConversationNotification(uid, this.__studyData["uuid"]);
252+
}
253+
const msg = "getLabel" in potentialCollaborators[userGid] ? potentialCollaborators[userGid].getLabel() + this.tr(" was notified") : this.tr("Notification sent");
254+
osparc.FlashMessenger.logAs(msg, "INFO");
255+
}
256+
});
214257
},
215258

216259
__getNextRequest: function() {
@@ -265,6 +308,9 @@ qx.Class.define("osparc.study.Conversation", {
265308
},
266309

267310
__updateMessagesNumber: function() {
311+
if (!this.__messagesTitle) {
312+
return;
313+
}
268314
const nMessages = this.__messages.filter(msg => msg["type"] === "MESSAGE").length;
269315
if (nMessages === 0) {
270316
this.__messagesTitle.setValue(this.tr("No Messages yet"));

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,37 @@ qx.Class.define("osparc.support.Conversation", {
127127
this.getChildControl("spacer-top");
128128
this.getChildControl("messages-container");
129129
const addMessages = this.getChildControl("add-message");
130-
addMessages.addListener("messageAdded", e => {
131-
const data = e.getData();
132-
if (data["conversationId"] && this.getConversation() === null) {
133-
osparc.store.ConversationsSupport.getInstance().getConversation(data["conversationId"])
134-
.then(conversation => {
135-
this.setConversation(conversation);
136-
});
130+
addMessages.addListener("addMessage", e => {
131+
const content = e.getData();
132+
const conversation = this.getConversation();
133+
if (conversation) {
134+
this.__postMessage(content);
137135
} else {
138-
this.getConversation().addMessage(data);
139-
this.addMessage(data);
136+
// create new conversation first
137+
const extraContext = {};
138+
const currentStudy = osparc.store.Store.getInstance().getCurrentStudy()
139+
if (currentStudy) {
140+
extraContext["projectId"] = currentStudy.getUuid();
141+
}
142+
osparc.store.ConversationsSupport.getInstance().postConversation(extraContext)
143+
.then(data => {
144+
const newConversation = new osparc.data.model.Conversation(data);
145+
this.setConversation(newConversation);
146+
this.__postMessage(content);
147+
});
140148
}
141149
});
142150
},
143151

152+
__postMessage: function(content) {
153+
const conversationId = this.getConversation().getConversationId();
154+
osparc.store.ConversationsSupport.getInstance().postMessage(conversationId, content)
155+
.then(data => {
156+
this.fireDataEvent("messageAdded", data);
157+
return data;
158+
});
159+
},
160+
144161
__applyConversation: function(conversation) {
145162
this.__reloadMessages(true);
146163

0 commit comments

Comments
 (0)