Skip to content

Commit

Permalink
Add user to track and feed discprov endpoints (#303)
Browse files Browse the repository at this point in the history
* Update libs w/ withUsers param

* Update discpro queries  w/ withUsers

* Clean up get user ids in tracks and feed query

* Bump libs version 0.11.66
  • Loading branch information
jowlee committed Feb 25, 2020
1 parent aa7f324 commit c362b63
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 32 deletions.
45 changes: 44 additions & 1 deletion discovery-provider/src/queries/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from src.queries import response_name_constants
from src.queries.query_helpers import get_current_user_id, parse_sort_param, populate_user_metadata, \
populate_track_metadata, populate_playlist_metadata, get_repost_counts, get_save_counts, \
get_pagination_vars, paginate_query
get_pagination_vars, paginate_query, get_users_by_id, get_users_ids

logger = logging.getLogger(__name__)
bp = Blueprint("queries", __name__)
Expand Down Expand Up @@ -139,6 +139,14 @@ def get_tracks():
# bundle peripheral info into track results
tracks = populate_track_metadata(session, track_ids, tracks, current_user_id)

if "with_users" in request.args and request.args.get("with_users") != 'false':
user_id_list = get_users_ids(tracks)
users = get_users_by_id(session, user_id_list)
for track in tracks:
user = users[track['owner_id']]
if user:
track['user'] = user

return api_helpers.success_response(tracks)


Expand Down Expand Up @@ -251,6 +259,14 @@ def get_playlists():
current_user_id
)

if "with_users" in request.args and request.args.get("with_users") != 'false':
user_id_list = get_users_ids(playlists)
users = get_users_by_id(session, user_id_list)
for playlist in playlists:
user = users[playlist['playlist_owner_id']]
if user:
playlist['user'] = user

except sqlalchemy.orm.exc.NoResultFound:
pass

Expand Down Expand Up @@ -502,6 +518,19 @@ def get_feed():
(limit, _) = get_pagination_vars()
feed_results = sorted_feed[0:limit]

if "with_users" in request.args and request.args.get("with_users") != 'false':
user_id_list = get_users_ids(feed_results)
users = get_users_by_id(session, user_id_list)
for result in feed_results:
if 'playlist_owner_id' in result:
user = users[result['playlist_owner_id']]
if user:
result['user'] = user
elif 'owner_id' in result:
user = users[result['owner_id']]
if user:
result['user'] = user

return api_helpers.success_response(feed_results)


Expand Down Expand Up @@ -746,6 +775,19 @@ def get_repost_feed_for_user(user_id):
feed_results = sorted(
unsorted_feed, key=lambda entry: entry[response_name_constants.activity_timestamp], reverse=True)

if "with_users" in request.args and request.args.get("with_users") != 'false':
user_id_list = get_users_ids(feed_results)
users = get_users_by_id(session, user_id_list)
for result in feed_results:
if 'playlist_owner_id' in result:
user = users[result['playlist_owner_id']]
if user:
result['user'] = user
elif 'owner_id' in result:
user = users[result['owner_id']]
if user:
result['user'] = user

return api_helpers.success_response(feed_results)


Expand Down Expand Up @@ -1342,4 +1384,5 @@ def get_saves(save_type):

query_results = paginate_query(query).all()
save_results = helpers.query_result_to_list(query_results)

return api_helpers.success_response(save_results)
28 changes: 26 additions & 2 deletions discovery-provider/src/queries/query_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from src import exceptions
from src.queries import response_name_constants
from src.models import Track, Repost, RepostType, Follow, Playlist, Save, SaveType
from src.models import User, Track, Repost, RepostType, Follow, Playlist, Save, SaveType
from src.utils import helpers
from src.utils.config import shared_config

Expand Down Expand Up @@ -699,4 +699,28 @@ def get_genre_list(genre):
genre_list = genre_list + electronic_sub_genres
return genre_list


def get_users_by_id(session, user_ids):
user_query = session.query(User).filter(User.is_current == True, User.wallet != None, User.handle != None)
users_results = user_query.filter(User.user_id.in_(user_ids)).all()
users = helpers.query_result_to_list(users_results)

current_user_id = get_current_user_id(required=False)
# bundle peripheral info into user results
populated_users = populate_user_metadata(session, user_ids, users, current_user_id)
user_map = {}
for user in populated_users:
user_map[user['user_id']] = user

return user_map

# Given an array of tracks and/or playlists, return an array of unique user ids
def get_users_ids(results):
user_ids = []
for result in results:
if 'playlist_owner_id' in result:
user_ids.append(int(result["playlist_owner_id"]))
elif 'owner_id' in result:
user_ids.append(int(result['owner_id']))
# Remove duplicate user ids
user_ids = list(set(user_ids))
return user_ids
2 changes: 1 addition & 1 deletion libs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@audius/libs",
"version": "0.11.65",
"version": "0.11.66",
"description": "",
"main": "src/index.js",
"browser": {
Expand Down
12 changes: 6 additions & 6 deletions libs/src/api/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Playlists extends Base {
* {Boolean} has_current_user_reposted - has current user reposted given playlist
* {Boolean} has_current_user_saved - has current user saved given playlist
*/
async getPlaylists (limit = 100, offset = 0, idsArray = null, targetUserId = null) {
async getPlaylists (limit = 100, offset = 0, idsArray = null, targetUserId = null, withUsers = false) {
this.REQUIRES(Services.DISCOVERY_PROVIDER)
return this.discoveryProvider.getPlaylists(limit, offset, idsArray, targetUserId)
return this.discoveryProvider.getPlaylists(limit, offset, idsArray, targetUserId, withUsers)
}

/**
Expand All @@ -30,9 +30,9 @@ class Playlists extends Base {
* @param {number} limit - max # of items to return
* @param {number} offset - offset into list to return from (for pagination)
*/
async getSavedPlaylists (limit = 100, offset = 0) {
async getSavedPlaylists (limit = 100, offset = 0, withUsers = false) {
this.REQUIRES(Services.DISCOVERY_PROVIDER)
return this.discoveryProvider.getSavedPlaylists(limit, offset)
return this.discoveryProvider.getSavedPlaylists(limit, offset, withUsers)
}

/**
Expand All @@ -41,9 +41,9 @@ class Playlists extends Base {
* @param {number} limit - max # of items to return
* @param {number} offset - offset into list to return from (for pagination)
*/
async getSavedAlbums (limit = 100, offset = 0) {
async getSavedAlbums (limit = 100, offset = 0, withUsers = false) {
this.REQUIRES(Services.DISCOVERY_PROVIDER)
return this.discoveryProvider.getSavedAlbums(limit, offset)
return this.discoveryProvider.getSavedAlbums(limit, offset, withUsers)
}

/* ------- SETTERS ------- */
Expand Down
10 changes: 5 additions & 5 deletions libs/src/api/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class Tracks extends Base {
* await getTracks()
* await getTracks(100, 0, [3,2,6]) - Invalid track ids will not be accepted
*/
async getTracks (limit = 100, offset = 0, idsArray = null, targetUserId = null, sort = null, minBlockNumber = null, filterDeleted = null) {
async getTracks (limit = 100, offset = 0, idsArray = null, targetUserId = null, sort = null, minBlockNumber = null, filterDeleted = null, withUsers = false) {
this.REQUIRES(Services.DISCOVERY_PROVIDER)
return this.discoveryProvider.getTracks(limit, offset, idsArray, targetUserId, sort, minBlockNumber, filterDeleted)
return this.discoveryProvider.getTracks(limit, offset, idsArray, targetUserId, sort, minBlockNumber, filterDeleted, withUsers)
}

/**
Expand Down Expand Up @@ -84,9 +84,9 @@ class Tracks extends Base {
* @param {number} limit - max # of items to return
* @param {number} offset - offset into list to return from (for pagination)
*/
async getSavedTracks (limit = 100, offset = 0) {
async getSavedTracks (limit = 100, offset = 0, withUsers = false) {
this.REQUIRES(Services.DISCOVERY_PROVIDER)
return this.discoveryProvider.getSavedTracks(limit, offset)
return this.discoveryProvider.getSavedTracks(limit, offset, withUsers)
}

/**
Expand All @@ -98,7 +98,7 @@ class Tracks extends Base {
* @param {?number} offset
* @returns {{listenCounts: Array<{trackId:number, listens:number}>}}
*/
async getTrendingTracks (genre = null, time = null, idsArray = null, limit = null, offset = null) {
async getTrendingTracks (genre = null, time = null, idsArray = null, limit = null, offset = null, withUsers = false) {
this.REQUIRES(Services.IDENTITY_SERVICE)
return this.discoveryProvider.getTrendingTracks(genre, time, idsArray, limit, offset)
}
Expand Down
8 changes: 4 additions & 4 deletions libs/src/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ class Users extends Base {
* {Boolean} has_current_user_reposted - has current user reposted given track/playlist
* {Array} followee_reposts - followees of current user that have reposted given track/playlist
*/
async getUserRepostFeed (userId, filter, limit = 100, offset = 0) {
async getUserRepostFeed (userId, filter, limit = 100, offset = 0, withUsers = false) {
this.REQUIRES(Services.DISCOVERY_PROVIDER)
return this.discoveryProvider.getUserRepostFeed(userId, filter, limit, offset)
return this.discoveryProvider.getUserRepostFeed(userId, filter, limit, offset, withUsers)
}

/**
Expand All @@ -125,11 +125,11 @@ class Users extends Base {
* {Boolean} has_current_user_reposted - has current user reposted given track/playlist
* {Array} followee_reposts - followees of current user that have reposted given track/playlist
*/
async getSocialFeed (filter, limit = 100, offset = 0) {
async getSocialFeed (filter, limit = 100, offset = 0, withUsers = false) {
this.REQUIRES(Services.DISCOVERY_PROVIDER)
const owner = this.userStateManager.getCurrentUser()
if (owner) {
return this.discoveryProvider.getSocialFeed(filter, limit, offset)
return this.discoveryProvider.getSocialFeed(filter, limit, offset, withUsers)
}

return []
Expand Down
31 changes: 19 additions & 12 deletions libs/src/services/discoveryProvider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class DiscoveryProvider {
* await getTracks()
* await getTracks(100, 0, [3,2,6]) - Invalid track ids will not be accepted
*/
async getTracks (limit = 100, offset = 0, idsArray = null, targetUserId = null, sort = null, minBlockNumber = null, filterDeleted = null) {
async getTracks (limit = 100, offset = 0, idsArray = null, targetUserId = null, sort = null, minBlockNumber = null, filterDeleted = null, withUsers = false) {
let req = { endpoint: 'tracks', queryParams: { limit: limit, offset: offset } }
if (idsArray) {
if (!Array.isArray(idsArray)) {
Expand All @@ -161,6 +161,9 @@ class DiscoveryProvider {
if (typeof filterDeleted === 'boolean') {
req.queryParams.filter_deleted = filterDeleted
}
if (withUsers) {
req.queryParams.with_users = true
}

return this._makeRequest(req)
}
Expand Down Expand Up @@ -198,7 +201,7 @@ class DiscoveryProvider {
* @param {?number} offset
* @returns {{listenCounts: Array<{trackId:number, listens:number}>}}
*/
async getTrendingTracks (genre = null, timeFrame = null, idsArray = null, limit = null, offset = null) {
async getTrendingTracks (genre = null, timeFrame = null, idsArray = null, limit = null, offset = null, withUsers = false) {
let queryUrl = '/trending/'

if (timeFrame != null) {
Expand Down Expand Up @@ -231,6 +234,10 @@ class DiscoveryProvider {
queryParams['genre'] = genre
}

if (withUsers) {
queryParams['with_users'] = withUsers
}

return this._makeRequest({
endpoint: queryUrl,
method: 'get',
Expand Down Expand Up @@ -279,10 +286,10 @@ class DiscoveryProvider {
* {Boolean} has_current_user_reposted - has current user reposted given track/playlist
* {Array} followee_reposts - followees of current user that have reposted given track/playlist
*/
async getSocialFeed (filter, limit = 100, offset = 0) {
async getSocialFeed (filter, limit = 100, offset = 0, withUsers = false) {
let req = {
endpoint: 'feed/',
queryParams: { filter: filter, limit: limit, offset: offset }
queryParams: { filter: filter, limit: limit, offset: offset, with_users: withUsers }
}

return this._makeRequest(req)
Expand All @@ -303,11 +310,11 @@ class DiscoveryProvider {
* {Boolean} has_current_user_reposted - has current user reposted given track/playlist
* {Array} followee_reposts - followees of current user that have reposted given track/playlist
*/
async getUserRepostFeed (userId, limit = 100, offset = 0) {
async getUserRepostFeed (userId, limit = 100, offset = 0, withUsers = false) {
let req = {
endpoint: 'feed',
urlParams: '/reposts/' + userId,
queryParams: { limit: limit, offset: offset }
queryParams: { limit: limit, offset: offset, with_users: withUsers }
}
return this._makeRequest(req)
}
Expand Down Expand Up @@ -519,10 +526,10 @@ class DiscoveryProvider {
* @param {number} limit - max # of items to return
* @param {number} offset - offset into list to return from (for pagination)
*/
async getSavedPlaylists (limit = 100, offset = 0) {
async getSavedPlaylists (limit = 100, offset = 0, withUsers = false) {
let req = {
endpoint: 'saves/playlists',
queryParams: { limit: limit, offset: offset }
queryParams: { limit: limit, offset: offset, with_users: withUsers }
}
return this._makeRequest(req)
}
Expand All @@ -533,10 +540,10 @@ class DiscoveryProvider {
* @param {number} limit - max # of items to return
* @param {number} offset - offset into list to return from (for pagination)
*/
async getSavedAlbums (limit = 100, offset = 0) {
async getSavedAlbums (limit = 100, offset = 0, withUsers = false) {
let req = {
endpoint: 'saves/albums',
queryParams: { limit: limit, offset: offset }
queryParams: { limit: limit, offset: offset, with_users: withUsers }
}
return this._makeRequest(req)
}
Expand All @@ -547,10 +554,10 @@ class DiscoveryProvider {
* @param {number} limit - max # of items to return
* @param {number} offset - offset into list to return from (for pagination)
*/
async getSavedTracks (limit = 100, offset = 0) {
async getSavedTracks (limit = 100, offset = 0, withUsers = false) {
let req = {
endpoint: 'saves/tracks',
queryParams: { limit: limit, offset: offset }
queryParams: { limit: limit, offset: offset, with_users: withUsers }
}
return this._makeRequest(req)
}
Expand Down

0 comments on commit c362b63

Please sign in to comment.