Search in sources :

Example 1 with AddChirp

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

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

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