Search in sources :

Example 36 with Lao

use of com.github.dedis.popstellar.model.objects.Lao 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)

Example 37 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class LaoDetailViewModel method dispatchLaoUpdate.

/**
 * Helper method for updateLaoWitnesses and updateLaoName to send a stateLao message
 */
private void dispatchLaoUpdate(String desc, UpdateLao updateLao, Lao lao, Channel channel, MessageGeneral msg) {
    StateLao stateLao = new StateLao(updateLao.getId(), updateLao.getName(), lao.getCreation(), updateLao.getLastModified(), lao.getOrganizer(), msg.getMessageId(), updateLao.getWitnesses(), new ArrayList<>());
    disposables.add(networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, stateLao).subscribe(() -> Log.d(TAG, "updated " + desc), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_state_lao)));
}
Also used : PackageManager(android.content.pm.PackageManager) HomeViewModel(com.github.dedis.popstellar.ui.home.HomeViewModel) NonNull(androidx.annotation.NonNull) ElectionEnd(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionEnd) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) LiveDataReactiveStreams(androidx.lifecycle.LiveDataReactiveStreams) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Manifest(android.Manifest) GeneralSecurityException(java.security.GeneralSecurityException) Lao(com.github.dedis.popstellar.model.objects.Lao) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) Gson(com.google.gson.Gson) Schedulers(io.reactivex.schedulers.Schedulers) Transformations(androidx.lifecycle.Transformations) ContextCompat(androidx.core.content.ContextCompat) Log(android.util.Log) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) ConsensusElect(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElect) KeyManager(com.github.dedis.popstellar.utility.security.KeyManager) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Set(java.util.Set) ElectionVote(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionVote) KeyGenerationException(com.github.dedis.popstellar.utility.error.keys.KeyGenerationException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup) HiltViewModel(dagger.hilt.android.lifecycle.HiltViewModel) Objects(java.util.Objects) ScanningAction(com.github.dedis.popstellar.ui.qrcode.ScanningAction) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Stream(java.util.stream.Stream) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) Application(android.app.Application) RollCall(com.github.dedis.popstellar.model.objects.RollCall) Optional(java.util.Optional) SingleEvent(com.github.dedis.popstellar.SingleEvent) Wallet(com.github.dedis.popstellar.model.objects.Wallet) CameraPermissionViewModel(com.github.dedis.popstellar.ui.qrcode.CameraPermissionViewModel) MutableLiveData(androidx.lifecycle.MutableLiveData) R(com.github.dedis.popstellar.R) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) EventType(com.github.dedis.popstellar.model.objects.event.EventType) EventState(com.github.dedis.popstellar.model.objects.event.EventState) ErrorUtils(com.github.dedis.popstellar.utility.error.ErrorUtils) Signature(com.github.dedis.popstellar.model.objects.security.Signature) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) LAORepository(com.github.dedis.popstellar.repository.LAORepository) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) UninitializedWalletException(com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Toast(android.widget.Toast) AndroidViewModel(androidx.lifecycle.AndroidViewModel) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) LiveData(androidx.lifecycle.LiveData) Channel(com.github.dedis.popstellar.model.objects.Channel) BackpressureStrategy(io.reactivex.BackpressureStrategy) Election(com.github.dedis.popstellar.model.objects.Election) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) ConsensusElectAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept) Barcode(com.google.android.gms.vision.barcode.Barcode) QRCodeScanningViewModel(com.github.dedis.popstellar.ui.qrcode.QRCodeScanningViewModel) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) VisibleForTesting(androidx.annotation.VisibleForTesting) GlobalNetworkManager(com.github.dedis.popstellar.repository.remote.GlobalNetworkManager) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)

Example 38 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class SocialMediaViewModel method isOwner.

/**
 * Check whether the sender of a chirp is the current user
 *
 * @param sender String of the PoPToken PublicKey
 * @return true if the sender is the current user
 */
public boolean isOwner(String sender) {
    Log.d(TAG, "Testing if the sender is also the owner");
    Lao lao = getCurrentLao();
    if (lao == null) {
        Log.e(TAG, LAO_FAILURE_MESSAGE);
        return false;
    }
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        return sender.equals(token.getPublicKey().getEncoded());
    } catch (KeyException e) {
        ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
        return false;
    }
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 39 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class HomeViewModel method launchLao.

/**
 * launchLao is invoked when the user tries to create a new LAO. The method creates a `CreateLAO`
 * message and publishes it to the root channel. It observers the response in the background and
 * switches to the home screen on success.
 */
public void launchLao() {
    String laoName = mLaoName.getValue();
    Log.d(TAG, "creating lao with name " + laoName);
    CreateLao createLao = new CreateLao(laoName, keyManager.getMainPublicKey());
    disposables.add(networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), Channel.ROOT, createLao).subscribe(() -> {
        Log.d(TAG, "got success result for create lao");
        // Create new LAO and add it to the LAORepository LAO lists
        Lao lao = new Lao(createLao.getId());
        laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
        laoRepository.setAllLaoSubject();
        // Send subscribe and catchup after creating a LAO
        networkManager.getMessageSender().subscribe(lao.getChannel()).subscribe();
        openHome();
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_create_lao)));
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) JsonParseException(com.google.gson.JsonParseException) R(com.github.dedis.popstellar.R) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) ErrorUtils(com.github.dedis.popstellar.utility.error.ErrorUtils) SeedValidationException(com.github.dedis.popstellar.utility.error.keys.SeedValidationException) LiveDataReactiveStreams(androidx.lifecycle.LiveDataReactiveStreams) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Inject(javax.inject.Inject) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) Manifest(android.Manifest) GeneralSecurityException(java.security.GeneralSecurityException) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) Gson(com.google.gson.Gson) Toast(android.widget.Toast) AndroidViewModel(androidx.lifecycle.AndroidViewModel) Log(android.util.Log) LiveData(androidx.lifecycle.LiveData) Channel(com.github.dedis.popstellar.model.objects.Channel) BackpressureStrategy(io.reactivex.BackpressureStrategy) KeyManager(com.github.dedis.popstellar.utility.security.KeyManager) ActivityCompat(androidx.core.app.ActivityCompat) LAOState(com.github.dedis.popstellar.repository.LAOState) Barcode(com.google.android.gms.vision.barcode.Barcode) HiltViewModel(dagger.hilt.android.lifecycle.HiltViewModel) QRCodeScanningViewModel(com.github.dedis.popstellar.ui.qrcode.QRCodeScanningViewModel) ScanningAction(com.github.dedis.popstellar.ui.qrcode.ScanningAction) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Application(android.app.Application) SingleEvent(com.github.dedis.popstellar.SingleEvent) Wallet(com.github.dedis.popstellar.model.objects.Wallet) GlobalNetworkManager(com.github.dedis.popstellar.repository.remote.GlobalNetworkManager) CameraPermissionViewModel(com.github.dedis.popstellar.ui.qrcode.CameraPermissionViewModel) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) LAOState(com.github.dedis.popstellar.repository.LAOState) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao)

Example 40 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class ChirpHandler method handleChirpAdd.

/**
 * Process an AddChirp message.
 *
 * @param context the HandlerContext of the message
 * @param addChirp the data of the message that was received
 */
public static void handleChirpAdd(HandlerContext context, AddChirp addChirp) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    PublicKey senderPk = context.getSenderPk();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Chirp chirp = new Chirp(messageId);
    chirp.setChannel(channel);
    chirp.setSender(senderPk);
    chirp.setText(addChirp.getText());
    chirp.setTimestamp(addChirp.getTimestamp());
    chirp.setParentId(addChirp.getParentId().orElse(new MessageID("")));
    lao.updateAllChirps(messageId, chirp);
}
Also used : DeleteChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp) AddChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp) Chirp(com.github.dedis.popstellar.model.objects.Chirp) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Lao(com.github.dedis.popstellar.model.objects.Lao) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Aggregations

Lao (com.github.dedis.popstellar.model.objects.Lao)46 Channel (com.github.dedis.popstellar.model.objects.Channel)31 LAORepository (com.github.dedis.popstellar.repository.LAORepository)24 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)18 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)18 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)16 Disposable (io.reactivex.disposables.Disposable)14 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)12 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)11 Election (com.github.dedis.popstellar.model.objects.Election)10 RollCall (com.github.dedis.popstellar.model.objects.RollCall)10 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)9 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)9 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)8 PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)8 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)8 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)7 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)6 ElectInstance (com.github.dedis.popstellar.model.objects.ElectInstance)6 LAOState (com.github.dedis.popstellar.repository.LAOState)6