Search in sources :

Example 1 with CommunicationUserIdentifier

use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.

the class MainActivity method createChatThreadAsyncClient.

public void createChatThreadAsyncClient() {
    if (chatAsyncClient == null) {
        createChatAsyncClient();
    }
    // A list of participants to start the thread with.
    List<ChatParticipant> participants = new ArrayList<>();
    // The display name for the thread participant.
    String displayName = "First participant";
    participants.add(new ChatParticipant().setCommunicationIdentifier(new CommunicationUserIdentifier(firstUserId)).setDisplayName(displayName));
    // The topic for the thread.
    final String topic = "General";
    // Optional, set a repeat request ID.
    final String repeatabilityRequestID = "";
    // Options to pass to the create method.
    CreateChatThreadOptions createChatThreadOptions = new CreateChatThreadOptions().setTopic(topic).setParticipants(participants).setIdempotencyToken(repeatabilityRequestID);
    try {
        CreateChatThreadResult createChatThreadResult = chatAsyncClient.createChatThread(createChatThreadOptions).get();
        ChatThreadProperties chatThreadProperties = createChatThreadResult.getChatThreadProperties();
        threadId = chatThreadProperties.getId();
        logAndToast("Created thread with ID: " + threadId);
        chatThreadAsyncClient = chatAsyncClient.getChatThreadClient(threadId);
        Log.d(TAG, "Created ChatThreadAsyncClient");
    } catch (InterruptedException | ExecutionException e) {
        Log.e("ChatThreadAsyncClient creation failed", Objects.requireNonNull(e.getMessage()));
    }
}
Also used : CreateChatThreadResult(com.azure.android.communication.chat.models.CreateChatThreadResult) ArrayList(java.util.ArrayList) ChatParticipant(com.azure.android.communication.chat.models.ChatParticipant) ChatThreadProperties(com.azure.android.communication.chat.models.ChatThreadProperties) CreateChatThreadOptions(com.azure.android.communication.chat.models.CreateChatThreadOptions) ExecutionException(java.util.concurrent.ExecutionException) CommunicationUserIdentifier(com.azure.android.communication.common.CommunicationUserIdentifier)

Example 2 with CommunicationUserIdentifier

use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.

the class MainActivity method registerPushNotificationListener.

public void registerPushNotificationListener(View view) {
    logAndToast("Register push notification listeners");
    try {
        chatAsyncClient.addPushNotificationHandler(CHAT_MESSAGE_RECEIVED, (ChatEvent payload) -> {
            Log.i(TAG, "Push Notification CHAT_MESSAGE_RECEIVED.");
            ChatMessageReceivedEvent event = (ChatMessageReceivedEvent) payload;
            Log.i(TAG, "Message received!" + " ThreadId: " + event.getChatThreadId() + " MessageId: " + event.getId() + " Content: " + event.getContent() + " Priority: " + event.getPriority() + " SenderDisplayName: " + event.getSenderDisplayName() + " SenderMri: " + ((CommunicationUserIdentifier) event.getSender()).getId() + " Version: " + event.getVersion() + " CreatedOn: " + event.getCreatedOn() + " Type: " + event.getType() + " RecipientMri: " + ((CommunicationUserIdentifier) event.getRecipient()).getId());
        });
        chatAsyncClient.addPushNotificationHandler(CHAT_MESSAGE_EDITED, (ChatEvent payload) -> {
            Log.i(TAG, "Push Notification CHAT_MESSAGE_EDITED.");
            ChatMessageEditedEvent event = (ChatMessageEditedEvent) payload;
            Log.i(TAG, "Message edited!" + " ThreadId: " + event.getChatThreadId() + " MessageId: " + event.getId() + " Content: " + event.getContent() + " SenderDisplayName: " + event.getSenderDisplayName() + " SenderMri: " + ((CommunicationUserIdentifier) event.getSender()).getId() + " Version: " + event.getVersion() + " CreatedOn: " + event.getCreatedOn() + " EditedOn: " + event.getEditedOn() + " RecipientMri: " + ((CommunicationUserIdentifier) event.getRecipient()).getId());
        });
        chatAsyncClient.addPushNotificationHandler(CHAT_MESSAGE_DELETED, (ChatEvent payload) -> {
            Log.i(TAG, "Push Notification CHAT_MESSAGE_DELETED.");
            ChatMessageDeletedEvent event = (ChatMessageDeletedEvent) payload;
            Log.i(TAG, "Message deleted!" + " ThreadId: " + event.getChatThreadId() + " MessageId: " + event.getId() + " SenderDisplayName: " + event.getSenderDisplayName() + " SenderMri: " + ((CommunicationUserIdentifier) event.getSender()).getId() + " Version: " + event.getVersion() + " CreatedOn: " + event.getCreatedOn() + " DeletedOn: " + event.getDeletedOn() + " RecipientMri: " + ((CommunicationUserIdentifier) event.getRecipient()).getId());
        });
        chatAsyncClient.addPushNotificationHandler(CHAT_THREAD_CREATED, (ChatEvent payload) -> {
            Log.i(TAG, "Push Notification CHAT_THREAD_CREATED.");
            ChatThreadCreatedEvent event = (ChatThreadCreatedEvent) payload;
            Log.i(TAG, "Thread Created!" + " ThreadId: " + event.getChatThreadId() + " Properties_Id: " + event.getProperties().getId() + " Properties_Topic: " + event.getProperties().getTopic() + " Properties_CreatedOn: " + event.getProperties().getCreatedOn() + " Properties_CreatedByMri: " + ((CommunicationUserIdentifier) event.getProperties().getCreatedByCommunicationIdentifier()).getId() + " Participants_size: " + event.getParticipants().size() + " Version: " + event.getVersion() + " CreatedOn: " + event.getCreatedOn() + " CreatedBy_DisplayName: " + event.getCreatedBy().getDisplayName() + " CreatedBy_Mri: " + ((CommunicationUserIdentifier) event.getCreatedBy().getCommunicationIdentifier()).getId());
        });
        chatAsyncClient.addPushNotificationHandler(CHAT_THREAD_PROPERTIES_UPDATED, (ChatEvent payload) -> {
            Log.i(TAG, "Push Notification CHAT_THREAD_PROPERTIES_UPDATED.");
            ChatThreadPropertiesUpdatedEvent event = (ChatThreadPropertiesUpdatedEvent) payload;
            Log.i(TAG, "Thread Updated!" + " ThreadId: " + event.getChatThreadId() + " Properties_Id: " + event.getProperties().getId() + " Properties_Topic: " + event.getProperties().getTopic() + " Version: " + event.getVersion() + " UpdatedOn: " + event.getUpdatedOn() + " UpdatedBy_DisplayName: " + event.getUpdatedBy().getDisplayName() + " UpdatedBy_Mri: " + ((CommunicationUserIdentifier) event.getUpdatedBy().getCommunicationIdentifier()).getId());
        });
        chatAsyncClient.addPushNotificationHandler(CHAT_THREAD_DELETED, (ChatEvent payload) -> {
            Log.i(TAG, "Push Notification CHAT_THREAD_DELETED.");
            ChatThreadDeletedEvent event = (ChatThreadDeletedEvent) payload;
            Log.i(TAG, "Thread Deleted!" + " ThreadId: " + event.getChatThreadId() + " Version: " + event.getVersion() + " DeletedOn: " + event.getDeletedOn() + " DeletedBy_DisplayName: " + event.getDeletedBy().getDisplayName() + " DeletedBy_Mri: " + ((CommunicationUserIdentifier) event.getDeletedBy().getCommunicationIdentifier()).getId());
        });
        chatAsyncClient.addPushNotificationHandler(PARTICIPANTS_ADDED, (ChatEvent payload) -> {
            Log.i(TAG, "Push Notification PARTICIPANTS_ADDED.");
            ParticipantsAddedEvent event = (ParticipantsAddedEvent) payload;
            Log.i(TAG, "Participant Added!" + " ThreadId: " + event.getChatThreadId() + " ParticipantsAdded_size: " + event.getParticipantsAdded().size() + " Version: " + event.getVersion() + " AddedOn: " + event.getAddedOn() + " AddedBy_DisplayName: " + event.getAddedBy().getDisplayName() + " AddedBy_Mri: " + ((CommunicationUserIdentifier) event.getAddedBy().getCommunicationIdentifier()).getId());
        });
        chatAsyncClient.addPushNotificationHandler(PARTICIPANTS_REMOVED, (ChatEvent payload) -> {
            Log.i(TAG, "Push Notification PARTICIPANTS_REMOVED.");
            ParticipantsRemovedEvent event = (ParticipantsRemovedEvent) payload;
            Log.i(TAG, "Participant Removed!" + " ThreadId: " + event.getChatThreadId() + " ParticipantsRemoved_size: " + event.getParticipantsRemoved().size() + " Version: " + event.getVersion() + " RemovedOn: " + event.getRemovedOn() + " RemovedBy_DisplayName: " + event.getRemovedBy().getDisplayName() + " RemovedBy_Mri: " + ((CommunicationUserIdentifier) event.getRemovedBy().getCommunicationIdentifier()).getId());
        });
    } catch (IllegalStateException error) {
        Log.i(TAG, "Push Notification not start yet.");
    }
}
Also used : ChatThreadDeletedEvent(com.azure.android.communication.chat.models.ChatThreadDeletedEvent) ChatThreadPropertiesUpdatedEvent(com.azure.android.communication.chat.models.ChatThreadPropertiesUpdatedEvent) ChatEvent(com.azure.android.communication.chat.models.ChatEvent) ChatMessageEditedEvent(com.azure.android.communication.chat.models.ChatMessageEditedEvent) ChatMessageDeletedEvent(com.azure.android.communication.chat.models.ChatMessageDeletedEvent) ParticipantsRemovedEvent(com.azure.android.communication.chat.models.ParticipantsRemovedEvent) ParticipantsAddedEvent(com.azure.android.communication.chat.models.ParticipantsAddedEvent) ChatMessageReceivedEvent(com.azure.android.communication.chat.models.ChatMessageReceivedEvent) CommunicationUserIdentifier(com.azure.android.communication.common.CommunicationUserIdentifier) ChatThreadCreatedEvent(com.azure.android.communication.chat.models.ChatThreadCreatedEvent)

Example 3 with CommunicationUserIdentifier

use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.

the class MainActivity method listReadReceipts.

public void listReadReceipts(View view) {
    if (chatThreadAsyncClient == null) {
        createChatThreadAsyncClient();
    }
    if (chatThreadAsyncClient != null) {
        // The maximum number of read receipts to be returned per page, optional.
        int maxPageSize = 10;
        // Skips participants up to a specified position in response.
        int skip = 0;
        // Options to pass to the list method.
        ListReadReceiptOptions listReadReceiptOptions = new ListReadReceiptOptions().setMaxPageSize(maxPageSize).setSkip(skip);
        try {
            PagedAsyncStream<ChatMessageReadReceipt> readReceipts = chatThreadAsyncClient.listReadReceipts(listReadReceiptOptions, RequestContext.NONE);
            readReceipts.forEach(readReceipt -> {
                Log.d(TAG, "Got receipt for participant " + ((CommunicationUserIdentifier) readReceipt.getSenderCommunicationIdentifier()).getId() + " for message with id: " + readReceipt.getChatMessageId());
            });
        } catch (Exception e) {
            logAndToast("Listing read receipts failed: " + e.getMessage());
        }
    } else {
        logAndToast("ChatThreadAsyncClient creation failed");
    }
}
Also used : ChatMessageReadReceipt(com.azure.android.communication.chat.models.ChatMessageReadReceipt) ListReadReceiptOptions(com.azure.android.communication.chat.models.ListReadReceiptOptions) ExecutionException(java.util.concurrent.ExecutionException) CommunicationUserIdentifier(com.azure.android.communication.common.CommunicationUserIdentifier)

Example 4 with CommunicationUserIdentifier

use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.

the class CommunicationIdentifierConverter method convert.

/**
 * Convert CommunicationIdentifierModel into CommunicationIdentifier
 * @param identifier CommunicationIdentifierModel to be converted
 * @return CommunicationIdentifier
 */
public static CommunicationIdentifier convert(CommunicationIdentifierModel identifier, ClientLogger logger) {
    assertSingleType(identifier);
    String rawId = identifier.getRawId();
    if (identifier.getCommunicationUser() != null) {
        final String userId = identifier.getCommunicationUser().getId();
        if (userId == null) {
            throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.CommunicationUserIdentifierModel.id"));
        }
        return new CommunicationUserIdentifier(userId);
    }
    if (identifier.getPhoneNumber() != null) {
        PhoneNumberIdentifierModel phoneNumberModel = identifier.getPhoneNumber();
        if (phoneNumberModel.getValue() == null) {
            throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.PhoneNumberIdentifierModel.value"));
        }
        return new PhoneNumberIdentifier(phoneNumberModel.getValue()).setRawId(rawId);
    }
    if (identifier.getMicrosoftTeamsUser() != null) {
        MicrosoftTeamsUserIdentifierModel teamsUserIdentifierModel = identifier.getMicrosoftTeamsUser();
        final String userId = teamsUserIdentifierModel.getUserId();
        if (userId == null) {
            throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.MicrosoftTeamsUserIdentifierModel.userId"));
        }
        final CommunicationCloudEnvironmentModel cloud = teamsUserIdentifierModel.getCloud();
        if (cloud == null) {
            throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.MicrosoftTeamsUserIdentifierModel.cloud"));
        }
        if (rawId == null) {
            throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.rawId"));
        }
        return new MicrosoftTeamsUserIdentifier(userId, teamsUserIdentifierModel.isAnonymous()).setRawId(rawId).setCloudEnvironment(CommunicationCloudEnvironment.fromString(cloud.toString()));
    }
    if (rawId == null) {
        throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.rawId"));
    }
    return new UnknownIdentifier(rawId);
}
Also used : MicrosoftTeamsUserIdentifier(com.azure.android.communication.common.MicrosoftTeamsUserIdentifier) CommunicationCloudEnvironmentModel(com.azure.android.communication.chat.implementation.models.CommunicationCloudEnvironmentModel) PhoneNumberIdentifierModel(com.azure.android.communication.chat.implementation.models.PhoneNumberIdentifierModel) PhoneNumberIdentifier(com.azure.android.communication.common.PhoneNumberIdentifier) MicrosoftTeamsUserIdentifierModel(com.azure.android.communication.chat.implementation.models.MicrosoftTeamsUserIdentifierModel) UnknownIdentifier(com.azure.android.communication.common.UnknownIdentifier) CommunicationUserIdentifier(com.azure.android.communication.common.CommunicationUserIdentifier)

Example 5 with CommunicationUserIdentifier

use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.

the class ChatThreadAsyncClientTest method cannotRemoveParticipantWithInvalidUser.

@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void cannotRemoveParticipantWithInvalidUser(HttpClient httpClient) throws ExecutionException, InterruptedException {
    setupTest(httpClient);
    ExecutionException executionException = assertThrows(ExecutionException.class, () -> {
        this.chatThreadClient.removeParticipant(new CommunicationUserIdentifier("8:acs:invalidUserId")).get();
    });
    Throwable cause = executionException.getCause();
    assertNotNull(cause);
    assertTrue(cause instanceof ChatErrorResponseException);
    ChatErrorResponseException exception = (ChatErrorResponseException) cause;
    assertNotNull(exception.getResponse());
    assertEquals(400, exception.getResponse().getStatusCode());
}
Also used : ChatErrorResponseException(com.azure.android.communication.chat.models.ChatErrorResponseException) ExecutionException(java.util.concurrent.ExecutionException) CommunicationUserIdentifier(com.azure.android.communication.common.CommunicationUserIdentifier) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

CommunicationUserIdentifier (com.azure.android.communication.common.CommunicationUserIdentifier)19 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 ChatParticipant (com.azure.android.communication.chat.models.ChatParticipant)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)6 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 ListParticipantsOptions (com.azure.android.communication.chat.models.ListParticipantsOptions)4 CommunicationIdentifier (com.azure.android.communication.common.CommunicationIdentifier)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExecutionException (java.util.concurrent.ExecutionException)4 ChatErrorResponseException (com.azure.android.communication.chat.models.ChatErrorResponseException)2 CreateChatThreadOptions (com.azure.android.communication.chat.models.CreateChatThreadOptions)2 CreateChatThreadResult (com.azure.android.communication.chat.models.CreateChatThreadResult)2 CommunicationCloudEnvironmentModel (com.azure.android.communication.chat.implementation.models.CommunicationCloudEnvironmentModel)1 CommunicationIdentifierModel (com.azure.android.communication.chat.implementation.models.CommunicationIdentifierModel)1 MicrosoftTeamsUserIdentifierModel (com.azure.android.communication.chat.implementation.models.MicrosoftTeamsUserIdentifierModel)1 PhoneNumberIdentifierModel (com.azure.android.communication.chat.implementation.models.PhoneNumberIdentifierModel)1 AddChatParticipantsResult (com.azure.android.communication.chat.models.AddChatParticipantsResult)1 ChatEvent (com.azure.android.communication.chat.models.ChatEvent)1 ChatMessageDeletedEvent (com.azure.android.communication.chat.models.ChatMessageDeletedEvent)1