use of com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall in project popstellar by dedis.
the class LaoDetailViewModel method closeRollCall.
/**
* Closes the roll call event currently open
*
* <p>Publish a GeneralMessage containing CloseRollCall data.
*/
public void closeRollCall(int nextFragment) {
Log.d(TAG, "call closeRollCall");
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
long end = Instant.now().getEpochSecond();
Channel channel = lao.getChannel();
CloseRollCall closeRollCall = new CloseRollCall(lao.getId(), currentRollCallId, end, new ArrayList<>(attendees));
Disposable disposable = networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, closeRollCall).subscribe(() -> {
Log.d(TAG, "closed the roll call");
currentRollCallId = "";
attendees.clear();
mCloseRollCallEvent.setValue(new SingleEvent<>(nextFragment));
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_close_rollcall));
disposables.add(disposable);
}
use of com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall in project popstellar by dedis.
the class RollCallHandler method handleCloseRollCall.
/**
* Process a CloseRollCall message.
*
* @param context the HandlerContext of the message
* @param closeRollCall the message that was received
*/
public static void handleCloseRollCall(HandlerContext context, CloseRollCall closeRollCall) throws DataHandlingException {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
MessageID messageId = context.getMessageId();
Lao lao = laoRepository.getLaoByChannel(channel);
Log.d(TAG, "handleCloseRollCall: " + channel);
String updateId = closeRollCall.getUpdateId();
String closes = closeRollCall.getCloses();
Optional<RollCall> rollCallOptional = lao.getRollCall(closes);
if (!rollCallOptional.isPresent()) {
Log.w(TAG, "Cannot find roll call to close : " + closes);
throw new InvalidDataException(closeRollCall, "close id", closes);
}
RollCall rollCall = rollCallOptional.get();
rollCall.setEnd(closeRollCall.getClosedAt());
rollCall.setId(updateId);
rollCall.getAttendees().addAll(closeRollCall.getAttendees());
rollCall.setState(EventState.CLOSED);
lao.updateRollCall(closes, rollCall);
lao.updateWitnessMessage(messageId, closeRollCallWitnessMessage(messageId, rollCall));
// Subscribe to the social media channels
try {
PoPToken token = context.getKeyManager().getValidPoPToken(lao, rollCall);
context.getMessageSender().subscribe(channel.subChannel("social").subChannel(token.getPublicKey().getEncoded())).subscribe();
} catch (InvalidPoPTokenException e) {
Log.i(TAG, "Received a close roll-call that you did not attend");
} catch (KeyException e) {
Log.e(TAG, "Could not retrieve your PoP Token to subscribe you to your social media channel", e);
}
}
use of com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall in project popstellar by dedis.
the class RollCallHandlerTest method testHandleCloseRollCall.
@Test
public void testHandleCloseRollCall() throws DataHandlingException {
// Create the close Roll Call message
CloseRollCall closeRollCall = new CloseRollCall(CREATE_LAO.getId(), rollCall.getId(), rollCall.getEnd(), new ArrayList<>());
MessageGeneral message = new MessageGeneral(SENDER_KEY, closeRollCall, GSON);
// Call the message handler
messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
// Check the Roll Call is present with state CLOSED and the correct ID
Optional<RollCall> rollCallOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getRollCall(closeRollCall.getUpdateId());
assertTrue(rollCallOpt.isPresent());
assertEquals(EventState.CLOSED, rollCallOpt.get().getState());
assertEquals(closeRollCall.getUpdateId(), rollCallOpt.get().getId());
// Check the WitnessMessage has been created
Optional<WitnessMessage> witnessMessage = laoRepository.getLaoByChannel(LAO_CHANNEL).getWitnessMessage(message.getMessageId());
assertTrue(witnessMessage.isPresent());
// Check the Witness message contains the expected title and description
WitnessMessage expectedMessage = closeRollCallWitnessMessage(message.getMessageId(), rollCallOpt.get());
assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
Aggregations