(session, tracks, current_user_id)
return remixes
Fetches tracks' remix parent owners and if they have saved/reposted the tracks

    Args:
        session: (DB) The scoped db session for running db queries
        tracks: (List<Track>) The tracks table objects to fetch remix parent user's information for
        current_user_id?: (int) Requesting user's id for adding additional metadata to the fetched users

    Returns:
        remixes: (dict) Mapping of child track ids to parent track ids to parent track user's metadata
        {
            [childTrackId] : {
                [parentTrackId]: {
                    has_remix_author_saved: boolean,
                    has_remix_author_reposted: boolean,
                    user: populated user metadata
                }
            }
        }
Fetches tracks' remix parent owners and if they have saved/reposted the tracks
Fetches tracks remix parent owners and if they have saved / reposted the tracks
def get_track_remix_metadata(session, tracks, current_user_id):
    """
    Fetches tracks' remix parent owners and if they have saved/reposted the tracks

    Args:
        session: (DB) The scoped db session for running db queries
        tracks: (List<Track>) The tracks table objects to fetch remix parent user's information for
        current_user_id?: (int) Requesting user's id for adding additional metadata to the fetched users

    Returns:
        remixes: (dict) Mapping of child track ids to parent track ids to parent track user's metadata
        {
            [childTrackId] : {
                [parentTrackId]: {
                    has_remix_author_saved: boolean,
                    has_remix_author_reposted: boolean,
                    user: populated user metadata
                }
            }
        }
    """
    track_ids_with_remix = []
    remix_query = []
    for track in tracks:
        if response_name_constants.remix_of in track:
            track_ids_with_remix.append(track["track_id"])

    if track_ids_with_remix:
        # Fetch the remix parent track's user and if that user has saved/favorited the child track
        remix_query = (
            session.query(
                Track.owner_id.label("track_owner_id"),
                Remix.parent_track_id.label("parent_track_id"),
                Remix.child_track_id.label("child_track_id"),
                Save.is_current.label("has_remix_author_saved"),
                Repost.is_current.label("has_remix_author_reposted"),
                User,
            )
            .join(
                Remix,
                and_(
                    Remix.parent_track_id == Track.track_id,
                    Remix.child_track_id.in_(track_ids_with_remix),
                ),
            )
            .join(User, and_(User.user_id == Track.owner_id, User.is_current == True))
            .outerjoin(
                Save,
                and_(
                    Save.save_item_id == Remix.child_track_id,
                    Save.save_type == SaveType.track,
                    Save.is_current == True,
                    Save.is_delete == False,
                    Save.user_id == Track.owner_id,
                ),
            )
            .outerjoin(
                Repost,
                and_(
                    Repost.repost_item_id == Remix.child_track_id,
                    Repost.user_id == Track.owner_id,
                    Repost.repost_type == RepostType.track,
                    Repost.is_current == True,
                    Repost.is_delete == False,
                ),
            )
            .filter(Track.is_current == True, Track.is_unlisted == False)
            .all()
        )

    remixes = {}
    remix_parent_owners = {}
    populated_users = {}

    # Build a dict of user id -> user model obj of the remixed track's parent owner to dedupe users
    for remix_relationship in remix_query:
        [track_owner_id, _, _, _, _, user] = remix_relationship
        if track_owner_id not in remix_parent_owners:
            remix_parent_owners[track_owner_id] = user

    # populate the user's metadata for the remixed track's parent owner
    # build `populated_users` as a map of userId -> json user
    if remix_parent_owners:
        [remix_parent_owner_ids, remix_parent_owners] = list(
            zip(
                *[
                    [k, remix_parent_owner]
                    for k, remix_parent_owner in remix_parent_owners.items()
                ]
            )
        )
        remix_parent_owners = helpers.query_result_to_list(list(remix_parent_owners))
        populated_remix_parent_users = populate_user_metadata(
            session, list(remix_parent_owner_ids), remix_parent_owners, current_user_id
        )
        for user in populated_remix_parent_users:
            populated_users[user["user_id"]] = user

    # Build a dict of child track id => parent track id => { user, has_remix_author_saved, has_remix_author_reposted }
    for remix_relationship in remix_query:
        [
            track_owner_id,
            parent_track_id,
            child_track_id,
            has_remix_author_saved,
            has_remix_author_reposted,
            _,
        ] = remix_relationship
        if child_track_id not in remixes:
            remixes[child_track_id] = {
                parent_track_id: {
                    response_name_constants.has_remix_author_saved: bool(
                        has_remix_author_saved
                    ),
                    response_name_constants.has_remix_author_reposted: bool(
                        has_remix_author_reposted
                    ),
                    "user": populated_users[track_owner_id],
                }
            }
        else:
            remixes[child_track_id][parent_track_id] = {
                response_name_constants.has_remix_author_saved: bool(
                    has_remix_author_saved
                ),
                response_name_constants.has_remix_author_reposted: bool(
                    has_remix_author_reposted
                ),
                "user": populated_users[track_owner_id],
            }

    return remixes
def get_track_remix_metadata ( session , tracks , current_user_id ) : track_ids_with_remix = [ ] remix_query = [ ] for track in tracks : if response_name_constants . remix_of in track : track_ids_with_remix . append ( track [ "track_id" ] ) if track_ids_with_remix : # Fetch the remix parent track's user and if that user has saved/favorited the child track remix_query = ( session . query ( Track . owner_id . label ( "track_owner_id" ) , Remix . parent_track_id . label ( "parent_track_id" ) , Remix . child_track_id . label ( "child_track_id" ) , Save . is_current . label ( "has_remix_author_saved" ) , Repost . is_current . label ( "has_remix_author_reposted" ) , User , ) . join ( Remix , and_ ( Remix . parent_track_id == Track . track_id , Remix . child_track_id . in_ ( track_ids_with_remix ) , ) , ) . join ( User , and_ ( User . user_id == Track . owner_id , User . is_current == True ) ) . outerjoin ( Save , and_ ( Save . save_item_id == Remix . child_track_id , Save . save_type == SaveType . track , Save . is_current == True , Save . is_delete == False , Save . user_id == Track . owner_id , ) , ) . outerjoin ( Repost , and_ ( Repost . repost_item_id == Remix . child_track_id , Repost . user_id == Track . owner_id , Repost . repost_type == RepostType . track , Repost . is_current == True , Repost . is_delete == False , ) , ) . filter ( Track . is_current == True , Track . is_unlisted == False ) . all ( ) ) remixes = { } remix_parent_owners = { } populated_users = { } # Build a dict of user id -> user model obj of the remixed track's parent owner to dedupe users for remix_relationship in remix_query : [ track_owner_id , _ , _ , _ , _ , user ] = remix_relationship if track_owner_id not in remix_parent_owners : remix_parent_owners [ track_owner_id ] = user # populate the user's metadata for the remixed track's parent owner # build `populated_users` as a map of userId -> json user if remix_parent_owners : [ remix_parent_owner_ids , remix_parent_owners ] = list ( zip ( * [ [ k , remix_parent_owner ] for k , remix_parent_owner in remix_parent_owners . items ( ) ] ) ) remix_parent_owners = helpers . query_result_to_list ( list ( remix_parent_owners ) ) populated_remix_parent_users = populate_user_metadata ( session , list ( remix_parent_owner_ids ) , remix_parent_owners , current_user_id ) for user in populated_remix_parent_users : populated_users [ user [ "user_id" ] ] = user # Build a dict of child track id => parent track id => { user, has_remix_author_saved, has_remix_author_reposted } for remix_relationship in remix_query : [ track_owner_id , parent_track_id , child_track_id , has_remix_author_saved , has_remix_author_reposted , _ , ] = remix_relationship if child_track_id not in remixes : remixes [ child_track_id ] = { parent_track_id : { response_name_constants . has_remix_author_saved : bool ( has_remix_author_saved ) , response_name_constants . has_remix_author_reposted : bool ( has_remix_author_reposted ) , "user" : populated_users [ track_owner_id ] , } } else : remixes [ child_track_id ] [ parent_track_id ] = { response_name_constants . has_remix_author_saved : bool ( has_remix_author_saved ) , response_name_constants . has_remix_author_reposted : bool ( has_remix_author_reposted ) , "user" : populated_users [ track_owner_id ] , } return remixes