|
7 | 7 | from synapseclient.api import ( |
8 | 8 | create_team, |
9 | 9 | delete_team, |
| 10 | + get_membership_status, |
10 | 11 | get_team, |
11 | 12 | get_team_members, |
12 | 13 | get_team_open_invitations, |
@@ -56,6 +57,80 @@ def fill_from_dict( |
56 | 57 | return self |
57 | 58 |
|
58 | 59 |
|
| 60 | +@dataclass |
| 61 | +class TeamMembershipStatus: |
| 62 | + """ |
| 63 | + Contains information about a user's membership status in a Team. |
| 64 | + Represents a [Synapse TeamMembershipStatus](<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/TeamMembershipStatus.html>). |
| 65 | + User definable fields are: |
| 66 | +
|
| 67 | + Attributes: |
| 68 | + team_id: The id of the Team. |
| 69 | + user_id: The principal id of the user. |
| 70 | + is_member: true if and only if the user is a member of the team |
| 71 | + has_open_invitation: true if and only if the user has an open invitation to join the team |
| 72 | + has_open_request: true if and only if the user has an open request to join the team |
| 73 | + can_join: true if and only if the user requesting this status information can join the user to the team |
| 74 | + membership_approval_required: true if and only if team admin approval is required for the user to join the team |
| 75 | + has_unmet_access_requirement: true if and only if there is at least one unmet access requirement for the user on the team |
| 76 | + can_send_email: true if and only if the user can send an email to the team |
| 77 | + """ |
| 78 | + |
| 79 | + team_id: Optional[str] = None |
| 80 | + """The ID of the team""" |
| 81 | + |
| 82 | + user_id: Optional[str] = None |
| 83 | + """The ID of the user""" |
| 84 | + |
| 85 | + is_member: Optional[bool] = None |
| 86 | + """Whether the user is a member of the team""" |
| 87 | + |
| 88 | + has_open_invitation: Optional[bool] = None |
| 89 | + """Whether the user has an open invitation to join the team""" |
| 90 | + |
| 91 | + has_open_request: Optional[bool] = None |
| 92 | + """Whether the user has an open request to join the team""" |
| 93 | + |
| 94 | + can_join: Optional[bool] = None |
| 95 | + """Whether the user can join the team""" |
| 96 | + |
| 97 | + membership_approval_required: Optional[bool] = None |
| 98 | + """Whether membership approval is required for the team""" |
| 99 | + |
| 100 | + has_unmet_access_requirement: Optional[bool] = None |
| 101 | + """Whether the user has unmet access requirements""" |
| 102 | + |
| 103 | + can_send_email: Optional[bool] = None |
| 104 | + """Whether the user can send email to the team""" |
| 105 | + |
| 106 | + def fill_from_dict( |
| 107 | + self, membership_status_dict: Dict[str, Union[str, bool]] |
| 108 | + ) -> "TeamMembershipStatus": |
| 109 | + """ |
| 110 | + Converts a response from the REST API into this dataclass. |
| 111 | +
|
| 112 | + Arguments: |
| 113 | + membership_status_dict: The response from the REST API. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + The TeamMembershipStatus object. |
| 117 | + """ |
| 118 | + self.team_id = membership_status_dict.get("teamId", None) |
| 119 | + self.user_id = membership_status_dict.get("userId", None) |
| 120 | + self.is_member = membership_status_dict.get("isMember", None) |
| 121 | + self.has_open_invitation = membership_status_dict.get("hasOpenInvitation", None) |
| 122 | + self.has_open_request = membership_status_dict.get("hasOpenRequest", None) |
| 123 | + self.can_join = membership_status_dict.get("canJoin", None) |
| 124 | + self.membership_approval_required = membership_status_dict.get( |
| 125 | + "membershipApprovalRequired", None |
| 126 | + ) |
| 127 | + self.has_unmet_access_requirement = membership_status_dict.get( |
| 128 | + "hasUnmetAccessRequirement", None |
| 129 | + ) |
| 130 | + self.can_send_email = membership_status_dict.get("canSendEmail", None) |
| 131 | + return self |
| 132 | + |
| 133 | + |
59 | 134 | @dataclass |
60 | 135 | @async_to_sync |
61 | 136 | class Team(TeamSynchronousProtocol): |
@@ -356,3 +431,57 @@ async def open_invitations_async( |
356 | 431 | team=self.id, synapse_client=synapse_client |
357 | 432 | ) |
358 | 433 | return list(invitations) |
| 434 | + |
| 435 | + async def get_user_membership_status_async( |
| 436 | + self, |
| 437 | + user_id: str, |
| 438 | + *, |
| 439 | + synapse_client: Optional[Synapse] = None, |
| 440 | + ) -> TeamMembershipStatus: |
| 441 | + """Retrieve a user's Team Membership Status bundle for this team. |
| 442 | +
|
| 443 | + Arguments: |
| 444 | + user_id: Synapse user ID |
| 445 | + synapse_client: If not passed in and caching was not disabled by |
| 446 | + `Synapse.allow_client_caching(False)` this will use the last created |
| 447 | + instance from the Synapse class constructor. |
| 448 | +
|
| 449 | + Returns: |
| 450 | + TeamMembershipStatus object |
| 451 | +
|
| 452 | + Example: Check if a user is a member of a team |
| 453 | + This example shows how to check a user's membership status in a team. |
| 454 | +
|
| 455 | + ```python |
| 456 | + import asyncio |
| 457 | + from synapseclient import Synapse |
| 458 | + from synapseclient.models import Team |
| 459 | +
|
| 460 | + syn = Synapse() |
| 461 | + syn.login() |
| 462 | +
|
| 463 | + async def check_membership(): |
| 464 | + # Get a team by ID |
| 465 | + team = await Team.from_id_async(123456) |
| 466 | +
|
| 467 | + # Check membership status for a specific user |
| 468 | + user_id = "3350396" # Replace with actual user ID |
| 469 | + status = await team.get_user_membership_status_async(user_id) |
| 470 | +
|
| 471 | + print(f"User ID: {status.user_id}") |
| 472 | + print(f"Is member: {status.is_member}") |
| 473 | + print(f"Can join: {status.can_join}") |
| 474 | + print(f"Has open invitation: {status.has_open_invitation}") |
| 475 | + print(f"Has open request: {status.has_open_request}") |
| 476 | + print(f"Membership approval required: {status.membership_approval_required}") |
| 477 | +
|
| 478 | + asyncio.run(check_membership()) |
| 479 | + ``` |
| 480 | + """ |
| 481 | + from synapseclient import Synapse |
| 482 | + |
| 483 | + client = Synapse.get_client(synapse_client=synapse_client) |
| 484 | + status = await get_membership_status( |
| 485 | + user_id=user_id, team=self.id, synapse_client=client |
| 486 | + ) |
| 487 | + return TeamMembershipStatus().fill_from_dict(status) |
0 commit comments