Compare commits

..

No commits in common. "b10d691bb3488bc65ae64ec089b0ba0e8bd03668" and "6225ace7e3fffe7b7b7ca5025ea3891e44e5056b" have entirely different histories.

View file

@ -4,7 +4,6 @@ import sys
import argparse import argparse
import os import os
import json import json
import itertools
from typing import Dict, Optional, List, Iterable, Tuple from typing import Dict, Optional, List, Iterable, Tuple
import re import re
from time import sleep from time import sleep
@ -51,7 +50,7 @@ def get_user_id(mm_api: mattermost.MMApi):
return mm_api._my_user_id return mm_api._my_user_id
def get_posts_for_channel(self, channel_id: str, progress=lambda x: None, after=None, maximum=None, **kwargs) -> Iterable[Dict]: def get_posts_for_channel(self, channel_id: str, progress=lambda x: None, after=None, **kwargs) -> Iterable[Dict]:
""" """
@raises ApiException: Passed on from lower layers. @raises ApiException: Passed on from lower layers.
""" """
@ -60,9 +59,6 @@ def get_posts_for_channel(self, channel_id: str, progress=lambda x: None, after=
total = 0 total = 0
if after: if after:
if maximum is not None:
raise ValueError("after and maximum cannot be used together")
# 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:
@ -90,19 +86,14 @@ def get_posts_for_channel(self, channel_id: str, progress=lambda x: None, after=
data_page = self._get(f"/v4/channels/{channel_id}/posts", params={"page": page, "per_page": per_page, **kwargs}) data_page = self._get(f"/v4/channels/{channel_id}/posts", params={"page": page, "per_page": per_page, **kwargs})
order = data_page["order"] order = data_page["order"]
this_maximum = None if maximum is None else maximum - len(posts) posts.extend(
posts.extend(itertools.islice(
(
data_page["posts"][post_id] data_page["posts"][post_id]
for post_id in order for post_id in order
if post_id not in post_ids if post_id not in post_ids
), )
this_maximum
))
post_ids |= set(order) post_ids |= set(order)
progress(len(posts)) progress(len(posts))
if len(order) < per_page or (maximum is not None and len(posts) >= maximum): if len(order) < per_page:
break break
page += 1 page += 1
sleep(0.1) sleep(0.1)
@ -263,8 +254,6 @@ def cat(mm_api: mattermost.MMApi, cmdline_args):
def tail(mm_api: mattermost.MMApi, cmdline_args): def tail(mm_api: mattermost.MMApi, cmdline_args):
desired_count = cmdline_args.number
team, channel = resolve_team_channel(mm_api, cmdline_args.channel) team, channel = resolve_team_channel(mm_api, cmdline_args.channel)
if not cmdline_args.ids: if not cmdline_args.ids:
@ -287,7 +276,12 @@ def tail(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"], maximum=desired_count) data_page = mm_api._get(f"/v4/channels/{channel['id']}/posts")
order = data_page["order"]
posts = [
data_page["posts"][post_id]
for post_id in reversed(order)
]
for post in posts: for post in posts:
print(str_for_post(attribute, post, cmdline_args)) print(str_for_post(attribute, post, cmdline_args))
@ -622,7 +616,6 @@ Security note: Other programs and users can typically read which arguments you g
parser_tail = subparsers.add_parser("tail", help="list newest messages in channel") parser_tail = subparsers.add_parser("tail", help="list newest messages in channel")
parser_tail.add_argument("channel", help="URL names of team and channel: '<team>/<channel>'") parser_tail.add_argument("channel", help="URL names of team and channel: '<team>/<channel>'")
parser_tail.add_argument("-f", "--follow", action="store_true", help="keep running, printing new posts as they come in") parser_tail.add_argument("-f", "--follow", action="store_true", help="keep running, printing new posts as they come in")
parser_tail.add_argument("-n", "--number", type=int, default=200, help="maximum amount of messages of backlog to fetch (when using with -f, new posts will be printed without limit)")
parser_ls = subparsers.add_parser("ls", help="list channels") parser_ls = subparsers.add_parser("ls", help="list channels")
parser_ls.add_argument("team", help="URL name of team") parser_ls.add_argument("team", help="URL name of team")