Skip to content

Commit

Permalink
Merge branch 'master' into fix/guest-viewers
Browse files Browse the repository at this point in the history
  • Loading branch information
odeimaiz authored Oct 21, 2024
2 parents e58f486 + 118b363 commit ec00b5a
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ qx.Class.define("osparc.Application", {
.then(data => {
if (data.role.toLowerCase() === "guest") {
// Logout a guest trying to access the Dashboard
osparc.auth.Manager.getInstance().logout();
this.logout();
} else {
this.__loadMainPage();
}
Expand Down Expand Up @@ -564,18 +564,22 @@ qx.Class.define("osparc.Application", {
* Resets session and restarts
*/
logout: function(forcedReason) {
if (forcedReason) {
osparc.FlashMessenger.getInstance().logAs(forcedReason, "WARNING", 0);
} else {
osparc.FlashMessenger.getInstance().logAs(this.tr("You are logged out"), "INFO");
}
const isLoggedIn = osparc.auth.Manager.getInstance().isLoggedIn();
if (isLoggedIn) {
osparc.auth.Manager.getInstance().logout()
.finally(() => this.__closeAllAndToLoginPage());
.finally(() => this.__loggedOut(forcedReason));
} else {
this.__loggedOut(forcedReason);
}
},

__loggedOut: function(forcedReason) {
if (forcedReason) {
osparc.FlashMessenger.getInstance().logAs(forcedReason, "WARNING", 0);
} else {
this.__closeAllAndToLoginPage();
osparc.FlashMessenger.getInstance().logAs(this.tr("You are logged out"), "INFO");
}
this.__closeAllAndToLoginPage();
},

__closeAllAndToLoginPage: function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,15 @@ qx.Class.define("osparc.auth.Manager", {
"client_session_id": osparc.utils.Utils.getClientSessionID()
}
};
return osparc.data.Resources.fetch("auth", "postLogout", params)
.then(data => this.fireEvent("loggedOut"))
.catch(error => console.log("already logged out"))
.finally(this.__logoutUser());
const options = {
timeout: 5000,
timeoutRetries: 5
};
return osparc.data.Resources.fetch("auth", "postLogout", params, options)
.finally(() => {
this.__logoutUser();
this.fireEvent("loggedOut");
});
},

resetPasswordRequest: function(email, successCbk, failCbk, context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ qx.Class.define("osparc.dashboard.ResourceFilter", {
});


if (this.__tagButtons.length >= maxTags) {
if (this.__tagButtons.length > maxTags) {
const showAllButton = new qx.ui.form.Button(this.tr("All Tags..."), "@FontAwesome5Solid/tags/20");
showAllButton.set({
appearance: "filter-toggle-button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ qx.Class.define("osparc.dashboard.ServiceBrowser", {
this._hideLoadingPage();
const params = {
url: {
"studyId": studyId
studyId
}
};
osparc.data.Resources.fetch("studies", "delete", params, studyId);
osparc.data.Resources.fetch("studies", "delete", params);
};
const isStudyCreation = true;
this._startStudyById(studyId, openCB, cancelCB, isStudyCreation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,24 +672,24 @@ qx.Class.define("osparc.dashboard.StudyBrowser", {
limit: osparc.dashboard.ResourceBrowserBase.PAGINATED_STUDIES,
}
};

const nextPageParams = this.__getNextPageParams();
if (nextPageParams) {
params.url.offset = nextPageParams.offset;
params.url.limit = nextPageParams.limit;
}
const options = {
resolveWResponse: true
};

const requestParams = this.__getRequestParams();
Object.entries(requestParams).forEach(([key, value]) => {
params.url[key] = value;
});

const options = {
resolveWResponse: true
};

if ("text" in requestParams) {
return osparc.data.Resources.fetch("studies", "getPageSearch", params, undefined, options);
return osparc.data.Resources.fetch("studies", "getPageSearch", params, options);
}
return osparc.data.Resources.fetch("studies", "getPage", params, undefined, options);
return osparc.data.Resources.fetch("studies", "getPage", params, options);
},

invalidateStudies: function() {
Expand Down Expand Up @@ -1220,7 +1220,7 @@ qx.Class.define("osparc.dashboard.StudyBrowser", {
studyId
}
};
osparc.data.Resources.fetch("studies", "delete", params, studyId);
osparc.data.Resources.fetch("studies", "delete", params);
};
const isStudyCreation = true;
this._startStudyById(studyId, openCB, cancelCB, isStudyCreation);
Expand Down Expand Up @@ -1545,7 +1545,10 @@ qx.Class.define("osparc.dashboard.StudyBrowser", {
"studyId": studyData["uuid"]
}
};
const fetchPromise = osparc.data.Resources.fetch("studies", "duplicate", params, null, {"pollTask": true});
const options = {
pollTask: true
};
const fetchPromise = osparc.data.Resources.fetch("studies", "duplicate", params, options);
const interval = 1000;
const pollTasks = osparc.data.PollTasks.getInstance();
pollTasks.createPollingTask(fetchPromise, interval)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ qx.Class.define("osparc.dashboard.TemplateBrowser", {
this._hideLoadingPage();
const params = {
url: {
"studyId": studyId
studyId
}
};
osparc.data.Resources.fetch("studies", "delete", params, studyId);
osparc.data.Resources.fetch("studies", "delete", params);
};
const isStudyCreation = true;
this._startStudyById(studyId, openCB, cancelCB, isStudyCreation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ qx.Class.define("osparc.data.Resources", {
"studies": {
useCache: true,
idField: "uuid",
deleteId: "studyId",
endpoints: {
get: {
method: "GET",
Expand Down Expand Up @@ -711,6 +712,7 @@ qx.Class.define("osparc.data.Resources", {
"tokens": {
useCache: true,
idField: "service",
deleteId: "service",
endpoints: {
get: {
method: "GET",
Expand Down Expand Up @@ -1200,8 +1202,9 @@ qx.Class.define("osparc.data.Resources", {
* TAGS
*/
"tags": {
idField: "id",
useCache: true,
idField: "id",
deleteId: "tagId",
endpoints: {
get: {
method: "GET",
Expand Down Expand Up @@ -1244,22 +1247,24 @@ qx.Class.define("osparc.data.Resources", {
* @param {String} resource Name of the resource as defined in the static property 'resources'.
* @param {String} endpoint Name of the endpoint. Several endpoints can be defined for each resource.
* @param {Object} params Object containing the parameters for the url and for the body of the request, under the properties 'url' and 'data', respectively.
* @param {String} deleteId When deleting, id of the element that needs to be deleted from the cache.
* @param {Object} options Collections of options
* @param {Object} options Collections of options (pollTask, resolveWResponse, timeout, timeoutRetries)
*/
fetch: function(resource, endpoint, params = {}, deleteId, options = {}) {
fetch: function(resource, endpoint, params = {}, options = {}) {
return new Promise((resolve, reject) => {
if (this.self().resources[resource] == null) {
reject(Error(`Error while fetching ${resource}: the resource is not defined`));
}

const resourceDefinition = this.self().resources[resource];
const res = new osparc.io.rest.Resource(resourceDefinition.endpoints);

const res = new osparc.io.rest.Resource(resourceDefinition.endpoints, options.timeout);
if (!res.includesRoute(endpoint)) {
reject(Error(`Error while fetching ${resource}: the endpoint is not defined`));
}

const sendRequest = () => {
res[endpoint](params.url || null, params.data || null);
}

res.addListenerOnce(endpoint + "Success", e => {
const response = e.getRequest().getResponse();
const endpointDef = resourceDefinition.endpoints[endpoint];
Expand All @@ -1276,7 +1281,8 @@ qx.Class.define("osparc.data.Resources", {
}
}
if (useCache) {
if (endpoint.includes("delete")) {
if (endpoint.includes("delete") && resourceDefinition["deleteId"] && resourceDefinition["deleteId"] in params.url) {
const deleteId = params.url[resourceDefinition["deleteId"]];
this.__removeCached(resource, deleteId);
} else if (endpointDef.method === "POST" && options.pollTask !== true) {
this.__addCached(resource, data);
Expand All @@ -1297,7 +1303,15 @@ qx.Class.define("osparc.data.Resources", {
}
}, this);

res.addListenerOnce(endpoint + "Error", e => {
res.addListener(endpoint + "Error", e => {
if (e.getPhase() === "timeout") {
if (options.timeout && options.timeoutRetries) {
options.timeoutRetries--;
sendRequest();
return;
}
}

let message = null;
let status = null;
if (e.getData().error) {
Expand Down Expand Up @@ -1330,7 +1344,7 @@ qx.Class.define("osparc.data.Resources", {
if ("status" in err && err.status === 401) {
// Unauthorized again, the cookie might have expired.
// We can assume that all calls after this will respond with 401, so bring the user ot the login page.
qx.core.Init.getApplication().logout(qx.locale.Manager.tr("You were logged out"));
qx.core.Init.getApplication().logout(qx.locale.Manager.tr("You were logged out. Your cookie might have expired."));
}
});
}
Expand All @@ -1345,7 +1359,7 @@ qx.Class.define("osparc.data.Resources", {
reject(err);
});

res[endpoint](params.url || null, params.data || null);
sendRequest();
});
},

Expand All @@ -1362,7 +1376,7 @@ qx.Class.define("osparc.data.Resources", {
const options = {
resolveWResponse: true
};
this.fetch(resource, endpoint, params, null, options)
this.fetch(resource, endpoint, params, options)
.then(resp => {
// sometimes there is a kind of a double "data"
const meta = ("_meta" in resp["data"]) ? resp["data"]["_meta"] : resp["_meta"];
Expand Down Expand Up @@ -1426,14 +1440,14 @@ qx.Class.define("osparc.data.Resources", {
* @param {Object} params Object containing the parameters for the url and for the body of the request, under the properties 'url' and 'data', respectively.
* @param {Boolean} useCache Whether the cache has to be used. If false, an API call will be issued.
*/
get: function(resource, params, useCache = true) {
get: function(resource, params = {}, useCache = true, options = {}) {
if (useCache) {
const stored = this.__getCached(resource);
if (stored) {
return Promise.resolve(stored);
}
}
return this.fetch(resource, "get", params || {});
return this.fetch(resource, "get", params, options);
},

/**
Expand Down Expand Up @@ -1489,14 +1503,14 @@ qx.Class.define("osparc.data.Resources", {

statics: {
API: "/v0",
fetch: function(resource, endpoint, params, deleteId, options = {}) {
return this.getInstance().fetch(resource, endpoint, params, deleteId, options);
fetch: function(resource, endpoint, params, options = {}) {
return this.getInstance().fetch(resource, endpoint, params, options);
},
getOne: function(resource, params, id, useCache) {
return this.getInstance().getOne(resource, params, id, useCache);
},
get: function(resource, params, useCache) {
return this.getInstance().get(resource, params, useCache);
get: function(resource, params, useCache, options) {
return this.getInstance().get(resource, params, useCache, options);
},

getServiceUrl: function(key, version) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ qx.Class.define("osparc.desktop.MainPage", {
},
data: data["studyData"]
};
const fetchPromise = osparc.data.Resources.fetch("studies", "postToTemplate", params, null, {"pollTask": true});
const options = {
pollTask: true
};
const fetchPromise = osparc.data.Resources.fetch("studies", "postToTemplate", params, options);
const pollTasks = osparc.data.PollTasks.getInstance();
const interval = 1000;
pollTasks.createPollingTask(fetchPromise, interval)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,18 @@ qx.Class.define("osparc.desktop.StudyEditorIdlingTracker", {
const timeSinceInactivityThreshold = this.__idlingTime - inactivityThresholdT;
if (timeSinceInactivityThreshold % this.self().INACTIVITY_REQUEST_PERIOD_S == 0) {
// check if backend reports project as inactive
osparc.data.Resources.fetch("studies", "getInactivity", {
const params = {
url: {
studyId: this.__studyUuid
}
}).then(data => {
if (data["is_inactive"]) {
this.__displayFlashMessage(flashMessageDurationS);
}
}).catch(err => {
console.error(err);
});
};
osparc.data.Resources.fetch("studies", "getInactivity", params)
.then(data => {
if (data["is_inactive"]) {
this.__displayFlashMessage(flashMessageDurationS);
}
})
.catch(err => console.error(err));
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ qx.Class.define("osparc.desktop.credits.TransactionsTableModel", {
members: {
// overridden
_loadRowCount() {
osparc.data.Resources.fetch("payments", "get", {
const params = {
url: {
limit: 1,
offset: 0
}
}, undefined, {
};
const options = {
resolveWResponse: true
})
};
osparc.data.Resources.fetch("payments", "get", params, options)
.then(({ data: resp }) => {
this._onRowCountLoaded(resp["_meta"].total)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ qx.Class.define("osparc.desktop.credits.UsageTableModel", {
// overridden
_loadRowCount() {
const endpoint = this.getWalletId() == null ? "get" : "getWithWallet"
osparc.data.Resources.fetch("resourceUsage", endpoint, {
const params = {
url: {
walletId: this.getWalletId(),
limit: 1,
Expand All @@ -89,9 +89,11 @@ qx.Class.define("osparc.desktop.credits.UsageTableModel", {
null,
orderBy: JSON.stringify(this.getOrderBy())
}
}, undefined, {
};
const options = {
resolveWResponse: true
})
};
osparc.data.Resources.fetch("resourceUsage", endpoint, params, options)
.then(resp => {
this._onRowCountLoaded(resp["_meta"].total)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
service
}
};
osparc.data.Resources.fetch("tokens", "delete", params, service)
osparc.data.Resources.fetch("tokens", "delete", params)
.then(() => this.__rebuildTokensList())
.catch(err => console.error(err));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ qx.Class.define("osparc.form.tag.TagItem", {
tagId: this.getId()
}
};
osparc.data.Resources.fetch("tags", "delete", params, this.getId())
osparc.data.Resources.fetch("tags", "delete", params)
.then(() => this.fireEvent("deleteTag"))
.catch(console.error)
.finally(() => deleteButton.setFetching(false));
Expand Down
Loading

0 comments on commit ec00b5a

Please sign in to comment.