Change how user data is get and subscribed

This commit is contained in:
Patrick Jentsch
2022-12-22 13:57:55 +01:00
parent 55b7428045
commit 598f67078a
3 changed files with 58 additions and 39 deletions

View File

@ -8,33 +8,20 @@ class App {
this.socket.on('PATCH', (patch) => {this.onPatch(patch);});
}
getUser(userId) {
getUser(userId, backrefs=false, relationships=false) {
if (userId in this.data.promises.getUser) {
return this.data.promises.getUser[userId];
}
this.data.promises.getUser[userId] = new Promise((resolve, reject) => {
fetch(`/users/${userId}?backrefs=true&relationships=true`, {headers: {Accept: 'application/json'}})
.then(
(response) => {
if (response.status === 403) {this.flash('Forbidden', 'error'); reject(response);}
return response.json();
},
(response) => {
this.flash('Something went wrong', 'error');
reject(response);
}
)
.then(
(user) => {
this.data.users[userId] = user;
resolve(this.data.users[userId]);
},
(error) => {
console.error(error, 'error');
reject(error);
}
);
this.socket.emit('GET /users/<user_id>', userId, backrefs, relationships, (response) => {
if (response.status !== 200) {
reject(response);
return;
}
this.data.users[userId] = response.body;
resolve(this.data.users[userId]);
});
});
return this.data.promises.getUser[userId];
@ -47,11 +34,11 @@ class App {
this.data.promises.subscribeUser[userId] = new Promise((resolve, reject) => {
this.socket.emit('SUBSCRIBE /users/<user_id>', userId, (response) => {
if (response.code === 200) {
resolve(response);
} else {
if (response.status !== 200) {
reject(response);
return;
}
resolve(response);
});
});