Skip to content

Commit

Permalink
Rename - remove_participant -> user -> identifier (Azure#17814)
Browse files Browse the repository at this point in the history
* Rename - remove_participant -> user -> identifier

* Minor doc updates

* test files modified

* Minor doc updates

* Rename ChatThreadParticipant to ChatParticipant

* modified tests

* Renamed ChatParticipant.user to ChatParticipant.identifier

* Docstring update MicrosoftTeamsUserIdentifier.is_anonymous 'str' to 'bool'

* Minor test refactoring

* Update CHANGELOG.md and README.md

* Import changes for CommunicationUserIdentifier in samples

* Update sdk/communication/azure-communication-chat/README.md

Co-authored-by: Dominik <domessin@microsoft.com>
  • Loading branch information
2 people authored and mccoyp committed Apr 13, 2021
1 parent 9f82b8a commit 2bb8a2c
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 190 deletions.
4 changes: 4 additions & 0 deletions sdk/communication/azure-communication-chat/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- Changed return type of `send_message` to `SendChatMessageResult`.
- Replaced `CommunicationError` with `ChatError`.
- Refactored `CommunicationTokenCredential` constructor to accept `token` instead of `CommunicationTokenRefreshOptions`.
- Renamed `ChatThreadParticipant` to `ChatParticipant`.
- Renamed attribute `ChatParticipant.user` to `ChatParticipant.identifier`.
- Renamed argument `user` to `identifier` in `ChatThreadClient.remove_participant`.
- Refactored implementation of `CommunicationUserIdentifier`, `PhoneNumberIdentifier`, `MicrosoftTeamsUserIdentifier`, `UnknownIdentifier` to use a `dict` property bag.

## 1.0.0b5 (2021-03-09)
### Breaking Changes
Expand Down
52 changes: 26 additions & 26 deletions sdk/communication/azure-communication-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Perform CRD(Create-Read-Delete) operations on thread participants
```Python
list_participants(**kwargs)
add_participants(thread_participants, **kwargs)
remove_participant(participant_id, **kwargs)
remove_participant(participant_identifier, **kwargs)
```

## Send typing notification
Expand Down Expand Up @@ -171,7 +171,7 @@ The following sections provide several code snippets covering some of the most c
Use the `create_chat_thread` method to create a chat thread.

- Use `topic`, required, to give a thread topic;
- Use `thread_participants`, optional, to provide a list the `ChatThreadParticipant` to be added to the thread;
- Use `thread_participants`, optional, to provide a list the `ChatParticipant` to be added to the thread;
- `user`, required, it is the `CommunicationUserIdentifier` you created by CommunicationIdentityClient.create_user()
from User Access Tokens
<!-- [User Access Tokens](#user-access-tokens) -->
Expand All @@ -194,7 +194,7 @@ chat_thread_client = chat_client.get_chat_thread_client(create_chat_thread_resul
```Python
# With idempotency_token and thread_participants
from azure.communication.identity import CommunicationIdentityClient
from azure.communication.chat import ChatThreadParticipant, ChatClient, CommunicationTokenCredential
from azure.communication.chat import ChatParticipant, ChatClient, CommunicationTokenCredential
import uuid
from datetime import datetime

Expand All @@ -207,7 +207,7 @@ tokenresponse = identity_client.get_token(user, scopes=["chat"])
token = tokenresponse.token

## OR pass existing user
# from azure.communication.identity import CommunicationUserIdentifier
# from azure.communication.chat import CommunicationUserIdentifier
# user_id = 'some_user_id'
# user = CommunicationUserIdentifier(user_id)

Expand All @@ -221,8 +221,8 @@ def get_unique_identifier_for_request(**kwargs):
return res

topic = "test topic"
thread_participants = [ChatThreadParticipant(
user=user,
thread_participants = [ChatParticipant(
identifier=user,
display_name='name',
share_history_time=datetime.utcnow()
)]
Expand Down Expand Up @@ -408,57 +408,57 @@ Use `list_participants` to retrieve the participants of the thread.
- Use `results_per_page`, optional, The maximum number of participants to be returned per page.
- Use `skip`, optional, to skips participants up to a specified position in response.

An iterator of `[ChatThreadParticipant]` is the response returned from listing participants
An iterator of `[ChatParticipant]` is the response returned from listing participants

```python
chat_thread_participants = chat_thread_client.list_participants(results_per_page=5, skip=5)
for chat_thread_participant_page in chat_thread_participants.by_page():
for chat_thread_participant in chat_thread_participant_page:
print("ChatThreadParticipant: ", chat_thread_participant)
chat_participants = chat_thread_client.list_participants(results_per_page=5, skip=5)
for chat_participant_page in chat_participants.by_page():
for chat_participant in chat_participant_page:
print("ChatParticipant: ", chat_participant)
```

### Add thread participants

Use `add_participants` method to add thread participants to the thread.

- Use `thread_participants`, required, to list the `ChatThreadParticipant` to be added to the thread;
- Use `thread_participants`, required, to list the `ChatParticipant` to be added to the thread;
- `user`, required, it is the `CommunicationUserIdentifier` you created by CommunicationIdentityClient.create_user() from User Access Tokens
<!-- [User Access Tokens](#user-access-tokens) -->
- `display_name`, optional, is the display name for the thread participant.
- `share_history_time`, optional, time from which the chat history is shared with the participant.

A `list(tuple(ChatThreadParticipant, ChatError))` is returned. When participant is successfully added,
A `list(tuple(ChatParticipant, ChatError))` is returned. When participant is successfully added,
an empty list is expected. In case of an error encountered while adding participant, the list is populated
with the failed participants along with the error that was encountered.

```Python
from azure.communication.identity import CommunicationIdentityClient
from azure.communication.chat import ChatThreadParticipant
from azure.communication.chat import ChatParticipant
from datetime import datetime

# create 2 users
identity_client = CommunicationIdentityClient.from_connection_string('<connection_string>')
new_users = [identity_client.create_user() for i in range(2)]

# # conversely, you can also add an existing user to a chat thread; provided the user_id is known
# from azure.communication.identity import CommunicationUserIdentifier
# from azure.communication.chat import CommunicationUserIdentifier
#
# user_id = 'some user id'
# user_display_name = "Wilma Flinstone"
# new_user = CommunicationUserIdentifier(user_id)
# participant = ChatThreadParticipant(
# user=new_user,
# participant = ChatParticipant(
# identifier=new_user,
# display_name=user_display_name,
# share_history_time=datetime.utcnow())

participants = []
for _user in new_users:
chat_thread_participant = ChatThreadParticipant(
user=_user,
chat_participant = ChatParticipant(
identifier=_user,
display_name='Fred Flinstone',
share_history_time=datetime.utcnow()
)
participants.append(chat_thread_participant)
participants.append(chat_participant)

response = chat_thread_client.add_participants(thread_participants=participants)

Expand All @@ -478,18 +478,18 @@ if retry:
### Remove thread participant

Use `remove_participant` method to remove thread participant from the thread identified by threadId.
`user` is the `CommunicationUserIdentifier` you created by CommunicationIdentityClient.create_user() from User Access Tokens
`identifier` is the `CommunicationUserIdentifier` you created by CommunicationIdentityClient.create_user() from `azure-communication-identity`
<!-- [User Access Tokens](#user-access-tokens) -->
and was added into this chat thread.
- Use `user` to specify the `CommunicationUserIdentifier` you created
- Use `identifier` to specify the `CommunicationUserIdentifier` you created
```python
chat_thread_client.remove_participant(user=new_user)
chat_thread_client.remove_participant(identifier=new_user)

# # converesely you can also do the following; provided the user_id is known
# from azure.communication.identity import CommunicationUserIdentifier
# # conversely you can also do the following; provided the user_id is known
# from azure.communication.chat import CommunicationUserIdentifier
#
# user_id = 'some user id'
# chat_thread_client.remove_participant(user=CommunincationUserIdentfier(new_user))
# chat_thread_client.remove_participant(identifier=CommunicationUserIdentifier(new_user))

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)

from ._models import (
ChatThreadParticipant,
ChatParticipant,
ChatMessage,
ChatThreadProperties,
ChatMessageReadReceipt,
Expand Down Expand Up @@ -39,7 +39,7 @@
'SendChatMessageResult',
'ChatThreadProperties',
'ChatThreadItem',
'ChatThreadParticipant',
'ChatParticipant',
'ChatMessageType',
'CreateChatThreadResult',
'ChatError',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def create_chat_thread(
:param topic: Required. The thread topic.
:type topic: str
:keyword thread_participants: Optional. Participants to be added to the thread.
:paramtype thread_participants: List[~azure.communication.chat.ChatThreadParticipant]
:paramtype thread_participants: List[~azure.communication.chat.ChatParticipant]
:keyword idempotency_token: Optional. If specified, the client directs that the request is
repeatable; that is, the client can make the request multiple times with the same
Idempotency_Token and get back an appropriate response without the server executing the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
SendChatMessageResult
)
from ._models import (
ChatThreadParticipant,
ChatParticipant,
ChatMessage,
ChatMessageReadReceipt,
ChatThreadProperties
Expand Down Expand Up @@ -464,13 +464,13 @@ def list_participants(
self,
**kwargs # type: Any
):
# type: (...) -> ItemPaged[ChatThreadParticipant]
# type: (...) -> ItemPaged[ChatParticipant]
"""Gets the participants of a thread.
:keyword int results_per_page: The maximum number of participants to be returned per page.
:keyword int skip: Skips participants up to a specified position in response.
:return: An iterator like instance of ChatThreadParticipant
:rtype: ~azure.core.paging.ItemPaged[~azure.communication.chat.ChatThreadParticipant]
:return: An iterator like instance of ChatParticipant
:rtype: ~azure.core.paging.ItemPaged[~azure.communication.chat.ChatParticipant]
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
.. admonition:: Example:
Expand All @@ -490,27 +490,27 @@ def list_participants(
self._thread_id,
max_page_size=results_per_page,
skip=skip,
cls=lambda objs: [ChatThreadParticipant._from_generated(x) for x in objs], # pylint:disable=protected-access
cls=lambda objs: [ChatParticipant._from_generated(x) for x in objs], # pylint:disable=protected-access
**kwargs)


@distributed_trace
def add_participants(
self,
thread_participants, # type: List[ChatThreadParticipant]
thread_participants, # type: List[ChatParticipant]
**kwargs # type: Any
):
# type: (...) -> List[Tuple[ChatThreadParticipant, ChatError]]
# type: (...) -> List[Tuple[ChatParticipant, ChatError]]
"""Adds thread participants to a thread. If participants already exist, no change occurs.
If all participants are added successfully, then an empty list is returned;
otherwise, a list of tuple(chat_thread_participant, chat_error) is returned,
of failed participants and its respective error
:param thread_participants: Thread participants to be added to the thread.
:type thread_participants: List[~azure.communication.chat.ChatThreadParticipant]
:return: List[Tuple[ChatThreadParticipant, ChatError]]
:rtype: List[Tuple[~azure.communication.chat.ChatThreadParticipant, ~azure.communication.chat.ChatError]]
:type thread_participants: List[~azure.communication.chat.ChatParticipant]
:return: List[Tuple[ChatParticipant, ChatError]]
:rtype: List[Tuple[~azure.communication.chat.ChatParticipant, ~azure.communication.chat.ChatError]]
:raises: ~azure.core.exceptions.HttpResponseError
.. admonition:: Example:
Expand Down Expand Up @@ -545,14 +545,14 @@ def add_participants(
@distributed_trace
def remove_participant(
self,
user, # type: CommunicationIdentifier
identifier, # type: CommunicationIdentifier
**kwargs # type: Any
):
# type: (...) -> None
"""Remove a participant from a thread.
:param user: Required. User identity of the thread participant to remove from the thread.
:type user: ~azure.communication.chat.CommunicationIdentifier
:param identifier: Required. Identifier of the thread participant to remove from the thread.
:type identifier: ~azure.communication.chat.CommunicationIdentifier
:return: None
:rtype: None
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
Expand All @@ -566,12 +566,12 @@ def remove_participant(
:dedent: 8
:caption: Removing participant from chat thread.
"""
if not user:
raise ValueError("user cannot be None.")
if not identifier:
raise ValueError("identifier cannot be None.")

return self._client.chat_thread.remove_chat_participant(
chat_thread_id=self._thread_id,
participant_communication_identifier=serialize_identifier(user),
participant_communication_identifier=serialize_identifier(identifier),
**kwargs)

def close(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Union, Tuple


class ChatThreadParticipant(object):
class ChatParticipant(object):
"""A participant of the chat thread.
All required parameters must be populated in order to send to Azure.
:ivar user: Required. The communication identifier.
:type user: CommunicationIdentifier
:ivar identifier: Required. The communication identifier.
:type identifier: CommunicationIdentifier
:ivar display_name: Display name for the chat thread participant.
:type display_name: str
:ivar share_history_time: Time from which the chat history is shared with the participant. The
Expand All @@ -37,21 +37,21 @@ def __init__(
):
# type: (...) -> None

self.user = kwargs['user']
self.identifier = kwargs['identifier']
self.display_name = kwargs.get('display_name', None)
self.share_history_time = kwargs.get('share_history_time', None)

@classmethod
def _from_generated(cls, chat_thread_participant):
return cls(
user=deserialize_identifier(chat_thread_participant.communication_identifier),
identifier=deserialize_identifier(chat_thread_participant.communication_identifier),
display_name=chat_thread_participant.display_name,
share_history_time=chat_thread_participant.share_history_time
)

def _to_generated(self):
return ChatParticipantAutorest(
communication_identifier=serialize_identifier(self.user),
communication_identifier=serialize_identifier(self.identifier),
display_name=self.display_name,
share_history_time=self.share_history_time
)
Expand Down Expand Up @@ -144,7 +144,7 @@ class ChatMessageContent(object):
:type topic: str
:ivar participants: Chat message content for messages of types participantAdded or
participantRemoved.
:type participants: List[~azure.communication.chat.models.ChatThreadParticipant]
:type participants: List[~azure.communication.chat.models.ChatParticipant]
:ivar initiator: Chat message content for messages of types participantAdded or
participantRemoved.
:type initiator: CommunicationIdentifier
Expand All @@ -166,7 +166,7 @@ def _from_generated(cls, chat_message_content):
participants_list = chat_message_content.participants
if participants_list is not None and len(participants_list) > 0:
participants = [
ChatThreadParticipant._from_generated(participant) for participant in # pylint:disable=protected-access
ChatParticipant._from_generated(participant) for participant in # pylint:disable=protected-access
participants_list
]
else:
Expand Down Expand Up @@ -270,7 +270,7 @@ class CreateChatThreadResult(object):
:ivar chat_thread: Chat thread.
:type chat_thread: ~azure.communication.chat.ChatThreadProperties
:ivar errors: Errors encountered during the creation of the chat thread.
:type errors: List[Tuple[~azure.communication.chat.ChatThreadParticipant, ~azure.communication.chat.ChatError]]
:type errors: List[Tuple[~azure.communication.chat.ChatParticipant, ~azure.communication.chat.ChatError]]
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class MicrosoftTeamsUserIdentifier(object):
- `cloud` (str): Cloud environment that this identifier belongs to.
:param str user_id: Microsoft Teams user id.
:keyword str is_anonymous: `True` if the identifier is anonymous. Default value is `False`.
:keyword bool is_anonymous: `True` if the identifier is anonymous. Default value is `False`.
:keyword cloud: Cloud environment that the user belongs to. Default value is `PUBLIC`.
:paramtype cloud: str or ~azure.communication.chat.CommunicationCloudEnvironment
"""
Expand Down
Loading

0 comments on commit 2bb8a2c

Please sign in to comment.