Compare commits

..

No commits in common. "80cd8cc4c451dc211e3e228c0f7bec5c6181b089" and "810a9d99cba83c7e0c9622671f082dc9e1c7405b" have entirely different histories.

View file

@ -4,7 +4,7 @@ import sys
import argparse import argparse
import os import os
import json import json
from typing import Dict, Optional, List, Iterable, Tuple from typing import Dict, Optional, List, Iterable
import re import re
from time import sleep from time import sleep
import threading import threading
@ -44,7 +44,7 @@ def warn_if_tty(you_can_type="the message objects", write_message_to=sys.stderr)
print(f"Reading from tty. (You can type {you_can_type} below. Or maybe you meant to redirect something to stdin.)", file=write_message_to) print(f"Reading from tty. (You can type {you_can_type} below. Or maybe you meant to redirect something to stdin.)", file=write_message_to)
def get_posts_for_channel(self, channel_id: str, progress=lambda x: None, after=None, **kwargs) -> Iterable[Dict]: def get_posts_for_channel(self, channel_id: str, progress=lambda x: None, after=None, since=None, **kwargs) -> Iterable[Dict]:
""" """
@raises ApiException: Passed on from lower layers. @raises ApiException: Passed on from lower layers.
""" """
@ -52,7 +52,29 @@ def get_posts_for_channel(self, channel_id: str, progress=lambda x: None, after=
page = 0 page = 0
total = 0 total = 0
if after: # if after and since:
# raise ValueError("after and since cannot be used together")
if since:
raise Exception("'since' functionality is broken in the API and behaves non-deterministically. It cannot be meaningfully used.")
# Posts in channel updated after a given timestamp: pagination is broken in the API
# current_since = since
# while True:
# data_page = self._get(f"/v4/channels/{channel_id}/posts", params={"since": current_since, **kwargs})
# order = data_page["order"]
# yield from (
# data_page["posts"][post_id]
# for post_id in reversed(order)
# )
# total += len(order)
# progress(total)
# if len(order) < 1000: # For some reason the pages go up to 1000 posts if 'since' is given
# break
# current_since = data_page["posts"][order[0]]["create_at"]
# sleep(0.1)
elif after:
# Posts in channel after a given ID: API gives pages with OLDEST messages first, so we can # Posts in channel after a given ID: API gives pages with OLDEST messages first, so we can
# yield each page when it is fetched # yield each page when it is fetched
while True: while True:
@ -136,7 +158,7 @@ def resolve_channel(mm_api: mattermost.MMApi, team_id: str, query: str) -> Optio
return joined_channel_result return joined_channel_result
def resolve_team_channel(mm_api: mattermost.MMApi, query: str) -> Tuple[Dict, Dict]: def resolve_team_channel(mm_api: mattermost.MMApi, query: str) -> Dict:
query_parts = query.split("/") query_parts = query.split("/")
del query del query
if len(query_parts) != 2: if len(query_parts) != 2:
@ -199,7 +221,7 @@ def cat(mm_api: mattermost.MMApi, cmdline_args):
backlog_lock = threading.Lock() backlog_lock = threading.Lock()
def print_initial_messages(): def print_initial_messages():
posts = get_posts_for_channel(mm_api, channel["id"], after=cmdline_args.after) posts = get_posts_for_channel(mm_api, channel["id"], after=cmdline_args.after, since=cmdline_args.since)
for post in posts: for post in posts:
print(str_for_post(attribute, post, cmdline_args)) print(str_for_post(attribute, post, cmdline_args))
@ -565,6 +587,7 @@ Security note: Other programs and users can typically read which arguments you g
parser_cat.add_argument("channel", help="URL names of team and channel: '<team>/<channel>'") parser_cat.add_argument("channel", help="URL names of team and channel: '<team>/<channel>'")
# --- # ---
parser_cat.add_argument("--after", help="all after post with ID") parser_cat.add_argument("--after", help="all after post with ID")
parser_cat.add_argument("--since", help="all after timestamp")
parser_cat.add_argument("-f", "--follow", action="store_true", help="keep running, printing new posts as they come in") parser_cat.add_argument("-f", "--follow", action="store_true", help="keep running, printing new posts as they come in")
parser_tail = subparsers.add_parser("tail", help="list newest messages in channel") parser_tail = subparsers.add_parser("tail", help="list newest messages in channel")