Source code for groupy.api.chats
from . import base
from . import messages
from groupy import pagers
from groupy import utils
[docs]class Chats(base.Manager):
"""A chat manager."""
def __init__(self, session):
super().__init__(session, 'chats')
def _raw_list(self, **params):
response = self.session.get(self.url, params=params)
return [Chat(self, **chat) for chat in response.data]
[docs] def list(self, page=1, per_page=10):
"""List a page of chats.
:param int page: which page
:param int per_page: how many chats per page
:return: chats with other users
:rtype: :class:`~groupy.pagers.ChatList`
"""
return pagers.ChatList(self, self._raw_list, per_page=per_page,
page=page)
[docs] def list_all(self, per_page=10):
"""List all chats.
:param int per_page: how many chats per page
:return: chats with other users
:rtype: :class:`~groupy.pagers.ChatList`
"""
return self.list(per_page=per_page).autopage()
[docs]class Chat(base.ManagedResource):
"""A chat with another user."""
def __init__(self, manager, **data):
super().__init__(manager, **data)
self.messages = messages.DirectMessages(self.manager.session,
self.other_user['id'])
self.created_at = utils.get_datetime(self.data['created_at'])
self.updated_at = utils.get_datetime(self.data['updated_at'])
def __repr__(self):
klass = self.__class__.__name__
return '<{}(other_user={!r})>'.format(klass, self.other_user['name'])
# TODO: figure out how best to achieve chat equality
[docs] def post(self, text=None, attachments=None):
"""Post a message to the chat.
:param str text: the text of the message
:param attachments: a list of attachments
:type attachments: :class:`list`
:return: ``True`` if successful
:rtype: bool
"""
return self.messages.create(text=text, attachments=attachments)