19

I am using FirebaseRecyclerAdapter to display chat messages.

private void attachRecyclerViewAdapter() {
    lastFifty = mChatRef.limitToLast(50).;
    mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
            Chat.class, R.layout.message, ChatHolder.class, lastFifty) {

        @Override
        public void populateViewHolder(ChatHolder chatView, Chat chat, int position) {
            chatView.setName(chat.getName());
            chatView.setText(chat.getText());
            chatView.setTimeLocation(chat.getTime());
            FirebaseUser currentUser = mAuth.getCurrentUser();

            if (currentUser != null && chat.getUid().equals(currentUser.getUid())) {
                chatView.setIsSender(true);
            } else {
                chatView.setIsSender(false);
            }
        }
    };

I have a list that contains list of specific users. I would like to apply filter to see only messages from those specific users. What should I do ?

Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
user655561
  • 659
  • 1
  • 9
  • 24
  • There is an open [feature request on FirebaseUI for adding the ability to filter](https://github.com/firebase/FirebaseUI-Android/issues/15). – Frank van Puffelen Jul 18 '16 at 14:48
  • 2
    It would always be best practice to try and do the filtering on the server side. Have you looked into stopping users from sending messages to other users which they have not yet added or have blocked? In an app I am working on I allow users to add any user they want but I only allow them to send messages if the other user has also added them. – Riley MacDonald Sep 04 '16 at 19:10
  • 1
    -1 down vote have you considered using a dedicated node on the Firebase Database for those specific users? – Andrey Solera Sep 06 '16 at 17:48
  • are you tryin to implemet a group chat? – Deepak John Sep 07 '16 at 12:13

1 Answers1

2

You can create messages with user id pair nodes. For example messages->'uid1-uid2'->...

To prevent which is first order uids alphatecially as the following messageId generator code does:

public static String getMessageId(String id1, String id2){
        String messageId;

        if(id1.compareTo(id2) < 0){
            messageId = id1 + "-" + id2;
        }else if(id1.compareTo(id2) > 0) {
            messageId = id2 + "-" + id1;
        }else{
            messageId = id1;
        }

        return messageId;
    }

When you want to see the chat history between a user and urself, obtain the user's id and generate messageId = getMessageId(id1, id2); or messageId = getMessageId(id2, id1); gives the same result since the order doesn't affect the result.

Then call the messages from the node messages -> messageId

P.S. you should restructure your messages node as i describe.

EDIT

You can convert messageId to md5 equivalent to save chars.

just change

return messageId;

to

return md5(messageId);

where md5 is:

public static String md5(final String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest
                .getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}
ugur
  • 3,604
  • 3
  • 26
  • 57