From d2b634d87df84dd90f38477c6acc8c119d839283 Mon Sep 17 00:00:00 2001 From: Midgard Date: Mon, 15 May 2023 17:44:25 +0200 Subject: [PATCH] Add lastread command --- mmcli.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/mmcli.py b/mmcli.py index 4e19535..39e5890 100755 --- a/mmcli.py +++ b/mmcli.py @@ -151,7 +151,10 @@ def resolve_team_channel(mm_api: mattermost.MMApi, query: str) -> Dict: if not team: raise NotFound("team", query_parts[0]) - channel = resolve_channel(mm_api, team["id"], query_parts[1]) + if query_parts[1].startswith("id:"): + channel = mm_api.get_channel(query_parts[1][3:]) + else: + channel = resolve_channel(mm_api, team["id"], query_parts[1]) if not channel: return NotFound("channel", query_parts[1]) @@ -443,6 +446,23 @@ def customstatus(mm_api: mattermost.MMApi, cmdline_args): mm_api._delete(f"/v4/users/me/status/custom") +def lastread(mm_api: mattermost.MMApi, cmdline_args): + team, channel = resolve_team_channel(mm_api, cmdline_args.channel) if cmdline_args.channel is not None else (None, None) + assert channel is not None + response = mm_api._get(f"/v4/users/me/channels/{channel['id']}/posts/unread?limit_after=1&limit_before=0") + + if response["order"]: + assert len(response["order"]) == 1 + last_read_id = response["order"][0] + else: + last_read_id = None + + if cmdline_args.format == "json": + print(json.dumps(last_read_id)) + if cmdline_args.format == "tsv": + print(last_read_id or "null") + + def tsv_escape(text): return text.replace("\\", "\\\\").replace("\t", r"\t").replace("\n", r"\n") @@ -488,6 +508,7 @@ ACTIONS = { "edit": {"function": edit}, "status": {"function": status}, "customstatus": {"function": customstatus}, + "lastread": {"function": lastread}, } FORMATTERS = { "json", "tsv" } @@ -597,6 +618,9 @@ The input format accepted on stdin is one JSON object per line. The possible fie parser_customstatus.add_argument("--emoji", help="Name of emoji (without colons), e.g. coffee") parser_customstatus.add_argument("text" , help="Text for the status", nargs="?") + parser_lastread = subparsers.add_parser("lastread", help="last read message in channel; will be null if all messages are read") + parser_lastread.add_argument("channel", help="URL names of team and channel: '/'") + parsed = argparser.parse_args() if not parsed.server: