Search in sources :

Example 1 with DeleteChirp

use of com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp 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 2 with DeleteChirp

use of com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp in project popstellar by dedis.

the class ChirpHandler method handleDeleteChirp.

/**
 * process a DeleteChirp message.
 *
 * @param context the HandlerContext of the message
 * @param deleteChirp the data of the message that was received
 */
public static void handleDeleteChirp(HandlerContext context, DeleteChirp deleteChirp) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Optional<Chirp> chirpOptional = lao.getChirp(deleteChirp.getChirpId());
    Chirp chirp;
    if (!chirpOptional.isPresent()) {
        throw new InvalidMessageIdException(deleteChirp, deleteChirp.getChirpId());
    }
    chirp = chirpOptional.get();
    if (chirp.getIsDeleted()) {
        Log.d(TAG, "The chirp is already deleted");
    } else {
        chirp.setIsDeleted(true);
        chirp.setText("");
    }
}
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) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidMessageIdException(com.github.dedis.popstellar.utility.error.InvalidMessageIdException) Lao(com.github.dedis.popstellar.model.objects.Lao)

Example 3 with DeleteChirp

use of com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp in project popstellar by dedis.

the class ChirpHandlerTest method testHandleDeleteChirp.

@Test
public void testHandleDeleteChirp() throws DataHandlingException {
    MessageGeneral message = new MessageGeneral(SENDER_KEY, ADD_CHIRP, GSON);
    messageHandler.handleMessage(laoRepository, messageSender, CHIRP_CHANNEL, message);
    final DeleteChirp DELETE_CHIRP = new DeleteChirp(message.getMessageId(), DELETION_TIME);
    MessageGeneral message2 = new MessageGeneral(SENDER_KEY, DELETE_CHIRP, GSON);
    messageHandler.handleMessage(laoRepository, messageSender, CHIRP_CHANNEL, message2);
    Optional<Chirp> chirpOpt = LAO.getChirp(message.getMessageId());
    assertTrue(chirpOpt.isPresent());
    Chirp chirp = chirpOpt.get();
    assertEquals(message.getMessageId(), chirp.getId());
    assertEquals(CHIRP_CHANNEL, chirp.getChannel());
    assertEquals(SENDER, chirp.getSender());
    assertEquals(EMPTY_STRING, chirp.getText());
    assertEquals(CREATION_TIME, chirp.getTimestamp());
    assertEquals(PARENT_ID, chirp.getParentId());
    Map<MessageID, Chirp> chirps = LAO.getAllChirps();
    assertEquals(1, chirps.size());
    assertEquals(chirp, chirps.get(chirp.getId()));
}
Also used : DeleteChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp) Chirp(com.github.dedis.popstellar.model.objects.Chirp) AddChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) DeleteChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp) Base64DataUtils.generateMessageID(com.github.dedis.popstellar.testutils.Base64DataUtils.generateMessageID) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) Test(org.junit.Test)

Aggregations

DeleteChirp (com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp)3 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)2 AddChirp (com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp)2 Channel (com.github.dedis.popstellar.model.objects.Channel)2 Chirp (com.github.dedis.popstellar.model.objects.Chirp)2 Lao (com.github.dedis.popstellar.model.objects.Lao)2 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)1 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)1 LAORepository (com.github.dedis.popstellar.repository.LAORepository)1 Base64DataUtils.generateMessageID (com.github.dedis.popstellar.testutils.Base64DataUtils.generateMessageID)1 InvalidMessageIdException (com.github.dedis.popstellar.utility.error.InvalidMessageIdException)1 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 Disposable (io.reactivex.disposables.Disposable)1 Test (org.junit.Test)1