Skip to content

Commit

Permalink
feature (Conversations/Conversations): Management APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Aug 13, 2024
1 parent 83d10a2 commit f411f1f
Show file tree
Hide file tree
Showing 52 changed files with 2,388 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public interface ConversationService {
*/
MessagesService messages();

/**
* Conversations Service instance
*
* @return service instance for project
* @since _NEXT_VERSION_
*/
ConversationsService conversations();

/**
* WebHooksService Service instance
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.sinch.sdk.domains.conversation.api.v1;

import com.sinch.sdk.domains.conversation.models.v1.conversation.Conversation;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRecentRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.CreateConversationRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListRecentResponse;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListResponse;
import com.sinch.sdk.domains.conversation.models.v1.request.MetadataUpdateStrategy;

/**
* Service for working with the conversation log
*
* @see <a
* href="https://developers.sinch.com/docs/conversation/api-reference/conversation/tag/Conversation">online
* documentation</a>
* @since _NEXT_VERSION_
*/
public interface ConversationsService {

/**
* Creates a new empty conversation.
*
* <p>It is generally not needed to create a conversation explicitly since sending or receiving a
* message automatically creates a new conversation if it does not already exist between the given
* app and contact.
*
* <p>Creating empty conversation is useful if the metadata of the conversation should be
* populated when the first message in the conversation is a contact message or the first message
* in the conversation comes out-of-band and needs to be injected with InjectMessage.
*
* @param request Conversation request for creation
* @return Created conversation
* @since _NEXT_VERSION_
*/
Conversation create(CreateConversationRequest request);

/**
* Retrieves a conversation by id. A conversation has two participating entities, an app and a
* contact.
*
* @param conversationId The unique ID of the conversation.
* @return Conversation details
* @since _NEXT_VERSION_
*/
Conversation get(String conversationId);

/**
* This operation lists all conversations that are associated with an app and/or a contact.
*
* @param request Request parameters
* @since _NEXT_VERSION_
*/
ConversationsListResponse list(ConversationsListRequest request);

/**
* This operation lists conversations and their most recent message, ordered by when the most
* recent message was sent for that conversation.
*
* @param request Request parameters
* @since _NEXT_VERSION_
*/
ConversationsListRecentResponse listRecent(ConversationsListRecentRequest request);

/**
* This operation stops the referenced conversation, if the conversation is still active.
*
* <p>A new conversation will be created if a new message is exchanged between the app or contact
* that was part of the stopped conversation.
*
* @param conversationId The unique ID of the conversation.
* @since _NEXT_VERSION_
*/
void stopActive(String conversationId);

/**
* Deletes a conversation together with all the messages sent as part of the conversation.
*
* @param conversationId The unique ID of the conversation.
* @since _NEXT_VERSION_
*/
void delete(String conversationId);

/**
* This operation updates a conversation which can, for instance, be used to update the metadata
* associated with a conversation.
*
* @param request Conversation instance with fields to be updated
* @return Update conversation
*/
Conversation update(
String conversationId, MetadataUpdateStrategy updateStrategy, Conversation request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ConversationService
private AppService app;
private ContactService contact;
private MessagesService messages;
private ConversationsService conversations;

private WebHooksService webhooks;

static {
Expand Down Expand Up @@ -115,6 +117,13 @@ public WebHooksService webhooks() {
return this.webhooks;
}

public ConversationsService conversations() {
if (null == this.conversations) {
this.conversations = new ConversationsService(uriUUID, context, httpClient, authManagers);
}
return this.conversations;
}

static final class LocalLazyInit {

private LocalLazyInit() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.sinch.sdk.domains.conversation.api.v1.adapters;

import com.sinch.sdk.core.http.AuthManager;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.core.http.HttpMapper;
import com.sinch.sdk.core.models.pagination.Page;
import com.sinch.sdk.core.models.pagination.TokenPageNavigator;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.conversation.api.v1.internal.ConversationApi;
import com.sinch.sdk.domains.conversation.models.v1.ConversationChannel;
import com.sinch.sdk.domains.conversation.models.v1.conversation.Conversation;
import com.sinch.sdk.domains.conversation.models.v1.conversation.internal.ListConversationsResponseInternal;
import com.sinch.sdk.domains.conversation.models.v1.conversation.internal.ListConversationsResponseInternalImpl;
import com.sinch.sdk.domains.conversation.models.v1.conversation.internal.ListRecentConversationsResponseInternal;
import com.sinch.sdk.domains.conversation.models.v1.conversation.internal.ListRecentConversationsResponseInternalImpl;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRecentRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRecentRequest.OrderEnum;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.ConversationsListRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.request.CreateConversationRequest;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationRecentMessage;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListRecentResponse;
import com.sinch.sdk.domains.conversation.models.v1.conversation.response.ConversationsListResponse;
import com.sinch.sdk.domains.conversation.models.v1.request.MetadataUpdateStrategy;
import com.sinch.sdk.models.ConversationContext;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

public class ConversationsService
implements com.sinch.sdk.domains.conversation.api.v1.ConversationsService {

private final String uriUUID;
private final ConversationApi api;

public ConversationsService(
String uriUUID,
ConversationContext context,
HttpClient httpClient,
Map<String, AuthManager> authManagers) {
this.uriUUID = uriUUID;
this.api = new ConversationApi(httpClient, context.getServer(), authManagers, new HttpMapper());
}

protected ConversationApi getApi() {
return this.api;
}

public Conversation create(CreateConversationRequest request) {
return getApi().conversationCreateConversation(uriUUID, request);
}

public Conversation get(String conversationId) {
return getApi().conversationGetConversation(uriUUID, conversationId);
}

public ConversationsListResponse list(ConversationsListRequest parameters) {

Boolean onlyActive = parameters.getOnlyActive().orElse(null);
String contactId = parameters.getContactId().orElse(null);
String appId = parameters.getAppId().orElse(null);

Integer pageSize = parameters.getPageSize().orElse(null);
String pageToken = parameters.getPageToken().orElse(null);
ConversationChannel activeChannel = parameters.getActiveChannel().orElse(null);

ListConversationsResponseInternal response =
getApi()
.conversationListConversations(
uriUUID, onlyActive, appId, contactId, pageSize, pageToken, activeChannel);

return mapForPaging(parameters, response);
}

public ConversationsListRecentResponse listRecent(ConversationsListRecentRequest parameters) {

Boolean onlyActive = parameters.getOnlyActive().orElse(null);
String appId = parameters.getAppId().orElse(null);

Integer pageSize = parameters.getPageSize().orElse(null);
String pageToken = parameters.getPageToken().orElse(null);
String order = parameters.getOrder().map(OrderEnum::value).orElse(null);

ListRecentConversationsResponseInternal response =
getApi()
.conversationListRecentConversations(
uriUUID, appId, onlyActive, pageSize, pageToken, order);

return mapForPaging(parameters, response);
}

public void stopActive(String conversationId) {
getApi().conversationStopActiveConversation(uriUUID, conversationId);
}

public void delete(String conversationId) {
getApi().conversationDeleteConversation(uriUUID, conversationId);
}

public Conversation update(
String conversationId, MetadataUpdateStrategy updateStrategy, Conversation request) {
return getApi()
.conversationUpdateConversation(uriUUID, conversationId, request, null, updateStrategy);
}

private ConversationsListResponse mapForPaging(
ConversationsListRequest parameters, ListConversationsResponseInternal _dto) {

ListConversationsResponseInternalImpl dto = (ListConversationsResponseInternalImpl) _dto;

String nextPageToken = dto.nextPageToken().orElse(null);
Collection<Conversation> list = dto.conversations().orElse(Collections.emptyList());

Pair<Collection<Conversation>, TokenPageNavigator> paginated =
new Pair<>(list, new TokenPageNavigator(nextPageToken));

return new ConversationsListResponse(
this, new Page<>(parameters, paginated.getLeft(), paginated.getRight()));
}

private ConversationsListRecentResponse mapForPaging(
ConversationsListRecentRequest parameters, ListRecentConversationsResponseInternal _dto) {

ListRecentConversationsResponseInternalImpl dto =
(ListRecentConversationsResponseInternalImpl) _dto;

String nextPageToken = dto.nextPageToken().orElse(null);
Collection<ConversationRecentMessage> list =
dto.conversations().orElse(Collections.emptyList());

Pair<Collection<ConversationRecentMessage>, TokenPageNavigator> paginated =
new Pair<>(list, new TokenPageNavigator(nextPageToken));

return new ConversationsListRecentResponse(
this, new Page<>(parameters, paginated.getLeft(), paginated.getRight()));
}
}
Loading

0 comments on commit f411f1f

Please sign in to comment.