From e32336aff89b87528cf1b81420e441f4c880734d Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Tue, 23 Apr 2019 13:13:58 -0500 Subject: [PATCH] Added support for loading all comments in topics --- changelog.md | 1 + src/interactors/postDisplayer.py | 13 +++++++++ src/presenters/displayPosts/topic.py | 40 ++++++++++++++++++++++++++-- src/views/dialogs/postDisplay.py | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 62c0af7..8e968bb 100644 --- a/changelog.md +++ b/changelog.md @@ -14,6 +14,7 @@ * When displaying a post, if you press enter in the button indicating how many people liked or shared the post, Socializer will display the listt of people who have liked or shared it. You can use the context menu in the list to do certain actions. [#38,](https://code.manuelcortez.net/manuelcortez/socializer/issues/38) * it is possible to retrieve more items for conversation buffers. Due to API limitations, it is possible to load up to the last 600 messages for every conversation. Take into account that the process of loading more items takes some time. You will hear a message when the process is done. * It is possible to delete entire conversations from the buffer's tree, by using the menu key and selecting "delete conversation". The conversation will be removed from VK. +* When loading a topic in a group, socializer will display the latest 100 messages. In order to load more messages, you will find a button that will load the previous 100 messages present in the topic, or the amount of messages not loaded yet. ### bugfixes diff --git a/src/interactors/postDisplayer.py b/src/interactors/postDisplayer.py index 4fffa83..8e86252 100644 --- a/src/interactors/postDisplayer.py +++ b/src/interactors/postDisplayer.py @@ -29,6 +29,11 @@ class displayPostInteractor(base.baseInteractor): for i in items: getattr(self.view, control).insert_item(False, *i) + def add_item(self, control, item, reversed=False): + if not hasattr(self.view, control): + raise AttributeError("The control is not present in the view.") + getattr(self.view, control).insert_item(reversed, *item) + def enable_attachments(self): self.view.attachments.list.Enable(True) @@ -59,11 +64,14 @@ class displayPostInteractor(base.baseInteractor): self.view.comments.list.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.on_focus) if hasattr(self.view, "reply"): widgetUtils.connect_event(self.view.reply, widgetUtils.BUTTON_PRESSED, self.on_reply) + if hasattr(self.view, "load_more_comments"): + widgetUtils.connect_event(self.view.load_more_comments, widgetUtils.BUTTON_PRESSED, self.on_load_more_comments) # self.view.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.on_show_menu, self.view.comments.list) # self.view.Bind(wx.EVT_LIST_KEY_DOWN, self.on_show_menu_by_key, self.view.comments.list) pub.subscribe(self.set, self.modulename+"_set") pub.subscribe(self.load_image, self.modulename+"_load_image") pub.subscribe(self.add_items, self.modulename+"_add_items") + pub.subscribe(self.add_item, self.modulename+"_add_item") pub.subscribe(self.enable_attachments, self.modulename+"_enable_attachments") pub.subscribe(self.enable_photo_controls, self.modulename+"_enable_photo_controls") pub.subscribe(self.post_deleted, self.modulename+"_post_deleted") @@ -75,6 +83,7 @@ class displayPostInteractor(base.baseInteractor): pub.unsubscribe(self.set, self.modulename+"_set") pub.unsubscribe(self.load_image, self.modulename+"_load_image") pub.unsubscribe(self.add_items, self.modulename+"_add_items") + pub.unsubscribe(self.add_item, self.modulename+"_add_item") pub.unsubscribe(self.enable_attachments, self.modulename+"_enable_attachments") pub.unsubscribe(self.enable_photo_controls, self.modulename+"_enable_photo_controls") pub.unsubscribe(self.post_deleted, self.modulename+"_post_deleted") @@ -103,6 +112,10 @@ class displayPostInteractor(base.baseInteractor): def on_add_comment(self, *args, **kwargs): self.presenter.add_comment() + def on_load_more_comments(self, *args, **kwargs): + if hasattr(self.presenter, "load_more_comments"): + self.presenter.load_more_comments() + def on_show_tools_menu(self, *args, **kwargs): menu = menus.toolsMenu() widgetUtils.connect_event(self.view, widgetUtils.MENU, self.on_open_url, menuitem=menu.url) diff --git a/src/presenters/displayPosts/topic.py b/src/presenters/displayPosts/topic.py index 1ee8b92..ffd3a3e 100644 --- a/src/presenters/displayPosts/topic.py +++ b/src/presenters/displayPosts/topic.py @@ -51,10 +51,19 @@ class displayTopicPresenter(basePost.displayPostPresenter): def get_comments(self): """ Get comments and insert them in a list.""" - self.comments = self.session.vk.client.board.getComments(group_id=self.group_id, topic_id=self.post["id"], need_likes=1, count=100, extended=1) + self.comments = self.session.vk.client.board.getComments(group_id=self.group_id, topic_id=self.post["id"], need_likes=1, count=100, extended=1, sort="desc") comments_ = [] data = dict(profiles=self.comments["profiles"], groups=[]) self.session.process_usernames(data) + self.comments["items"].reverse() + # If there are less than 100 comments in the topic we should disable the "load previous" button. + if self.comments["count"] <= 100: + self.send_message("disable_control", control="load_more_comments") + else: + left_comments = self.comments["count"]-len(self.comments["items"]) + if left_comments > 100: + left_comments = 100 + self.send_message("set_label", control="load_more_comments", label=_("Load {comments} previous comments").format(comments=left_comments)) for i in self.comments["items"]: # If comment has a "deleted" key it should not be displayed, obviously. if "deleted" in i: @@ -134,4 +143,31 @@ class displayTopicPresenter(basePost.displayPostPresenter): def show_comment(self, comment_index): c = self.comments["items"][comment_index] c["post_id"] = self.post["id"] - a = displayTopicCommentPresenter(session=self.session, postObject=c, interactor=interactors.displayPostInteractor(), view=views.displayComment()) \ No newline at end of file + a = displayTopicCommentPresenter(session=self.session, postObject=c, interactor=interactors.displayPostInteractor(), view=views.displayComment()) + + def load_more_comments(self): + offset = len(self.comments["items"]) + comments = self.session.vk.client.board.getComments(group_id=self.group_id, topic_id=self.post["id"], need_likes=1, count=100, extended=1, sort="desc", offset=offset) + data = dict(profiles=comments["profiles"], groups=[]) + self.session.process_usernames(data) + # If there are less than 100 comments in the topic we should disable the "load previous" button. + for i in comments["items"]: + self.comments["items"].insert(0, i) + for i in comments["items"]: + # If comment has a "deleted" key it should not be displayed, obviously. + if "deleted" in i: + continue + from_ = self.session.get_user(i["from_id"])["user1_nom"] + # match user mentions inside text comment. + original_date = arrow.get(i["date"]) + created_at = original_date.humanize(locale=languageHandler.curLang[:2]) + likes = str(i["likes"]["count"]) + text = utils.clean_text(text=i["text"]) + self.send_message("add_item", control="comments", item=(from_, text, created_at, likes), reversed=True) + if len(self.comments["items"]) == self.comments["count"]: + self.send_message("disable_control", control="load_more_comments") + else: + left_comments = self.comments["count"]-len(self.comments["items"]) + if left_comments > 100: + left_comments = 100 + self.send_message("set_label", control="load_more_comments", label=_("Load {comments} previous comments").format(comments=left_comments)) diff --git a/src/views/dialogs/postDisplay.py b/src/views/dialogs/postDisplay.py index 0ed1c62..c4d067f 100644 --- a/src/views/dialogs/postDisplay.py +++ b/src/views/dialogs/postDisplay.py @@ -221,6 +221,7 @@ class displayTopic(displayBasicPost): def create_comments_list(self): lbl = wx.StaticText(self.panel, -1, _("Posts")) self.comments = widgetUtils.list(self.panel, _("User"), _("Comment"), _("Date"), _("Likes"), style=wx.LC_REPORT) + self.load_more_comments = wx.Button(self.panel, wx.NewId(), _("Load previous comments")) self.reply = wx.Button(self.panel, -1, _("Reply")) box = wx.BoxSizer(wx.HORIZONTAL) box.Add(lbl, 0, wx.ALL, 5)