def process_nylas_rsvps(db_session, message, account, rsvps): # The invite sending code generates invites with uids of the form # `[email protected]`. We couldn't use Event.uid for this because # it wouldn't work with Exchange (Exchange uids are of the form # 1:2323 and aren't guaranteed to be unique). new_uids = [ _cleanup_nylas_uid(event.uid) for event in rsvps if '@nylas.com' in event.uid ] # Drop uids which aren't base36 uids. new_uids = [uid for uid in new_uids if valid_base36(uid)] # Get the list of events which share a uid with those we received. # Note that we're excluding events from "Emailed events" because # we don't want to process RSVPs to invites we received. existing_events = db_session.query(Event).filter( Event.namespace_id == account.namespace.id, Event.calendar_id != account.emailed_events_calendar_id, Event.public_id.in_(new_uids)).all() existing_events_table = { event.public_id: event for event in existing_events } for event in rsvps: event_uid = _cleanup_nylas_uid(event.uid) if event_uid not in existing_events_table: # We've received an RSVP to an event we never heard about. Save it, # maybe we'll sync the invite later. event.message = message else: # This is an event we already have in the db. existing_event = existing_events_table[event_uid] # Is the current event an update? if existing_event.sequence_number == event.sequence_number: merged_participants = existing_event.\ _partial_participants_merge(event) # We have to do this mumbo-jumbo because MutableList does # not register changes to nested elements. # We could probably change MutableList to handle it (see: # https://groups.google.com/d/msg/sqlalchemy/i2SIkLwVYRA/mp2WJFaQxnQJ) # but it seems very brittle. existing_event.participants = [] for participant in merged_participants: existing_event.participants.append(participant) # We need to sync back changes to the event manually if existing_event.calendar != account.emailed_events_calendar: schedule_action('update_event', existing_event, existing_event.namespace.id, db_session, calendar_uid=existing_event.calendar.uid) db_session.flush()
def process_nylas_rsvps(db_session, message, account, rsvps): # The invite sending code generates invites with uids of the form # `[email protected]`. We couldn't use Event.uid for this because # it wouldn't work with Exchange (Exchange uids are of the form # 1:2323 and aren't guaranteed to be unique). new_uids = [_cleanup_nylas_uid(event.uid) for event in rsvps if '@nylas.com' in event.uid] # Drop uids which aren't base36 uids. new_uids = [uid for uid in new_uids if valid_base36(uid)] # Get the list of events which share a uid with those we received. # Note that we're excluding events from "Emailed events" because # we don't want to process RSVPs to invites we received. existing_events = db_session.query(Event).filter( Event.namespace_id == account.namespace.id, Event.calendar_id != account.emailed_events_calendar_id, Event.public_id.in_(new_uids)).all() existing_events_table = {event.public_id: event for event in existing_events} for event in rsvps: event_uid = _cleanup_nylas_uid(event.uid) if event_uid not in existing_events_table: # We've received an RSVP to an event we never heard about. Save it, # maybe we'll sync the invite later. event.message = message else: # This is an event we already have in the db. existing_event = existing_events_table[event_uid] # Is the current event an update? if existing_event.sequence_number == event.sequence_number: merged_participants = existing_event.\ _partial_participants_merge(event) # We have to do this mumbo-jumbo because MutableList does # not register changes to nested elements. # We could probably change MutableList to handle it (see: # https://groups.google.com/d/msg/sqlalchemy/i2SIkLwVYRA/mp2WJFaQxnQJ) # but it seems very brittle. existing_event.participants = [] for participant in merged_participants: existing_event.participants.append(participant) # We need to sync back changes to the event manually if existing_event.calendar != account.emailed_events_calendar: schedule_action('update_event', existing_event, existing_event.namespace.id, db_session, calendar_uid=existing_event.calendar.uid) db_session.flush()