added admins and banning
this took more time than i thought it would
This commit is contained in:
+187
-2
@@ -1,5 +1,6 @@
|
||||
import sys, os, io
|
||||
import asyncio, threading, time, textwrap, re
|
||||
import humanize, datetime
|
||||
import encodings.aliases
|
||||
|
||||
import prompt_toolkit as pt
|
||||
@@ -317,6 +318,31 @@ async def spawnSimplePopup(title,body):
|
||||
|
||||
return await res
|
||||
|
||||
async def spawnYesNoPopup(title,body):
|
||||
title = str(title)
|
||||
if type(body)==str:
|
||||
body=Label(body)
|
||||
|
||||
res = asyncio.get_running_loop().create_future()
|
||||
|
||||
def handler(r):
|
||||
if not res.done():
|
||||
res.set_result(r)
|
||||
despawn()
|
||||
|
||||
dlg = Dialog(
|
||||
title=title,
|
||||
body=body,
|
||||
buttons=[
|
||||
Button("Yes",handler=lambda: handler(True)),
|
||||
Button("No",handler=lambda: handler(False))
|
||||
]
|
||||
)
|
||||
|
||||
despawn = spawnDialog(dlg)
|
||||
|
||||
return await res
|
||||
|
||||
def HButtonSelect(buttonArgs):
|
||||
kb = KeyBindings()
|
||||
|
||||
@@ -347,10 +373,12 @@ async def mainScreen():
|
||||
|
||||
if guestLogin:
|
||||
name = "[guest]"
|
||||
admin = False
|
||||
else:
|
||||
stat = await server.status()
|
||||
if stat["type"]=="error" or not stat["response"]["login"]: return
|
||||
name = stat["response"]["name"]
|
||||
admin = stat["response"]["admin"]
|
||||
|
||||
def restart():
|
||||
get_app().exit(result="restart")
|
||||
@@ -596,6 +624,161 @@ async def mainScreen():
|
||||
("","Text encoding: "),("bold",encoding),("","\n")
|
||||
]
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Settings -> Admin panel -> Ban user
|
||||
# ------------------------------------------------------------
|
||||
|
||||
async def banuserconfirm(name,duration,motive):
|
||||
res = await server.getuserinfo(name)
|
||||
if res["type"]=="error":
|
||||
return await spawnSimplePopup("Error",f"{errorTxt(res)}\nCould not get information about the user.")
|
||||
info = res["response"]
|
||||
create_time = datetime.datetime.fromtimestamp(info["created"])
|
||||
motivestr = f"\"{motive}\"" if len(motive)>0 else "N/A"
|
||||
conf = await spawnYesNoPopup("Confirm Ban?",Window(FormattedTextControl([
|
||||
("","You are about to ban "),
|
||||
("bold",info["name"]),
|
||||
(""," for "),
|
||||
("bold",({0: "an indefinite amount of time",24*60*60: "24 hours",3*24*60*60: "3 days",7*24*60*60: "7 days"})[duration]),
|
||||
("",".\n\nMotive: "),("bold",motivestr),("","\n"),
|
||||
("","\nUser information:\n"),
|
||||
("","- Internal ID: "),("bold",str(info["id"])),("","\n"),
|
||||
("","- Created: "),("bold",humanize.naturaldate(create_time)),("",f" ({create_time})\n")
|
||||
],focusable=False),dont_extend_width=True,dont_extend_height=True))
|
||||
if conf:
|
||||
res = await server.banuser(info["id"],duration,motive)
|
||||
if res["type"]=="error":
|
||||
await spawnSimplePopup("Error",f"{errorTxt(res)}\nCould not ban this user.")
|
||||
|
||||
def banuser():
|
||||
def dlgclose():
|
||||
despawn()
|
||||
get_app().create_background_task(banuserconfirm(usertextfield.text,timeselect.current_value,motivetextfield.text))
|
||||
|
||||
def nameaccept(buf: Buffer):
|
||||
get_app().layout.focus(motivetextfield)
|
||||
return True
|
||||
|
||||
def motiveaccept(buf: Buffer):
|
||||
get_app().layout.focus(timeselect)
|
||||
return True
|
||||
|
||||
usernameValidator = Validator.from_callable(isValidUsername, error_message='Invalid username')
|
||||
|
||||
ok_button = Button(text="OK", handler=dlgclose, width=16)
|
||||
cancel_button = Button(text="Cancel", handler=lambda: despawn(), width=16)
|
||||
|
||||
usertextfield = TextArea(
|
||||
text="",
|
||||
multiline=False,
|
||||
password=False,
|
||||
completer=None,
|
||||
validator=usernameValidator,
|
||||
accept_handler=nameaccept,
|
||||
)
|
||||
|
||||
motivetextfield = TextArea(
|
||||
text="",
|
||||
multiline=False,
|
||||
password=False,
|
||||
completer=None,
|
||||
validator=None,
|
||||
accept_handler=motiveaccept,
|
||||
)
|
||||
|
||||
timeselect = RadioList([(24*60*60,"24 hours"),(3*24*60*60,"3 days"),(7*24*60*60,"7 days"),(0,"Permanent")],default=intset["color"])
|
||||
|
||||
dlg = Dialog(
|
||||
title="Ban user",
|
||||
body=HSplit(
|
||||
[
|
||||
Label(text="Username:", dont_extend_height=True),
|
||||
usertextfield,
|
||||
Label(text="Motive:", dont_extend_height=True),
|
||||
motivetextfield,
|
||||
Label(text="Time:", dont_extend_height=True),
|
||||
timeselect,
|
||||
ValidationToolbar(),
|
||||
],
|
||||
padding=D(preferred=1, max=1),
|
||||
width=70
|
||||
),
|
||||
buttons=[ok_button, cancel_button],
|
||||
with_background=True,
|
||||
)
|
||||
|
||||
despawn = spawnDialog(dlg)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Settings -> Admin panel -> Unban user
|
||||
# ------------------------------------------------------------
|
||||
|
||||
async def unbanuserconfirm(name):
|
||||
res = await server.getuserinfo(name)
|
||||
if res["type"]=="error":
|
||||
return await spawnSimplePopup("Error",f"{errorTxt(res)}\nCould not get information about the user.")
|
||||
info = res["response"]
|
||||
if info["banned"]==False:
|
||||
return await spawnSimplePopup("Error",f"The user {name} is not banned.")
|
||||
create_time = datetime.datetime.fromtimestamp(info["created"])
|
||||
ban_date = datetime.datetime.fromtimestamp(info["ban_details"]["ban_date"])
|
||||
expire_date = datetime.datetime.fromtimestamp(info["ban_details"]["expire_date"]) if info["ban_details"]["expire_date"]>0 else "Indefinite"
|
||||
motive = f"\"{info["ban_details"]["motive"]}\"" if ("motive" in info["ban_details"]) and (len(info["ban_details"]["motive"])>0) else "N/A"
|
||||
conf = await spawnYesNoPopup("Confirm Ban?",Window(FormattedTextControl([
|
||||
("","You are about to unban "),
|
||||
("bold",info["name"]),
|
||||
("",".\n\nUser information:\n"),
|
||||
("","- Internal ID: "),("bold",str(info["id"])),("","\n"),
|
||||
("","- Created: "),("bold",humanize.naturaldate(create_time)),("",f" ({create_time})\n"),
|
||||
("","\nBan information:\n"),
|
||||
("","- Motive: "),("bold",motive),("","\n"),
|
||||
("","- Banned on: "),("bold",humanize.naturaldate(ban_date)),("",f" ({ban_date})\n"),
|
||||
("","- Ban expires on: "),("bold",humanize.naturaldate(expire_date)),("",f" ({expire_date})\n"),
|
||||
],focusable=False),dont_extend_width=True,dont_extend_height=True))
|
||||
if conf:
|
||||
res = await server.unbanuser(info["id"])
|
||||
if res["type"]=="error":
|
||||
await spawnSimplePopup("Error",f"{errorTxt(res)}\nCould not unban this user.")
|
||||
|
||||
def unbanuser():
|
||||
def dlgclose():
|
||||
despawn()
|
||||
get_app().create_background_task(unbanuserconfirm(usertextfield.text))
|
||||
|
||||
def nameaccept(buf: Buffer):
|
||||
dlgclose()
|
||||
return True
|
||||
|
||||
usernameValidator = Validator.from_callable(isValidUsername, error_message='Invalid username')
|
||||
|
||||
ok_button = Button(text="OK", handler=dlgclose, width=16)
|
||||
cancel_button = Button(text="Cancel", handler=lambda: despawn(), width=16)
|
||||
|
||||
usertextfield = TextArea(
|
||||
text="",
|
||||
multiline=False,
|
||||
password=False,
|
||||
completer=None,
|
||||
validator=usernameValidator,
|
||||
accept_handler=nameaccept,
|
||||
)
|
||||
|
||||
dlg = Dialog(
|
||||
title="Unban user",
|
||||
body=HSplit(
|
||||
[
|
||||
Label(text="Username:"),
|
||||
usertextfield,
|
||||
ValidationToolbar(),
|
||||
],
|
||||
padding=D(preferred=1, max=1),
|
||||
),
|
||||
buttons=[ok_button, cancel_button],
|
||||
with_background=True,
|
||||
)
|
||||
|
||||
despawn = spawnDialog(dlg)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Settings Menu
|
||||
# ------------------------------------------------------------
|
||||
@@ -615,10 +798,12 @@ async def mainScreen():
|
||||
Box(HButtonSelect([("Change color depth",changecolor),("Change text encoding",changeenc),("Apply changes",restart)]),padding=0),
|
||||
Label("\n\nYou will need to restart the interface or relog to apply any new changes."),
|
||||
])
|
||||
],[
|
||||
] + ([ HButtonSelect([("Ban user",banuser),("Unban user",unbanuser)]) ] if admin else []),
|
||||
[
|
||||
None,
|
||||
None,
|
||||
None
|
||||
],["Account","Interface"])
|
||||
],["Account","Interface"]+(["Admin panel"] if admin else []))
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Tab Layout
|
||||
|
||||
Reference in New Issue
Block a user