diff options
Diffstat (limited to 'packages/meshbay-cli/src/meshbay_cli/commands/invitations.py')
| -rw-r--r-- | packages/meshbay-cli/src/meshbay_cli/commands/invitations.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/packages/meshbay-cli/src/meshbay_cli/commands/invitations.py b/packages/meshbay-cli/src/meshbay_cli/commands/invitations.py new file mode 100644 index 0000000..df2a23e --- /dev/null +++ b/packages/meshbay-cli/src/meshbay_cli/commands/invitations.py @@ -0,0 +1,66 @@ +""" +`invitations`: the groups somebody added this account to, waiting for an answer. + +Until it is accepted an invitation is not a membership: the group is not in +`groups`, and its nodes cannot be reached. `join` accepts one, `decline` refuses +it. +""" + +import json +import sys + +from ..client.paths import group_arg, handle, resolve_group +from ..errors import NOT_FOUND, CliError +from ..output import safe, sentence +from .context import Context +from .groups import local_time + +HELP = "list the invitations waiting for your answer" + + +def add_parser(sub, parent) -> None: + sub.add_parser("invitations", parents=[parent], help=HELP, description=sentence(HELP) + + ". The GROUP column is what `join` and `decline` take.") + + +def as_group(inv: dict) -> dict: + """An invitation row in the shape `groups` rows have, for `handle` and lookups.""" + return {"id": inv["group_id"], "name": inv["name"], + "owner_username": inv.get("owner_username") or "", "invitation": inv} + + +def find_invitation(invitations: list[dict], spec: str) -> dict: + try: + return resolve_group([as_group(i) for i in invitations], group_arg(spec)) + except CliError as e: + if e.code != NOT_FOUND: + raise + raise CliError(NOT_FOUND, f"No invitation to a group called {spec!r} is " + "waiting; `meshbay-cli invitations` lists them.") from None + + +async def run(ctx: Context) -> int: + await ctx.hub.check_version() + account = ctx.account() + try: + invitations = await account.call(account.hub.invitations) + finally: + await account.close() + rows = sorted((as_group(i) for i in invitations), + key=lambda g: (g["name"].lower(), g["owner_username"])) + if ctx.args.json: + json.dump([{"group": g["name"], "owner": g["owner_username"], "id": g["id"], + "path": handle(g), "invited_by": g["invitation"].get("invited_by") or "", + "created_at": g["invitation"].get("created_at")} + for g in rows], sys.stdout, indent=1) + print() + return 0 + if not rows: + print("No invitation is waiting.", file=sys.stderr) + return 0 + print(f"{'INVITED BY':<22}{'DATE':<18}GROUP") + for g in rows: + inv = g["invitation"] + print(f"{safe(inv.get('invited_by') or ''):<22}{local_time(inv.get('created_at')):<18}" + f"{safe(handle(g))}") + return 0 |