From 21932dc329efeb19e7773796a2e41aaa4f317937 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Sun, 3 Feb 2019 18:54:31 -0600 Subject: [PATCH] Group mentions are displayed properly when mentioned in comments --- src/presenters/postDisplayer.py | 25 ++++++++++++------------- src/sessionmanager/utils.py | 4 ++-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/presenters/postDisplayer.py b/src/presenters/postDisplayer.py index 820a51b..76afd5c 100644 --- a/src/presenters/postDisplayer.py +++ b/src/presenters/postDisplayer.py @@ -22,14 +22,6 @@ from .postCreation import createPostPresenter log = logging.getLogger(__file__) -def get_user(id, profiles): - """ Returns an user name and last name based in the id receibed.""" - for i in profiles: - if i["id"] == id: - return "{0} {1}".format(i["first_name"], i["last_name"]) - # Translators: This string is used when socializer can't find the right user information. - return _("Unknown username") - def get_message(status): message = "" if "text" in status: @@ -77,13 +69,18 @@ class displayPostPresenter(base.basePresenter): id = self.post[self.post_identifier] self.comments = self.session.vk.client.wall.getComments(owner_id=user, post_id=id, need_likes=1, count=100, extended=1, preview_length=0, thread_items_count=10) comments_ = [] + # Save profiles in session local storage for a future usage. + # Although community objects are returned here, we should not add those because their names are changed. + # For example, self reference to a group is marked as "Administrator", which would ruin this profile to be rendered somewhere else. + data = dict(groups=[], profiles=self.comments["profiles"]) + self.session.process_usernames(data) for i in self.comments["items"]: # If comment has a "deleted" key it should not be displayed, obviously. if "deleted" in i: continue - from_ = get_user(i["from_id"], self.comments["profiles"]) + from_ = self.session.get_user(i["from_id"])["user1_nom"] if "reply_to_user" in i: - extra_info = get_user(i["reply_to_user"], self.comments["profiles"]) + extra_info = self.session.get_user(i["reply_to_user"])["user1_nom"] from_ = _("{0} > {1}").format(from_, extra_info) # As we set the comment reply properly in the from_ field, let's remove the first username from here if it exists. fixed_text = re.sub("^\[id\d+\|\D+\], ", "", i["text"]) @@ -543,13 +540,15 @@ class displayTopicPresenter(displayPostPresenter): """ 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) comments_ = [] + data = dict(profiles=self.comments["profiles"], groups=[]) + self.session.process_usernames(data) for i in self.comments["items"]: # If comment has a "deleted" key it should not be displayed, obviously. if "deleted" in i: continue - from_ = get_user(i["from_id"], self.comments["profiles"]) + from_ = self.session.get_user(i["from_id"])["user1_nom"] # match user mentions inside text comment. - matched_data = re.match(".*(\[id\d+:bp-\d+_\d+\|)(\D+)(\])", i["text"]) + matched_data = re.match(".*(\[)(id|club)(\d+:bp-\d+_\d+\|)(\D+)(\])", i["text"]) # If matched data exists we should modify the title. # if len(matched_data.groups()) > 2: # from_ = "{from_} > {to_}".format(from_=from_, to_=matched_data.groups()[1]) @@ -557,7 +556,7 @@ class displayTopicPresenter(displayPostPresenter): created_at = original_date.humanize(locale=languageHandler.curLang[:2]) likes = str(i["likes"]["count"]) if matched_data != None: - text = re.sub("\[id\d+:bp-\d+_\d+\|\D+\]", matched_data.groups()[1]+", ", i["text"]) + text = re.sub("\[(id|club)\d+:bp-\d+_\d+\|\D+\]", matched_data.groups()[3]+", ", i["text"]) else: text = i["text"] comments_.append((from_, text, created_at, likes)) diff --git a/src/sessionmanager/utils.py b/src/sessionmanager/utils.py index b697509..1617000 100644 --- a/src/sessionmanager/utils.py +++ b/src/sessionmanager/utils.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- """ Some utilities. I no have idea how I should put these, so...""" -from __future__ import division -from __future__ import unicode_literals import os import requests import re @@ -12,6 +10,8 @@ url_re = re.compile("(?:\w+://|www\.)[^ ,.?!#%=+][^ ]*") bad_chars = '\'\\.,[](){}:;"' def seconds_to_string(seconds, precision=0): + """ convert a number of seconds in a string representation.""" + # ToDo: Improve it to handle properly Russian plurals. day = seconds // 86400 hour = seconds // 3600 min = (seconds // 60) % 60