Add upper limit to count GET param for inbox and feed

This commit is contained in:
Jessica Tallon 2015-01-06 12:06:12 +00:00
parent 90b78c4a28
commit 2663394688

View File

@ -158,12 +158,24 @@ def inbox_endpoint(request, inbox=None):
if inbox is None: if inbox is None:
inbox = Activity.query inbox = Activity.query
# Count how many items for the "totalItems" field
total_items = inbox.count()
# We want to make a query for all media on the site and then apply GET # We want to make a query for all media on the site and then apply GET
# limits where we can. # limits where we can.
inbox = inbox.order_by(Activity.published.desc()) inbox = inbox.order_by(Activity.published.desc())
# Limit by the "count" (default: 20) # Limit by the "count" (default: 20)
inbox = inbox.limit(request.args.get("count", 20)) try:
limit = int(request.args.get("count", 20))
except ValueError:
limit = 20
# Prevent the count being too big (pump uses 200 so we shall)
limit = limit if limit <= 200 else 200
# Apply the limit
inbox = inbox.limit(limit)
# Offset (default: no offset - first <count> results) # Offset (default: no offset - first <count> results)
inbox = inbox.offset(request.args.get("offset", 0)) inbox = inbox.offset(request.args.get("offset", 0))
@ -176,6 +188,7 @@ def inbox_endpoint(request, inbox=None):
"url": request.base_url, "url": request.base_url,
"links": {"self": {"href": request.url}}, "links": {"self": {"href": request.url}},
"items": [], "items": [],
"totalItems": total_items,
} }
for activity in inbox: for activity in inbox:
@ -188,7 +201,6 @@ def inbox_endpoint(request, inbox=None):
# should just skip them. # should just skip them.
pass pass
feed["totalItems"] = len(feed["items"])
return json_response(feed) return json_response(feed)
@oauth_required @oauth_required
@ -533,7 +545,18 @@ def feed_endpoint(request, outbox=None):
outbox = outbox.order_by(Activity.published.desc()) outbox = outbox.order_by(Activity.published.desc())
# Limit by the "count" (default: 20) # Limit by the "count" (default: 20)
outbox = outbox.limit(request.args.get("count", 20)) limit = request.args.get("count", 20)
try:
limit = int(limit)
except ValueError:
limit = 20
# The upper most limit should be 200
limit = limit if limit < 200 else 200
# apply the limit
outbox = outbox.limit(limit)
# Offset (default: no offset - first <count> result) # Offset (default: no offset - first <count> result)
outbox = outbox.offset(request.args.get("offset", 0)) outbox = outbox.offset(request.args.get("offset", 0))