diff --git a/hive/db/schema.py b/hive/db/schema.py index 66266fb1f53a7316012a343516cb2ef9869faa91..568fa7864f80dd417019465cc06002891ad7ba5b 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -774,21 +774,31 @@ def setup(db): db.query_no_return(sql) sql = """ - DROP FUNCTION IF EXISTS find_comment_id(character varying, character varying) + DROP FUNCTION IF EXISTS find_comment_id(character varying, character varying, boolean) ; CREATE OR REPLACE FUNCTION find_comment_id( in _author hive_accounts.name%TYPE, - in _permlink hive_permlink_data.permlink%TYPE) - RETURNS INT AS + in _permlink hive_permlink_data.permlink%TYPE, + in _check boolean) + RETURNS INT + LANGUAGE 'plpgsql' + AS $function$ - SELECT COALESCE( (SELECT hp.id - FROM hive_posts hp - JOIN hive_accounts ha ON ha.id = hp.author_id - JOIN hive_permlink_data hpd ON hpd.id = hp.permlink_id - WHERE ha.name = _author AND hpd.permlink = _permlink AND hp.counter_deleted = 0 - ), 0 ); + DECLARE + post_id INT; + BEGIN + SELECT INTO post_id COALESCE( (SELECT hp.id + FROM hive_posts hp + JOIN hive_accounts ha ON ha.id = hp.author_id + JOIN hive_permlink_data hpd ON hpd.id = hp.permlink_id + WHERE ha.name = _author AND hpd.permlink = _permlink AND hp.counter_deleted = 0 + ), 0 ); + IF _check AND (_author <> '' OR _permlink <> '') AND post_id = 0 THEN + RAISE EXCEPTION 'Post/comment %/% does not exist', _author, _permlink; + END IF; + RETURN post_id; + END $function$ - LANGUAGE sql ; """ db.query_no_return(sql) @@ -852,7 +862,7 @@ def setup(db): DECLARE __post_id INT; BEGIN - __post_id = find_comment_id(_author,_permlink); + __post_id = find_comment_id(_author,_permlink, False); RETURN QUERY SELECT hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, @@ -932,8 +942,8 @@ def setup(db): __root_id INT; __post_id INT; BEGIN - __root_id = find_comment_id(_root_author,_root_permlink); - __post_id = find_comment_id(_start_post_author,_start_post_permlink); + __root_id = find_comment_id(_root_author, _root_permlink, True); + __post_id = find_comment_id(_start_post_author, _start_post_permlink, True); RETURN QUERY SELECT hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, @@ -972,11 +982,10 @@ def setup(db): in _limit INT) RETURNS SETOF database_api_post LANGUAGE sql - COST 100 STABLE ROWS 1000 - AS $BODY$ + AS $function$ SELECT hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout, @@ -995,7 +1004,7 @@ def setup(db): WHERE h.parent_author > _parent_author OR h.parent_author = _parent_author AND ( h.parent_permlink_or_category > _parent_permlink OR - h.parent_permlink_or_category = _parent_permlink AND h.id >= find_comment_id(_start_post_author,_start_post_permlink) ) + h.parent_permlink_or_category = _parent_permlink AND h.id >= find_comment_id(_start_post_author, _start_post_permlink, True) ) ORDER BY h.parent_author ASC, h.parent_permlink_or_category ASC, @@ -1006,8 +1015,8 @@ def setup(db): WHERE NOT hp.is_muted ; - $BODY$; - ; + $function$ + ; DROP FUNCTION IF EXISTS list_comments_by_last_update(character varying, timestamp, character varying, character varying, int) ; @@ -1023,7 +1032,7 @@ def setup(db): DECLARE __post_id INT; BEGIN - __post_id = find_comment_id(_start_post_author,_start_post_permlink); + __post_id = find_comment_id(_start_post_author, _start_post_permlink, True); RETURN QUERY SELECT hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, @@ -1067,7 +1076,7 @@ def setup(db): DECLARE __post_id INT; BEGIN - __post_id = find_comment_id(_start_post_author,_start_post_permlink); + __post_id = find_comment_id(_start_post_author, _start_post_permlink, True); RETURN QUERY SELECT hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, diff --git a/hive/server/common/helpers.py b/hive/server/common/helpers.py index 2db68fe342e3ef2a0714bbf0a630e0c9bb7836ac..3a6f1c6fab83136332f496d46839b29562f9df70 100644 --- a/hive/server/common/helpers.py +++ b/hive/server/common/helpers.py @@ -115,3 +115,23 @@ def valid_follow_type(follow_type: str): """Ensure follow type is valid steemd type.""" assert follow_type in ['blog', 'ignore'], 'invalid follow_type `%s`' % follow_type return follow_type + +def valid_date(date, allow_empty=False): + """ Ensure that date is in correct format """ + if not date: + assert allow_empty, 'Date is blank' + check_date = False + # check format "%Y-%m-%d %H:%M:%S" + try: + check_date = (date == datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S").strftime('%Y-%m-%d %H:%M%S')) + except ValueError: + check_date = False + # if check failed for format above try another format + # check format "%Y-%m-%dT%H:%M:%S" + if not check_date: + try: + check_date = (date == datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S").strftime('%Y-%m-%dT%H:%M:%S')) + except ValueError: + pass + + assert check_date, "Date should be in format Y-m-d H:M:S or Y-m-dTH:M:S" diff --git a/hive/server/database_api/methods.py b/hive/server/database_api/methods.py index ca0f03a2f94dc5b9321d575a04648f5d538f21a1..90384e273c8e02d621f0bba41f186a7f2de4ff0d 100644 --- a/hive/server/database_api/methods.py +++ b/hive/server/database_api/methods.py @@ -1,7 +1,7 @@ # pylint: disable=too-many-arguments,line-too-long,too-many-lines from enum import Enum -from hive.server.common.helpers import return_error_info, valid_limit, valid_account, valid_permlink +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.utils.normalize import rep_to_raw, number_to_json_value, time_string_with_t @@ -20,48 +20,69 @@ async def list_comments(context, start: list, limit: int, order: str): if order == 'by_cashout_time': assert len(start) == 3, "Expecting three arguments" cashout_time = start[0] + valid_date(cashout_time) if cashout_time[0:4] == '1969': cashout_time = "infinity" author = start[1] + valid_account(author, allow_empty=True) permlink = start[2] + valid_permlink(permlink, allow_empty=True) sql = "SELECT * FROM list_comments_by_cashout_time(:cashout_time, :author, :permlink, :limit)" result = await db.query_all(sql, cashout_time=cashout_time, author=author, permlink=permlink, limit=limit) elif order == 'by_permlink': assert len(start) == 2, "Expecting two arguments" author = start[0] + valid_account(author, allow_empty=True) permlink = start[1] + valid_permlink(permlink, allow_empty=True) sql = "SELECT * FROM list_comments_by_permlink(:author, :permlink, :limit)" result = await db.query_all(sql, author=author, permlink=permlink, limit=limit) elif order == 'by_root': assert len(start) == 4, "Expecting 4 arguments" root_author = start[0] + valid_account(root_author, allow_empty=True) root_permlink = start[1] + valid_permlink(root_permlink, allow_empty=True) start_post_author = start[2] + valid_account(start_post_author, allow_empty=True) start_post_permlink = start[3] + valid_permlink(start_post_permlink, allow_empty=True) sql = "SELECT * FROM list_comments_by_root(:root_author, :root_permlink, :start_post_author, :start_post_permlink, :limit)" result = await db.query_all(sql, root_author=root_author, root_permlink=root_permlink, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit) elif order == 'by_parent': assert len(start) == 4, "Expecting 4 arguments" parent_author = start[0] + valid_account(parent_author, allow_empty=True) parent_permlink = start[1] + valid_permlink(parent_permlink, allow_empty=True) start_post_author = start[2] + valid_account(start_post_author, allow_empty=True) start_post_permlink = start[3] + valid_permlink(start_post_permlink, allow_empty=True) sql = "SELECT * FROM list_comments_by_parent(:parent_author, :parent_permlink, :start_post_author, :start_post_permlink, :limit)" result = await db.query_all(sql, parent_author=parent_author, parent_permlink=parent_permlink, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit) elif order == 'by_last_update': assert len(start) == 4, "Expecting 4 arguments" parent_author = start[0] + valid_account(parent_author, allow_empty=True) updated_at = start[1] + valid_date(updated_at) start_post_author = start[2] + valid_account(start_post_author, allow_empty=True) start_post_permlink = start[3] + valid_permlink(start_post_permlink, allow_empty=True) sql = "SELECT * FROM list_comments_by_last_update(:parent_author, :updated_at, :start_post_author, :start_post_permlink, :limit)" result = await db.query_all(sql, parent_author=parent_author, updated_at=updated_at, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit) elif order == 'by_author_last_update': assert len(start) == 4, "Expecting 4 arguments" author = start[0] + valid_account(author, allow_empty=True) updated_at = start[1] + valid_date(updated_at) start_post_author = start[2] + valid_account(start_post_author, allow_empty=True) start_post_permlink = start[3] + valid_permlink(start_post_permlink, allow_empty=True) sql = "SELECT * FROM list_comments_by_author_last_update(:author, :updated_at, :start_post_author, :start_post_permlink, :limit)" result = await db.query_all(sql, author=author, updated_at=updated_at, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit)