use of com.github.dedis.popstellar.model.objects.Chirp in project popstellar by dedis.
the class ChirpListAdapter method getView.
@Override
public View getView(int position, View chirpView, ViewGroup viewGroup) {
if (chirpView == null) {
chirpView = layoutInflater.inflate(R.layout.chirp_card, null);
}
Chirp chirp = getItem(position);
if (chirp == null) {
throw new IllegalArgumentException("The chirp does not exist");
}
PublicKey publicKey = chirp.getSender();
long timestamp = chirp.getTimestamp();
String text;
TextView itemUsername = chirpView.findViewById(R.id.social_media_username);
TextView itemTime = chirpView.findViewById(R.id.social_media_time);
TextView itemText = chirpView.findViewById(R.id.social_media_text);
if (socialMediaViewModel.isOwner(publicKey.getEncoded())) {
ImageButton deleteChirp = chirpView.findViewById(R.id.delete_chirp_button);
deleteChirp.setVisibility(View.VISIBLE);
deleteChirp.setOnClickListener(v -> socialMediaViewModel.deleteChirpEvent(chirp.getId()));
}
if (chirp.getIsDeleted()) {
text = "Chirp is deleted.";
ImageButton deleteChirp = chirpView.findViewById(R.id.delete_chirp_button);
deleteChirp.setVisibility(View.GONE);
itemText.setTextColor(Color.GRAY);
} else {
text = chirp.getText();
}
itemUsername.setText(publicKey.getEncoded());
itemTime.setText(getRelativeTimeSpanString(timestamp * 1000));
itemText.setText(text);
return chirpView;
}
use of com.github.dedis.popstellar.model.objects.Chirp 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);
}
use of com.github.dedis.popstellar.model.objects.Chirp 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.objects.Chirp in project popstellar by dedis.
the class ChirpHandlerTest method testHandleAddChirp.
@Test
public void testHandleAddChirp() throws DataHandlingException {
MessageGeneral message = new MessageGeneral(SENDER_KEY, ADD_CHIRP, GSON);
messageHandler.handleMessage(laoRepository, messageSender, CHIRP_CHANNEL, message);
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(TEXT, 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()));
}
use of com.github.dedis.popstellar.model.objects.Chirp 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