Skip to content
Snippets Groups Projects
Commit a8cd6357 authored by Bartek Wrona's avatar Bartek Wrona
Browse files

Merge branch 'abw_pmaniora_find_comments_1000_pairs_and_more' into 'develop'

Various fixes to problems discovered by 1000_pairs test and more

See merge request !418
parents 70981a86 9e897ba9
No related branches found
No related tags found
2 merge requests!456Release candidate v1 24,!418Various fixes to problems discovered by 1000_pairs test and more
......@@ -144,12 +144,13 @@ END
$function$
;
DROP FUNCTION if exists delete_hive_post(character varying,character varying,character varying, integer)
DROP FUNCTION if exists delete_hive_post(character varying,character varying,character varying, integer, timestamp)
;
CREATE OR REPLACE FUNCTION delete_hive_post(
in _author hive_accounts.name%TYPE,
in _permlink hive_permlink_data.permlink%TYPE,
in _block_num hive_blocks.num%TYPE)
in _block_num hive_blocks.num%TYPE,
in _date hive_posts.active%TYPE)
RETURNS VOID
LANGUAGE plpgsql
AS
......@@ -174,6 +175,7 @@ BEGIN
WHERE ha.name = _author AND hpd.permlink = _permlink
)
,block_num = _block_num
,active = _date
WHERE id = __post_id;
DELETE FROM hive_reblogs
......
......@@ -124,3 +124,27 @@ BEGIN
END
$function$
;
DROP FUNCTION IF EXISTS public.find_community_id CASCADE
;
CREATE OR REPLACE FUNCTION public.find_community_id(
in _community_name hive_communities.name%TYPE,
in _check BOOLEAN
)
RETURNS INTEGER
LANGUAGE 'plpgsql' STABLE
AS
$function$
DECLARE
__community_id INT = 0;
BEGIN
IF (_community_name <> '') THEN
SELECT INTO __community_id COALESCE( ( SELECT id FROM hive_communities WHERE name=_community_name ), 0 );
IF _check AND __community_id = 0 THEN
RAISE EXCEPTION 'Community % does not exist', _community_name;
END IF;
END IF;
RETURN __community_id;
END
$function$
;
......@@ -266,7 +266,7 @@ class Blocks:
elif op_type == 'delete_comment_operation':
key = "{}/{}".format(op['author'], op['permlink'])
if ( ineffective_deleted_ops is None ) or ( key not in ineffective_deleted_ops ):
Posts.delete_op(op)
Posts.delete_op(op, cls._head_block_date)
elif op_type == 'comment_options_operation':
Posts.comment_options_op(op)
elif op_type == 'vote_operation':
......
This diff is collapsed.
......@@ -19,8 +19,11 @@ from hive.server.common.mutes import Mutes
async def get_profile(context, account, observer=None):
"""Load account/profile data."""
db = context['db']
account = valid_account(account)
observer = valid_account(observer, allow_empty=True)
ret = await load_profiles(db, [valid_account(account)])
assert ret, 'Account \'{}\' does not exist'.format(account)
assert ret, 'Account \'{}\' does not exist'.format(account) # should not be needed
observer_id = await get_account_id(db, observer) if observer else None
if observer_id:
......
......@@ -4,6 +4,7 @@ from enum import Enum
from hive.server.common.helpers import return_error_info, valid_limit, valid_account, valid_permlink, valid_date
from hive.server.database_api.objects import database_post_object
from hive.server.common.helpers import json_date
from hive.utils.normalize import escape_characters
@return_error_info
async def list_comments(context, start: list, limit: int = 1000, order: str = None):
......@@ -140,9 +141,10 @@ async def find_comments(context, comments: list):
hp.author_rewards
FROM
hive_posts_view hp
JOIN (VALUES {}) AS t (author, permlink) ON hp.author = t.author AND hp.permlink = t.permlink
JOIN (VALUES {}) AS t (author, permlink, number) ON hp.author = t.author AND hp.permlink = t.permlink
WHERE
NOT hp.is_muted
ORDER BY t.number
"""
idx = 0
......@@ -156,7 +158,7 @@ async def find_comments(context, comments: list):
continue
if idx > 0:
values += ","
values += "('{}','{}')".format(author, permlink) # escaping most likely needed
values += "({},{},{})".format(escape_characters(author), escape_characters(permlink), idx)
idx += 1
sql = SQL_TEMPLATE.format(values)
......
......@@ -14,15 +14,11 @@ def __used_refs():
async def get_community_id(db, name):
"""Get community id from db."""
return await db.query_one("SELECT id FROM hive_communities WHERE name = :name",
name=name)
return await db.query_one("SELECT find_community_id( (:name)::VARCHAR, True )", name=name)
async def get_account_id(db, name):
"""Get account id from account name."""
assert name, 'no account name specified'
_id = await db.query_one("SELECT id FROM hive_accounts WHERE name = :n", n=name)
assert _id, "account not found: `%s`" % name
return _id
return await db.query_one("SELECT find_account_id( (:name)::VARCHAR, True )", name=name)
def estimated_sp(vests):
"""Convert VESTS to SP units for display."""
......
......@@ -35,11 +35,10 @@ async def get_community(context, name, observer=None):
"""
db = context['db']
cid = await get_community_id(db, name)
assert cid, 'community not found'
communities = await load_communities(db, [cid], lite=False)
if observer:
valid_account(observer)
observer = valid_account(observer)
observer_id = await get_account_id(db, observer)
await _append_observer_roles(db, communities, observer_id)
await _append_observer_subs(db, communities, observer_id)
......@@ -50,9 +49,8 @@ async def get_community(context, name, observer=None):
async def get_community_context(context, name, account):
"""For a community/account: returns role, title, subscribed state"""
db = context['db']
valid_account(account)
account = valid_account(account)
cid = await get_community_id(db, name)
assert cid, 'community not found'
aid = await get_account_id(db, account)
assert aid, 'account not found'
......@@ -111,7 +109,7 @@ async def list_pop_communities(context, limit:int=25):
async def list_all_subscriptions(context, account):
"""Lists all communities `account` subscribes to, plus role and title in each."""
db = context['db']
valid_account(account)
account = valid_account(account)
account_id = await get_account_id(db, account)
sql = """SELECT c.name, c.title, COALESCE(r.role_id, 0), COALESCE(r.title, '')
......@@ -183,6 +181,7 @@ async def list_communities(context, last='', limit=100, query=None, sort='rank',
# append observer context, leadership data
communities = await load_communities(db, ids, lite=True)
if observer:
observer = valid_account(observer)
observer_id = await get_account_id(db, observer)
await _append_observer_subs(db, communities, observer_id)
await _append_observer_roles(db, communities, observer_id)
......@@ -349,11 +348,12 @@ async def top_community_authors(context, community):
async def top_community_muted(context, community):
"""Get top authors (by SP) who are muted in a community."""
db = context['db']
cid = await get_community_id(db, community)
sql = """SELECT a.name, a.voting_weight, r.title FROM hive_accounts a
JOIN hive_roles r ON a.id = r.account_id
WHERE r.community_id = :community_id AND r.role_id < 0
ORDER BY voting_weight DESC LIMIT 5"""
return await db.query(sql, community_id=await get_community_id(db, community))
return await db.query(sql, community_id=cid)
async def _top_community_posts(db, community, limit=50):
# TODO: muted equivalent
......
......@@ -36,6 +36,7 @@ async def accounts_by_name(db, names, observer=None, lite=True):
accounts[account['id']] = account
if observer:
observer = valid_account(observer)
await _follow_contexts(db, accounts,
observer_id=await get_account_id(db, observer),
include_mute=not lite)
......
......@@ -58,6 +58,7 @@ async def list_following(context, account:str, start:str='', limit:int=50, obser
async def list_all_muted(context, account):
"""Get a list of all account names muted by `account`."""
db = context['db']
account = valid_account(account)
sql = """SELECT a.name FROM hive_follows f
JOIN hive_accounts a ON f.following_id = a.id
WHERE follower = :follower AND state = 2"""
......
......@@ -6,54 +6,55 @@ DB = Db.instance()
There are three cases when 'active' field in post is updated:
1) when a descendant post comment was added (recursivly on any depth)
2) when a descendant post comment was deleted (recursivly on any depth)
3) when the post is updated
3) when the post is updated - that one only updates that post active (not here)
It means that, when the comment for posts is updated then its 'active' field
does not propagate for its ancestors.
"""
update_active_sql = """
WITH RECURSIVE parent_posts ( parent_id, post_id, intrusive_active) AS (
SELECT
parent_id as parent_id,
id as post_id,
CASE WHEN hp1.active = hp1.created_at OR hp1.counter_deleted > 0 THEN hp1.active
ELSE hp1.created_at
END as intrusive_active
FROM hive_posts hp1 {}
UNION
SELECT
hp2.parent_id as parent_id,
id as post_id,
max_time_stamp(
CASE WHEN hp2.active = hp2.created_at OR hp2.counter_deleted > 0 THEN hp2.active
ELSE hp2.created_at
END
, pp.intrusive_active
) as intrusive_active
FROM parent_posts pp
JOIN hive_posts hp2 ON pp.parent_id = hp2.id
WHERE hp2.depth > 0 AND pp.intrusive_active > hp2.active
WITH RECURSIVE parent_posts ( parent_id, post_id, intrusive_active ) AS (
SELECT
hp1.parent_id as parent_id,
hp1.id as post_id,
CASE WHEN hp1.counter_deleted > 0 THEN hp1.active
ELSE hp1.created_at
END as intrusive_active
FROM hive_posts hp1
WHERE hp1.depth > 0 {}
UNION
SELECT
hp2.parent_id as parent_id,
hp2.id as post_id,
max_time_stamp(
CASE WHEN hp2.counter_deleted > 0 THEN hp2.active
ELSE hp2.created_at
END
, pp.intrusive_active
) as intrusive_active
FROM parent_posts pp
JOIN hive_posts hp2 ON pp.parent_id = hp2.id
WHERE hp2.depth > 0
)
UPDATE
hive_posts
SET
active = new_active
FROM
(
SELECT hp.id as post_id, max_time_stamp( hp.active, MAX(pp.intrusive_active)) as new_active
FROM parent_posts pp
JOIN hive_posts hp ON pp.parent_id = hp.id GROUP BY hp.id
UPDATE
hive_posts
SET
active = new_active
FROM
(
SELECT hp.id as post_id, max_time_stamp( hp.active, MAX(pp.intrusive_active) ) as new_active
FROM parent_posts pp
JOIN hive_posts hp ON pp.parent_id = hp.id GROUP BY hp.id
) as dataset
WHERE dataset.post_id = hive_posts.id;
"""
def update_all_posts_active():
DB.query_no_return(update_active_sql.format( "WHERE ( children = 0 OR hp1.counter_deleted > 0 ) AND depth > 0" ))
DB.query_no_return(update_active_sql.format( "AND ( hp1.children = 0 )" ))
@time_it
def update_active_starting_from_posts_on_block( first_block_num, last_block_num ):
if first_block_num == last_block_num:
DB.query_no_return(update_active_sql.format( "WHERE block_num={} AND depth > 0" ).format(first_block_num) )
DB.query_no_return(update_active_sql.format( "AND hp1.block_num = {}" ).format(first_block_num) )
return
DB.query_no_return(update_active_sql.format( "WHERE block_num>={} AND block_num <={} AND depth > 0" ).format(first_block_num, last_block_num) )
DB.query_no_return(update_active_sql.format( "AND hp1.block_num >= {} AND hp1.block_num <= {}" ).format(first_block_num, last_block_num) )
Subproject commit 395f7109b2544cd1ace33a6027a0078d65fb1722
Subproject commit 527f27b14bdf10c8a543b015dc3cad0afb0ada6c
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