tidy UpdateChatFlow

This commit is contained in:
mattbradburyr3 2023-01-29 15:42:32 +00:00
parent 1c620b2fbc
commit d56db30724
4 changed files with 34 additions and 21 deletions

View File

@ -15,6 +15,7 @@ import net.corda.v5.ledger.utxo.transaction.UtxoSignedTransaction;
import net.corda.v5.ledger.utxo.transaction.UtxoTransactionBuilder;
import net.corda.v5.membership.MemberInfo;
import net.corda.v5.membership.NotaryInfo;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -47,11 +48,11 @@ public class CreateNewChatFlow implements RPCStartableFlow {
@CordaInject
public FlowEngine flowEngine;
// @NotNull
@Suspendable
@Override
// public String call(@NotNull RPCRequestData requestBody) throws IllegalArgumentException {
public String call(RPCRequestData requestBody) {
public String call( RPCRequestData requestBody) {
log.info("CreateNewChatFlow.call() called");

View File

@ -16,9 +16,9 @@ import org.slf4j.LoggerFactory;
import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkForBannedWords;
import static com.r3.developers.csdetemplate.utxoexample.workflows.ResponderValidationHelpers.checkMessageFromMatchesCounterparty;
@InitiatedBy(protocol = "append-chat-protocol")
public class AppendChatResponderFlow implements ResponderFlow {
private final static Logger log = LoggerFactory.getLogger(AppendChatResponderFlow.class);
@InitiatedBy(protocol = "finalize-chat-protocol")
public class FinalizeChatResponderFlow implements ResponderFlow {
private final static Logger log = LoggerFactory.getLogger(FinalizeChatResponderFlow.class);
@CordaInject
public UtxoLedgerService utxoLedgerService;

View File

@ -13,7 +13,7 @@ import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.List;
@InitiatingFlow(protocol = "append-chat-protocol")
@InitiatingFlow(protocol = "finalize-chat-protocol")
public class FinalizeChatSubFlow implements SubFlow<String> {
public FinalizeChatSubFlow(UtxoSignedTransaction signedTransaction, MemberX500Name otherMember) {

View File

@ -10,6 +10,7 @@ import net.corda.v5.application.marshalling.JsonMarshallingService;
import net.corda.v5.application.membership.MemberLookup;
import net.corda.v5.application.messaging.FlowMessaging;
import net.corda.v5.base.annotations.Suspendable;
import net.corda.v5.base.exceptions.CordaRuntimeException;
import net.corda.v5.ledger.common.NotaryLookup;
import net.corda.v5.ledger.utxo.StateAndRef;
import net.corda.v5.ledger.utxo.UtxoLedgerService;
@ -27,6 +28,7 @@ import java.util.stream.Collectors;
import static com.r3.developers.csdetemplate.utilities.CorDappHelpers.findAndExpectExactlyOne;
import static java.util.Objects.*;
import static java.util.stream.Collectors.toList;
public class UpdateChatFlow implements RPCStartableFlow {
@ -41,16 +43,17 @@ public class UpdateChatFlow implements RPCStartableFlow {
@CordaInject
public UtxoLedgerService ledgerService;
@CordaInject
public NotaryLookup notaryLookup;
@CordaInject
public FlowMessaging flowMessaging;
// @CordaInject
// public NotaryLookup notaryLookup;
//
// @CordaInject
// public FlowMessaging flowMessaging;
@CordaInject
public FlowEngine flowEngine;
@NotNull
// @NotNull // this is a jetbrains annotation - we don't want to depend on jet brains packages.
@Suspendable
@Override
public String call(RPCRequestData requestBody) {
@ -61,24 +64,33 @@ public class UpdateChatFlow implements RPCStartableFlow {
UpdateChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, UpdateChatFlowArgs.class);
// Look up state (this is very inefficient)
StateAndRef<ChatState> stateAndRef = findAndExpectExactlyOne(
ledgerService.findUnconsumedStatesByType(ChatState.class),
sAndR -> sAndR.getState().getContractState().getId().equals(flowArgs.getId()),
"Multiple or zero Chat states with id " + flowArgs.getId() + " found"
);
// Removing this because it's an unnecessary level of abstraction, as it's an example for all abilities of programmer
// we want it to be as straight forward as possible.
// Also it's inconsistent with code below it.
// StateAndRef<ChatState> stateAndRef = findAndExpectExactlyOne(
// ledgerService.findUnconsumedStatesByType(ChatState.class),
// sAndR -> sAndR.getState().getContractState().getId().equals(flowArgs.getId()),
// "Multiple or zero Chat states with id " + flowArgs.getId() + " found"
// );
List<StateAndRef<ChatState>> chatStates = ledgerService.findUnconsumedStatesByType(ChatState.class);
List<StateAndRef<ChatState>> chatStatesWithId = chatStates.stream()
.filter(sar -> sar.getState().getContractState().getId() == flowArgs.getId()).collect(toList());
if (chatStatesWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.id + " found");
StateAndRef<ChatState> stateAndRef = chatStatesWithId.get(0);
MemberInfo myInfo = memberLookup.myInfo();
ChatState state = stateAndRef.getState().getContractState();
List<MemberInfo> members = state.getParticipants().stream().map(
it -> requireNonNull(memberLookup.lookup(it), "Member not found from Key")
).collect(Collectors.toList());
).collect(toList());
// Now we want to check that there is only one member other than ourselves in the chat.
members.remove(myInfo);
if(members.size() != 1) {
throw new RuntimeException("Should be only one participant other than the initiator");
}
if(members.size() != 1) throw new RuntimeException("Should be only one participant other than the initiator");
MemberInfo otherMember = members.get(0);
ChatState newChatState = state.updateMessage(myInfo.getName(), flowArgs.getMessage());