Skip to content

Commit

Permalink
feat: Centralize Social Rest Endpoint management in Social - MEED-7489
Browse files Browse the repository at this point in the history
…- Meeds-io/MIPs#147 (#1735)

This change will centralize the usage of Social Rest endpoint in Social
Webapp instead of duplicating JS code in this addon.
  • Loading branch information
boubaker committed Sep 26, 2024
1 parent 24988bf commit 06c825c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,45 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
<v-flex
xs12>
<v-list>
<template v-for="item in sort(spacesRequests)">
<v-list-item
:key="item.id"
class="py-0 px-2">
<v-list-item-avatar class="my-1 me-2" size="30">
<v-img :src="item.avatar" />
</v-list-item-avatar>
<v-list-item
v-for="item in sort(spacesRequests)"
:key="item.id"
class="py-0 px-2">
<v-list-item-avatar class="my-1 me-2" size="30">
<v-img :src="item.avatarUrl" />
</v-list-item-avatar>

<v-list-item-content class="py-0">
<v-list-item-title class="request-user-name darken-2" v-text="item.displayName" />
<v-list-item-subtitle v-sanitized-html="item.description" />
</v-list-item-content>
<v-list-item-action>
<v-btn-toggle
class="transparent"
dark>
<v-btn
text
icon
small
min-width="auto"
class="px-0 connexion-accept-btn"
@click="replyInvitationToJoinSpace(item.id, 'approved')">
<v-icon color="primary-color" size="20">mdi-checkbox-marked-circle</v-icon>
</v-btn>
<v-btn
text
icon
small
min-width="auto"
class="px-0 connexion-refuse-btn"
@click="replyInvitationToJoinSpace(item.id, 'ignored')">
<v-icon color="grey lighten-1" size="20">mdi-close-circle</v-icon>
</v-btn>
</v-btn-toggle>
</v-list-item-action>
</v-list-item>
</template>
<v-list-item-content class="py-0">
<v-list-item-title class="request-user-name darken-2" v-text="item.displayName" />
<v-list-item-subtitle v-sanitized-html="item.description" />
</v-list-item-content>
<v-list-item-action>
<v-btn-toggle
class="transparent"
dark>
<v-btn
:loading="saving"
text
icon
small
min-width="auto"
class="px-0 connexion-accept-btn"
@click="replyInvitationToJoinSpace(item, 'approved')">
<v-icon color="success" size="20">mdi-checkbox-marked-circle</v-icon>
</v-btn>
<v-btn
:loading="saving"
text
icon
small
min-width="auto"
class="px-0 connexion-refuse-btn"
@click="replyInvitationToJoinSpace(item, 'ignored')">
<v-icon color="error" size="20">mdi-close-circle</v-icon>
</v-btn>
</v-btn-toggle>
</v-list-item-action>
</v-list-item>
</v-list>
</v-flex>
<v-flex
Expand All @@ -109,49 +110,32 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
</v-layout>
</template>
<script>
import {getSpacesRequests, replyInvitationToJoinSpace} from '../profilStatsAPI';
export default {
data() {
return {
spacesRequests: [],
spacesRequestsSize: '',
saving: false,
invitationSpaceUrl: `${eXo.env.portal.context}/${eXo.env.portal.metaPortalName}/spaces/receivedInvitations`
};
},
created(){
this.getSpacesRequests();
},
methods: {
getSpacesRequests() {
this.spacesRequests = [];
getSpacesRequests().then(
(data) => {
this.spacesRequestsSize = data.size;
this.$emit('showRequestsSpace', this.spacesRequestsSize);
if (this.spacesRequestsSize > 0) {
for (let i = 0; i < data.spacesMemberships.length; i++) {
const spaceRequest = {};
spaceRequest.id = data.spacesMemberships[i].id;
fetch(`${data.spacesMemberships[i].space}`, {
method: 'GET',
credentials: 'include',
}).then((resp) => {
if (resp?.ok) {
return resp.json();
}
else {
throw new Error ('Error when getting space');
}
}).then((data) => {
spaceRequest.avatar = data.avatarUrl || data.avatarUrl || `/portal/rest/v1/social/spaces/${spaceRequest.id.split(':')[0]}/avatar`;
spaceRequest.displayName = data.displayName;
this.spacesRequests.splice(i, 0, spaceRequest);
});
}
}
}
);
return this.$spaceService.getSpaceMemberships({
user: eXo.env.portal.userName,
status: 'invited',
expand: 'spaces',
returnSize: true,
limit: 3,
}).then((data) => {
this.spacesRequestsSize = data.size;
this.$emit('showRequestsSpace', this.spacesRequestsSize);
this.spacesRequests = data.spacesMemberships?.map?.(m => m.space);
});
},
sort(array) {
return array.slice().sort(function(a, b) {
Expand All @@ -161,21 +145,23 @@ export default {
openSpaceRequests() {
window.location.href = `${this.invitationSpaceUrl}`;
},
replyInvitationToJoinSpace(spaceId, reply) {
replyInvitationToJoinSpace(spaceId, reply)
.then(() => {
if (reply === 'approved') {
const confirmedRequest = this.spacesRequests.filter(request => request.id === spaceId)[0];
const space = {
id: confirmedRequest.id,
displayName: confirmedRequest.displayName,
avatarUrl: confirmedRequest.avatar,
};
this.$emit('invitationReplied', space);
}
this.getSpacesRequests();
});
async replyInvitationToJoinSpace(item, reply) {
this.saving = true;
try {
if (reply === 'approved') {
await this.$spaceService.accept(item.id);
this.$emit('invitationReplied', {
id: item.id,
displayName: item.displayName,
avatarUrl: item.avatarUrl,
});
} else if (reply === 'ignored') {
await this.$spaceService.deny(item.id);
}
this.getSpacesRequests();
} finally {
this.saving = false;
}
},
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
</v-flex>
</template>
<script>
import {getUserInformations, getSpacesRequests, getConnectionsRequests, getGamificationPoints, getReputationStatus} from '../profilStatsAPI';
import {getUserInformations, getConnectionsRequests, getGamificationPoints, getReputationStatus} from '../profilStatsAPI';
export default {
props: {
commonsSpaceDefaultSize: {
Expand Down Expand Up @@ -214,9 +214,11 @@ export default {
this.profileUrl = this.profileUrl + (this.isCurrentUserProfile ? '' : `/${ eXo.env.portal.profileOwner}`);
this.loadingWidgets = 5;
this.loadingWidgets = this.isCurrentUserProfile && 5 || 4;
this.retrieveUserData().finally(() => this.loadingWidgets--);
this.getSpacesRequestsSize().finally(() => this.loadingWidgets--);
if (this.isCurrentUserProfile) {
this.getSpacesRequestsSize().finally(() => this.loadingWidgets--);
}
this.getConnectionsRequestsSize().finally(() => this.loadingWidgets--);
this.getGamificationRank().finally(() => this.loadingWidgets--);
this.getGamificationPoints().finally(() => this.loadingWidgets--);
Expand All @@ -240,7 +242,12 @@ export default {
});
},
getSpacesRequestsSize() {
return getSpacesRequests().then(
return this.$spaceService.getSpaceMemberships({
user: eXo.env.portal.userName,
status: 'invited',
returnSize: true,
limit: 1,
}).then(
(data) => {
this.spacesRequestsSize = data.size;
this.$emit('showRequestsSpace', this.spacesRequestsSize);
Expand Down
33 changes: 0 additions & 33 deletions portlets/src/main/webapp/vue-app/profileStats/profilStatsAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,6 @@ export function getCommonsSpaces(offset, limit) {
});
}

export function getSpacesRequests() {
return fetch( '/portal/rest/v1/social/spacesMemberships?status=invited&returnSize=true&limit=3', {
method: 'GET',
credentials: 'include',
}).then((resp) => {
if (resp?.ok) {
return resp.json();
}
else {
throw new Error ('Error when getting spaces requests');
}
});
}

export function getSuggestionsSpace(){
return fetch('/portal/rest/homepage/intranet/spaces/suggestions', {
credentials: 'include'
Expand All @@ -112,25 +98,6 @@ export function getSuggestionsSpace(){
});
}

export function replyInvitationToJoinSpace(spaceMembershipId, reply) {
const data = {status: `${reply}`};
return fetch(`/portal/rest/v1/social/spacesMemberships/${spaceMembershipId}`, {
method: 'PUT',
credentials: 'include',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
}).then((resp) => {
if (resp?.ok) {
return resp.json();
}
else {
throw new Error ('Error when replying invitation to join space');
}
});
}

export function getUserConnections(query, offset, limit, expand) {
return fetch(`${eXo.env.portal.context}/${eXo.env.portal.rest}/v1/social/users/${eXo.env.portal.profileOwner}/connections?q=${query || ''}&offset=${offset || 0}&limit=${limit|| 0}&expand=${expand || ''}&returnSize=true`, {
method: 'GET',
Expand Down

0 comments on commit 06c825c

Please sign in to comment.