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

Add ability to respond to event invites #93

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
67 changes: 67 additions & 0 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self, username, password):
self.chat_url = None
self.auth = None
self.token = None
self.profile = None
self.groups = None
self.events = None

Expand Down Expand Up @@ -43,6 +44,22 @@ async def login(self):
self.chat_url = result["url"]
self.auth = result["auth"]

async def get_profile(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the function name get_profile() should be get_current_profile() or similar, as the former sounds like a general purpose function to read an arbitrary item, like get_event(), get_group(), etc.

"""
Get the current users profile.
Subject to authenticated user's access.

Returns
-------
json response of the current user
"""
if not self.token:
await self.login()
url = f"{self.api_url}profile"
async with self.clientsession.get(url, headers=self.auth_headers) as r:
self.profile = await r.json()
return self.profile

async def get_groups(self):
"""
Get all groups.
Expand Down Expand Up @@ -214,6 +231,7 @@ async def get_events(
self,
group_id: Optional[str] = None,
include_scheduled: bool = False,
response: Optional[str] = None,
max_end: Optional[datetime] = None,
min_end: Optional[datetime] = None,
max_start: Optional[datetime] = None,
Expand All @@ -233,6 +251,9 @@ async def get_events(
(TO DO: probably events for which invites haven't been sent yet?)
Defaults to False for performance reasons.
Uses `scheduled` API parameter.
response : str, optional
Only include events which this response. Valid known options are:
`unanswered`, there may be more.
Copy link
Collaborator

@elliot-100 elliot-100 Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should read "Only include events with this response from the authenticated user ...", to make it clearer that this parameter (unlike the others) is not intrinsic to the event itself.

max_end : datetime, optional
Only include events which end before or at this datetime.
Uses `maxEndTimestamp` API parameter; relates to `endTimestamp` event
Expand Down Expand Up @@ -276,6 +297,8 @@ async def get_events(
url += f"&minStartTimestamp={min_start.strftime('%Y-%m-%dT00:00:00.000Z')}"
if group_id:
url += f"&groupId={group_id}"
if response:
url += f"&response={response}"

async with self.clientsession.get(url, headers=self.auth_headers) as r:
self.events = await r.json()
Expand Down Expand Up @@ -380,3 +403,47 @@ async def update_event(self, uid, updates: dict):
async with self.clientsession.post(url, json=data, headers=headers) as r:
self.events_update = await r.json()
return self.events

async def update_event_response(
self,
uid,
accepted: bool = True,
) -> List[dict]:
"""
Respond to an event invite.

Parameters:
----------
uid : str
UID of the event.
accepted : bool
Accept invite to an event.

Returns:
----------
json results of put command, containing:
`acceptedIds`,
`unconfirmedIds`,
`waitinglistIds`,
`votes`
"""
if not self.token:
await self.login()
profile = await self.get_profile()
event = await self.get_event(uid)

membershipId = None
members = event["recipients"]["group"]["members"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a similar test also needed in other functions?
Seems like it might be a good idea maybe?
And if so - it should probably be abstracted out to a separate function?

What do you think @elliot-100 ?

Copy link
Collaborator

@elliot-100 elliot-100 Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raise exception if the current user isn't a member of the event's group?

It makes sense that you can't respond to an event if you're not in its group - the event isn't available for you to attend. It's needed because you may or may not be able to read it anyway, depending on visibility settings.

I guess there are other 'write' cases where this should be checked but I'm not really familiar with them. Which ones do you think?

for m in members:
if m["profile"]["id"] == profile["id"]:
membershipId = m["id"]
break

if membershipId == None:
raise ValueError("You don't seem to be a member of the event's group")

url = f"{self.api_url}sponds/{uid}/responses/{membershipId}"

data = {"accepted": accepted}
r = await self.clientsession.put(url, json=data, headers=self.auth_headers)
return await r.json()