Skip to content

Commit

Permalink
implement pagination to fetch created posts (#1000)
Browse files Browse the repository at this point in the history
Co-authored-by: CI Automation <[email protected]>
  • Loading branch information
LittleChimera and CI Automation authored Oct 9, 2024
1 parent 2de9b45 commit ec78e34
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
22 changes: 15 additions & 7 deletions release-controller/forum.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
nns_proposal_discussions_category,
):
"""Create a new topic."""
self.posts_count = 1
self.release = release
self.client = client
self.nns_proposal_discussions_category = nns_proposal_discussions_category
Expand All @@ -60,6 +61,7 @@ def __init__(
)
if topic:
self.topic_id = topic["id"]
self.posts_count = topic["posts_count"]
else:
post = client.create_post(
category_id=nns_proposal_discussions_category["id"],
Expand All @@ -74,13 +76,19 @@ def __init__(

def created_posts(self):
"""Return a list of posts created by the current user."""
topic_posts = self.client.topic_posts(topic_id=self.topic_id)
if not topic_posts:
raise RuntimeError("failed to list topic posts")

return [
p for p in topic_posts.get("post_stream", {}).get("posts", {}) if p["yours"]
]
results = []
for p in range((self.posts_count - 1) // 20 + 1):
topic_posts = self.client._get(f"/t/{self.topic_id}.json", page=p + 1)
if not topic_posts:
raise RuntimeError("failed to list topic posts")
results.extend(
[
p
for p in topic_posts.get("post_stream", {}).get("posts", {})
if p["yours"]
]
)
return results

def update(
self,
Expand Down
20 changes: 18 additions & 2 deletions release-controller/mock_discourse.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from pydiscourse import DiscourseClient


Expand All @@ -13,6 +14,18 @@ def __init__(self):
self.api_key = "test_api_key"
self.timeout = 10

def _get(self, path, **kwargs):
if not re.search(r"/t/(\d+).json", path):
return super()._get(path, **kwargs)
id = re.search(r"/t/(\d+).json", path).group(1) # type: ignore

return {
"raw": "bogus text",
"can_edit": True,
"posts_count": 1,
"id": id,
} | self.topic_posts(topic_id=id)

def categories(self, **kwargs): # pylint: disable=unused-argument
"""Return a list of categories."""
return [
Expand All @@ -33,7 +46,10 @@ def categories(self, **kwargs): # pylint: disable=unused-argument

def topics_by(self, _: str):
"""Return a list of topics."""
return [{"id": i + 1} | t for i, t in enumerate(self.created_topics)]
return [
{"id": i + 1, "posts_count": 1} | t
for i, t in enumerate(self.created_topics)
]

def topic_posts(self, topic_id: str):
"""Return a list of posts in a topic."""
Expand All @@ -44,7 +60,7 @@ def topic_posts(self, topic_id: str):
for p in [
{"id": i + 1} | p for i, p in enumerate(self.created_posts)
]
if p["topic_id"] == topic_id
if str(p["topic_id"]) == str(topic_id)
]
}
}
Expand Down

0 comments on commit ec78e34

Please sign in to comment.