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);
}
}
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);
}
Aggregations