python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

Selecting a field in a query breaks is_event_message and get_event()

Open antti-ngp opened this issue 4 years ago • 1 comments

Continuing on 683

I'm looping through messages in a mailbox and filtering results using a query to limit fields that I want:

q = mailbox.q().select('id')
for message in mailbox.get_messages(query=q):
    print(message.get_event())
    print(message.id)

I'm expecting to get the id of the message and all the data from the associated event. Instead I get:

None AA_MY_MESSAGE_ID_AAA=

I've found some discussion on what to include in the select to get the info whether a Message is a n EventMessage or not. This lead me into trying to include microsoft.graph.eventMessage/meetingMessageType in the select list which didn't work as there are multiple steps of handling done to the attributes before they are passed to the API. I tried modifying several of those steps to be able to pass the above attribute to the API with no luck.

However, in the process, I discovered that commenting rows 630 to 634 in /utils/utils.py:

@fluent
    def select(self, *attributes):
        """ Adds the attribute to the $select parameter

        :param str attributes: the attributes tuple to select.
         If empty, the on_attribute previously set is added.
        :rtype: Query
        """
        if attributes:
            for attribute in attributes:
                attribute = self.protocol.convert_case(
                    attribute) if attribute and isinstance(attribute,
                                                           str) else None
                # if attribute:
                #     if '/' in attribute:
                #         # only parent attribute can be selected
                #         attribute = attribute.split('/')[0]
                #     self._selects.add(attribute)
        else:
            if self._attribute:
                self._selects.add(self._attribute)

        return self

gave me the expected results even when no additional fields were added to the select:

Subject: My calendar event (on: 2021-09-27 from: 14:00:00 to: 16:00:00) AA_MY_MESSAGE_ID_AAA=

I don't understand how, but maybe microsoft.graph.eventMessage/meetingMessageType is somehow always implicitly included in the select and splitting on the character / somehow breaks it.

antti-ngp avatar Sep 14 '21 09:09 antti-ngp

Having written that I now realise that what I've commented out basically equals to removing the whole select clause 😅 .

Did some further testing. Here's how to make it work:

  1. Remove the hardcoded camelcasing from the MSGraphProtocol class on row 216 in connection.py
  2. Pass None as the casing_function to the protocol
  3. Remove lines 631-633 from utils/utils.py
  4. Add microsoft.graph.eventMessage/meetingMessageType to the select list.
account = Account(credentials, auth_flow_type='credentials', tenant_id=tenant_id, protocol=MSGraphProtocol(casing_function=None))
account.authenticate()
mailbox = account.mailbox('[email protected]')
q = mailbox.q().select(
   'id'
    ,'microsoft.graph.eventMessage/meetingMessageType'
)
for message in mailbox.get_messages(query=q):
    print(message.get_event())
    print(message.id)

and you will get:

Subject: My calendar event (on: 2021-09-27 from: 14:00:00 to: 16:00:00) AA_MY_MESSAGE_ID_AAA=

antti-ngp avatar Sep 14 '21 09:09 antti-ngp