Format single-change power level event in one line

This commit is contained in:
miruka 2020-07-16 12:40:47 -04:00
parent 9674bf6a84
commit 49a93ebf2b

View File

@ -5,7 +5,7 @@ import logging as log
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime, timedelta from datetime import datetime, timedelta
from html import escape from html import escape
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
from urllib.parse import quote from urllib.parse import quote
import nio import nio
@ -343,9 +343,9 @@ class NioCallbacks:
# Event formatting # Event formatting
changes = [] changes: List[Tuple[str, int, int]] = []
event_changes = [] event_changes: List[Tuple[str, int, int]] = []
user_changes = [] user_changes: List[Tuple[str, int, int]] = []
def lvl(level: int) -> str: def lvl(level: int) -> str:
return ( return (
@ -397,7 +397,7 @@ class NioCallbacks:
) )
if level != old or not previous: if level != old or not previous:
event_changes.append(f"{ev_type} | {lvl(old)} | {lvl(level)}") event_changes.append((ev_type, old, level))
# User level changes # User level changes
@ -405,25 +405,35 @@ class NioCallbacks:
old = users_previous.get(user_id, levels.defaults.users_default) old = users_previous.get(user_id, levels.defaults.users_default)
if level != old or not previous: if level != old or not previous:
user_changes.append(f"{user_id} | {lvl(old)} | {lvl(level)}") user_changes.append((user_id, old, level))
# Gather and format changes # Gather and format changes
if changes or event_changes or user_changes: if changes or event_changes or user_changes:
changes.sort(key=lambda c: (c[2], c[0])) changes.sort(key=lambda c: (c[2], c[0]))
changes_lines = [ event_changes.sort(key=lambda c: (c[2], c[0]))
f"{name} | {lvl(old)} | {lvl(now)}" user_changes.sort(key=lambda c: (c[2], c[0]))
for name, old, now in changes
]
all_changes = changes + event_changes + user_changes
if len(all_changes) == 1:
co = HTML_PROCESSOR.from_markdown(
"%%1 changed the level for **%s**: %s%s " % (
all_changes[0][0],
lvl(all_changes[0][1]).lower(),
lvl(all_changes[0][2]).lower(),
),
)
else:
co = HTML_PROCESSOR.from_markdown("\n".join([ co = HTML_PROCESSOR.from_markdown("\n".join([
"%1 changed the room's permissions", "%1 changed the room's permissions",
"", "",
"Change | Previous | Current ", "Change | Previous | Current ",
"--- | --- | ---", "--- | --- | ---",
*changes_lines, *[
*sorted(event_changes), f"{name} | {lvl(old)} | {lvl(now)}"
*sorted(user_changes), for name, old, now in all_changes
],
])) ]))
else: else:
co = "%1 didn't change the room's permissions" co = "%1 didn't change the room's permissions"