From d78bdb971833d7f11bc03f98d6e5a5459c8c8802 Mon Sep 17 00:00:00 2001 From: Markus-Rost Date: Sat, 22 Aug 2020 19:51:15 +0200 Subject: [PATCH 1/3] link article comments --- src/bot.py | 12 +++++++++++- src/formatters/discussions.py | 24 +++++++++++++----------- src/wiki.py | 8 ++++++-- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/bot.py b/src/bot.py index a69fa96..a681fe7 100644 --- a/src/bot.py +++ b/src/bot.py @@ -369,12 +369,22 @@ async def discussion_handler(): DBHandler.add(db_wiki["wikiid"], "0", True) DBHandler.update_db() continue + comment_events = [] targets = generate_targets(db_wiki["wiki"], "AND NOT wikiid IS NULL") for post in discussion_feed: + if post["_embedded"]["thread"][0]["containerType"] == "ARTICLE_COMMENT" and post["id"] > db_wiki["postid"]: + comment_events.append(post["forumId"]) + comment_pages = {} + if len(comment_events): + comment_pages = await local_wiki.safe_request( + "{wiki}wikia.php?controller=FeedsAndPosts&method=getArticleNamesAndUsernames&stablePageIds={pages}&format=json".format( + wiki=db_wiki["wiki"], pages=','.join(comment_events) + ), rate_limiter, "articleNames") # rate_limiter is undefined, needs fixing + for post in discussion_feed: # Yeah, second loop since the comments require an extra request if post["id"] > db_wiki["postid"]: for target in targets.items(): try: - await essential_feeds(post, db_wiki, target) + await essential_feeds(post, comment_pages, db_wiki, target) except asyncio.CancelledError: raise except: diff --git a/src/formatters/discussions.py b/src/formatters/discussions.py index ad0c940..5ee255b 100644 --- a/src/formatters/discussions.py +++ b/src/formatters/discussions.py @@ -11,7 +11,7 @@ from src.i18n import langs logger = logging.getLogger("rcgcdw.discussion_formatters") -async def feeds_compact_formatter(post_type, post, message_target, wiki): +async def feeds_compact_formatter(post_type, post, message_target, wiki, article_page=None): """Compact formatter for Fandom discussions.""" _ = langs[message_target[0][0]]["discussion_formatters"].gettext message = None @@ -46,11 +46,12 @@ async def feeds_compact_formatter(post_type, post, message_target, wiki): else: message = _("[{author}]({author_url}) created a [reply](<{url}wiki/Message_Wall:{user_wall}?threadId={threadId}#{replyId}>) to [{title}](<{url}wiki/Message_Wall:{user_wall}?threadId={threadId}>) on [{user}'s Message Wall](<{url}wiki/Message_Wall:{user_wall}>)").format(author=author, author_url=author_url, url=wiki, title=post["_embedded"]["thread"][0]["title"], user=user_wall, user_wall=quote_plus(user_wall.replace(" ", "_")), threadId=post["threadId"], replyId=post["id"]) elif post_type == "ARTICLE_COMMENT": - article_page = _("unknown") # No page known + if article_page is None: + article_page = {"title": _("unknown"), "fullUrl": "{wiki}wiki/{article}".format(wiki=wiki, article=_("unknown").replace(" ", "_"))} # No page known if not post["isReply"]: - message = _("[{author}]({author_url}) created a [comment](<{url}wiki/{article}?commentId={commentId}>) on [{article}](<{url}wiki/{article}>)").format(author=author, author_url=author_url, url=wiki, article=article_page, commentId=post["threadId"]) + message = _("[{author}]({author_url}) created a [comment](<{url}?commentId={commentId}>) on [{article}](<{url}>)").format(author=author, author_url=author_url, url=article_page["fullUrl"], article=article_page["title"], commentId=post["threadId"]) else: - message = _("[{author}]({author_url}) created a [reply](<{url}wiki/{article}?threadId={threadId}) to a [comment](<{url}wiki/{article}?commentId={commentId}&replyId={replyId}>) on [{article}](<{url}wiki/{article}>)").format(author=author, author_url=author_url, url=wiki, article=article_page, commentId=post["threadId"], replyId=post["id"]) + message = _("[{author}]({author_url}) created a [reply](<{url}?threadId={threadId}) to a [comment](<{url}?commentId={commentId}&replyId={replyId}>) on [{article}](<{url}>)").format(author=author, author_url=author_url, url=article_page["fullUrl"], article=article_page["title"], commentId=post["threadId"], replyId=post["id"]) else: logger.warning("No entry for {event} with params: {params}".format(event=post_type, params=post)) if not settings["support"]: @@ -60,7 +61,7 @@ async def feeds_compact_formatter(post_type, post, message_target, wiki): await send_to_discord(DiscordMessage("compact", "discussion", message_target[1], content=message, wiki=wiki)) -async def feeds_embed_formatter(post_type, post, message_target, wiki): +async def feeds_embed_formatter(post_type, post, message_target, wiki, article_page=None): """Embed formatter for Fandom discussions.""" _ = langs[message_target[0][0]]["discussion_formatters"].gettext embed = DiscordMessage("embed", "discussion", message_target[1], wiki=wiki) @@ -135,16 +136,17 @@ async def feeds_embed_formatter(post_type, post, message_target, wiki): embed["url"] = "{url}wiki/Message_Wall:{user_wall}?threadId={threadId}#{replyId}".format(url=wiki, user_wall=quote_plus(user_wall.replace(" ", "_")), threadId=post["threadId"], replyId=post["id"]) embed["title"] = _("Replied to \"{title}\" on {user}'s Message Wall").format(title=post["_embedded"]["thread"][0]["title"], user=user_wall) elif post_type == "ARTICLE_COMMENT": - article_page = _("unknown") # No page known + if article_page is None: + article_page = {"title": _("unknown"), "fullUrl": "{wiki}wiki/{article}".format(wiki=wiki, article=_("unknown").replace(" ", "_"))} # No page known if not post["isReply"]: embed.event_type = "discussion/comment/post" - # embed["url"] = "{url}wiki/{article}?commentId={commentId}".format(url=wiki, article=quote_plus(article_page.replace(" ", "_")), commentId=post["threadId"]) - embed["title"] = _("Commented on {article}").format(article=article_page) + embed["url"] = "{url}?commentId={commentId}".format(url=article_page["fullUrl"], commentId=post["threadId"]) + embed["title"] = _("Commented on {article}").format(article=article_page["title"]) else: embed.event_type = "discussion/comment/reply" - # embed["url"] = "{url}wiki/{article}?commentId={commentId}&replyId={replyId}".format(url=wiki, article=quote_plus(article_page.replace(" ", "_")), commentId=post["threadId"], replyId=post["id"]) - embed["title"] = _("Replied to a comment on {article}").format(article=article_page) - embed["footer"]["text"] = article_page + embed["url"] = "{url}?commentId={commentId}&replyId={replyId}".format(url=article_page["fullUrl"], commentId=post["threadId"], replyId=post["id"]) + embed["title"] = _("Replied to a comment on {article}").format(article=article_page["title"]) + embed["footer"]["text"] = article_page["title"] else: logger.warning("No entry for {event} with params: {params}".format(event=post_type, params=post)) embed["title"] = _("Unknown event `{event}`").format(event=post_type) diff --git a/src/wiki.py b/src/wiki.py index 8f05f28..db67c9a 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -225,8 +225,12 @@ async def essential_info(change: dict, changed_categories, local_wiki: Wiki, tar await appearance_mode(identification_string, change, parsed_comment, changed_categories, local_wiki, target, paths, rate_limiter, additional_data=additional_data) -async def essential_feeds(change: dict, db_wiki: tuple, target: tuple): +async def essential_feeds(change: dict, comment_pages, db_wiki: tuple, target: tuple): """Prepares essential information for both embed and compact message format.""" appearance_mode = feeds_embed_formatter if target[0][1] > 0 else feeds_compact_formatter identification_string = change["_embedded"]["thread"][0]["containerType"] - await appearance_mode(identification_string, change, target, db_wiki["wiki"]) + comment_page = None + if identification_string == "ARTICLE_COMMENT": + comment_page = comment_pages[change["forumId"]] + comment_page["fullUrl"] = "/".join(db_wiki["wiki"].split("/", 3)[:3]) + comment_page["relativeUrl"] + await appearance_mode(identification_string, change, target, db_wiki["wiki"], article_page=comment_page) From 5ca8ccc83bd127fc0e7596159f88dd405d6ee27a Mon Sep 17 00:00:00 2001 From: Frisk Date: Sat, 22 Aug 2020 20:28:02 +0200 Subject: [PATCH 2/3] Very small fixes/improvements --- src/bot.py | 8 ++++---- src/wiki.py | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/bot.py b/src/bot.py index a681fe7..1e48162 100644 --- a/src/bot.py +++ b/src/bot.py @@ -374,12 +374,12 @@ async def discussion_handler(): for post in discussion_feed: if post["_embedded"]["thread"][0]["containerType"] == "ARTICLE_COMMENT" and post["id"] > db_wiki["postid"]: comment_events.append(post["forumId"]) - comment_pages = {} - if len(comment_events): + comment_pages: dict = {} + if comment_events: comment_pages = await local_wiki.safe_request( "{wiki}wikia.php?controller=FeedsAndPosts&method=getArticleNamesAndUsernames&stablePageIds={pages}&format=json".format( - wiki=db_wiki["wiki"], pages=','.join(comment_events) - ), rate_limiter, "articleNames") # rate_limiter is undefined, needs fixing + wiki=db_wiki["wiki"], pages=",".join(comment_events) + ), RateLimiter(), "articleNames") for post in discussion_feed: # Yeah, second loop since the comments require an extra request if post["id"] > db_wiki["postid"]: for target in targets.items(): diff --git a/src/wiki.py b/src/wiki.py index db67c9a..6b31afc 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -8,6 +8,7 @@ from src.formatters.discussions import feeds_embed_formatter, feeds_compact_form from src.misc import parse_link from src.i18n import langs from src.wiki_ratelimiter import RateLimiter +import sqlite3 import src.discord import asyncio from src.config import settings @@ -225,7 +226,7 @@ async def essential_info(change: dict, changed_categories, local_wiki: Wiki, tar await appearance_mode(identification_string, change, parsed_comment, changed_categories, local_wiki, target, paths, rate_limiter, additional_data=additional_data) -async def essential_feeds(change: dict, comment_pages, db_wiki: tuple, target: tuple): +async def essential_feeds(change: dict, comment_pages: dict, db_wiki: sqlite3.Row, target: tuple): """Prepares essential information for both embed and compact message format.""" appearance_mode = feeds_embed_formatter if target[0][1] > 0 else feeds_compact_formatter identification_string = change["_embedded"]["thread"][0]["containerType"] From 0a914677d518d22c256c2c1bcf81e2de20031089 Mon Sep 17 00:00:00 2001 From: Markus-Rost Date: Sat, 22 Aug 2020 21:26:01 +0200 Subject: [PATCH 3/3] fix malformed translation string --- src/formatters/discussions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/formatters/discussions.py b/src/formatters/discussions.py index 5ee255b..68eaf38 100644 --- a/src/formatters/discussions.py +++ b/src/formatters/discussions.py @@ -51,7 +51,7 @@ async def feeds_compact_formatter(post_type, post, message_target, wiki, article if not post["isReply"]: message = _("[{author}]({author_url}) created a [comment](<{url}?commentId={commentId}>) on [{article}](<{url}>)").format(author=author, author_url=author_url, url=article_page["fullUrl"], article=article_page["title"], commentId=post["threadId"]) else: - message = _("[{author}]({author_url}) created a [reply](<{url}?threadId={threadId}) to a [comment](<{url}?commentId={commentId}&replyId={replyId}>) on [{article}](<{url}>)").format(author=author, author_url=author_url, url=article_page["fullUrl"], article=article_page["title"], commentId=post["threadId"], replyId=post["id"]) + message = _("[{author}]({author_url}) created a [reply](<{url}?commentId={commentId}&replyId={replyId}>) to a [comment](<{url}?commentId={commentId}>) on [{article}](<{url}>)").format(author=author, author_url=author_url, url=article_page["fullUrl"], article=article_page["title"], commentId=post["threadId"], replyId=post["id"]) else: logger.warning("No entry for {event} with params: {params}".format(event=post_type, params=post)) if not settings["support"]: