Merge branch 'comments' into 'master'

link article comments

See merge request chicken-riders/RcGcDb!15
This commit is contained in:
MarkusRost 2020-08-22 19:29:28 +00:00
commit 7808c4b47d
3 changed files with 31 additions and 14 deletions

View file

@ -369,12 +369,22 @@ async def discussion_handler():
DBHandler.add(db_wiki["wikiid"], "0", True) DBHandler.add(db_wiki["wikiid"], "0", True)
DBHandler.update_db() DBHandler.update_db()
continue continue
comment_events = []
targets = generate_targets(db_wiki["wiki"], "AND NOT wikiid IS NULL") targets = generate_targets(db_wiki["wiki"], "AND NOT wikiid IS NULL")
for post in discussion_feed: 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: 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)
), RateLimiter(), "articleNames")
for post in discussion_feed: # Yeah, second loop since the comments require an extra request
if post["id"] > db_wiki["postid"]: if post["id"] > db_wiki["postid"]:
for target in targets.items(): for target in targets.items():
try: try:
await essential_feeds(post, db_wiki, target) await essential_feeds(post, comment_pages, db_wiki, target)
except asyncio.CancelledError: except asyncio.CancelledError:
raise raise
except: except:

View file

@ -11,7 +11,7 @@ from src.i18n import langs
logger = logging.getLogger("rcgcdw.discussion_formatters") 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.""" """Compact formatter for Fandom discussions."""
_ = langs[message_target[0][0]]["discussion_formatters"].gettext _ = langs[message_target[0][0]]["discussion_formatters"].gettext
message = None message = None
@ -46,11 +46,12 @@ async def feeds_compact_formatter(post_type, post, message_target, wiki):
else: 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"]) 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": 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"]: 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: 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}?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: else:
logger.warning("No entry for {event} with params: {params}".format(event=post_type, params=post)) logger.warning("No entry for {event} with params: {params}".format(event=post_type, params=post))
if not settings["support"]: 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)) 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.""" """Embed formatter for Fandom discussions."""
_ = langs[message_target[0][0]]["discussion_formatters"].gettext _ = langs[message_target[0][0]]["discussion_formatters"].gettext
embed = DiscordMessage("embed", "discussion", message_target[1], wiki=wiki) 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["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) 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": 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"]: if not post["isReply"]:
embed.event_type = "discussion/comment/post" 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["url"] = "{url}?commentId={commentId}".format(url=article_page["fullUrl"], commentId=post["threadId"])
embed["title"] = _("Commented on {article}").format(article=article_page) embed["title"] = _("Commented on {article}").format(article=article_page["title"])
else: else:
embed.event_type = "discussion/comment/reply" 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["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) embed["title"] = _("Replied to a comment on {article}").format(article=article_page["title"])
embed["footer"]["text"] = article_page embed["footer"]["text"] = article_page["title"]
else: else:
logger.warning("No entry for {event} with params: {params}".format(event=post_type, params=post)) logger.warning("No entry for {event} with params: {params}".format(event=post_type, params=post))
embed["title"] = _("Unknown event `{event}`").format(event=post_type) embed["title"] = _("Unknown event `{event}`").format(event=post_type)

View file

@ -8,6 +8,7 @@ from src.formatters.discussions import feeds_embed_formatter, feeds_compact_form
from src.misc import parse_link from src.misc import parse_link
from src.i18n import langs from src.i18n import langs
from src.wiki_ratelimiter import RateLimiter from src.wiki_ratelimiter import RateLimiter
import sqlite3
import src.discord import src.discord
import asyncio import asyncio
from src.config import settings from src.config import settings
@ -225,8 +226,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) 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: dict, db_wiki: sqlite3.Row, target: tuple):
"""Prepares essential information for both embed and compact message format.""" """Prepares essential information for both embed and compact message format."""
appearance_mode = feeds_embed_formatter if target[0][1] > 0 else feeds_compact_formatter appearance_mode = feeds_embed_formatter if target[0][1] > 0 else feeds_compact_formatter
identification_string = change["_embedded"]["thread"][0]["containerType"] 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)