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,26 +405,36 @@ 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
]
co = HTML_PROCESSOR.from_markdown("\n".join([ all_changes = changes + event_changes + user_changes
"%1 changed the room's permissions",
"", if len(all_changes) == 1:
"Change | Previous | Current ", co = HTML_PROCESSOR.from_markdown(
"--- | --- | ---", "%%1 changed the level for **%s**: %s%s " % (
*changes_lines, all_changes[0][0],
*sorted(event_changes), lvl(all_changes[0][1]).lower(),
*sorted(user_changes), lvl(all_changes[0][2]).lower(),
])) ),
)
else:
co = HTML_PROCESSOR.from_markdown("\n".join([
"%1 changed the room's permissions",
"",
"Change | Previous | Current ",
"--- | --- | ---",
*[
f"{name} | {lvl(old)} | {lvl(now)}"
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"