Skip to content
Snippets Groups Projects
Commit b61d06b5 authored by Mariusz Trela's avatar Mariusz Trela
Browse files

Unnecessary methods `load_posts`, `load_posts_keyed` are removed

parent 32d9770b
No related branches found
No related tags found
2 merge requests!456Release candidate v1 24,!339Further simplifications regarding `get_pids*` methods
"""Bridge API public endpoints for posts"""
import hive.server.bridge_api.cursor as cursor
from hive.server.bridge_api.objects import load_posts, load_profiles, _bridge_post_object, append_statistics_to_post
from hive.server.bridge_api.objects import load_profiles, _bridge_post_object, append_statistics_to_post
from hive.server.database_api.methods import find_votes_impl, VotesPresentation
from hive.server.common.helpers import (
return_error_info,
......
......@@ -57,99 +57,6 @@ async def load_profiles(db, names):
rows = await db.query_all(sql, names=tuple(names))
return [_bridge_profile_object(row) for row in rows]
async def load_posts_keyed(db, ids, truncate_body=0):
"""Given an array of post ids, returns full posts objects keyed by id."""
# pylint: disable=too-many-locals
assert ids, 'no ids passed to load_posts_keyed'
# fetch posts and associated author reps
sql = """
SELECT
hp.id,
hp.author,
hp.parent_author,
hp.author_rep,
hp.root_title,
hp.beneficiaries,
hp.max_accepted_payout,
hp.percent_hbd,
hp.url,
hp.permlink,
hp.parent_permlink_or_category,
hp.title,
hp.body,
hp.category,
hp.depth,
hp.promoted,
hp.payout,
hp.pending_payout,
hp.payout_at,
hp.is_paidout,
hp.children,
hp.votes,
hp.created_at,
hp.updated_at,
hp.rshares,
hp.abs_rshares,
hp.json,
hp.is_hidden,
hp.is_grayed,
hp.total_votes,
hp.sc_trend,
hp.role_title,
hp.community_title,
hp.role_id,
hp.is_pinned,
hp.curator_payout_value
FROM hive_posts_view hp
WHERE hp.id IN :ids
"""
result = await db.query_all(sql, ids=tuple(ids))
# TODO: author affiliation?
posts_by_id = {}
for row in result:
row = dict(row)
post = _bridge_post_object(row, truncate_body=truncate_body)
post['active_votes'] = await find_votes_impl(db, row['author'], row['permlink'], VotesPresentation.BridgeApi)
append_statistics_to_post(post, row, row['is_pinned'], None, True)
posts_by_id[row['id']] = post
return posts_by_id
async def load_posts(db, ids, truncate_body=0):
"""Given an array of post ids, returns full objects in the same order."""
if not ids:
return []
# posts are keyed by id so we can return output sorted by input order
posts_by_id = await load_posts_keyed(db, ids, truncate_body=truncate_body)
# in rare cases of cache inconsistency, recover and warn
missed = set(ids) - posts_by_id.keys()
if missed:
log.info("get_posts do not exist in cache: %s", repr(missed))
for _id in missed:
ids.remove(_id)
sql = """
SELECT
hp.id, ha_a.name as author, hpd_p.permlink as permlink, hp.depth, hp.created_at
FROM
hive_posts hp
INNER JOIN hive_accounts ha_a ON ha_a.id = hp.author_id
INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id
WHERE hp.id = :id """
post = await db.query_row(sql, id=_id)
if post is None:
# TODO: This should never happen. See #173 for analysis
log.error("missing post: id %i", _id)
else:
log.info("requested deleted post: %s", dict(post))
return [posts_by_id[_id] for _id in ids]
def _bridge_profile_object(row):
"""Convert an internal account record into legacy-steemd style."""
......
......@@ -11,8 +11,6 @@ from hive.server.common.mutes import Mutes
from hive.server.condenser_api.objects import (
load_accounts,
load_posts,
load_posts_keyed,
_mute_votes,
_condenser_post_object)
from hive.server.common.helpers import (
......
......@@ -2,7 +2,6 @@
from functools import wraps
import hive.server.condenser_api.cursor as cursor
from hive.server.condenser_api.objects import load_posts
from hive.server.condenser_api.objects import _mute_votes, _condenser_post_object
from hive.server.common.helpers import (
ApiError,
......
......@@ -19,100 +19,11 @@ async def load_accounts(db, names, lite = False):
rows = await db.query_all(sql, names=tuple(names))
return [_condenser_account_object(row) for row in rows]
async def load_posts_keyed(db, ids, truncate_body=0):
"""Given an array of post ids, returns full posts objects keyed by id."""
assert ids, 'no ids passed to load_posts_keyed'
# fetch posts and associated author reps
sql = """
SELECT hp.id,
hp.author,
hp.author_rep,
hp.permlink,
hp.title,
hp.body,
hp.category,
hp.depth,
hp.promoted,
hp.payout,
hp.pending_payout,
hp.payout_at,
hp.is_paidout,
hp.children,
hp.votes,
hp.created_at,
hp.updated_at,
hp.rshares,
hp.json as json,
hp.is_hidden,
hp.is_grayed,
hp.total_votes,
hp.parent_author,
hp.parent_permlink_or_category,
hp.curator_payout_value,
hp.root_author,
hp.root_permlink,
hp.max_accepted_payout,
hp.percent_hbd,
hp.allow_replies,
hp.allow_votes,
hp.allow_curation_rewards,
hp.beneficiaries,
hp.url,
hp.root_title
FROM hive_posts_view hp
WHERE hp.id IN :ids"""
result = await db.query_all(sql, ids=tuple(ids))
muted_accounts = Mutes.all()
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, row['author'], row['permlink'], VotesPresentation.CondenserApi)
post['active_votes'] = _mute_votes(post['active_votes'], muted_accounts)
posts_by_id[row['id']] = post
return posts_by_id
def _mute_votes(votes, muted_accounts):
if not muted_accounts:
return votes
return [v for v in votes if v['voter'] not in muted_accounts]
async def load_posts(db, ids, truncate_body=0):
"""Given an array of post ids, returns full objects in the same order."""
if not ids:
return []
# posts are keyed by id so we can return output sorted by input order
posts_by_id = await load_posts_keyed(db, ids, truncate_body=truncate_body)
# in rare cases of cache inconsistency, recover and warn
missed = set(ids) - posts_by_id.keys()
if missed:
log.info("get_posts do not exist in cache: %s", repr(missed))
for _id in missed:
ids.remove(_id)
sql = """
SELECT
hp.id, ha_a.name as author, hpd_p.permlink as permlink, hp.depth, hp.created_at
FROM
hive_posts hp
INNER JOIN hive_accounts ha_a ON ha_a.id = hp.author_id
INNER JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id
WHERE hp.id = :id """
post = await db.query_row(sql, id=_id)
if post is None:
# TODO: This should never happen. See #173 for analysis
log.error("missing post: id %i", _id)
else:
log.info("requested deleted post: %s", dict(post))
return [posts_by_id[_id] for _id in ids]
def _condenser_account_object(row):
"""Convert an internal account record into legacy-steemd style."""
#The member `vote_weight` from `hive_accounts` is removed, so currently the member `net_vesting_shares` is equals to zero.
......
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