Esempio n. 1
0
    def get_conversation(self, conversation_id, auth=''):
        if GroupMe._is_direct_message(conversation_id):
            # Expected conversation id: D + other user ID
            other_user_id = self._convo_to_groupme_id(conversation_id)

            dm_data = self._make_request('/direct_messages', auth,
                                         {'other_user_id': other_user_id})

            # Determine name of other person. Groupme never returns a DM, only
            # the messages, so we have to find a message _from_ the other
            # person
            other_user_name = ''
            for message in dm_data['direct_messages']:
                if message['sender_id'] == other_user_id:
                    other_user_name = message['name']


            return Conversation(
                    cid=self._dm_to_convo_id(other_user_id),
                    name=other_user_name,
                    last_updated=float(
                            dm_data['direct_messages'][0]['created_at']))
        else:
            gid = self._convo_to_groupme_id(conversation_id)
            group_data = self._make_request('/groups/{}'.format(gid), auth)
            return Conversation(
                    cid=group_data['id'],
                    name=group_data['name'],
                    last_updated=float(group_data['updated_at']))
Esempio n. 2
0
 def get_conversations_list(self, auth='', page=''):
     return ConversationCollection(
             [
                 Conversation(cid='4d7123',
                              name='IW Chat Group',
                              last_updated=123456789),
                 Conversation(cid='9d2asdf',
                              name='Some other Group',
                              last_updated=123456799),
             ],
             next_page='somepagetoken4321')
Esempio n. 3
0
    def get_conversations_list(self, auth='', page=''):
        # Retrieve conversations data, populate list
        if page == Slack.PAGING_DONE_TOKEN:
            return ConversationCollection([], '')

        channels = []
        pages_remain = True
        while pages_remain:
            data = self._make_request('/conversations.list', auth,
                                      {'cursor': page})
            # Add conversations retrieved
            if 'channels' not in data:
                raise ServiceError('Missing \'channels\' field: '
                                   '{}'.format(data))
            for channel in data['channels']:
                channels.append(Conversation(cid=channel['id'],
                                             name=channel['name'],
                                             last_updated='')) # TODO workaround
            pages_remain = self._has_pages_remaining(data)
            if pages_remain:
                page = sef._get_page_cursor(data)
            else:
                page = Slack.PAGING_DONE_TOKEN

        return ConversationCollection(channels, page)
Esempio n. 4
0
    def get_conversation(self, conversation_id, auth=''):
        data = self._make_request('/conversations.info', auth,
                                  {'channel': conversation_id})

        return Conversation(cid=data['channel']['id'],
                            name=data['channel']['name'],
                            # is last_read good enough for last_updated?
                            last_updated=data['channel']['last_read'])
Esempio n. 5
0
    def get_conversation(self, conversation_id, auth=''):
        client = self._get_client(auth)
        thread = client.fetchThreadInfo(conversation_id)

        # TODO unify date format
        return Conversation(cid=thread.uid,
                            name=thread.name,
                            # is last_read good enough for last_updated?
                            last_updated=thread.last_message_timestamp)
Esempio n. 6
0
    def get_conversations_list(self, auth='', page=''):
        client = self._get_client(auth)

        # TODO unify date format
        convos = map(lambda t: Conversation(t.uid, t.name,
                                            t.last_message_timestamp),
                     client.fetchThreadList())

        return ConversationCollection(list(convos), '')  # TODO paging
Esempio n. 7
0
 def get_conversation(self, conversation_id, auth='', page=''):
     return Conversation(
             cid=conversation_id,
             name='IW Chat Group',
             last_updated=123456788)
Esempio n. 8
0
    def get_conversations_list(self, auth='', page='1:0-1:0'):

        # Page token format: '(DM Page):(DM Index)-(Groups Page):(Groups index)
        dms, groups = page.split('-')
        dms_page, dms_offset = [int(x) for x in dms.split(':')]
        groups_page, groups_offset = [int(x) for x in groups.split(':')]

        # Retrieve direct messages, convert to Conversations
        dms = []
        dms_raw = self._make_request('/chats', auth,
            {'page': dms_page, 'per_page': GroupMe.DMS_PER_PAGE}
        )
        i = 0
        # Skip to offset
        dms_raw = dms_raw[dms_offset:]
        for dm in dms_raw:
            dm_object = Conversation(
                    cid=self._dm_to_convo_id(dm['other_user']['id']),
                    name=dm['other_user']['name'],
                    last_updated=float(dm['updated_at']))
            dm_object.index = i # Ad-hoc property, determines new index later
            dms.append(dm_object)
            i += 1

        # Retrieve group messages, convert to Conversations
        groups = []
        groups_raw = self._make_request('/groups', auth,
            {'page': groups_page, 'per_page': GroupMe.GROUPS_PER_PAGE}
        )
        i = 0
        for group in groups_raw:
            group_object = Conversation(
                cid=self._group_to_convo_id(group['id']),
                name=group['name'],
                last_updated=float(group['updated_at']))
            group_object.index = i # Ad-hoc property, determins new index later
            groups.append(group_object)
            i += 1

        # Combine chats and groups, sort by updated_at
        combined = sorted((dms + groups), key=lambda c: c.last_updated,
                          reverse=True)

        # Find new offsets
        num_groups, num_dms = len(groups), len(dms)
        groups_count, dms_count = 0, 0
        for conversation in combined:
            if GroupMe._is_direct_message(conversation.cid):
                dms_count += 1
                if dms_count == num_dms and num_dms != 0:
                    break
            else:
                groups_count += 1
                if groups_count == num_groups and num_groups != 0:
                    break
        assert dms_count <= GroupMe.GROUPS_PER_PAGE
        assert groups_count <= GroupMe.GROUPS_PER_PAGE

        # Trim data
        combined = combined[:(dms_count + groups_count)]

        # Increment offsets and pages
        if (dms_offset + dms_count) % GroupMe.DMS_PER_PAGE == 0:
            dms_offset = 0
            dms_page += 1
        else:
            dms_offset += dms_count

        if (groups_offset + groups_count) % GroupMe.GROUPS_PER_PAGE == 0:
            groups_offset = 0
            groups_page += 1
        else:
            groups_offset += groups_count

        new_page_token = '{}:{}-{}:{}'.format(dms_page, dms_offset,
                                              groups_page, groups_offset)
        return ConversationCollection(combined, new_page_token)