Skip to content
Snippets Groups Projects
Commit a0a76171 authored by E. Madison Bray's avatar E. Madison Bray
Browse files

[bug] change the API so that recommendation systems return

*only* article IDs when recommendations are requested, rather than the full
article documents

the web server then makes a database request for articles based on those
article IDs; this way the backend is fully responsible for ensuring that
the article documents returned to the app are correctly formatted; otherwise
recsystems would have been free to insert any content they want into the
articles

I believe this design was the original intent, but I got sloppy while
prototyping.
parent ac3e39ae
No related branches found
No related tags found
No related merge requests found
......@@ -80,12 +80,18 @@ async def recommend(user_id, limit=RECOMMEND_DEFAULT_LIMIT, since_id=None,
# Currently just supports the 'random' strategy: Take a random selection
# of up to limit articles from the given range.
start = since_id + 1 if since_id is not None else None
if since_id is None:
# If no since_id is given (i.e. we are being asked for the most recent
# articles, just take the top `limit * 2` articles and then take a
# random selection from them
start = -2 * limit
else:
start = since_id + 1
end = max_id
selection = articles[start:end]
limit = min(limit, len(selection))
sample = sorted(random.sample(range(len(selection)), limit))
return [selection[idx] for idx in sample]
sample = sorted(random.sample(range(len(selection)), limit), reverse=True)
return [selection[idx]['article_id'] for idx in sample]
# websocket server loops
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment