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

ChatSDK.thread().create1to1Thread or any similar thread method creates a new chat instead of returning existing chat (existing threadID) with the same user

anonym24 opened this issue · comments

  1. Is the bug present in the demo Chat SDK project? yes

  2. What modifications have you made to the Chat SDK? none

  3. Android Version: 21...30 API

  4. Steps taken to reproduce the problem:

  • install app
  • login to user1
  • programmatically create chat with user2 ChatSDK.thread().create1to1Thread(user2, null) and send some message to that user using this newly created thread
  • uninstall app
  • install app
  • login to user1 and quickly try to create chat again with user2 using the same method
  1. Expected result:
  • existing chat with user2 should have been opened
  1. Actual result:
  • new second thread was created with user2 which didn't contained any messages
  1. Comments:
    I believe this error is related to some ChatSDK caching mechanism and asynchronization
    If you do these steps fast/quickly use you will reproduce this issue if slowly than mb not

attach ChatSDK.thread().create1to1Thread(user2, null).subscribe(...) to some button which will be available on main screen after login and in onSuccess callback call immediately ChatSDK.ui().startChatActivityForID(context, threadId)

The thing is that if you delete and reinstall the app and make a new thread before the app can synchronize then a new thread will be created.

However, this is highly unlikely ever to happen in real-world usage. I think it could only be achieved by creating the thread programmatically because Firebase should have fully synchronized within 500ms.

If you want to work around this, just do the following:

    Single<Thread> createThread(@Nullable String name, List<User> users, int type, @Nullable String entityID);

Call this method to create the thread. Then you can define a custom entity ID for the thread. Make that ID a concatenation of the current user's ID and the other user's id. For example:

[User 1 ID][User 2 ID]

Then there will be no duplication.

@bensmiley thanks, I'll try it. Also there is a similar issue (after clean reinstall) with list of threads (PrivateThreadFragments) and it's much serious problem which users can meet, it can be reproduced with demo app #666

@bensmiley but there a problem that iOS version doesn't allow creating thread with custom ID, only Android

@bensmiley is there any news/updates how can we do it on iOS - creating custom threadID? Because if iOS doesn't allow it then it's not possible create cross platform chat app

If you want to do this you can just look at the code that's executed to create the thread.

-(RXPromise *) createThreadWithUsers: (NSArray *) users
                                name: (NSString *) name
                                type: (bThreadType) type
                                entityID: (NSString *) entityID
                         forceCreate: (BOOL) force
                       threadCreated: (void(^)(NSError * error, id<PThread> thread)) threadCreated {
    
    id<PThread> threadModel = [self fetchThreadWithUsers: users];
    if (threadModel && threadCreated != Nil && !force) {
        threadCreated(Nil, threadModel);
        return [RXPromise resolveWithResult:Nil];
    }
    else {
        threadModel = [self createThreadWithUsers:users name:name type: type];
        CCThreadWrapper * thread = [CCThreadWrapper threadWithModel:threadModel];
        
        if (entityID) {
            threadModel.entityID = entityID;
        }
        
        __weak __typeof(self) weakSelf = self;

        return [thread push].thenOnMain(^id(id<PThread> thread) {
                        
            // Add the users to the thread
            if (threadCreated != Nil) {
                threadCreated(Nil, thread);
            }
            return [weakSelf addUsers:threadModel.users.allObjects toThread:threadModel];
            
        },^id(NSError * error) {
            //[BChatSDK.db undo];
            
            if (threadCreated != Nil) {
                threadCreated(error, Nil);
            }
            return error;
        });
    }
}

Just add this function to your code and call it. I have added this to the core framework and it will be available with the next release.