From 17757025437df41afb82ed4aaaf7a1bd54a7f8a0 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Sun, 21 Jul 2024 15:50:52 +0100 Subject: [PATCH] feat: Implement Permanent Link API - Meeds-io/MIPs#134 --- .../exoplatform/wallet/utils/WalletUtils.java | 4 + .../plugin/WalletPermanentLinkPlugin.java | 91 +++++++++++++++++++ .../FundsRequestNotificationPlugin.java | 2 + .../WalletReceiverNotificationPlugin.java | 4 + .../WalletSenderNotificationPlugin.java | 2 + .../conf/portal/wallet-configuration.xml | 4 + 6 files changed, 107 insertions(+) create mode 100644 wallet-services/src/main/java/io/meeds/wallet/permlink/plugin/WalletPermanentLinkPlugin.java diff --git a/wallet-api/src/main/java/org/exoplatform/wallet/utils/WalletUtils.java b/wallet-api/src/main/java/org/exoplatform/wallet/utils/WalletUtils.java index d8deca8869..9f8706b755 100644 --- a/wallet-api/src/main/java/org/exoplatform/wallet/utils/WalletUtils.java +++ b/wallet-api/src/main/java/org/exoplatform/wallet/utils/WalletUtils.java @@ -273,6 +273,8 @@ private WalletUtils() { public static final String AVATAR = "avatar"; + public static final String SENDER_ID = "senderIdentityId"; + public static final String SENDER = "sender"; public static final String USER = "userFullname"; @@ -283,6 +285,8 @@ private WalletUtils() { public static final String RECEIVER = "receiver"; + public static final String RECEIVER_ID = "receiverIdentityId"; + public static final String RECEIVER_URL = "receiverUrl"; public static final String FUNDS_ACCEPT_URL = "fundsAcceptUrl"; diff --git a/wallet-services/src/main/java/io/meeds/wallet/permlink/plugin/WalletPermanentLinkPlugin.java b/wallet-services/src/main/java/io/meeds/wallet/permlink/plugin/WalletPermanentLinkPlugin.java new file mode 100644 index 0000000000..28b167fd31 --- /dev/null +++ b/wallet-services/src/main/java/io/meeds/wallet/permlink/plugin/WalletPermanentLinkPlugin.java @@ -0,0 +1,91 @@ +/** + * This file is part of the Meeds project (https://meeds.io/). + * + * Copyright (C) 2020 - 2024 Meeds Association contact@meeds.io + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package io.meeds.wallet.permlink.plugin; + +import org.exoplatform.commons.exception.ObjectNotFoundException; +import org.exoplatform.portal.config.UserPortalConfigService; +import org.exoplatform.services.security.Identity; +import org.exoplatform.social.core.space.model.Space; +import org.exoplatform.social.core.space.spi.SpaceService; +import org.exoplatform.wallet.model.Wallet; +import org.exoplatform.wallet.model.WalletType; +import org.exoplatform.wallet.service.WalletAccountService; +import org.exoplatform.wallet.utils.WalletUtils; + +import io.meeds.portal.permlink.model.PermanentLinkObject; +import io.meeds.portal.permlink.plugin.PermanentLinkPlugin; + +public class WalletPermanentLinkPlugin implements PermanentLinkPlugin { + + private static final String OBJECT_TYPE = "wallet"; + + public static final String URL_FORMAT = "/portal/%s/wallet"; + + private SpaceService spaceService; + + private WalletAccountService walletAccountService; + + private UserPortalConfigService portalConfigService; + + public WalletPermanentLinkPlugin(SpaceService spaceService, + WalletAccountService walletAccountService, + UserPortalConfigService portalConfigService) { + this.spaceService = spaceService; + this.walletAccountService = walletAccountService; + this.portalConfigService = portalConfigService; + } + + @Override + public String getObjectType() { + return OBJECT_TYPE; + } + + @Override + public boolean canAccess(PermanentLinkObject object, Identity identity) throws ObjectNotFoundException { + String identityId = object.getObjectId(); + Wallet wallet = walletAccountService.getWalletByIdentityId(Long.parseLong(identityId)); + return wallet != null && WalletUtils.canAccessWallet(wallet, identity.getUserId()); + } + + @Override + public String getDirectAccessUrl(PermanentLinkObject object) throws ObjectNotFoundException { + String identityId = object.getObjectId(); + Wallet wallet = walletAccountService.getWalletByIdentityId(Long.parseLong(identityId)); + String url = WalletType.isSpace(wallet.getType()) ? getSpaceUrl(spaceService.getSpaceByPrettyName(wallet.getName())) : + getProfileUrl(); + if (object.getParameters() != null && object.getParameters().containsKey("transactionHash")) { + url += "?hash=" + object.getParameters().get("transactionHash"); + } + return url; + } + + public String getSpaceUrl(Space space) { + StringBuilder spaceUrl = new StringBuilder("/portal/g/"); + spaceUrl.append(space.getGroupId().replace("/", ":")) + .append("/") + .append(space.getPrettyName()) + .append("/SpaceWallet"); + return spaceUrl.toString(); + } + + public String getProfileUrl() { + return String.format(URL_FORMAT, portalConfigService.getMetaPortal()); + } + +} diff --git a/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/FundsRequestNotificationPlugin.java b/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/FundsRequestNotificationPlugin.java index abc120b020..59f7751734 100644 --- a/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/FundsRequestNotificationPlugin.java +++ b/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/FundsRequestNotificationPlugin.java @@ -94,7 +94,9 @@ protected NotificationInfo makeNotification(NotificationContext ctx) { .with(USER, requestSenderDetail.getName()) .with(USER_URL, getPermanentLink(requestSenderDetail)) .with(SENDER, requestSenderAccountDetail.getName()) + .with(SENDER_ID, String.valueOf(requestSenderAccountDetail.getTechnicalId())) .with(RECEIVER, requestReceiverAccountDetail.getName()) + .with(RECEIVER_ID, String.valueOf(requestReceiverAccountDetail.getTechnicalId())) .with(SYMBOL, symbol) .with(MESSAGE, message) .key(getKey()) diff --git a/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/WalletReceiverNotificationPlugin.java b/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/WalletReceiverNotificationPlugin.java index 16dabda2c3..b8b7354f40 100644 --- a/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/WalletReceiverNotificationPlugin.java +++ b/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/WalletReceiverNotificationPlugin.java @@ -28,10 +28,12 @@ import static org.exoplatform.wallet.utils.WalletUtils.MESSAGE_PARAMETER; import static org.exoplatform.wallet.utils.WalletUtils.RECEIVER; import static org.exoplatform.wallet.utils.WalletUtils.RECEIVER_ACCOUNT_DETAIL_PARAMETER; +import static org.exoplatform.wallet.utils.WalletUtils.RECEIVER_ID; import static org.exoplatform.wallet.utils.WalletUtils.RECEIVER_TYPE; import static org.exoplatform.wallet.utils.WalletUtils.RECEIVER_URL; import static org.exoplatform.wallet.utils.WalletUtils.SENDER; import static org.exoplatform.wallet.utils.WalletUtils.SENDER_ACCOUNT_DETAIL_PARAMETER; +import static org.exoplatform.wallet.utils.WalletUtils.SENDER_ID; import static org.exoplatform.wallet.utils.WalletUtils.SENDER_URL; import static org.exoplatform.wallet.utils.WalletUtils.SYMBOL; import static org.exoplatform.wallet.utils.WalletUtils.SYMBOL_PARAMETER; @@ -114,7 +116,9 @@ protected NotificationInfo makeNotification(NotificationContext ctx) { .with(SENDER_URL, getPermanentLink(senderAccountDetail)) .with(RECEIVER_URL, getPermanentLink(receiverAccountDetail)) .with(SENDER, senderAccountDetail.getName()) + .with(SENDER_ID, String.valueOf(senderAccountDetail.getTechnicalId())) .with(RECEIVER, receiverAccountDetail.getName()) + .with(RECEIVER_ID, String.valueOf(receiverAccountDetail.getTechnicalId())) .key(getKey()) .end(); } diff --git a/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/WalletSenderNotificationPlugin.java b/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/WalletSenderNotificationPlugin.java index 59fc52c046..b8203f472e 100644 --- a/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/WalletSenderNotificationPlugin.java +++ b/wallet-services/src/main/java/org/exoplatform/wallet/notification/plugin/WalletSenderNotificationPlugin.java @@ -99,7 +99,9 @@ protected NotificationInfo makeNotification(NotificationContext ctx) { .with(SENDER_URL, getPermanentLink(senderAccountDetail)) .with(RECEIVER_URL, getPermanentLink(receiverAccountDetail)) .with(SENDER, senderAccountDetail.getName()) + .with(SENDER_ID, String.valueOf(senderAccountDetail.getTechnicalId())) .with(RECEIVER, receiverAccountDetail.getName()) + .with(RECEIVER_ID, String.valueOf(receiverAccountDetail.getTechnicalId())) .key(getKey()) .end(); } diff --git a/wallet-services/src/main/resources/conf/portal/wallet-configuration.xml b/wallet-services/src/main/resources/conf/portal/wallet-configuration.xml index 0fe7f74670..c447396c63 100644 --- a/wallet-services/src/main/resources/conf/portal/wallet-configuration.xml +++ b/wallet-services/src/main/resources/conf/portal/wallet-configuration.xml @@ -20,6 +20,10 @@ --> + + io.meeds.wallet.permlink.plugin.WalletPermanentLinkPlugin + + WalletMetamaskFeatureProperties org.exoplatform.container.ExtendedPropertyConfigurator