Added error reports to failed posts in VK (pubsub). Added an extra param called from_post which specifies which buffer will get updated if the post is created successfully

This commit is contained in:
Manuel Cortez 2019-08-27 16:59:57 -05:00
parent a7173adc22
commit f41dd51dc7
3 changed files with 26 additions and 19 deletions

View File

@ -130,7 +130,7 @@ class baseBuffer(object):
attachments = []
if hasattr(p, "attachments"):
attachments = p.attachments
call_threaded(pub.sendMessage, "post", parent_endpoint="wall", child_endpoint="post", attachments_list=attachments, post_arguments=post_arguments)
call_threaded(pub.sendMessage, "post", parent_endpoint="wall", child_endpoint="post", from_buffer=self.name, attachments_list=attachments, post_arguments=post_arguments)
def connect_events(self):
""" Bind all events to this buffer"""
@ -417,7 +417,7 @@ class feedBuffer(baseBuffer):
attachments = []
if hasattr(p, "attachments"):
attachments = p.attachments
call_threaded(pub.sendMessage, "post", parent_endpoint="wall", child_endpoint="post", attachments_list=attachments, post_arguments=post_arguments)
call_threaded(pub.sendMessage, "post", parent_endpoint="wall", child_endpoint="post", from_buffer=self.name, attachments_list=attachments, post_arguments=post_arguments)
def open_in_browser(self, *args, **kwargs):
post = self.get_post()
@ -478,7 +478,7 @@ class communityBuffer(feedBuffer):
attachments = []
if hasattr(p, "attachments"):
attachments = p.attachments
call_threaded(pub.sendMessage, "post", parent_endpoint="wall", child_endpoint="post", attachments_list=attachments, post_arguments=post_arguments)
call_threaded(pub.sendMessage, "post", parent_endpoint="wall", child_endpoint="post", from_buffer=self.name, attachments_list=attachments, post_arguments=post_arguments)
class topicBuffer(feedBuffer):
@ -531,7 +531,7 @@ class topicBuffer(feedBuffer):
attachments = []
if hasattr(p, "attachments"):
attachments = p.attachments
call_threaded(pub.sendMessage, "post", parent_endpoint="board", child_endpoint="addTopic", attachments_list=attachments, post_arguments=post_arguments)
call_threaded(pub.sendMessage, "post", parent_endpoint="board", child_endpoint="addTopic", from_buffer=self.name, attachments_list=attachments, post_arguments=post_arguments)
class documentBuffer(feedBuffer):
can_get_items = False

View File

@ -332,14 +332,10 @@ class Controller(object):
pub.unsubscribe(self.notify, "notify")
pub.subscribe(self.create_timeline, "create-timeline")
def in_post(self, buffer):
""" This event is triggered whenever an user requires an update in their buffers. For example after sending a post successfully.
The function updates the main newsfeed buffer, and the buffer from where the post was sent.
@buffer str: name of the buffer where the post has been generated.
"""
buffer = self.search(buffer)
buffer.get_items()
buffer = self.search("home_timeline")
def in_post(self, from_buffer=None):
if from_buffer != None:
log.debug("Post received in buffer %s, updating... " % (from_buffer,))
buffer = self.search(from_buffer)
buffer.get_items()
def download(self, url, filename):

View File

@ -308,14 +308,18 @@ class vkSession(object):
user = self.vk.client.users.get(fields="uid, first_name, last_name")
self.user_id = user[0]["id"]
def post(self, parent_endpoint, child_endpoint, attachments_list=[], post_arguments={}):
def post(self, parent_endpoint, child_endpoint, from_buffer=None, attachments_list=[], post_arguments={}):
""" Generic function to be called whenever user wants to post something to VK.
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."""
attachments = ""
print(attachments_list)
if len(attachments_list) > 0:
try:
attachments = self.upload_attachments(attachments_list, post_arguments.get("peer_id"))
except Exception as error:
log.exception("Error calling method %s.%s with arguments: %r. Failed during loading attachments. Error: %s" % (parent_endpoint, child_endpoint, post_arguments, str(error)))
# Report a failed function here too with same arguments so the client should be able to recreate it again.
pub.sendMessage("postFailed", parent_endpoint=parent_endpoint, child_endpoint=child_endpoint, from_buffer=from_buffer, attachments_list=attachments_list, post_arguments=post_arguments)
# 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"]
for i in possible_message_keys:
@ -334,9 +338,16 @@ class vkSession(object):
else:
post_arguments.update(attachments=attachments)
# Determines the correct functions to call here.
parent_endpoint = getattr(self.vk.client, parent_endpoint)
endpoint = getattr(parent_endpoint, child_endpoint)
endpoint = getattr(self.vk.client, parent_endpoint)
endpoint = getattr(endpoint, child_endpoint)
try:
post = endpoint(**post_arguments)
# Once the post has been send, let's report it to the interested objects.
pub.sendMessage("posted", from_buffer=from_buffer)
except Exception as error:
log.exception("Error calling method %s.%s with arguments: %r. Error: %s" % (parent_endpoint, child_endpoint, post_arguments, str(error)))
# Report a failed function here too with same arguments so the client should be able to recreate it again.
pub.sendMessage("postFailed", parent_endpoint=parent_endpoint, child_endpoint=child_endpoint, from_buffer=from_buffer, attachments_list=attachments_list, post_arguments=post_arguments)
def upload_attachments(self, attachments, peer_id=None):
""" Upload attachments to VK before posting them.