Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement pagination to fetch created posts #1000

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
NikolaMilosa marked this conversation as resolved.
Show resolved Hide resolved
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
Loading