support notification types: trade update, chat message, keep alive

This commit is contained in:
woodser 2022-01-15 15:43:27 -05:00
parent 422267de79
commit 2715e1bba8
5 changed files with 35 additions and 19 deletions

View file

@ -1,9 +1,10 @@
package bisq.core.api; package bisq.core.api;
import bisq.core.api.CoreApi.NotificationListener; import bisq.core.api.CoreApi.NotificationListener;
import bisq.core.api.model.TradeInfo;
import bisq.core.trade.Trade;
import bisq.proto.grpc.NotificationMessage; import bisq.proto.grpc.NotificationMessage;
import bisq.proto.grpc.NotificationMessage.NotificationType;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.util.Iterator; import java.util.Iterator;
@ -33,10 +34,19 @@ public class CoreNotificationService {
try { try {
listener.onMessage(notification); listener.onMessage(notification);
} catch (RuntimeException e) { } catch (RuntimeException e) {
log.warn("Failed to send message {} to listener {}", notification, listener, e); log.warn("Failed to send notification to listener {}: {}", listener, e.getMessage());
iter.remove(); iter.remove();
} }
} }
} }
} }
public void sendTradeNotification(Trade trade, String title, String message) {
sendNotification(NotificationMessage.newBuilder()
.setType(NotificationType.TRADE_UPDATE)
.setTrade(TradeInfo.toTradeInfo(trade).toProtoMessage())
.setTimestamp(System.currentTimeMillis())
.setTitle(title)
.setMessage(message).build());
}
} }

View file

@ -94,8 +94,8 @@ public class ContractInfo implements Payload {
proto.getIsBuyerMakerAndSellerTaker(), proto.getIsBuyerMakerAndSellerTaker(),
proto.getMakerAccountId(), proto.getMakerAccountId(),
proto.getTakerAccountId(), proto.getTakerAccountId(),
PaymentAccountPayloadInfo.fromProto(proto.getMakerPaymentAccountPayload()), proto.getMakerPaymentAccountPayload() == null ? null : PaymentAccountPayloadInfo.fromProto(proto.getMakerPaymentAccountPayload()),
PaymentAccountPayloadInfo.fromProto(proto.getTakerPaymentAccountPayload()), proto.getTakerPaymentAccountPayload() == null ? null : PaymentAccountPayloadInfo.fromProto(proto.getTakerPaymentAccountPayload()),
proto.getMakerPayoutAddressString(), proto.getMakerPayoutAddressString(),
proto.getTakerPayoutAddressString(), proto.getTakerPayoutAddressString(),
proto.getLockTime()); proto.getLockTime());
@ -103,18 +103,18 @@ public class ContractInfo implements Payload {
@Override @Override
public bisq.proto.grpc.ContractInfo toProtoMessage() { public bisq.proto.grpc.ContractInfo toProtoMessage() {
return bisq.proto.grpc.ContractInfo.newBuilder() bisq.proto.grpc.ContractInfo.Builder builder = bisq.proto.grpc.ContractInfo.newBuilder()
.setBuyerNodeAddress(buyerNodeAddress) .setBuyerNodeAddress(buyerNodeAddress)
.setSellerNodeAddress(sellerNodeAddress) .setSellerNodeAddress(sellerNodeAddress)
.setArbitratorNodeAddress(arbitratorNodeAddress) .setArbitratorNodeAddress(arbitratorNodeAddress)
.setIsBuyerMakerAndSellerTaker(isBuyerMakerAndSellerTaker) .setIsBuyerMakerAndSellerTaker(isBuyerMakerAndSellerTaker)
.setMakerAccountId(makerAccountId) .setMakerAccountId(makerAccountId)
.setTakerAccountId(takerAccountId) .setTakerAccountId(takerAccountId)
.setMakerPaymentAccountPayload(makerPaymentAccountPayload.toProtoMessage())
.setTakerPaymentAccountPayload(takerPaymentAccountPayload.toProtoMessage())
.setMakerPayoutAddressString(makerPayoutAddressString) .setMakerPayoutAddressString(makerPayoutAddressString)
.setTakerPayoutAddressString(takerPayoutAddressString) .setTakerPayoutAddressString(takerPayoutAddressString)
.setLockTime(lockTime) .setLockTime(lockTime);
.build(); if (makerPaymentAccountPayload != null) builder.setMakerPaymentAccountPayload(makerPaymentAccountPayload.toProtoMessage());
if (takerPaymentAccountPayload != null) builder.setTakerPaymentAccountPayload(takerPaymentAccountPayload.toProtoMessage());
return builder.build();
} }
} }

View file

@ -47,6 +47,7 @@ public class PaymentAccountPayloadInfo implements Payload {
} }
public static PaymentAccountPayloadInfo toPaymentAccountPayloadInfo(PaymentAccountPayload paymentAccountPayload) { public static PaymentAccountPayloadInfo toPaymentAccountPayloadInfo(PaymentAccountPayload paymentAccountPayload) {
if (paymentAccountPayload == null) return null;
Optional<String> address = Optional.empty(); Optional<String> address = Optional.empty();
if (paymentAccountPayload instanceof CryptoCurrencyAccountPayload) if (paymentAccountPayload instanceof CryptoCurrencyAccountPayload)
address = Optional.of(((CryptoCurrencyAccountPayload) paymentAccountPayload).getAddress()); address = Optional.of(((CryptoCurrencyAccountPayload) paymentAccountPayload).getAddress());

View file

@ -66,7 +66,6 @@ import bisq.network.p2p.DecryptedMessageWithPubKey;
import bisq.network.p2p.NodeAddress; import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.P2PService; import bisq.network.p2p.P2PService;
import bisq.network.p2p.network.TorNetworkNode; import bisq.network.p2p.network.TorNetworkNode;
import bisq.proto.grpc.NotificationMessage;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import bisq.common.ClockWatcher; import bisq.common.ClockWatcher;
import bisq.common.config.Config; import bisq.common.config.Config;
@ -93,7 +92,6 @@ import javafx.collections.ObservableList;
import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.KeyParameter;
import org.fxmisc.easybind.EasyBind; import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.Subscription;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -522,10 +520,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
// TODO (woodser): save subscription, bind on startup // TODO (woodser): save subscription, bind on startup
EasyBind.subscribe(trade.statePhaseProperty(), phase -> { EasyBind.subscribe(trade.statePhaseProperty(), phase -> {
if (phase == Phase.DEPOSIT_PUBLISHED) { if (phase == Phase.DEPOSIT_PUBLISHED) {
notificationService.sendNotification(NotificationMessage.newBuilder() notificationService.sendTradeNotification(trade, "Offer Taken", "Your offer " + offer.getId() + " has been accepted"); // TODO (woodser): use language translation
.setTimestamp(System.currentTimeMillis())
.setTitle("Offer Taken")
.setMessage("Your offer " + offer.getId() + " has been accepted").build());
} }
}); });

View file

@ -55,9 +55,19 @@ message RegisterNotificationListenerRequest {
} }
message NotificationMessage { message NotificationMessage {
int64 timestamp = 1; enum NotificationType {
string title = 2; TRADE_UPDATE = 0;
string message = 3; CHAT_MESSAGE = 1;
KEEP_ALIVE = 2;
}
string id = 1;
NotificationType type = 2;
int64 timestamp = 3;
string title = 4;
string message = 5;
TradeInfo trade = 6;
ChatMessage chat_message = 7;
} }
message SendNotificationRequest { message SendNotificationRequest {