QuickBlox / q-municate-android

Qmunicate Android chat application
MIT License
253 stars 198 forks source link

Android Q-municate register all users as friends #137

Closed dawkinsjeff closed 1 year ago

dawkinsjeff commented 7 years ago

I'm developing an application that uses Q-municate as base code. In my application, all users are already friends so I would like to skip the following steps:

To summarize, I want to store all the users from the Quickblox platform in my table Friend at the opening of the application so that the friends list can appear when creating a new chat. The LogCat doesn't show any error but it seems like no user is stored in Table Friend because when i execute addChat() methode, the Toast new_message_no_friends_for_new_message appears.

I need your help Please.

Here is my code:

public void getAllUsersByTag(){
    QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
    pagedRequestBuilder.setPage(1);
    pagedRequestBuilder.setPerPage(100);

    ArrayList<String> userTags = new ArrayList<String>();
    userTags.add("patient");

    QBUsers.getUsersByTags(userTags, pagedRequestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() {
        @Override
        public void onSuccess(ArrayList<QBUser> qbUsers, Bundle bundle) {
            Collection<User> usersListResult = UserFriendUtils.createUsersList(qbUsers);
            QBFriendListHelper friendListHelper = new QBFriendListHelper(getApplicationContext());
            if (!usersListResult.isEmpty()){
                System.out.print(TAG+" \ngetAllUsersByTag usercollection.size() = "+usersListResult.size());
                friendListHelper.saveUsersAndFriends(usersListResult);
            }else{
                System.out.print(TAG+" \ngetAllUsersByTag userCollection is null\n ");
            }
        }

        @Override
        public void onError(QBResponseException e) {

            System.out.print(TAG+" \ngetAllUsersByTag error \n "+e+"\n");
        }
    });
}

  public void saveUsersAndFriends(Collection<User> usersCollection) {
        System.out.print(TAG+" \nsaveUsersAndFriends usercollection.size() = "+usersCollection.size()+"\n");
        for (User user : usersCollection) {
            saveUser(user);
            saveFriend(user);
        }
    }

private void saveUser(User user) {
        System.out.print("\n "+TAG+"\n ");
        System.out.print("\n saveUser user.getFullName(): "+user.getFullName()+"\n ");
        System.out.print("\n saveUser user.getEmail(): "+user.getEmail()+"\n ");
        System.out.print("\n saveUser user.getLogin(): "+user.getLogin()+"\n ");
        dataManager.getUserDataManager().createOrUpdate(user);
    }

private void saveFriend(User user) {
        System.out.print("\n "+TAG+"\n ");
        System.out.print("\n saveFriend user.getFullName(): "+user.getFullName()+"\n ");
        System.out.print("\n saveFriend user.getEmail(): "+user.getEmail()+"\n ");
        System.out.print("\n saveFriend user.getLogin(): "+user.getLogin()+"\n ");
        dataManager.getFriendDataManager().createOrUpdate(new Friend(user));
    }

 @Override
public void createOrUpdate(Object object) {
    createOrUpdate(object, true);
}

@Override
public void createOrUpdate(Object object, boolean notify) {
    try {
        dao.createOrUpdate((T) object);

        if (notify) {
            notifyObservers(OBSERVE_KEY);
        }
    } catch (SQLException e) {
        ErrorUtils.logError(TAG, "createOrUpdateAll(Object) - " + e.getMessage());
    }
}

private void addChat(){
    boolean isFriends = !dataManager.getFriendDataManager().getAll().isEmpty();
    if (isFriends) {
        NewMessageActivity.start(getActivity());
    } else {
        ToastUtils.longToast(R.string.new_message_no_friends_for_new_message);
    }
}

@Override
public List<T> getAll() {
    try {
        return dao.queryForAll();
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return Collections.emptyList();
}

LogCat

    I/System.out: close [socket][/0.0.0.0:59806]
I/System.out: close [socket][/0.0.0.0:59806]
D/QBASDK: *********************************************************
          *** RESPONSE *** 4c4b0e38-7e30-478e-82bb-128e8a444a74 ***
          STATUS : 201 
          HEADERS
              QuickBlox-REST-API-Version=0.1.1
              X-Runtime=0.033672
              ETag="af85b4c48139e51065492cbc838ea70e"
              Content-Length=265
              X-UA-Compatible=IE=Edge,chrome=1
              X-Request-Id=29433178f986339c198b72c38fbfe1d9
              X-Rack-Cache=invalidate, pass
              Connection=Close
              Server=openresty/1.9.15.1
              QB-Token-ExpirationDate=2016-12-18 17:13:28 UTC
              Cache-Control=max-age=0, private, must-revalidate
              Status=201 Created
              Strict-Transport-Security=max-age=15768000;
              Date=Sun, 18 Dec 2016 15:13:28 GMT
              Access-Control-Allow-Origin=*
              X-Android-Received-Millis=1482074012079
              Content-Type=application/json; charset=utf-8
              X-Android-Sent-Millis=1482074009201
          BODY
              '{"session":{"_id":"5856a798a0eb4782020000a0","application_id":-----,"created_at":"2016-12-18T15:13:28Z","device_id":0,"nonce":-1344848502,"token":"6030858f52decc94c829a1fb6eb6b1058700c124","ts":1482074007,"updated_at":"2016-12-18T15:13:28Z","user_id":0,"id":15185}}'
D/QBASDK: *********************************************************
          *** RESPONSE *** f13457c3-3cc3-4e13-ab64-294f0c8b0897 ***
          STATUS : 200 
          HEADERS
              QuickBlox-REST-API-Version=0.1.1
              X-Runtime=0.094265
              ETag="d36d9f2a6ccd8e24a6da7711ecc56c34"
              Content-Length=3429
              X-UA-Compatible=IE=Edge,chrome=1
              X-Request-Id=6d49abd609df1cf9948948bf9f28dd6f
              X-Rack-Cache=miss
              Connection=Close
              Server=openresty/1.9.15.1
              QB-Token-ExpirationDate=2016-12-18 17:13:22 UTC
              Cache-Control=max-age=0, private, must-revalidate
              Status=200 OK
              Strict-Transport-Security=max-age=15768000;
              Date=Sun, 18 Dec 2016 15:13:28 GMT
              Access-Control-Allow-Origin=*
              X-Android-Received-Millis=1482074012127
              Content-Type=application/json; charset=utf-8
              X-Android-Sent-Millis=1482074009115
          BODY
              '{"current_page":1,"per_page":50,"total_entries":8,"items":[{"user":{"id":20956279,"owner_id":24764,"full_name":"Jeff Dawkin's","email":"jeff@gmail.com","login":"jeff","phone":"09779338","website":null,"created_at":"2016-11-25T14:30:10Z","updated_at":"2016-11-25T14:30:10Z","last_request_at":"2016-11-30T12:36:37Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"twitter_digits_id":null,"user_tags":"Patient"}},{"user":{"id":21477556,"owner_id":24764,"full_name":"Kam Kam","email":"kam@gmail.com","login":"kam","phone":"02713120","website":null,"created_at":"2016-12-09T01:42:24Z","updated_at":"2016-12-09T01:42:24Z","last_request_at":"2016-12-09T01:49:11Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"twitter_digits_id":null,"user_tags":"Patient"}},{"user":{"id":21477783,"owner_id":24764,"full_name":"Jeff Sam","email":"jeff7@gmail.com","login":"jeff7","phone":"45188847","website":null,"created_at":"2016-12-09T01:53:02Z","updated_at":"2016-12-09T01:53:02Z","last_request_at":"2016-12-09T01:53:04Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"twitter_digits_id":null,"user_tags":"Patient"}},{"user":{"id":21477910,"owner_id":24764,"full_name":"Kamara Kam","email":"kam7@gmail.com","login":"kam7","phone":"80858985","website":null,"created_at":"2016-12-09T01:58:38Z","updated_at":"2016-12-09T01:58:38Z","last_request_at":"2016-12-09T01:58:46Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"twitter_digits_id":null,"user_tags":"Patient"}},{"user":{"id":21478496,"owner_id":24764,"full_name":"Dawkin's","email":"dawkins@gmail.com","login":"dawkins","phone":"85452563","website":null,"created_at":"2016-12-09T02:26:36Z","updated_at":"2016-12-09T02:26:36Z","last_request_at":"2016-12-09T11:37:31Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"twitter_digits_id":null,"user_tags":"Patient"}},{"user":{"id":21632254,"owner_id":24764,"full_name":"Kamara Dawkin's","email":"kamdaw7@gmail.com","login":"kamdaw7","phone":"09773655","website":null,"created_at":"2016-12-13T12:01:54Z","updated_at":"2016-12-16T19:28:22Z","last_request_at":"2016-12-15T20:41:20Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":"{}","twitter_digits_id":null,"user_tags":"patient"}},{"user":{"id":21714339,"owner_id":24764,"full_name":"AZERTY","email":"azerty@gmail.com","login":"azerty","phone":"08528523","website":null,"created_at":"2016-12-15T16:59:11Z","updated_at":"2016-12-16T15:47:53Z","last_request_at":"2016-12-15T21:12:30Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":"{\"avatar_url\":\"https:\\/\\/api.quickblox.com\\/blobs\\/7afcff1943294ec4af84b56b060ccd0b00\"}","twitter_digits_id":null,"user_tags":"patient"}},{"user":{"id":21781548,"owner_id":24764,"full_name":"Saturnin Yao","email":"syao@gmail.com","login":"syao","phone":"09197989","website":null,"created_at":"2016-12-17T19:37:55Z","updated_at":"2016-12-17T19:38:36Z","last_request_at":"2016-12-18T15:13:22Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"
I/System.out: MainActivity 
I/System.out: getAllUsersByTag usercollection.size() = 8QBFriendListHelper 
I/System.out: saveUsersAndFriends usercollection.size() = 8
I/System.out:  QBFriendListHelper
I/System.out:  
I/System.out:  saveUser user.getFullName(): Jeff Dawkin's
I/System.out:  
I/System.out:  saveUser user.getEmail(): jeff@gmail.com
I/System.out:  
I/System.out:  saveUser user.getLogin(): jeff
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x412579a8)
W/dalvikvm: threadid=1: uncaught exception occurred
W/System.err: java.lang.NullPointerException
W/System.err:     at com.quickblox.sample.core.qb.helpers.QBFriendListHelper.saveUser(QBFriendListHelper.java:256)
W/System.err:     at com.quickblox.sample.core.qb.helpers.QBFriendListHelper.saveUsersAndFriends(QBFriendListHelper.java:262)
W/System.err:     at com.abi.medecinplus.ui.activities.MainActivity$1.onSuccess(MainActivity.java:122)
W/System.err:     at com.abi.medecinplus.ui.activities.MainActivity$1.onSuccess(MainActivity.java:115)
W/System.err:     at com.quickblox.core.Query.notifySuccess(Query.java:397)
W/System.err:     at com.quickblox.core.Query$VersionEntityCallback.completedWithResponse(Query.java:429)
W/System.err:     at com.quickblox.core.Query.completedWithResponse(Query.java:297)
W/System.err:     at com.quickblox.core.server.HttpRequestRunnable$1.handleMessage(HttpRequestRunnable.java:43)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:107)
W/System.err:     at android.os.Looper.loop(Looper.java:194)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5434)
W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
W/System.err:     at dalvik.system.NativeStart.main(Native Method)
W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
E/AndroidRuntime: FATAL EXCEPTION: main
                  java.lang.NullPointerException
                      at com.quickblox.sample.core.qb.helpers.QBFriendListHelper.saveUser(QBFriendListHelper.java:256)
                      at com.quickblox.sample.core.qb.helpers.QBFriendListHelper.saveUsersAndFriends(QBFriendListHelper.java:262)
                      at com.abi.medecinplus.ui.activities.MainActivity$1.onSuccess(MainActivity.java:122)
                      at com.abi.medecinplus.ui.activities.MainActivity$1.onSuccess(MainActivity.java:115)
                      at com.quickblox.core.Query.notifySuccess(Query.java:397)
                      at com.quickblox.core.Query$VersionEntityCallback.completedWithResponse(Query.java:429)
                      at com.quickblox.core.Query.completedWithResponse(Query.java:297)
                      at com.quickblox.core.server.HttpRequestRunnable$1.handleMessage(HttpRequestRunnable.java:43)
                      at android.os.Handler.dispatchMessage(Handler.java:107)
                      at android.os.Looper.loop(Looper.java:194)
                      at android.app.ActivityThread.main(ActivityThread.java:5434)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:525)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                      at dalvik.system.NativeStart.main(Native Method)
D/SMACK: RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='chat.quickblox.com' id='99f00360-2d60-43f0-917e-c260a63bc9cd' version='1.0' xml:lang='en'>
D/jdwp: processIncoming

tatanka987 commented 7 years ago

hello @dawkinsjeff I can see NullPointerException on QBFriendListHelper in your log, because of this friends are not saved, try to fix this error

dawkinsjeff commented 7 years ago

Hello @tatanka987, i saw the error but I don't know how to solve it. Can you help me please.

dawkinsjeff commented 7 years ago

Here is the Line 256: dataManager.getUserDataManager().createOrUpdate(user);

tatanka987 commented 7 years ago

sorry, but I don't know logic of your app, try to check object friendListHelper on null in method getAllUsersByTag() in onSuccess callback before saving friends, if this object is null try to create new object of friendListHelper

dawkinsjeff commented 7 years ago

@tatanka987 the object is not null and data are shown in logcat but the problem is when trying to save the object on Line 256. dataManager.getUserDataManager().createOrUpdate(user);

tatanka987 commented 7 years ago

you must init friendListHelper after creation for init dataManager or you can init dataManager in constructor

dawkinsjeff commented 7 years ago

@tatanka987 sorry I don't understand your answer can you show me the way to do it please?

tatanka987 commented 7 years ago

@dawkinsjeff after string QBFriendListHelper friendListHelper = new QBFriendListHelper(getApplicationContext()); you must call method friendListHelper.init(privateChatHelper); for getting privateChatHelper you can use next snippet QBPrivateChatHelper privateChatHelper = (QBPrivateChatHelper) service.getHelper(QBService.PRIVATE_CHAT_HELPER); or other code in your app.

ganeshub commented 7 years ago
all users are already friends so I would like to skip the following steps:

send an initation
accept the invitation
save as friend

Other way to say this is, "Someone doesn't need to be a friend to start a Chat, anybody can start conversation with anyone within the app"

I've similar need, Is this possible to do in quickblox ?

ganeshub commented 7 years ago

@dawkinsjeff do you found any solution ? cc: @tatanka987

tatveshwebmigrates commented 6 years ago

anyone there? Someone doesn't need to be a friend to start a Chat, anybody can start conversation with anyone within the app it is possible or not in android??