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.
To use all these functionality you must have instance of CometChatCloud, which can be obtained as follows:

CometChatCloud cloud = CometChatCloud.getInstance(context)

Once you have cloud object you can access the cloud APIs.

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

createUser(String UID, String USERNAME, String PASSWORD, String DISPLAY_NAME, String URL_OF_LINK, String URL_OF_AVATAR, Callbacks callback)

e.g

cloud.createUser("","James", "123**", "James Smith", "", "http://www.abc.com/image.jpg", new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {
	}

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

The parameters description is as follows:
1. UID : Pass UID if you are creating user from your CMS, else “” for custom coded integration.
2. User name : This has to be unique, if you are creating user whose name already exist then you will get fail response.
3. Password : Password is mandatory, which can not be empty.
4. Display name : It can be anything and is optional you can pass “” if don’t want to pass the display name.
5. Link : You can provide any link for your profile, which is also optional.
6. Avatar : You can provide link/url of your avatar. If you keep this parameter “” then default avatar will be set for the profile which will be your site specific.

All the optional parameters can be updated by using our “Update user” API.
The success response will be as follows:

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

The field data will contain the userid of newly created user.
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(String USERNAME, Callbacks callback)
removeUser(Long USERID, Callbacks callback)

e.g

cloud.removeUser("James", new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {
	}

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

You just need to pass the username of user to whom you want to remove, if username is correct the user will be removed, else you will receive fail response.

You can also remove user by using “ID” of user.

cloud.removeUser(15, 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, status, status message. The syntax is as follows:

updateUser(String PASSWORD ,String DISPLAY_NAME, String LINK, String AVATAR_LINK , String NEW_PASSWORD,StatusOption OPTION, String STATUS_MESSAGE ,Boolean flag, Callbacks callback)

e.g

cloud.updateUser(“123**”,”James L Smith”,””,”http://www.abc.com/image.jpg”,”123**”,StatusOption.AVAILABLE,”Wonderful!”,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. Avatar link : This parameter will update the avatar link.
5. New password : Pass new password when you want to change your password.
6. Status option : You can choose your status by using “StatusOption” enum. The available status are “AVAILABLE”, “INVISIBLE”, “OFFLINE”, “BUSY”.
7. Status message : You can set your status message as you wish.
8. Set null flag : This is boolean value which specifies following purpose.

If you pass “true” and set “null” or “” to any parameter, then it 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, for the status message, it will set to “I’m available”.
If you pass “false” and set “null” to any value, then that value will not be updated.

So if you want to update your avatar then you can use update user api as follows:

cloud.updateUser("password",null,null,"new avatar link",null,Status option,null,false, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {

	}

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

If you want to reset your avatar to default one then you can do as follows:

cloud.updateUser("password","display name","link",null or "" ,null,Status option,"Status message",true, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {

	}

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

Note that the value for parameter “New password” will not affect in any of the “set null flag” condition if you pass null or “”. The password will only change when it is provided.
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 cloud 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);
cloud.addFriends(friends, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {

	}

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

To add friends to your friends list, you need to pass a JSONArray containing the list of friend ID.
You will get list of friends id in success response which are 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);
cloud.removeFriends(friends, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {

	}

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

To remove friends from your friends 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!