diff --git a/hive/db/sql_scripts/bridge_list_subscribers.sql b/hive/db/sql_scripts/bridge_list_subscribers.sql index 8733b7d32a88a01fca7b7eae969c88df79aac79c..fd16863aa4f6198e5f40c42ddb32ff798a1c7c4e 100644 --- a/hive/db/sql_scripts/bridge_list_subscribers.sql +++ b/hive/db/sql_scripts/bridge_list_subscribers.sql @@ -3,7 +3,7 @@ CREATE TYPE bridge_api_list_subscribers AS ( name VARCHAR, role VARCHAR, title VARCHAR, - created_at VARCHAR(19) + created_at TIMESTAMP WITHOUT TIME ZONE ); DROP FUNCTION IF EXISTS bridge_list_subscribers @@ -19,22 +19,22 @@ AS $function$ DECLARE __community_id INT := find_community_id( _community, True ); - __last_id INT := find_account_id(_last, True); + __last_id INT := find_subscription_id(_last, _community, True); BEGIN RETURN QUERY - SELECT ha.name, get_role_name(COALESCE(hr.role_id,0)), hr.title, hs.created_at::VARCHAR(19) + SELECT ha.name, get_role_name(COALESCE(hr.role_id,0)), hr.title, hs.created_at FROM hive_subscriptions hs LEFT JOIN hive_roles hr ON hs.account_id = hr.account_id AND hs.community_id = hr.community_id JOIN hive_accounts ha ON hs.account_id = ha.id WHERE hs.community_id = __community_id AND (__last_id = 0 OR ( - hs.created_at <= (SELECT min(created_at) FROM hive_subscriptions WHERE account_id = __last_id AND community_id = __community_id) AND - hs.id > (SELECT max(id) FROM hive_subscriptions WHERE account_id = __last_id AND community_id = __community_id) + hs.id < __last_id ) ) - ORDER BY hs.created_at DESC, hs.id ASC + ORDER BY hs.id DESC LIMIT _limit; END $function$ -; \ No newline at end of file +; + diff --git a/hive/db/sql_scripts/utility_functions.sql b/hive/db/sql_scripts/utility_functions.sql index b3edc394ef50667ee734a21805f172fe0c5394cd..b0d5748eaf37ed5ad126c8297c79814ab256be69 100644 --- a/hive/db/sql_scripts/utility_functions.sql +++ b/hive/db/sql_scripts/utility_functions.sql @@ -183,4 +183,34 @@ BEGIN ; END $function$ -; \ No newline at end of file +; + +DROP FUNCTION IF EXISTS public.find_subscription_id CASCADE +; +CREATE OR REPLACE FUNCTION public.find_subscription_id( + in _account hive_accounts.name%TYPE, + in _community_name hive_communities.name%TYPE, + in _check BOOLEAN +) +RETURNS INTEGER +LANGUAGE 'plpgsql' STABLE +AS +$function$ +DECLARE + __subscription_id INT = 0; +BEGIN + IF (_account <> '') THEN + SELECT INTO __subscription_id COALESCE( ( + SELECT hs.id FROM hive_subscriptions hs + JOIN hive_accounts ha ON ha.id = hs.account_id + JOIN hive_communities hc ON hc.id = hs.community_id + WHERE ha.name = _account AND hc.name = _community_name + ), 0 ); + IF _check AND __subscription_id = 0 THEN + RAISE EXCEPTION '% subscription on % does not exist', _account, _community_name USING ERRCODE = 'CEHM9'; + END IF; + END IF; + RETURN __subscription_id; +END +$function$ +; diff --git a/hive/server/hive_api/community.py b/hive/server/hive_api/community.py index 733c49eb55244dbe09324e0a307087503c7f109d..a39b2c178714b7202b9880c281690e00ffea688a 100644 --- a/hive/server/hive_api/community.py +++ b/hive/server/hive_api/community.py @@ -2,7 +2,7 @@ import logging from hive.server.hive_api.common import (get_community_id) -from hive.server.common.helpers import (return_error_info, valid_community, valid_account, valid_limit) +from hive.server.common.helpers import (return_error_info, valid_community, valid_account, valid_limit, json_date) # pylint: disable=too-many-lines @@ -82,7 +82,7 @@ async def list_subscribers(context, community, last='', limit=100): db = context['db'] sql = "SELECT * FROM bridge_list_subscribers( (:community)::VARCHAR, (:last)::VARCHAR, (:limit)::INT )" rows = await db.query_all(sql, community=community, last=last, limit=limit) - return [(r[0], r[1], r[2], r[3]) for r in rows] + return [(r[0], r[1], r[2], json_date(r[3])) for r in rows] @return_error_info async def list_communities(context, last='', limit=100, query=None, sort='rank', observer=None): diff --git a/mock_data/block_data/community_op/mock_block_data_community.json b/mock_data/block_data/community_op/mock_block_data_community.json index 1038940b5e6dbb884957594010696caff32ab275..2ce026a1dfcfd266c3f02288f023bcb1f519cf92 100644 --- a/mock_data/block_data/community_op/mock_block_data_community.json +++ b/mock_data/block_data/community_op/mock_block_data_community.json @@ -2952,6 +2952,39 @@ "ref_block_prefix": 1, "expiration": "2020-03-23T12:17:00", "operations": [ + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "hive-188204" + ], + "id": "community", + "json": "[\"setRole\",{\"community\":\"hive-188204\",\"account\":\"gtg\",\"role\":\"muted\"}]" + } + }, + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "agartha" + ], + "id": "community", + "json": "[\"subscribe\",{\"community\":\"hive-186669\"}]" + } + }, + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "alice" + ], + "id": "community", + "json": "[\"subscribe\",{\"community\":\"hive-165317\"}]" + } + }, { "type": "custom_json_operation", "value": { @@ -3163,6 +3196,28 @@ "ref_block_prefix": 1, "expiration": "2020-03-23T12:17:00", "operations": [ + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "hive-188204" + ], + "id": "community", + "json": "[\"setRole\",{\"community\":\"hive-188204\",\"account\":\"alice\",\"role\":\"mod\"}]" + } + }, + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "camilla" + ], + "id": "community", + "json": "[\"subscribe\",{\"community\":\"hive-165317\"}]" + } + }, { "type": "custom_json_operation", "value": { @@ -3319,6 +3374,50 @@ "ref_block_prefix": 1, "expiration": "2020-03-23T12:17:00", "operations": [ + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "hive-188204" + ], + "id": "community", + "json": "[\"setRole\",{\"community\":\"hive-188204\",\"account\":\"agartha\",\"role\":\"admin\"}]" + } + }, + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "abit" + ], + "id": "community", + "json": "[\"subscribe\",{\"community\":\"hive-186669\"}]" + } + }, + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "gtg" + ], + "id": "community", + "json": "[\"subscribe\",{\"community\":\"hive-165317\"}]" + } + }, + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "abit" + ], + "id": "community", + "json": "[\"subscribe\",{\"community\":\"hive-165317\"}]" + } + }, { "type": "custom_json_operation", "value": { @@ -3518,6 +3617,39 @@ "ref_block_prefix": 1, "expiration": "2020-03-23T12:17:00", "operations": [ + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "hive-188204" + ], + "id": "community", + "json": "[\"setRole\",{\"community\":\"hive-188204\",\"account\":\"camilla\",\"role\":\"owner\"}]" + } + }, + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "abit" + ], + "id": "community", + "json": "[\"subscribe\",{\"community\":\"hive-188204\"}]" + } + }, + { + "type": "custom_json_operation", + "value": { + "required_auths": [], + "required_posting_auths": [ + "agartha" + ], + "id": "community", + "json": "[\"subscribe\",{\"community\":\"hive-165317\"}]" + } + }, { "type": "comment_operation", "value": { diff --git a/tests/tests_api b/tests/tests_api index 2f3d374cb2a79cadc6a5fd3f1696f94b914b6939..7a28ed11bdb362020a3d688cfd1acc93f5f0a44d 160000 --- a/tests/tests_api +++ b/tests/tests_api @@ -1 +1 @@ -Subproject commit 2f3d374cb2a79cadc6a5fd3f1696f94b914b6939 +Subproject commit 7a28ed11bdb362020a3d688cfd1acc93f5f0a44d