Example #1
0
async def get_discussions_by_comments(context,
                                      start_author: str,
                                      start_permlink: str = '',
                                      limit: int = 20,
                                      truncate_body: int = 0,
                                      filter_tags: list = None):
    """Get comments by made by author."""
    assert not filter_tags, 'filter_tags not supported'
    start_author = valid_account(start_author)
    start_permlink = valid_permlink(start_permlink, allow_empty=True)
    limit = valid_limit(limit, 100, 20)
    truncate_body = valid_truncate(truncate_body)

    posts = []
    db = context['db']

    sql = "SELECT * FROM bridge_get_account_posts_by_comments( (:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT )"
    result = await db.query_all(sql,
                                account=start_author,
                                author=start_author if start_permlink else '',
                                permlink=start_permlink,
                                limit=limit)

    for row in result:
        row = dict(row)
        post = _condenser_post_object(row, truncate_body=truncate_body)
        post['active_votes'] = await find_votes_impl(
            db, post['author'], post['permlink'],
            VotesPresentation.CondenserApi)
        posts.append(post)

    return posts
Example #2
0
async def get_discussions_by_feed_impl(db,
                                       account: str,
                                       start_author: str = '',
                                       start_permlink: str = '',
                                       limit: int = 20,
                                       truncate_body: int = 0,
                                       observer: str = None):
    """Get a list of posts for an account's feed."""
    sql = "SELECT * FROM bridge_get_by_feed_with_reblog((:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::INTEGER)"
    result = await db.query_all(sql,
                                account=account,
                                author=start_author,
                                permlink=start_permlink,
                                limit=limit,
                                observer=observer)

    posts = []
    for row in result:
        row = dict(row)
        post = _condenser_post_object(row, truncate_body=truncate_body)
        reblogged_by = set(row['reblogged_by'])
        reblogged_by.discard(
            row['author'])  # Eliminate original author of reblogged post
        if reblogged_by:
            reblogged_by_list = list(reblogged_by)
            reblogged_by_list.sort()
            post['reblogged_by'] = reblogged_by_list

        post['active_votes'] = await find_votes_impl(
            db, row['author'], row['permlink'], VotesPresentation.CondenserApi)
        posts.append(post)

    return posts
Example #3
0
async def get_discussions_by_blog(context,
                                  tag: str,
                                  start_author: str = '',
                                  start_permlink: str = '',
                                  limit: int = 20,
                                  truncate_body: int = 0,
                                  filter_tags: list = None):
    """Retrieve account's blog posts, including reblogs."""
    assert not filter_tags, 'filter_tags not supported'
    tag = valid_account(tag)
    start_author = valid_account(start_author, allow_empty=True)
    start_permlink = valid_permlink(start_permlink, allow_empty=True)
    limit = valid_limit(limit, 100, 20)
    truncate_body = valid_truncate(truncate_body)

    sql = "SELECT * FROM bridge_get_account_posts_by_blog( (:account)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::INTEGER, False )"

    db = context['db']
    result = await db.query_all(sql,
                                account=tag,
                                author=start_author,
                                permlink=start_permlink,
                                limit=limit)
    posts_by_id = []

    for row in result:
        row = dict(row)
        post = _condenser_post_object(row, truncate_body=truncate_body)
        post['active_votes'] = await find_votes_impl(
            db, post['author'], post['permlink'],
            VotesPresentation.CondenserApi)
        posts_by_id.append(post)

    return posts_by_id
Example #4
0
async def process_posts(db, sql_result, truncate_body: int = 0):
    posts = []
    for row in sql_result:
        row = dict(row)
        post = _condenser_post_object(row, truncate_body=truncate_body)

        post['active_votes'] = await find_votes_impl(
            db, row['author'], row['permlink'], VotesPresentation.CondenserApi)
        posts.append(post)

    return posts
Example #5
0
async def get_blog(context,
                   account: str,
                   start_entry_id: int = 0,
                   limit: int = None):
    """Get posts for an author's blog (w/ reblogs), paged by index/limit.

    Equivalent to get_discussions_by_blog, but uses offset-based pagination.

    Examples: (ABW: old description and examples were misleading as in many cases code worked differently, also now more cases actually work that gave error earlier)
    (acct, -1, limit) for limit 1..500 - returns latest (no more than) limit posts
    (acct, 0) - returns latest single post (ABW: this is a bug but I left it here because I'm afraid it was actively used - it should return oldest post)
    (acct, 0, limit) for limit 1..500 - same as (acct, -1, limit) - see above
    (acct, last_idx) for positive last_idx - returns last_idx oldest posts, or posts in range [last_idx..last_idx-500) when last_idx >= 500
    (acct, last_idx, limit) for positive last_idx and limit 1..500 - returns posts in range [last_idx..last_idx-limit)
    """
    db = context['db']

    account = valid_account(account)
    if not start_entry_id:
        start_entry_id = -1
    start_entry_id = valid_offset(start_entry_id)
    if not limit:
        limit = max(start_entry_id + 1, 1)
        limit = min(limit, 500)
    limit = valid_limit(limit, 500, None)

    sql = "SELECT * FROM condenser_get_blog(:account, :last, :limit)"
    result = await db.query_all(sql,
                                account=account,
                                last=start_entry_id,
                                limit=limit)

    out = []
    for row in result:
        row = dict(row)
        post = _condenser_post_object(row)

        post['active_votes'] = await find_votes_impl(
            db, row['author'], row['permlink'], VotesPresentation.CondenserApi)
        out.append({
            "blog": account,
            "entry_id": row['entry_id'],
            "comment": post,
            "reblogged_on": json_date(row['reblogged_at'])
        })

    return list(reversed(out))
Example #6
0
async def _get_content_replies_impl(db, fat_node_style, author: str,
                                    permlink: str):
    """Get a list of post objects based on parent."""
    valid_account(author)
    valid_permlink(permlink)

    sql = "SELECT * FROM condenser_get_content_replies(:author, :permlink)"
    result = await db.query_all(sql, author=author, permlink=permlink)

    posts = []
    for row in result:
        row = dict(row)
        post = _condenser_post_object(row,
                                      get_content_additions=fat_node_style)
        post['active_votes'] = await find_votes_impl(
            db, row['author'], row['permlink'], VotesPresentation.ActiveVotes
            if fat_node_style else VotesPresentation.CondenserApi)
        posts.append(post)

    return posts
Example #7
0
async def _get_content_impl(db,
                            fat_node_style,
                            author: str,
                            permlink: str,
                            observer=None):
    """Get a single post object."""
    valid_account(author)
    valid_permlink(permlink)

    sql = "SELECT * FROM condenser_get_content(:author, :permlink)"

    post = None
    result = await db.query_all(sql, author=author, permlink=permlink)
    if result:
        result = dict(result[0])
        post = _condenser_post_object(result, 0, fat_node_style)
        post['active_votes'] = await find_votes_impl(
            db, author, permlink, VotesPresentation.ActiveVotes
            if fat_node_style else VotesPresentation.CondenserApi)

    return post
Example #8
0
async def _load_discussion(db, author, permlink, observer=None):
    """Load a full discussion thread."""

    sql = "SELECT * FROM bridge_get_discussion(:author,:permlink,:observer)"
    sql_result = await db.query_all(sql,
                                    author=author,
                                    permlink=permlink,
                                    observer=observer)

    posts = []
    posts_by_id = {}
    replies = {}

    for row in sql_result:
        post = _condenser_post_object(row)

        post['active_votes'] = await find_votes_impl(
            db, row['author'], row['permlink'], VotesPresentation.CondenserApi)
        posts.append(post)

        parent_key = _ref_parent(post)
        _key = _ref(post)
        if parent_key not in replies:
            replies[parent_key] = []
        replies[parent_key].append(_key)

    for post in posts:
        _key = _ref(post)
        if _key in replies:
            replies[_key].sort()
            post['replies'] = replies[_key]

    for post in posts:
        posts_by_id[_ref(post)] = post

    return posts_by_id
Example #9
0
async def get_posts_by_given_sort(context,
                                  sort: str,
                                  start_author: str = '',
                                  start_permlink: str = '',
                                  limit: int = 20,
                                  tag: str = None,
                                  truncate_body: int = 0,
                                  filter_tags: list = None,
                                  observer: str = None):
    """Query posts, sorted by creation date."""
    assert not filter_tags, 'filter_tags not supported'

    db = context['db']

    start_author = valid_account(start_author, allow_empty=True),
    start_permlink = valid_permlink(start_permlink, allow_empty=True),
    limit = valid_limit(limit, 100, 20),
    tag = valid_tag(tag, allow_empty=True)
    observer = valid_account(observer, allow_empty=True)
    truncate_body = valid_truncate(truncate_body)

    posts = []
    is_community = tag[:5] == 'hive-'

    if sort == 'created':
        if is_community:
            sql = "SELECT * FROM bridge_get_ranked_post_by_created_for_community( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, False, (:observer)::VARCHAR )"
        elif tag == '':
            sql = "SELECT * FROM bridge_get_ranked_post_by_created( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
        else:
            sql = "SELECT * FROM bridge_get_ranked_post_by_created_for_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
    elif sort == 'trending':
        if is_community:
            sql = "SELECT * FROM bridge_get_ranked_post_by_trends_for_community( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, False, (:observer)::VARCHAR )"
        elif tag == '':
            sql = "SELECT * FROM bridge_get_ranked_post_by_trends( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
        else:
            sql = "SELECT * FROM bridge_get_ranked_post_by_trends_for_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
    elif sort == 'hot':
        if is_community:
            sql = "SELECT * FROM bridge_get_ranked_post_by_hot_for_community( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
        elif tag == '':
            sql = "SELECT * FROM bridge_get_ranked_post_by_hot( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
        else:
            sql = "SELECT * FROM bridge_get_ranked_post_by_hot_for_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
    elif sort == 'promoted':
        if is_community:
            sql = "SELECT * FROM bridge_get_ranked_post_by_promoted_for_community( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
        elif tag == '':
            sql = "SELECT * FROM bridge_get_ranked_post_by_promoted( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
        else:
            sql = "SELECT * FROM bridge_get_ranked_post_by_promoted_for_tag( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
    elif sort == 'post_by_payout':
        if tag == '':
            sql = "SELECT * FROM bridge_get_ranked_post_by_payout( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, False, (:observer)::VARCHAR )"
        else:
            sql = "SELECT * FROM bridge_get_ranked_post_by_payout_for_category( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, False, (:observer)::VARCHAR )"
    elif sort == 'comment_by_payout':
        if tag == '':
            sql = "SELECT * FROM bridge_get_ranked_post_by_payout_comments( (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
        else:
            sql = "SELECT * FROM bridge_get_ranked_post_by_payout_comments_for_category( (:tag)::VARCHAR, (:author)::VARCHAR, (:permlink)::VARCHAR, (:limit)::SMALLINT, (:observer)::VARCHAR )"
    else:
        return posts

    sql_result = await db.query_all(sql,
                                    tag=tag,
                                    author=start_author,
                                    permlink=start_permlink,
                                    limit=limit,
                                    observer=observer)

    for row in sql_result:
        post = _condenser_post_object(row, truncate_body)
        post['active_votes'] = await find_votes_impl(
            db, row['author'], row['permlink'], VotesPresentation.CondenserApi)
        posts.append(post)
    return posts