Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add teams presence #561

Merged
merged 3 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions O365/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'settings_all': ['MailboxSettings.ReadWrite'],
'tasks': ['Tasks.Read'],
'tasks_all': ['Tasks.ReadWrite'],
'presence': ['Presence.Read']
}


Expand Down
68 changes: 67 additions & 1 deletion O365/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,53 @@
log = logging.getLogger(__name__)


class Presence(ApiComponent):
""" Microsoft Teams Presence """

_endpoints = {}

def __init__(self, *, parent=None, con=None, **kwargs):
""" Microsoft Teams Presence

:param parent: parent object
:type parent: Teams
:param Connection con: connection to use if no parent specified
:param Protocol protocol: protocol to use if no parent specified
(kwargs)
:param str main_resource: use this resource instead of parent resource
(kwargs)
"""
if parent and con:
raise ValueError('Need a parent or a connection but not both')
self.con = parent.con if parent else con

cloud_data = kwargs.get(self._cloud_data_key, {})

self.object_id = cloud_data.get('id')


# Choose the main_resource passed in kwargs over parent main_resource
main_resource = kwargs.pop('main_resource', None) or (
getattr(parent, 'main_resource', None) if parent else None)

main_resource = '{}{}'.format(main_resource, '')

super().__init__(
protocol=parent.protocol if parent else kwargs.get('protocol'),
main_resource=main_resource)

self.availability = cloud_data.get('availability')
self.activity = cloud_data.get('activity')

def __str__(self):
return self.__repr__()

def __repr__(self):
return 'availability: {}'.format(self.availability)

def __eq__(self, other):
return self.object_id == other.object_id

class Team(ApiComponent):
""" A Microsoft Teams team """

Expand Down Expand Up @@ -156,13 +203,14 @@ class Teams(ApiComponent):
"""

_endpoints = {
'get_my_presence': '/me/presence',
'get_my_teams': '/me/joinedTeams',
'get_channels': '/teams/{team_id}/channels',
'create_channel': '/teams/{team_id}/channels',
'get_channel_info': '/teams/{team_id}/channels/{channel_id}',
'get_apps_in_team': '/teams/{team_id}/installedApps?$expand=teamsAppDefinition',
}

presence_constructor = Presence
team_constructor = Team
channel_constructor = Channel
app_constructor = App
Expand Down Expand Up @@ -195,6 +243,24 @@ def __str__(self):
def __repr__(self):
return 'Microsoft Teams'

def get_my_presence(self, *args):
""" Returns my availability and activity

:rtype: teams
"""

url = self.build_url(self._endpoints.get('get_my_presence'))

response = self.con.get(url)

if not response:
return None

data = response.json()

return self.presence_constructor(parent=self, **{self._cloud_data_key: data})


def get_my_teams(self, *args):
""" Returns a list of teams that I am in

Expand Down
4 changes: 4 additions & 0 deletions tests/test_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ def setup_class(self):

def teardown_class(self):
pass
# Depends on actual teams status, use for local testing
# def test_get_presence(self):
# assert(self.account.teams().get_my_presence().activity == 'Away')