Chat messages were upgraded to the new post method, too

This commit is contained in:
Manuel Cortez 2019-08-27 12:13:13 -05:00
parent bbef34d125
commit a7173adc22
2 changed files with 33 additions and 70 deletions

View File

@ -1182,67 +1182,14 @@ class chatBuffer(baseBuffer):
wx.Bell() wx.Bell()
return return
self.tab.text.SetValue("") self.tab.text.SetValue("")
call_threaded(self._send_message, text=text) post_arguments = dict(random_id = random.randint(0, 100000), peer_id=self.kwargs["peer_id"])
if len(text) > 0:
def upload_attachments(self, attachments): post_arguments.update(message=text)
""" Upload attachments to VK before posting them. if hasattr(self, "attachments_to_be_sent") and len(self.attachments_to_be_sent) > 0:
Returns attachments formatted as string, as required by VK API. attachments = self.attachments_to_be_sent[::]
"""
local_attachments = ""
uploader = upload.VkUpload(self.session.vk.session_object)
for i in attachments:
if i["from"] == "online":
local_attachments += "{0}{1}_{2},".format(i["type"], i["owner_id"], i["id"])
elif i["from"] == "local" and i["type"] == "photo":
photos = i["file"]
description = i["description"]
r = uploader.photo_messages(photos)
id = r[0]["id"]
owner_id = r[0]["owner_id"]
local_attachments += "photo{0}_{1},".format(owner_id, id)
elif i["from"] == "local" and i["type"] == "audio":
audio = i["file"]
title = "untitled"
artist = "unnamed"
if "artist" in i:
artist = i["artist"]
if "title" in i:
title = i["title"]
r = uploader.audio(audio, title=title, artist=artist)
id = r["id"]
owner_id = r["owner_id"]
local_attachments += "audio{0}_{1},".format(owner_id, id)
elif i["from"] == "local" and i["type"] == "voice_message":
r = uploader.audio_message(i["file"], peer_id=self.kwargs["peer_id"])
id = r["audio_message"]["id"]
owner_id = r["audio_message"]["owner_id"]
local_attachments += "audio_message{0}_{1},".format(owner_id, id)
elif i["from"] == "local" and i["type"] == "document":
document = i["file"]
title = i["title"]
r = uploader.document(document, title=title, message_peer_id=self.kwargs["peer_id"])
id = r["doc"]["id"]
owner_id = r["doc"]["owner_id"]
local_attachments += "doc{0}_{1},".format(owner_id, id)
return local_attachments
def _send_message(self, text, attachments=[]):
if hasattr(self, "attachments_to_be_sent") and type(self.attachments_to_be_sent) == list:
self.attachments_to_be_sent = self.upload_attachments(self.attachments_to_be_sent)
try:
# Let's take care about the random_id attribute.
# This should be unique per message and should be changed right after the message has been sent.
# If the message is tried to be sent twice this random_id should be the same for both copies.
# At the moment we just calculate len(text)_user_id, hope that will work.
random_id = random.randint(0, 100000)
if hasattr(self, "attachments_to_be_sent"):
response = self.session.vk.client.messages.send(peer_id=self.kwargs["peer_id"], message=text, attachment=self.attachments_to_be_sent, random_id=random_id)
else: else:
response = self.session.vk.client.messages.send(peer_id=self.kwargs["peer_id"], message=text, random_id=random_id) attachments = []
except ValueError as ex: call_threaded(pub.sendMessage, "post", parent_endpoint="messages", child_endpoint="send", attachments_list=attachments, post_arguments=post_arguments)
if ex.code == 9:
output.speak(_("You have been sending a message that is already sent. Try to update the buffer if you can't see the new message in the history."))
finally:
if hasattr(self, "attachments_to_be_sent"): if hasattr(self, "attachments_to_be_sent"):
del self.attachments_to_be_sent del self.attachments_to_be_sent

View File

@ -313,8 +313,9 @@ class vkSession(object):
This function should be capable of uploading all attachments before posting, and send a special event in case the post has failed, This function should be capable of uploading all attachments before posting, and send a special event in case the post has failed,
So the program can recreate the post and show it back to the user.""" So the program can recreate the post and show it back to the user."""
attachments = "" attachments = ""
print(attachments_list)
if len(attachments_list) > 0: if len(attachments_list) > 0:
attachments = self.upload_attachments(attachments_list) attachments = self.upload_attachments(attachments_list, post_arguments.get("peer_id"))
# VK generally defines all kind of messages under "text", "message" or "body" so let's try with all of those # VK generally defines all kind of messages under "text", "message" or "body" so let's try with all of those
possible_message_keys = ["text", "message", "body"] possible_message_keys = ["text", "message", "body"]
for i in possible_message_keys: for i in possible_message_keys:
@ -328,16 +329,20 @@ class vkSession(object):
post_arguments[i] = post_arguments[i].replace(urls[0], "") post_arguments[i] = post_arguments[i].replace(urls[0], "")
# After modifying everything, let's update the post arguments if needed. # After modifying everything, let's update the post arguments if needed.
if len(attachments) > 0: if len(attachments) > 0:
if parent_endpoint == "messages":
post_arguments.update(attachment=attachments)
else:
post_arguments.update(attachments=attachments) post_arguments.update(attachments=attachments)
# Determines the correct functions to call here. # Determines the correct functions to call here.
parent_endpoint = getattr(self.vk.client, parent_endpoint) parent_endpoint = getattr(self.vk.client, parent_endpoint)
endpoint = getattr(parent_endpoint, child_endpoint) endpoint = getattr(parent_endpoint, child_endpoint)
post = endpoint(**post_arguments) post = endpoint(**post_arguments)
print(post)
def upload_attachments(self, attachments): def upload_attachments(self, attachments, peer_id=None):
""" Upload attachments to VK before posting them. """ Upload attachments to VK before posting them.
Returns attachments formatted as string, as required by VK API.""" Returns attachments formatted as string, as required by VK API.
@ peer_id int: if this value is passed, let's assume attachments will be send in private messages.
"""
# To do: Check the caption and description fields for this kind of attachments. # To do: Check the caption and description fields for this kind of attachments.
local_attachments = "" local_attachments = ""
uploader = upload.VkUpload(self.vk.session_object) uploader = upload.VkUpload(self.vk.session_object)
@ -347,7 +352,10 @@ class vkSession(object):
elif i["from"] == "local" and i["type"] == "photo": elif i["from"] == "local" and i["type"] == "photo":
photos = i["file"] photos = i["file"]
description = i["description"] description = i["description"]
if peer_id == None:
r = uploader.photo_wall(photos, caption=description) r = uploader.photo_wall(photos, caption=description)
else:
r = uploader.photo_messages(photos)
id = r[0]["id"] id = r[0]["id"]
owner_id = r[0]["owner_id"] owner_id = r[0]["owner_id"]
local_attachments += "photo{0}_{1},".format(owner_id, id) local_attachments += "photo{0}_{1},".format(owner_id, id)
@ -363,10 +371,18 @@ class vkSession(object):
id = r["id"] id = r["id"]
owner_id = r["owner_id"] owner_id = r["owner_id"]
local_attachments += "audio{0}_{1},".format(owner_id, id) local_attachments += "audio{0}_{1},".format(owner_id, id)
elif i["from"] == "local" and i["type"] == "voice_message":
r = uploader.audio_message(i["file"], peer_id=peer_id)
id = r["audio_message"]["id"]
owner_id = r["audio_message"]["owner_id"]
local_attachments += "audio_message{0}_{1},".format(owner_id, id)
elif i["from"] == "local" and i["type"] == "document": elif i["from"] == "local" and i["type"] == "document":
document = i["file"] document = i["file"]
title = i["title"] title = i["title"]
if peer_id == None:
r = uploader.document(document, title=title, to_wall=True) r = uploader.document(document, title=title, to_wall=True)
else:
r = uploader.document(document, title=title, message_peer_id=peer_id)
id = r["doc"]["id"] id = r["doc"]["id"]
owner_id = r["doc"]["owner_id"] owner_id = r["doc"]["owner_id"]
local_attachments += "doc{0}_{1},".format(owner_id, id) local_attachments += "doc{0}_{1},".format(owner_id, id)