Add lastread command

This commit is contained in:
Midgard 2023-05-15 17:44:25 +02:00
parent 7be2620c28
commit d2b634d87d
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -151,6 +151,9 @@ def resolve_team_channel(mm_api: mattermost.MMApi, query: str) -> Dict:
if not team:
raise NotFound("team", query_parts[0])
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: '<team>/<channel>'")
parsed = argparser.parse_args()
if not parsed.server: