Search in sources :

Example 1 with Chirp

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;
}
Also used : ImageButton(android.widget.ImageButton) Chirp(com.github.dedis.popstellar.model.objects.Chirp) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) TextView(android.widget.TextView) DateUtils.getRelativeTimeSpanString(android.text.format.DateUtils.getRelativeTimeSpanString)

Example 2 with Chirp

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

Example 3 with 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("");
    }
}
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 4 with Chirp

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()));
}
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) Base64DataUtils.generateMessageID(com.github.dedis.popstellar.testutils.Base64DataUtils.generateMessageID) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) Test(org.junit.Test)

Example 5 with Chirp

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

Chirp (com.github.dedis.popstellar.model.objects.Chirp)5 AddChirp (com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp)4 DeleteChirp (com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp)4 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)3 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)2 Channel (com.github.dedis.popstellar.model.objects.Channel)2 Lao (com.github.dedis.popstellar.model.objects.Lao)2 PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)2 LAORepository (com.github.dedis.popstellar.repository.LAORepository)2 Base64DataUtils.generateMessageID (com.github.dedis.popstellar.testutils.Base64DataUtils.generateMessageID)2 Test (org.junit.Test)2 DateUtils.getRelativeTimeSpanString (android.text.format.DateUtils.getRelativeTimeSpanString)1 ImageButton (android.widget.ImageButton)1 TextView (android.widget.TextView)1 InvalidMessageIdException (com.github.dedis.popstellar.utility.error.InvalidMessageIdException)1