def _condenser_profile_object(row): """Convert an internal account record into legacy-steemd style.""" blacklists = Mutes.lists(row['name'], row['reputation']) return { 'id': row['id'], 'name': row['name'], 'created': json_date(row['created_at']), 'active': json_date(row['active_at']), 'post_count': row['post_count'], 'reputation': row['reputation'], 'blacklists': blacklists, 'stats': { 'sp': int(row['vote_weight'] * 0.0005037), 'rank': row['rank'], 'following': row['following'], 'followers': row['followers'], }, 'metadata': { 'profile': { 'name': row['display_name'], 'about': row['about'], 'website': row['website'], 'location': row['location'], 'cover_image': row['cover_image'], 'profile_image': row['profile_image'], } } }
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 post_id, community_id, author, permlink, title, body, category, depth, promoted, payout, payout_at, is_paidout, children, votes, created_at, updated_at, rshares, raw_json, json, is_hidden, is_grayed, total_votes, flag_weight FROM hive_posts_cache WHERE post_id IN :ids""" result = await db.query_all(sql, ids=tuple(ids)) author_map = await _query_author_map(db, result) # TODO: author affiliation? ctx = {} posts_by_id = {} author_ids = {} post_cids = {} for row in result: row = dict(row) author = author_map[row['author']] author_ids[author['id']] = author['name'] row['author_rep'] = author['reputation'] post = _condenser_post_object(row, truncate_body=truncate_body) post['blacklists'] = Mutes.lists(post['author'], author['reputation']) posts_by_id[row['post_id']] = post post_cids[row['post_id']] = row['community_id'] cid = row['community_id'] if cid: if cid not in ctx: ctx[cid] = [] ctx[cid].append(author['id']) # TODO: optimize titles = {} roles = {} for cid, account_ids in ctx.items(): sql = "SELECT title FROM hive_communities WHERE id = :id" titles[cid] = await db.query_one(sql, id=cid) sql = """SELECT account_id, role_id, title FROM hive_roles WHERE community_id = :cid AND account_id IN :ids""" roles[cid] = {} ret = await db.query_all(sql, cid=cid, ids=tuple(account_ids)) for row in ret: name = author_ids[row['account_id']] roles[cid][name] = (row['role_id'], row['title']) for pid, post in posts_by_id.items(): author = post['author'] cid = post_cids[pid] if cid: post['community'] = post['category'] # TODO: True? post['community_title'] = titles[cid] or post['category'] role = roles[cid][author] if author in roles[cid] else (0, '') post['author_role'] = ROLES[role[0]] post['author_title'] = role[1] else: post['stats']['gray'] = ('irredeemables' in post['blacklists'] or len(post['blacklists']) >= 2) post['stats']['hide'] = 'irredeemables' in post['blacklists'] sql = """SELECT id FROM hive_posts WHERE id IN :ids AND is_pinned = '1' AND is_deleted = '0'""" for pid in await db.query_col(sql, ids=tuple(ids)): if pid in posts_by_id: posts_by_id[pid]['stats']['is_pinned'] = True return posts_by_id