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

Implement creating new conversations and adding users to existing conversation #86

Merged
merged 3 commits into from
Mar 31, 2015
Merged
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
62 changes: 62 additions & 0 deletions hangups/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,65 @@ def sendeasteregg(self, conversation_id, easteregg):
.format(res_status))
raise exceptions.NetworkError()


@asyncio.coroutine
def createconversation(self, chat_id_list):
"""Create new conversation.

conversation_id must be a valid conversation ID.
chat_id_list is list of users which should be invited to conversation
(except from yourself).

New conversation ID is returned as res['conversation']['id']['id']

Raises hangups.NetworkError if the request fails.
"""
client_generated_id = random.randint(0, 2**32)
body = [
self._get_request_header(),
1 if len(chat_id_list) == 1 else 2,
client_generated_id,
None,
[[str(chat_id), None, None, "unknown", None, []]
for chat_id in chat_id_list]
]

res = yield from self._request('conversations/createconversation', body)
# can return 200 but still contain an error
res = json.loads(res.body.decode())
res_status = res['response_header']['status']
if res_status != 'OK':
raise exceptions.NetworkError('Unexpected status: {}'
.format(res_status))
return res


@asyncio.coroutine
def adduser(self, conversation_id, chat_id_list):
"""Add user to existing conversation.

conversation_id must be a valid conversation ID.
chat_id_list is list of users which should be invited to conversation.

Raises hangups.NetworkError if the request fails.
"""
client_generated_id = random.randint(0, 2**32)
body = [
self._get_request_header(),
None,
[[str(chat_id), None, None, "unknown", None, []]
for chat_id in chat_id_list],
None,
[
[conversation_id], client_generated_id, 2, None, 4
]
]

res = yield from self._request('conversations/adduser', body)
# can return 200 but still contain an error
res = json.loads(res.body.decode())
res_status = res['response_header']['status']
if res_status != 'OK':
raise exceptions.NetworkError('Unexpected status: {}'
.format(res_status))
return res