Skip to content

Commit 0e12b7c

Browse files
bdgeiseseratch
authored andcommitted
Fix pagination bug where latest is used for offset
1 parent a878189 commit 0e12b7c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

slack_discovery_sdk/response.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
from .internal_utils import _next_cursor_is_present # type:ignore
1010

1111

12+
_LATEST_OFFSET_APIS = [
13+
'discovery.conversations.history',
14+
'discovery.conversations.edits',
15+
'discovery.conversations.renames',
16+
'discovery.conversations.reactions',
17+
]
18+
19+
1220
class DiscoveryResponse:
1321
"""An iterable container of response data.
1422
Attributes:
@@ -120,6 +128,9 @@ def __next__(self):
120128
# offset for https://api.slack.com/enterprise/discovery/methods#users_list etc.
121129
params.update({"offset": self.body.get("offset")})
122130

131+
if any([latest_offset_api in self.api_url for latest_offset_api in _LATEST_OFFSET_APIS]):
132+
params.update({"latest": self.body.get("offset")})
133+
123134
response = self._client.fetch_next_page( # skipcq: PYL-W0212
124135
http_method=self.http_method,
125136
api_url=self.api_url,

tests/test_conversations.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2021, Slack Technologies, LLC. All rights reserved.
22
from typing import Any
33

4-
import os, pytest, time
4+
import os, pytest, time, json
55
from slack_sdk import WebClient
66
from slack_discovery_sdk import DiscoveryClient
77
from slack_discovery_sdk.internal_utils import (
@@ -91,6 +91,21 @@ def test_conversations_list_pagination(self):
9191
break
9292
assert len(conversations) > limit_size
9393

94+
def test_conversations_history_pagination(self):
95+
conversations = []
96+
limit_size = 1
97+
page_num = 0
98+
for page in self.client.discovery_conversations_history(channel=self.channel, team=self.team, limit=limit_size):
99+
for message in page['messages']:
100+
conversations.append(json.dumps(message))
101+
page_num += 1
102+
if page_num > 5:
103+
break
104+
105+
assert len(conversations) > limit_size
106+
# ensure we are getting different messages for each page
107+
assert sorted(set(conversations)) == sorted(conversations)
108+
94109
def test_conversations_history_from_one_minute_ago(self):
95110
test_text = "This first message will be used to test getting conversation history from the last minute"
96111
first_msg = None

0 commit comments

Comments
 (0)