User management

With the user management API, you can create, delete users, update your profile information, add friends to your profile, remove friends from your profile.

Note that this user management will only work with CometChat v5.7 and custom coded integration.
The users information will be stored in CometChat’s database and not in other user table you have for your website.

To use all these functionalities, you must have CometChat instance which can be obtained as follows:

CometChat cometchat = CometChat.getInstance(context,"API-KEY")

The API key can be obtained from CometChat administration panel. For this, perform the following steps.

  1. login to your CometChat admin
  2. Go to the settings tab, at the bottom you will find the following lines
  3. APIkey

1. Creating user
You can create a new user in CometChat, by providing the essential information.
The syntax is as follows:

createUser(String username, String password, String displayName, String link, File imageFile, Callbacks callbacks)

e.g

cometchat.createUser("James", "mypassword123", "James Mustang", "", file object for image, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) { }

	@Override
	public void failCallback(JSONObject response) { }
    });

The parameters description is as follows:
1. User name : This has to be unique, if you are creating a user whose name already exist then you will get a fail response.
2. Password : Password is mandatory, which can not be empty.
3. Display name : It can be anything and is optional. You can pass “” if don’t want to pass the display name.
4. Link : You can provide any link for your profile, which is also optional.
5. imageFile : You can set avatar for your profile. Pass file object which specifies the avatar to be uploaded for your profile.

Note that you can not pass null imageFile object.

The success response will be as follows:

{
    "status":"1000", "message":"User created successfully!"
}

You will get fail response if the user is already existing with the name you have specified.

{
    "message":"Username already exists.", "code":"1001"
}

2. Removing user
As you can create a user, you can also remove the user from your website. Note that the user will be permanently deleted, so be careful while removing the user. The syntax is as follows:

removeUser(Long USERID, Callbacks callback)

e.g

cometchat.removeUser(60, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {
	}

	@Override
	public void failCallback(JSONObject response) {
	}
});

The success response you will get will be as follows:

{
    "status":"1000", "message":"User deleted successfully!", "data":{"userid":"60"}
}

If removing user fails then you will get following response:

{
    "message":"User not found.", "code":"1003"
}

3. Updating user information
You can update the user information using our update user API. You can change your display name, link, avatar, password. The syntax is as follows:

updateUser(String userID, String password, String displayName, String link, File imageFile, String newPassword, boolean setNull, Callbacks callbacks)

e.g

cometchat.updateUser("60", "123", null, "abc.com", null, null, false, new Callbacks() {

	@Override
	public void successCallback(JSONObject response) { }

	@Override
	public void failCallback(JSONObject response) { }
});

The parameters description is as follows:
1. Password : To update any kind of information, you need to pass your password.
2. Display name : Pass display name when you want to update it.
3. link : This will update your link of profile.
4. imageFile : This parameter will update the your profile avatar.
5. New password : Pass new password when you want to change your password.
6. Set null flag : This is boolean value which specifies following purpose.

If you pass “true” and set “null” or “” to any parameter, then that value will be set to “” i.e that value will reset. In case of avatar, the avatar will be reset to default avatar of your website.
If you pass “false” and set “null” to any value, then that value will not be updated.
The success response will be as follows:

{
    "status":"1000", "message":"User details updated successfully!"
}

Failure response will be as follows:

{
    "message":"Incorrect password!", "code":"1008"
}

4. Adding friends
If you have friends functionality on your website, then you can add friend or list of friends using our API. The syntax is as follows:

addFriends(JSONArray array, Callbacks callback)

e.g

JSONArray friends = new JSONArray();
friends.put(1);
friends.put(2);
friends.put(3);
friends.put(4);
cometchat.addFriends(friends, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {
	}

	@Override
	public void failCallback(JSONObject response) {
				
	}
);

To add friends to your friend list, you need to pass a JSONArray containing the list of friend ID.
You will get the list of friends id in success response which is added as your friends.
Success response will be as follows:

{
    "status":"1000", "message":"Friends added successfully!","data":{"userid":"1,2,4"}
}

The data will contain user id of friends which are added to your friend list if the user id is missing then that friend is not added to your friend list.
Possible failure response will be as follows:

{
    "message":"Failed to add friends!", "data":{"userid":"1,2,3,4"},"code":"1006"
}
 OR
{
    "message": "Invalid user ID", "data":{"userid":"123"}, "code":"1007"
}

5. Removing friends
As you can add friends, you can also unfriend a user or list of users. The syntax is as follows:

removeFriends(JSONArray array, Callbacks callback)

e.g

JSONArray friends = new JSONArray();
friends.put(1);
friends.put(2);
friends.put(3);
friends.put(4);
cometchat.removeFriends(friends, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {

	}

	@Override
	public void failCallback(JSONObject response) {
			
	}
});

To remove friends from your friend list, you need to pass a JSONArray containing the list of friend IDs.
The success response will be as follows:

{
    "status":"1000", "message":"Friends removed successfully!","data":{"userid":"1,2,4"}
}

If the user id is missing from the list then that friend is not removed.
Possible fail response will be as follows:

{
    "message":"Failed to remove friends!","data":{"userid":"1,2,3"},"code":"1002"
}
OR
{
    "message":"Invalid user ID", "data":{"userid":"1"}, "code":"1007"
}

Not finding what you need?

The CometChat team is here to help!