User

Identity

Amity SDKs do not store or manage any user data. This means that you do not have to import or migrate existing user profiles into the system, user management should be handled by your application code. Instead, every user is simply represented by a unique userID, which can be any string that uniquely identifies the user and is immutable throughout its lifetime.
A database primary key would make an ideal userID. Conversely, something like username or emails is not recommended as those values may change over time.
If you wish to assign additional permissions for a user, for example, moderation privileges or different sending limits, you can provide an array of roles to assign to this user. Roles are defined in the admin panel and can be tied to an unlimited number of users. Once again, Amity does not store or manage any user data, which means you do not have to import or migrate existing users into the system. It also means that Amity cannot provide user management functionalities like lists of users, or limit actions of certain users (e.g. user permissions). Instead, these functionalities should be handled by the rest of your application's capabilities and your server.

Descriptions of Users

Name
Data Type
Description
Attributes
userId
string
The id of this user
roles
Array.<string>
A list of user's roles
displayName
string
The display name of the user
flagCount
integer
The number of users that have flagged this user
metadata
Object
The metadata of the user
hashFlag
Object
A hash for checking internally if this user was flagged by the user
createdAt
date
The date/time the user was created at
updatedAt
date
The date/time the user was updated at
Though the SDK does not store and should not be responsible for handling User profile data for your application; We do provide tools to make some surface-level queries and searches for existing user accounts. With the help of our AmityUserRepository class, you would be able to list all the users, search for list of users whose display name matches your search query and get AmityUser object from user id.

Create User

You can create a new user into the system with the following code:
1
AmityCoreClient.registerDevice(userId)
2
.displayName(displayName)
3
.build()
4
.submit()
5
.subscribe()
Copied!
  1. 1.
    If user doesn’t exist, it will be created upon calling this method.
  2. 2.
    If displayName or avatarFileId is passed, it will update the user directly.
  3. 3.
    if displayName or avatarFileId is passed, but secured mode is enabled, the values will be ignored.

Delete User

SDK does not have a method for deleting users. You can however ban a user globally. When users are globally banned, they can no longer access Amity's network and will be forcibly removed from all their existing channels.
Refer to our global ban documentation for the instructions on how to global-ban a user.

User Details

Each Amityser consists of a userId and displayName. The userId is immutable once the account is created, however, the displayName can be updated at all times.
Below is a sample code for fetching a single user:
1
val userId = "alex123"
2
userRepository.getUser(userId)
3
.doOnNext { user ->
4
val displayname = user.getDisplayName()
5
}
6
.subscribe()
Copied!

User repository

To get AmityUserRepository instance
1
val userRepository = AmityCoreClient.newUserRepository()
Copied!

Query User

AmityUserRepository provides a convenient method getUsers() to fetch all users. You can observe for changes in collection, similar to message or channel. The method accepts AmityUserSortOption as an optional parameter. The list can be sorted by displayName, firstCreated or lastCreated.
1
userRepository.getUsers()
2
.sortBy(sortOption) // optional
3
.build()
4
.query()
5
.subscribe( { adpater.submitList(it) } )
Copied!
AmityUserRepository provides searchUserByDisplayName() method which allows you to query for users using their display name. It provides you with a LiveCollection of AmityUser whose display name matches your search query. AmityUserSortOption is an optional parameter.
1
userRepository.searchUserByDisplayName("John")
2
.sortBy(sortOption) // optional
3
.build()
4
.query()
5
.subscribe( { adpater.submitList(it) } )
Copied!
The code above will provide you with the list of users which matches with display name "John".

Update User Information

You can update information related to current user such as display name, avatar, user description, metadata, etc.
1
AmityCoreClient.updateUser()
2
.avatar(AmityImage)
3
.description(String)
4
.displayName(String)
5
.metadata(JsonObject)
6
.build()
7
.update()
8
.subscribe()
Copied!

User Avatar

You can upload an image and set it as an avatar for the current user. First, you need to upload image and then update the image info.
1
// upload image
2
AmityCoreClient.newFileRepository()
3
.uploadImage(Uri)
4
.isFullImage(Boolean)
5
.build()
6
.transfer()
7
.doOnNext { amityResult ->
8
when(amityResult) {
9
is AmityUploadResult.Complete -> {
10
val uploadedImage : AmityImage = amityResult.getFile()
11
}
12
is AmityUploadResult.Error -> {
13
// handle upload error
14
}
15
16
}
17
}.subscribe()
18
19
// update user profile image
20
val image : AmityImage = … // the uploaded Image
21
AmityCoreClient.updateUser()
22
.avatar(image)
23
.build()
24
.update()
25
.subscribe()
Copied!

AmityUserSortOption

has 3 enums type 1. AmityUserSortOption.DISPLAYNAME Sort by displayName 2. AmityUserSortOption.FIRST_CREATED Sort by firstCreated 3. AmityUserSortOption.LAST_CREATED Sort by lastCreated
Last modified 19d ago