Search in sources :

Example 1 with KeyException

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);
    }
}
Also used : AddChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 2 with KeyException

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);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) Lao(com.github.dedis.popstellar.model.objects.Lao) DeleteChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 3 with KeyException

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);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Channel(com.github.dedis.popstellar.model.objects.Channel) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) Election(com.github.dedis.popstellar.model.objects.Election) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 4 with KeyException

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);
    }
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) InvalidPoPTokenException(com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidDataException(com.github.dedis.popstellar.utility.error.InvalidDataException) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 5 with KeyException

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);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Channel(com.github.dedis.popstellar.model.objects.Channel) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Aggregations

KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)7 Lao (com.github.dedis.popstellar.model.objects.Lao)6 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)6 Channel (com.github.dedis.popstellar.model.objects.Channel)5 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)4 Disposable (io.reactivex.disposables.Disposable)4 RollCall (com.github.dedis.popstellar.model.objects.RollCall)3 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)2 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)2 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)2 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)2 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)2 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)2 Bitmap (android.graphics.Bitmap)1 Nullable (androidx.annotation.Nullable)1 CastVote (com.github.dedis.popstellar.model.network.method.message.data.election.CastVote)1 AddChirp (com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp)1 DeleteChirp (com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp)1 Election (com.github.dedis.popstellar.model.objects.Election)1 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)1