chat-sdk / chat-sdk-android

Chat SDK Android - Open Source Mobile Messenger

Home Page:https://chatsdk.co

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get user by entity id (UID from firebase)?

anonym24 opened this issue · comments

To create a thread it needs User model of other user, but I have only entity ids of other users, from my backend.
So how can I create a thread using entity id (string)?

It seems there was UserWrapper.initWithEntityId(userID) sometime ago, but now it's not available

Hi @anonym24 you can see examples here:

https://github.com/chat-sdk/chat-sdk-android/blob/master/chat-sdk-demo/src/main/java/sdk/chat/demo/examples/api/ApiExamples.java

Specifically, you need:

Get the user:

User user = ChatSDK.core().getUserNowForEntityID("userID");

Make the thread:

Disposable d = ChatSDK.thread().createThread(name, user, ChatSDK.currentUser())
        .observeOn(RX.main())
        .doFinally(() -> {
            // Runs when process completed from error or success
        })
        .subscribe(thread -> {
            // When the thread type created
        }, throwable -> {
            // If there type an error
        });

Start Chat Activity:

public void openChatActivityWithThread (Context context, Thread thread) {
    ChatSDK.ui().startChatActivityForID(context, thread.getEntityID());
}

@bensmiley thank you. I've already tried val user = ChatSDK.db().fetchOrCreateEntityWithEntityID(User::class.java, fbUid)
should I use val user = ChatSDK.core().getUserNowForEntityID("userID") instead?

Or both methods are ok?

@anonym24 both are ok. The only difference is that the one I suggested will also synchronize the user with Firebase.

both are ok. The only difference is that the one I suggested will also synchronize the user with Firebase.

great. Thank you. I think method with sync is better. Also there is RX alternative of it, will be even better:

ChatSDK.core().getUserForEntityID(fbUid).flatMap { ... createThread ...

This will only create the thread when it has synchronized the user. But that is fine too.