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