use of com.github.dedis.popstellar.utility.error.keys.KeyException in project popstellar by dedis.
the class SocialMediaViewModel method sendChirp.
/**
* Send a chirp to your own channel.
*
* <p>Publish a MessageGeneral containing AddChirp data.
*
* @param text the text written in the chirp
* @param parentId the id of the chirp to which you replied
* @param timestamp the time at which you sent the chirp
*/
public void sendChirp(String text, @Nullable MessageID parentId, long timestamp) {
Log.d(TAG, "Sending a chirp");
Lao lao = getCurrentLao();
if (lao == null) {
Log.e(TAG, LAO_FAILURE_MESSAGE);
return;
}
AddChirp addChirp = new AddChirp(text, parentId, timestamp);
try {
PoPToken token = keyManager.getValidPoPToken(lao);
Channel channel = lao.getChannel().subChannel(SOCIAL).subChannel(token.getPublicKey().getEncoded());
Log.d(TAG, PUBLISH_MESSAGE);
MessageGeneral msg = new MessageGeneral(token, addChirp, gson);
Disposable disposable = networkManager.getMessageSender().publish(token, channel, addChirp).subscribe(() -> Log.d(TAG, "sent chirp with messageId: " + msg.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_sending_chirp));
disposables.add(disposable);
} catch (KeyException e) {
ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
}
}
use of com.github.dedis.popstellar.utility.error.keys.KeyException in project popstellar by dedis.
the class SocialMediaViewModel method deleteChirp.
public void deleteChirp(MessageID chirpId, long timestamp) {
Log.d(TAG, "Deleting the chirp with id: " + chirpId);
Lao lao = getCurrentLao();
if (lao == null) {
Log.e(TAG, LAO_FAILURE_MESSAGE);
return;
}
DeleteChirp deleteChirp = new DeleteChirp(chirpId, timestamp);
try {
PoPToken token = keyManager.getValidPoPToken(lao);
Channel channel = lao.getChannel().subChannel(SOCIAL).subChannel(token.getPublicKey().getEncoded());
Log.d(TAG, PUBLISH_MESSAGE);
MessageGeneral msg = new MessageGeneral(token, deleteChirp, gson);
Disposable disposable = networkManager.getMessageSender().publish(token, channel, deleteChirp).subscribe(() -> {
Log.d(TAG, "Deleted chirp with messageId: " + msg.getMessageId());
Toast.makeText(getApplication().getApplicationContext(), "Deleted chirp!", Toast.LENGTH_LONG).show();
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_delete_chirp));
disposables.add(disposable);
} catch (KeyException e) {
ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
}
}
use of com.github.dedis.popstellar.utility.error.keys.KeyException in project popstellar by dedis.
the class LaoDetailViewModel method sendVote.
/**
* Sends a ElectionCastVotes message .
*
* <p>Publish a GeneralMessage containing ElectionCastVotes data.
*
* @param votes the corresponding votes for that election
*/
public void sendVote(List<ElectionVote> votes) {
Election election = mCurrentElection.getValue();
if (election == null) {
Log.d(TAG, "failed to retrieve current election");
return;
}
Log.d(TAG, "sending a new vote in election : " + election + " with election start time" + election.getStartTimestamp());
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
try {
PoPToken token = keyManager.getValidPoPToken(lao);
CastVote castVote = new CastVote(votes, election.getId(), lao.getId());
Channel electionChannel = election.getChannel();
Log.d(TAG, PUBLISH_MESSAGE);
Disposable disposable = networkManager.getMessageSender().publish(token, electionChannel, castVote).doFinally(this::openLaoDetail).subscribe(() -> {
Log.d(TAG, "sent a vote successfully");
// Toast ? + send back to election screen or details screen ?
Toast.makeText(getApplication(), "vote successfully sent !", Toast.LENGTH_LONG).show();
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_send_vote));
disposables.add(disposable);
} catch (KeyException e) {
ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
}
}
use of com.github.dedis.popstellar.utility.error.keys.KeyException 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.utility.error.keys.KeyException in project popstellar by dedis.
the class LaoDetailViewModel method openRollCall.
/**
* Opens a roll call event.
*
* <p>Publish a GeneralMessage containing OpenRollCall data.
*
* @param id the roll call id to open
*/
public void openRollCall(String id) {
Log.d(TAG, "call openRollCall");
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
long openedAt = Instant.now().getEpochSecond();
Channel channel = lao.getChannel();
String laoId = lao.getId();
Optional<RollCall> optRollCall = lao.getRollCall(id);
if (!optRollCall.isPresent()) {
Log.d(TAG, "failed to retrieve roll call with id " + id + "laoID: " + laoId);
return;
}
RollCall rollCall = optRollCall.get();
OpenRollCall openRollCall = new OpenRollCall(laoId, id, openedAt, rollCall.getState());
attendees = new HashSet<>(rollCall.getAttendees());
try {
attendees.add(keyManager.getPoPToken(lao, rollCall).getPublicKey());
} catch (KeyException e) {
ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
}
Disposable disposable = networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, openRollCall).subscribe(() -> {
Log.d(TAG, "opened the roll call");
currentRollCallId = openRollCall.getUpdateId();
scanningAction = ScanningAction.ADD_ROLL_CALL_ATTENDEE;
openScanning();
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_open_rollcall));
disposables.add(disposable);
}
Aggregations