Skip to content

Commit

Permalink
get_feed_es supports explicit IDs (#3983)
Browse files Browse the repository at this point in the history
on signup user passes in IDs for initial feed render.
  • Loading branch information
stereosteve committed Sep 30, 2022
1 parent 6725a58 commit 7f0044a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Expand Up @@ -73,9 +73,17 @@ def test_get_feed_es(app):
# test feed
with app.app_context():
feed_results = get_feed_es({"user_id": "1"})
assert len(feed_results) == 2
assert feed_results[0]["playlist_id"] == 1
assert feed_results[0]["save_count"] == 1
assert len(feed_results[0]["followee_reposts"]) == 1

assert feed_results[1]["track_id"] == 1
assert feed_results[1]["save_count"] == 1

feed_results = get_feed_es({"user_id": "2"})
assert len(feed_results) == 0

# user 2 with explicit IDs
feed_results = get_feed_es({"user_id": "2", "followee_user_ids": [1]})
assert len(feed_results) == 2
35 changes: 25 additions & 10 deletions discovery-provider/src/queries/get_feed_es.py
Expand Up @@ -20,6 +20,8 @@ def get_feed_es(args, limit=10):
load_orig = feed_filter in ["original", "all"]
exclude_premium = args.get("exclude_premium", False)

explicit_ids = args.get("followee_user_ids", [])

mdsl = []

if load_reposts:
Expand All @@ -30,7 +32,9 @@ def get_feed_es(args, limit=10):
"query": {
"bool": {
"must": [
following_ids_terms_lookup(current_user_id, "user_id"),
following_ids_terms_lookup(
current_user_id, "user_id", explicit_ids
),
{"term": {"is_delete": False}},
{"range": {"created_at": {"gte": "now-30d"}}},
]
Expand Down Expand Up @@ -59,7 +63,9 @@ def get_feed_es(args, limit=10):
"query": {
"bool": {
"must": [
following_ids_terms_lookup(current_user_id, "owner_id"),
following_ids_terms_lookup(
current_user_id, "owner_id", explicit_ids
),
{"term": {"is_unlisted": False}},
{"term": {"is_delete": False}},
],
Expand All @@ -75,7 +81,7 @@ def get_feed_es(args, limit=10):
"bool": {
"must": [
following_ids_terms_lookup(
current_user_id, "playlist_owner_id"
current_user_id, "playlist_owner_id", explicit_ids
),
{"term": {"is_private": False}},
{"term": {"is_delete": False}},
Expand Down Expand Up @@ -256,18 +262,27 @@ def get_feed_es(args, limit=10):
return sorted_feed[0:limit]


def following_ids_terms_lookup(current_user_id, field):
def following_ids_terms_lookup(current_user_id, field, explicit_ids=None):
"""
does a "terms lookup" to query a field
with the user_ids that the current user follows
"""
if not explicit_ids:
explicit_ids = []
return {
"terms": {
field: {
"index": ES_USERS,
"id": str(current_user_id),
"path": "following_ids",
},
"bool": {
"should": [
{
"terms": {
field: {
"index": ES_USERS,
"id": str(current_user_id),
"path": "following_ids",
},
}
},
{"terms": {field: explicit_ids}},
]
}
}

Expand Down

0 comments on commit 7f0044a

Please sign in to comment.