This commit is contained in:
Frisk 2020-11-15 01:43:34 +01:00
parent 8d7da1d237
commit 0cdc0cef32
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
2 changed files with 44 additions and 13 deletions

View file

@ -1,21 +1,27 @@
import logging
from src.configloader import settings
from src.discord.message import DiscordMessageMetadata
from src.discord.message import DiscordMessageMetadata, DiscordMessage
from src.discord.queue import send_to_discord, messagequeue
from src.fileio.database import db_cursor, db_connection
logger = logging.getLogger("rcgcdw.discord.redaction")
def delete_messages(pageid: int):
"""Delete messages that match that pageid"""
logger.debug(type(pageid))
to_delete = db_cursor.execute("SELECT msg_id FROM event WHERE pageid = ?", (pageid,))
def delete_messages(matching_data: dict):
"""Delete messages that match given data"""
sql_conditions = ""
args = []
for data in matching_data.items():
sql_conditions += "? = ? AND"
args.extend(data)
else:
sql_conditions = sql_conditions[0:-4] # remove last AND statement
to_delete = db_cursor.execute("SELECT msg_id FROM event WHERE {CON}".format(CON=sql_conditions), args)
if len(messagequeue) > 0:
messagequeue.delete_all_with_matching_metadata(pageid=pageid)
messagequeue.delete_all_with_matching_metadata(**matching_data)
msg_to_remove = []
logger.debug("Deleting messages for pageid: {}".format(pageid))
logger.debug("Deleting messages for data: {}".format(matching_data))
for message in to_delete:
webhook_url = "{main_webhook}/messages/{message_id}".format(main_webhook=settings["webhookURL"], message_id=message[0])
msg_to_remove.append(message[0])
@ -31,4 +37,27 @@ def redact_messages(ids: list, entry_type: int, to_censor: dict):
ids: list of ints
entry_type: int - 0 for revdel, 1 for logdel
to_censor: dict - logparams of message parts to censor"""
for event_id in ids:
if entry_type == 0: # TODO check if queries are proper
message = db_cursor.execute("SELECT content FROM messages INNER JOIN event ON event.msg_id = messages.message_id WHERE event.revid = ?;", event_id)
else:
message = db_cursor.execute(
"SELECT content FROM messages INNER JOIN event ON event.msg_id = messages.message_id WHERE event.logid = ?;",
event_id)
if settings["appearance"]["mode"] == "embed":
if message is not None:
message = message.fetchone()
new_embed = message["embeds"][0]
if "user" in to_censor:
new_embed["author"]["name"] = _("Removed")
del new_embed["author"]["url"]
if "action" in to_censor:
new_embed["title"] = _("Removed")
del new_embed["url"]
if "comment" in to_censor:
new_embed["description"] = _("Removed")
message["embeds"][0] = new_embed
# TODO somehow send nly important data as PATCH?
send_to_discord()
raise NotImplemented

View file

@ -112,13 +112,13 @@ def compact_formatter(action, change, parsed_comment, categories, recent_changes
content = "🗑️ "+_("[{author}]({author_url}) deleted [{page}]({page_link}){comment}").format(author=author, author_url=author_url, page=change["title"], page_link=page_link,
comment=parsed_comment)
if AUTO_SUPPRESSION_ENABLED:
delete_messages(change.get("pageid"))
delete_messages(dict(pageid=change.get("pageid")))
elif action == "delete/delete_redir":
page_link = link_formatter(create_article_path(change["title"]))
content = "🗑️ "+_("[{author}]({author_url}) deleted redirect by overwriting [{page}]({page_link}){comment}").format(author=author, author_url=author_url, page=change["title"], page_link=page_link,
comment=parsed_comment)
if AUTO_SUPPRESSION_ENABLED:
delete_messages(change.get("pageid"))
delete_messages(dict(pageid=change.get("pageid")))
elif action == "move/move":
link = link_formatter(create_article_path(change["logparams"]['target_title']))
redirect_status = _("without making a redirect") if "suppressredirect" in change["logparams"] else _("with a redirect")
@ -278,10 +278,11 @@ def compact_formatter(action, change, parsed_comment, categories, recent_changes
if AUTO_SUPPRESSION_ENABLED:
try:
logparams = change["logparams"]
pageid = change["pageid"]
except KeyError:
pass
else:
# TODO Get pageid
delete_messages(dict(pageid=pageid))
elif action == "import/upload":
link = link_formatter(create_article_path(change["title"]))
content = "📥 "+ngettext("[{author}]({author_url}) imported [{article}]({article_url}) with {count} revision{comment}",
@ -298,7 +299,8 @@ def compact_formatter(action, change, parsed_comment, categories, recent_changes
except KeyError:
pass
else:
delete_messages(logparams.get("ids", []), 1, logparams.get("new", {})) # TODO Check validity
for revid in logparams.get("ids", []):
delete_messages(dict(revid=revid))
elif action == "import/interwiki":
content = "📥 "+_("[{author}]({author_url}) imported interwiki{comment}").format(author=author, author_url=author_url, comment=parsed_comment)
elif action == "abusefilter/modify":
@ -581,12 +583,12 @@ def embed_formatter(action, change, parsed_comment, categories, recent_changes):
link = create_article_path(change["title"])
embed["title"] = _("Deleted page {article}").format(article=change["title"])
if AUTO_SUPPRESSION_ENABLED:
delete_messages(change.get("pageid"))
delete_messages(dict(pageid=change.get("pageid")))
elif action == "delete/delete_redir":
link = create_article_path(change["title"])
embed["title"] = _("Deleted redirect {article} by overwriting").format(article=change["title"])
if AUTO_SUPPRESSION_ENABLED:
delete_messages(change.get("pageid"))
delete_messages(dict(pageid=change.get("pageid")))
elif action == "move/move":
link = create_article_path(change["logparams"]['target_title'])
parsed_comment = "{supress}. {desc}".format(desc=parsed_comment,