Follow
Description
Following feature allows users to follow other users they are interested in, so they can timely receive the latest news, feeds, or event information of these users. The system can provide personalized content recommendations based on the user's following list.
Followers feature refers to the state of a user being followed by others. When user A follows user B, A becomes a follower of B. Users can view the count of followers, followers list, or profile of followers on their own profile page.
Through the features, you can create an active, interconnected user network, promoting the spread of information and the building of the community.
Note:
This feature is available only in SDK enhanced edition v7.8 or later.
This feature is only supported by the premium edition. Please purchase the premium edition to use it.
Display Effect
API Description
Follow user
Sample code:
List<String> userIDList = Arrays.asList("useridA", "useridB");V2TIMManager.getFriendshipManager().followUser
(userIDList, new V2TIMValueCallback<List<V2TIMFollowOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMFollowOperationResult> followOperationResultList) {// Follow users successfully}@Overridepublic void onError(int code, String desc) {// Failed to follow users}});
NSArray *useridList = @[@"useridA", @"useridB"];[[V2TIMManager sharedInstance] followUser:userIDList succ:^(NSArray<V2TIMFollowOperationResult *> *resultList) {// Follow users succeeded} fail:^(int code, NSString *desc) {// Failed to follow users}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMStringVector userIDList;userIDList.PushBack(u8"useridA");userIDList.PushBack(u8"useridB");auto callback = new ValueCallback<V2TIMFollowOperationResultVector>{};callback->SetCallback([=](const V2TIMFollowOperationResultVector& followOperationResultList) {// Follow users successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to follow usersdelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->FollowUser(userIDList, callback);
Note:
This API supports following up to 20 users at a time (excluding yourself, you can follow any other user).
The maximum number of followers per user is 5000, and there is no limit on the number of fan users.
Unfollow user
Sample code:
List<String> userIDList = Arrays.asList("useridA", "useridB");V2TIMManager.getFriendshipManager().unfollowUser
(userIDList, new V2TIMValueCallback<List<V2TIMFollowOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMFollowOperationResult> followOperationResultList) {// Unfollow users successfully}@Overridepublic void onError(int code, String desc) {// Failed to unfollow users}});
NSArray *useridList = @[@"useridA", @"useridB"];[[V2TIMManager sharedInstance]unfollowUser
:userIDList succ:^(NSArray<V2TIMFollowOperationResult *> *resultList) {// Unfollow users successfully} fail:^(int code, NSString *desc) {// Failed to unfollow users}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMStringVector userIDList;userIDList.PushBack(u8"useridA");userIDList.PushBack(u8"useridB");auto callback = new ValueCallback<V2TIMFollowOperationResultVector>{};callback->SetCallback([=](const V2TIMFollowOperationResultVector& followOperationResultList) {// Unfollow users successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to unfollow usersdelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->UnfollowUser
(userIDList, callback);
Note:
This API supports unfollowing up to 20 users at a time.
Get my following list
You can use the
getMyFollowingList
API (Android / iOS & Mac / Windows) to pull the list of users you are following.Sample code:
{...String nextCursor = "";getMyFollowingList(nextCursor);...}public void getMyFollowingList(String nextCursor) {V2TIMManager.getFriendshipManager().getMyFollowingList(nextCursor, new V2TIMValueCallback<V2TIMUserInfoResult>() {@Overridepublic void onSuccess(V2TIMUserInfoResult v2TIMUserInfoResult) {StringBuilder stringBuilder = new StringBuilder();// Continue with the paged pullif (v2TIMUserInfoResult.getNextCursor().length() > 0) {// Continue paged pullgetMyFollowingList(v2TIMUserInfoResult.getNextCursor());...} else {// Pull complete}}@Overridepublic void onError(int code, String desc) {// Failed to pull}});}
- (void)getMyFollowingList:(NSString *)cursor {[[V2TIMManager sharedInstance] getMyFollowingList:cursor succ:^(NSString *nextCursor, NSArray<V2TIMUserFullInfo *> *userInfoList) {if (nextCursor.length > 0) {// Continue with the paged pull[self getMyFollowingList:nextCursor];//...} else {// Pull complete}} fail:^(int code, NSString *desc) {// Failed to pull}];}
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString nextCursor = u8"";void GetMyFollowingList(const V2TIMString& nextCursor) {auto callback = new ValueCallback<V2TIMUserInfoResult>{};callback->SetCallback([=](const V2TIMUserInfoResult& userInfoResult) {if (userInfoResult.nextCursor.Length() > 0) {// Continue with the paged pullGetMyFollowingList(userInfoResult.nextCursor);...} else {// Pull complete}delete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to pulldelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->GetMyFollowingList(nextCursor, callback);}
Note:
This API returns a maximum of 500 users at a time.
When pulling the API for the first time, you can enter the nextCursor parameter of the API as "". If the returned nextCursor is not "" after the callback is successful, you can pass this value and pull it again until nextCursor returns "", indicating that the pull is complete.
Get my followers list
You can use the
getMyFollowersList
API (Android / iOS & Mac / Windows) to fetch your own list of follower users.Sample code:
{...String nextCursor = "";getMyFollowersList
(nextCursor);...}public voidgetMyFollowersList
(String nextCursor) {V2TIMManager.getFriendshipManager().getMyFollowersList
(nextCursor, new V2TIMValueCallback<V2TIMUserInfoResult>() {@Overridepublic void onSuccess(V2TIMUserInfoResult v2TIMUserInfoResult) {StringBuilder stringBuilder = new StringBuilder();if (v2TIMUserInfoResult.getNextCursor().length() > 0) {// Continue with the paged pullgetMyFollowersList
(v2TIMUserInfoResult.getNextCursor());...} else {// Pull complete}}@Overridepublic void onError(int code, String desc) {// Failed to pull}});}
- (void)getMyFollowersList
:(NSString *)cursor {[[V2TIMManager sharedInstance]getMyFollowersList
:cursor succ:^(NSString *nextCursor, NSArray<V2TIMUserFullInfo *> *userInfoList) {if (nextCursor.length > 0) {// Continue with the paged pull[selfgetMyFollowersList
:nextCursor];//...} else {// Pull complete}} fail:^(int code, NSString *desc) {// Failed to pull}];}
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString nextCursor = u8"";void GetMyFollowersList(const V2TIMString& nextCursor) {auto callback = new ValueCallback<V2TIMUserInfoResult>{};callback->SetCallback([=](const V2TIMUserInfoResult& userInfoResult) {if (userInfoResult.nextCursor.Length() > 0) {// Continue with the paged pullGetMyFollowersList(userInfoResult.nextCursor);...} else {// Pull complete}delete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to pulldelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->GetMyFollowersList(nextCursor, callback);}
Note:
This API returns a maximum of 500 users at a time.
For the first fetch, you can fill the
nextCursor
parameter of the API with ""
. After a successful callback, if the returned nextCursor
is not ""
, you can input this value to fetch again, until nextCursor
returns ""
, indicating that the fetch is complete.Get mutual followers list
You can call the
getMutualFollowersList
API (Android / iOS & Mac / Windows) to pull the list of users who mutually follow each other (Users who mutually follow can find each other in their following and followers lists).Sample code:
{...String nextCursor = "";getMutualFollowersList(nextCursor);...}public void getMutualFollowersList(String nextCursor) {V2TIMManager.getFriendshipManager().getMutualFollowersList(nextCursor, new V2TIMValueCallback<V2TIMUserInfoResult>() {@Overridepublic void onSuccess(V2TIMUserInfoResult v2TIMUserInfoResult) {StringBuilder stringBuilder = new StringBuilder();if (v2TIMUserInfoResult.getNextCursor().length() > 0) {// Continue with the paged pullgetMutualFollowersList(v2TIMUserInfoResult.getNextCursor());...} else {// Pull complete}}@Overridepublic void onError(int code, String desc) {// Failed to pull}});}
- (void)getMutualFollowersList:(NSString *)cursor {[[V2TIMManager sharedInstance] getMutualFollowersList:cursor succ:^(NSString *nextCursor, NSArray<V2TIMUserFullInfo *> *userInfoList) {if (nextCursor.length > 0) {// Continue with the paged pull[self getMutualFollowersList:nextCursor];//...} else {// Pull complete}} fail:^(int code, NSString *desc) {// Failed to pull}];}
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMString nextCursor = u8"";void GetMutualFollowersList(const V2TIMString& nextCursor) {auto callback = new ValueCallback<V2TIMUserInfoResult>{};callback->SetCallback([=](const V2TIMUserInfoResult& userInfoResult) {if (userInfoResult.nextCursor.Length() > 0) {// Continue with the paged pullGetMutualFollowersList(userInfoResult.nextCursor);...} else {// Pull complete}delete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to pulldelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->GetMutualFollowersList(nextCursor, callback);}
Note:
This API returns a maximum of 500 users at a time.
For the first fetch, you can fill the
nextCursor
parameter of the API with ""
. After a successful callback, if the returned nextCursor
is not ""
, you can input this value to fetch again, until nextCursor
returns ""
, indicating that the fetch is complete.Get the count of following/followers/mutual followers
You can call the
getUserFollowInfo
API (Android / iOS & Mac / Windows) to get the count of following/followers/mutual followers for specific users.In the successful callback, you can get the count of following, followers, and mutual followers of a specific user from
followingCount
, followersCount
, and mutualFollowersCount
of V2TIMFollowInfo
(Android / iOS & Mac / Windows) .Sample code:
List<String> userIDList = Arrays.asList("useridA", "useridB");V2TIMManager.getFriendshipManager().getUserFollowInfo(userIDList, new V2TIMValueCallback<List<V2TIMFollowInfo>>() {@Overridepublic void onSuccess(List<V2TIMFollowInfo> followInfoList) {// Obtained successfullyfor (V2TIMFollowInfo followInfo : V2TIMFollowInfoList) {if (followInfo.getResultCode() == 0) {// Here, you can get the count of following/followers/mutual followers corresponding to the user, followInfo.getUserID().// That is, followInfo.getFollowingCount(), followInfo.getFollowersCount() and followInfo.getMutualFollowersCount().}}}@Overridepublic void onError(int code, String desc) {// Failed to obtain}});
NSArray *useridList = @[@"useridA", @"useridB"];[[V2TIMManager sharedInstance] getUserFollowInfo:userIDList succ:^(NSArray<V2TIMFollowInfo *> *resultList) {// Obtained successfullyfor (V2TIMFollowInfo *result in resultList) {if (result.resultCode == 0) {// Here, you can get the count of following/followers/mutual followers corresponding to the user, result.userID.// That is, result.followingCount, result.followersCount, and result.mutualFollowersCount.}}} fail:^(int code, NSString *desc) {// Failed to obtain}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMStringVector userIDList;userIDList.PushBack(u8"useridA");userIDList.PushBack(u8"useridB");auto callback = new ValueCallback<V2TIMFollowInfoVector>{};callback->SetCallback([=](const V2TIMFollowInfoVector& followInfoList) {// Obtained successfullyfor (size_t i = 0; i < followInfoList.Size(); ++i) {if (followInfoList[i].resultCode == 0) {// Here, you can get the count of following/followers/mutual followers for the user, followInfoList[i].userID.// That is, followInfoList[i].followingCount, followInfoList[i].followersCount, and followInfoList[i].mutualFollowersCount.}}delete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to obtaindelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->GetUserFollowInfo
(userIDList, callback);
Check follow type
You can call the
checkFollowType
API (Android / iOS & Mac / Windows) to check your following relationship with specified users.In the successful callback, you can obtain your relationship with the specified user through the
followType
of V2TIMFollowTypeCheckResult
(Android / iOS & Mac / Windows):V2TIMFollowTypeCheckResult.followType | Relationship with yourself |
V2TIM_FOLLOW_TYPE_NONE | No following relationship |
V2TIM_FOLLOW_TYPE_IN_MY_FOLLOWING_LIST | The user is only in my following list |
V2TIM_FOLLOW_TYPE_IN_MY_FOLLOWERS_LIST | The user is only in my followers list |
V2TIM_FOLLOW_TYPE_IN_BOTH_FOLLOWERS_LIST | Mutually follow with the user (the user is both in my following list and in my followers list) |
Sample code:
List<String> userIDList = Arrays.asList("useridA", "useridB");V2TIMManager.getFriendshipManager().checkFollowType(userIDList, new V2TIMValueCallback<List<V2TIMFollowTypeCheckResult>>() {@Overridepublic void onSuccess(List<V2TIMFollowTypeCheckResult> followTypeCheckResultList) {// Successfully checked the follow relationshipfor (V2TIMFollowTypeCheckResult followTypeCheckResult : followTypeCheckResultList) {if (followTypeCheckResult.getResultCode() == 0) {// Here, you can get the relationship with the user followTypeCheckResult.getUserID(), the follow type followTypeCheckResult.getFollowType()}}}@Overridepublic void onError(int code, String desc) {// Failed to check the follow relationship}});
NSArray *useridList = @[@"useridA", @"useridB"];[[V2TIMManager sharedInstance] checkFollowType:userIDList succ:^(NSArray<V2TIMFollowTypeCheckResultListSucc *> *resultList) {// Successfully checked the follow relationshipfor (V2TIMFollowTypeCheckResult *result in resultList) {if (result.resultCode == 0) {// Here, you can get the relationship with the user result.userID, the follow type result.followType}}} fail:^(int code, NSString *desc) {// Failed to check the follow relationship}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};V2TIMStringVector userIDList;userIDList.PushBack(u8"useridA");userIDList.PushBack(u8"useridB");auto callback = new ValueCallback<V2TIMFollowTypeCheckResultVector>{};callback->SetCallback([=](const V2TIMFollowTypeCheckResultVector& followTypeCheckResultList) {// Successfully checked the follow relationshipfor (size_t i = 0; i < followTypeCheckResultList.Size(); ++i) {if (followTypeCheckResultList[i].resultCode == 0) {// Here, you can get the relationship with the user followTypeCheckResultList[i].userID, the follow type followTypeCheckResultList[i].followType}}delete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to check the follow relationshipdelete callback;});V2TIMManager::GetInstance()->GetFriendshipManager()->FollowUser(userIDList, callback);
Note:
The frequency limit of
checkFollowType
API is up to 20 calls within 5 seconds, and each call can support up to 100 users.Notifications
The specific list change notifications are as follows:
List Change Notification | Description |
When the isAdd in the notification is true , it signifies a notification for adding a user to the list, and at this time, the complete user profile is returnedWhen the isAdd in the notification is false , it signifies a notification for deleting a user from the list, and at this time, only the user ID is included in the returned user profile | |
| |
Mutual Followers List Change Notification onMutualFollowersListChanged | |
Note:
To receive notifications for the aforementioned events, please call
addFriendListener
(Android / iOS and macOS / Windows) beforehand to add a relationship event listener.Notifications of adding users to a list
Suppose there are two users
Alice
and Bob
. During the process of mutual following, the related list change notifications and relationship changes are as follows:Event | Alice | Bob | ||
| Notifications received | Relationship with Bob | Notifications received | Relationship with Alice |
Alice follows Bob | ||||
Bob follows Alice | |
Notifications of deleting users from a list
Assuming there are two users,
Alice
and Bob
, who are mutual followers, during the process of mutually unfollowing, the related list change notifications and relationship changes are as follows:Event | Alice | Bob | ||
| Notifications received | Relationship with Bob | Notifications received | Relationship with Alice |
Alice unfollows Bob | | | ||
Bob unfollows Alice |
Sample code:
V2TIMFriendshipListener v2TIMFriendshipListener = new V2TIMFriendshipListener() {@Overridepublic void onMyFollowingListChanged(List<V2TIMUserFullInfo> userInfoList, boolean isAdd) {if (isAdd) {// Receive notification of users added to following list} else {// Receive notification of users deleted from following list}}@Overridepublic void onMyFollowersListChanged(List<V2TIMUserFullInfo> userInfoList, boolean isAdd) {if (isAdd) {// Receive notification of users added to followers list} else {// Receive notification of users deleted from followers list}}@Overridepublic void onMutualFollowersListChanged(List<V2TIMUserFullInfo> userInfoList, boolean isAdd) {if (isAdd) {// Receive notification of users added to mutual followers list} else {// Receive notification of users deleted from mutual follow list}}};// Add a relationship chain listenerV2TIMManager.getFriendshipManager().addFriendListener(v2TIMFriendshipListener);
// Add a relationship event listener[[V2TIMManager sharedInstance] addFriendListener:self];- (void)onMyFollowingListChanged:(NSArray<V2TIMUserFullInfo *> *)userInfoList isAdd:(BOOL)isAdd {if (isAdd) {// Receive notification of users added to following list} else {// User deletion notification from following list}}- (void)onMyFollowersListChanged:(NSArray<V2TIMUserFullInfo *> *)userInfoList isAdd:(BOOL)isAdd {if (isAdd) {// Receive notification of users added to followers list} else {// Receive notification of users deleted from followers list}}- (void)onMutualFollowersListChanged:(NSArray<V2TIMUserFullInfo *> *)userInfoList isAdd:(BOOL)isAdd {if (isAdd) {// Receive notification of users added to mutual followers list} else {// Receive notification of users deleted from mutual followers list}}
class FriendshipListener final : public V2TIMFriendshipListener {public:FriendshipListener() = default;~FriendshipListener() override = default;void OnMyFollowingListChanged(const V2TIMUserFullInfoVector &userInfoList, bool isAdd) override {if (isAdd) {// Receive notification of users added to following list} else {// Receive notification of users deleted from following list}}void OnMyFollowersListChanged(const V2TIMUserFullInfoVector &userInfoList, bool isAdd) override {if (isAdd) {// Receive notification of users added to followers list} else {// Receive notification of users deleted from followers list}}void OnMutualFollowersListChanged(const V2TIMUserFullInfoVector &userInfoList, bool isAdd) override {if (isAdd) {// Receive notification of users added to mutual followers list} else {// Receive notification of users deleted from mutual followers list}}// Other member functions ...};// Add a relationship event listener. Note that the lifespan of the friendshipListener must be maintained before removing the listener to ensure event callbacks are receivedFriendshipListener friendshipListener;V2TIMManager::GetInstance()->GetFriendshipManager()->AddFriendListener(&friendshipListener);
Contact Us
If you have any questions about this article, feel free to join the Telegram Technical Group, where you will receive reliable technical support.