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 = [] attachments = []
if hasattr(p, "attachments"): if hasattr(p, "attachments"):
attachments = 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): def connect_events(self):
""" Bind all events to this buffer""" """ Bind all events to this buffer"""
@ -417,7 +417,7 @@ class feedBuffer(baseBuffer):
attachments = [] attachments = []
if hasattr(p, "attachments"): if hasattr(p, "attachments"):
attachments = 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): def open_in_browser(self, *args, **kwargs):
post = self.get_post() post = self.get_post()
@ -478,7 +478,7 @@ class communityBuffer(feedBuffer):
attachments = [] attachments = []
if hasattr(p, "attachments"): if hasattr(p, "attachments"):
attachments = 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): class topicBuffer(feedBuffer):
@ -531,7 +531,7 @@ class topicBuffer(feedBuffer):
attachments = [] attachments = []
if hasattr(p, "attachments"): if hasattr(p, "attachments"):
attachments = 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): class documentBuffer(feedBuffer):
can_get_items = False can_get_items = False

View File

@ -332,14 +332,10 @@ class Controller(object):
pub.unsubscribe(self.notify, "notify") pub.unsubscribe(self.notify, "notify")
pub.subscribe(self.create_timeline, "create-timeline") pub.subscribe(self.create_timeline, "create-timeline")
def in_post(self, buffer): def in_post(self, from_buffer=None):
""" This event is triggered whenever an user requires an update in their buffers. For example after sending a post successfully. if from_buffer != None:
The function updates the main newsfeed buffer, and the buffer from where the post was sent. log.debug("Post received in buffer %s, updating... " % (from_buffer,))
@buffer str: name of the buffer where the post has been generated. buffer = self.search(from_buffer)
"""
buffer = self.search(buffer)
buffer.get_items()
buffer = self.search("home_timeline")
buffer.get_items() buffer.get_items()
def download(self, url, filename): 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") user = self.vk.client.users.get(fields="uid, first_name, last_name")
self.user_id = user[0]["id"] 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. """ 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, 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:
try:
attachments = self.upload_attachments(attachments_list, post_arguments.get("peer_id")) 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 # 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:
@ -334,9 +338,16 @@ class vkSession(object):
else: 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) endpoint = getattr(self.vk.client, parent_endpoint)
endpoint = getattr(parent_endpoint, child_endpoint) endpoint = getattr(endpoint, child_endpoint)
try:
post = endpoint(**post_arguments) 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): def upload_attachments(self, attachments, peer_id=None):
""" Upload attachments to VK before posting them. """ Upload attachments to VK before posting them.