How to Manage User Connections?
Introduction
In this guide, you'll learn how to manage user connections using the sendConnection and updateConnection methods. These methods facilitate sending connection requests and updating connection statuses, similar to functionality found on platforms like LinkedIn and Instagram. We will cover various scenarios including two-way connections and one-way connections with or without approval.
Prerequisites
Before you begin, ensure the following:
- LikeMinds Feed Flutter SDK: The SDK must be properly installed and initialized in your Flutter project. Refer to the installation guide if needed.
Steps
Step 1: Get an instance of LMFeedClient
final LMFeedClient lmFeedClient = LMFeedCore.client;
Step 2: Send a Connection Request
Sending a connection request allows a user to initiate a connection with another user. This process can be configured for different types of connections, including two-way or one-way connections with or without auto-acceptance. Below are the steps to create and send a connection request based on the type of connections you want to establish.
Two-Way Connection
To initiate a two-way connection request, user X sends a request to user Y. User Y must accept or reject the request. Upon acceptance, both users become connected.
// Create a SendConnectionRequest object using the builder
final SendConnectionRequest sendConnectionRequest = SendConnectionRequestBuilder()
..receiverUUID('y_uuid') // Replace with the UUID of user Y
..connectionType(ConnectionType.twoWay) // Indicates a mutual connection is required
.build();
// Send the connection request
final LMResponse<void> sendConnectionResponse = await lmFeedClient.sendConnection(sendConnectionRequest);
// Process the response
if (sendConnectionResponse.success) {
// Handle successful connection request
handleSendConnectionSuccess(sendConnectionResponse);
} else {
// Handle error message
handleSendConnectionError(sendConnectionResponse.errorMessage);
}One-Way Connection (Auto-Accepted)
In this scenario, user X follows user Y without requiring approval from Y. User X becomes a follower automatically.
// Create a SendConnectionRequest object using the builder
final SendConnectionRequest sendConnectionRequest = SendConnectionRequestBuilder()
..receiverUUID('y_uuid') // Replace with the UUID of user Y
..connectionType(ConnectionType.oneWay) // Indicates a one-way connection
..connectionRequestAutoAccepted(true) // Indicates auto-acceptance
.build();
// Send the connection request
final LMResponse<void> sendConnectionResponse = await lmFeedClient.sendConnection(sendConnectionRequest);
// Process the response
if (sendConnectionResponse.success) {
// Handle successful connection request
handleSendConnectionSuccess(sendConnectionResponse);
} else {
// Handle error message
handleSendConnectionError(sendConnectionResponse.errorMessage);
}One-Way Connection (Approval Required)
User X follows user Y, but Y must approve the request for X to become a follower.
// Create a SendConnectionRequest object using the builder
final SendConnectionRequest sendConnectionRequest = SendConnectionRequestBuilder()
..receiverUUID('y_uuid') // Replace with the UUID of user Y
..connectionType(ConnectionType.oneWay) // Indicates a one-way connection
..connectionRequestAutoAccepted(false) // Indicates approval is required
.build();
// Send the connection request
final LMResponse<void> sendConnectionResponse = await lmFeedClient.sendConnection(sendConnectionRequest);
// Process the response
if (sendConnectionResponse.success) {
// Handle successful connection request
handleSendConnectionSuccess(sendConnectionResponse);
} else {
// Handle error message
handleSendConnectionError(sendConnectionResponse.errorMessage);
}
Step 3: Update a Connection
Once a connection request is sent, the status of the request can be updated based on user actions. This includes accepting or rejecting requests for two-way connections or one-way connections with approval. The steps below show how to build and send an update request to manage the connection status.
Two-Way Connection
When user Y receives a two-way connection request from user X, Y can either accept or reject the request. If accepted, both users will be connected.
// Create an UpdateConnectionRequest object using the builder
final UpdateConnectionRequest updateConnectionRequest = UpdateConnectionRequestBuilder()
..receiverUUID('x_uuid') // Replace with the UUID of user X
..action(ConnectionAction.accept) // Action to be taken by Y
..connectionType(ConnectionType.twoWay) // Indicates a two-way connection
.build();
// Update the connection
final LMResponse<void> updateConnectionResponse = await lmFeedClient.updateConnection(updateConnectionRequest);
// Process the response
if (updateConnectionResponse.success) {
// Handle successful connection update
handleUpdateConnectionSuccess(updateConnectionResponse);
} else {
// Handle error message
handleUpdateConnectionError(updateConnectionResponse.errorMessage);
}One-Way Connection (Approval Required)
For a one-way connection where approval is required, when user Y receives a follow request from user X, Y can accept or reject the request. If accepted, user X becomes a follower.
// Create an UpdateConnectionRequest object using the builder
final UpdateConnectionRequest updateConnectionRequest = UpdateConnectionRequestBuilder()
..receiverUUID('x_uuid') // Replace with the UUID of user X
..action(ConnectionAction.accept) // Action to be taken by Y
..connectionType(ConnectionType.oneWay) // Indicates a one-way connection
.build();
// Update the connection
final LMResponse<void> updateConnectionResponse = await lmFeedClient.updateConnection(updateConnectionRequest);
// Process the response
if (updateConnectionResponse.success) {
// Handle successful connection update
handleUpdateConnectionSuccess(updateConnectionResponse);
} else {
// Handle error message
handleUpdateConnectionError(updateConnectionResponse.errorMessage);
}Reject One-Way Connection (Unfollow)
If user X decides to reject a one-way follow request from user Y, user X will become an unfollower.
// Create an UpdateConnectionRequest object using the builder
final UpdateConnectionRequest updateConnectionRequest = UpdateConnectionRequestBuilder()
..receiverUUID('y_uuid') // Replace with the UUID of user Y
..action(ConnectionAction.reject) // Indicates rejection of the request
..connectionType(ConnectionType.oneWay) // Indicates a one-way connection
.build();
// Update the connection
final LMResponse<void> updateConnectionResponse = await lmFeedClient.updateConnection(updateConnectionRequest);
// Process the response
if (updateConnectionResponse.success) {
// Handle successful connection update
handleUpdateConnectionSuccess(updateConnectionResponse);
} else {
// Handle error message
handleUpdateConnectionError(updateConnectionResponse.errorMessage);
}